Fix timestamps with years before 100 being parsed as 19xx/20xx - #1185
Open
spokodev wants to merge 1 commit into
Open
Fix timestamps with years before 100 being parsed as 19xx/20xx#1185spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
The date and timestamp parser passed PostgreSQL's text output straight to new Date(x). For a year below 100 such as '0050-06-15 12:30:00+00', JavaScript's legacy date parser maps the year into the 1900s (or returns Invalid Date), so the value was silently off by about 1900 years. Normalize the text to ISO 8601 (space to T, two-digit timezone offset to +HH:00) so the ISO parser is used, which handles years below 100 correctly. 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 isparse: x => new Date(x), which hands PostgreSQL's text output straight to JavaScript'sDate. For a year below 100, PostgreSQL's text (e.g.0050-06-15 12:30:00+00) 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 timestamp with a year of 1 to 99 is silently corrupted (off by ~1900 years, or
Invalid 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, and plaindatevalues, produce byte-identical output to before.Verified against PostgreSQL 17 that the parsed values now match the
pgdriver for years 1 through 2020 acrossdate,timestamp, andtimestamptz, and that years >= 100 are unchanged.Test
Added
Date before year 100totests/index.js. It fails before the change and passes after.