0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

Convert saved non-UTC dates to UTC (#10967)

* Convert saved non-UTC dates to UTC

* Simplify the check

* Simplify check and add link
This commit is contained in:
Matthew Phillips 2024-05-08 08:27:55 -04:00 committed by GitHub
parent fa240ff2f2
commit a1343184da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---
Convert non-ISO date to UTC time

View file

@ -19,6 +19,11 @@ export function hasPrimaryKey(column: DBColumn) {
return 'primaryKey' in column.schema && !!column.schema.primaryKey; 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 }>({ const dateType = customType<{ data: Date; driverData: string }>({
dataType() { dataType() {
return 'text'; return 'text';
@ -27,6 +32,11 @@ const dateType = customType<{ data: Date; driverData: string }>({
return value.toISOString(); return value.toISOString();
}, },
fromDriver(value) { 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); return new Date(value);
}, },
}); });