From a1343184da2a67439de4792e9e404d17ec3943df Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 8 May 2024 08:27:55 -0400 Subject: [PATCH] Convert saved non-UTC dates to UTC (#10967) * Convert saved non-UTC dates to UTC * Simplify the check * Simplify check and add link --- .changeset/tiny-gifts-fry.md | 5 +++++ packages/db/src/runtime/index.ts | 10 ++++++++++ 2 files changed, 15 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 0000000000..b798e6c6a5 --- /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 ba157f01da..93c034cf56 100644 --- a/packages/db/src/runtime/index.ts +++ b/packages/db/src/runtime/index.ts @@ -19,6 +19,11 @@ export function hasPrimaryKey(column: DBColumn) { return 'primaryKey' in column.schema && !!column.schema.primaryKey; } +// 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() { return 'text'; @@ -27,6 +32,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 += 'Z'; + } return new Date(value); }, });