fix: parse dates with years before 100 correctly - #1074
Open
spokodev wants to merge 1 commit into
Open
Conversation
PostgreSQL emits years below 100 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 (0050 became 1950, 0099 became 1999) or returned Invalid Date. Normalize the text to ISO 8601 (space to T, two-digit timezone offset to +HH:00) before parsing. Years 100 and above are unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
date/timestamp/timestamptzparser inpackages/pglite/src/types.tsisparse: (x) => new Date(x). PostgreSQL emits a year below 100 as e.g.0050-06-15 12:30:00+00, which is not strict ISO 8601 (space separator, two-digit offset), sonew Datefalls back to its legacy parser, which maps the year into the 1900s or returns garbage:Any
date/timestamp/timestamptzwith a year of 1 to 99 is silently corrupted (off by ~1900 years, orInvalid Date).Fix
Normalize the text to ISO 8601 before
new Date: replace the date/time space withT, and expand a two-digit timezone offset+HHto+HH:00. The ISO parser then handles years below 100 correctly. Years 100 and above produce identical output to before.Test
Added
timestamptz 1184 before year 100topackages/pglite/tests/types.test.ts. Verified viaparseTypethat years 1–99 now parse to the correct year, and that years ≥ 100 and thedate(1082) /timestamp(1114) cases are unchanged.Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.