Skip to content
Open
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/fix-date-year-before-100.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite': patch
---

Fix `date` / `timestamp` / `timestamptz` values with a year before 100 being parsed as 19xx/20xx. PostgreSQL emits such years as e.g. `0050-06-15 12:30:00+00`, which is not strict ISO 8601, so `new Date()` fell back to its legacy parser and mapped the year into the 1900s (or returned `Invalid Date`). For example `0050` became `1950` and `0001` became `2015`. The text is now normalized to ISO 8601 before parsing; years 100 and above are unaffected.
8 changes: 7 additions & 1 deletion packages/pglite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ export const types = {
throw new Error('Invalid input for date type')
}
},
parse: (x: string | number) => new Date(x),
parse: (x: string | number) => {
if (typeof x === 'string') {
const iso = x.replace(' ', 'T')
return new Date(iso === x ? x : iso.replace(/([+-]\d{2})$/, '$1:00'))
}
return new Date(x)
},
},
bytea: {
to: BYTEA,
Expand Down
6 changes: 6 additions & 0 deletions packages/pglite/tests/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ describe('parse', () => {
).toEqual(new Date('2021-01-01T12:00:00.000Z').getUTCMilliseconds())
})

it('timestamptz 1184 before year 100', () => {
expect(types.parseType('0050-06-15 12:30:00+00', 1184)).toEqual(
new Date('0050-06-15T12:30:00.000Z'),
)
})

it('bytea 17', () => {
expect(types.parseType('\\x010203', 17)).toEqual(Uint8Array.from([1, 2, 3]))
})
Expand Down