Skip to content

Commit

Permalink
Convert saved non-UTC dates to UTC (#10967)
Browse files Browse the repository at this point in the history
* Convert saved non-UTC dates to UTC

* Simplify the check

* Simplify check and add link
  • Loading branch information
matthewp committed May 8, 2024
1 parent fa240ff commit a134318
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-gifts-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Convert non-ISO date to UTC time
10 changes: 10 additions & 0 deletions packages/db/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
},
});
Expand Down

0 comments on commit a134318

Please sign in to comment.