From 13b4f79e3eba8c48a964c6285c9424dedf14e844 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 7 May 2024 15:55:35 -0400 Subject: [PATCH 1/3] Convert saved non-UTC dates to UTC --- .changeset/tiny-gifts-fry.md | 5 +++++ packages/db/src/runtime/index.ts | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .changeset/tiny-gifts-fry.md diff --git a/.changeset/tiny-gifts-fry.md b/.changeset/tiny-gifts-fry.md new file mode 100644 index 000000000000..b798e6c6a5ec --- /dev/null +++ b/.changeset/tiny-gifts-fry.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +Convert non-ISO date to UTC time diff --git a/packages/db/src/runtime/index.ts b/packages/db/src/runtime/index.ts index ba157f01da40..90452d5fddce 100644 --- a/packages/db/src/runtime/index.ts +++ b/packages/db/src/runtime/index.ts @@ -19,6 +19,12 @@ export function hasPrimaryKey(column: DBColumn) { return 'primaryKey' in column.schema && !!column.schema.primaryKey; } +function isISODateString(str: string) { + if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false; + const d = new Date(str); + return d instanceof Date && !isNaN(d.getTime()) && d.toISOString() === str; // valid date +} + const dateType = customType<{ data: Date; driverData: string }>({ dataType() { return 'text'; @@ -27,6 +33,11 @@ const dateType = customType<{ data: Date; driverData: string }>({ return value.toISOString(); }, fromDriver(value) { + if(!isISODateString(value)) { + // values saved using CURRENT_TIMESTAMP are not valid ISO strings + // but *are* in UTC, so append the UTC zone. + value += '.000Z'; + } return new Date(value); }, }); From d40bd3c874bb371882c077a8cdbc25bf54420dde Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 7 May 2024 16:28:50 -0400 Subject: [PATCH 2/3] Simplify the check --- packages/db/src/runtime/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/db/src/runtime/index.ts b/packages/db/src/runtime/index.ts index 90452d5fddce..ed3565e13d65 100644 --- a/packages/db/src/runtime/index.ts +++ b/packages/db/src/runtime/index.ts @@ -36,7 +36,7 @@ const dateType = customType<{ data: Date; driverData: string }>({ if(!isISODateString(value)) { // values saved using CURRENT_TIMESTAMP are not valid ISO strings // but *are* in UTC, so append the UTC zone. - value += '.000Z'; + value += 'Z'; } return new Date(value); }, From ae2e831c24bcfe1fda8ec790410302882d870a56 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 8 May 2024 08:11:21 -0400 Subject: [PATCH 3/3] Simplify check and add link --- packages/db/src/runtime/index.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/db/src/runtime/index.ts b/packages/db/src/runtime/index.ts index ed3565e13d65..93c034cf5655 100644 --- a/packages/db/src/runtime/index.ts +++ b/packages/db/src/runtime/index.ts @@ -19,11 +19,10 @@ export function hasPrimaryKey(column: DBColumn) { return 'primaryKey' in column.schema && !!column.schema.primaryKey; } -function isISODateString(str: string) { - if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false; - const d = new Date(str); - return d instanceof Date && !isNaN(d.getTime()) && d.toISOString() === str; // valid date -} +// Taken from: +// https://stackoverflow.com/questions/52869695/check-if-a-date-string-is-in-iso-and-utc-format +const isISODateString = (str: string) => + /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str); const dateType = customType<{ data: Date; driverData: string }>({ dataType() {