mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -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:
parent
fa240ff2f2
commit
a1343184da
2 changed files with 15 additions and 0 deletions
5
.changeset/tiny-gifts-fry.md
Normal file
5
.changeset/tiny-gifts-fry.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/db": patch
|
||||
---
|
||||
|
||||
Convert non-ISO date to UTC time
|
|
@ -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);
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue