Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert saved non-UTC dates to UTC #10967

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading