Repository navigation
fix: parse infinite dates - #1202
Merged
tdrz merged 2 commits intoNov 9, 2026
Merged
Conversation
tdrz
requested changes
Nov 9, 2026
tdrz
left a comment
Collaborator
There was a problem hiding this comment.
Thank you for this!
Unsure about the 'BC' part right now, see comment. I'd take the changes for the 'infinity' for now.
Comment on lines
+195
to
+222
| const dateParts = | ||
| /^(\d{4,})-(\d\d)-(\d\d)(?: (\d\d):(\d\d):(\d\d)(?:\.(\d+))?)?(?:([+-])(\d\d)(?::(\d\d))?(?::(\d\d))?)?( BC)?$/ | ||
|
|
||
| function parseDateParts(x: string): Date | null { | ||
| const m = dateParts.exec(x) | ||
| if (!m) return null | ||
| const year = m[12] ? 1 - +m[1] : +m[1] | ||
| const month = +m[2] - 1 | ||
| const day = +m[3] | ||
| const [hour, minute, second] = [+(m[4] ?? 0), +(m[5] ?? 0), +(m[6] ?? 0)] | ||
| const ms = m[7] ? +(m[7] + '00').slice(0, 3) : 0 | ||
|
|
||
| // A timestamp without time zone is local time, like `new Date` reads it. | ||
| if (m[4] !== undefined && m[8] === undefined) { | ||
| const date = new Date(2000, 0, 1) | ||
| date.setFullYear(year, month, day) | ||
| date.setHours(hour, minute, second, ms) | ||
| return date | ||
| } | ||
|
|
||
| const date = new Date(Date.UTC(2000, month, day, hour, minute, second, ms)) | ||
| date.setUTCFullYear(year) | ||
| if (m[8]) { | ||
| const offset = (+m[9] * 60 + +(m[10] ?? 0)) * 60 + +(m[11] ?? 0) | ||
| date.setTime(date.getTime() - (m[8] === '-' ? -offset : offset) * 1000) | ||
| } | ||
| return date | ||
| } |
Collaborator
There was a problem hiding this comment.
This is ... quite something! Isn't there a more humane way to do this?
Contributor
Author
|
Thanks, that's fair. I removed the BC and offset parsing, so this now only handles infinity and -infinity. |
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.
The
date,timestampandtimestamptzparser isnew Date(x), which returnsInvalid Dateforinfinityand-infinity:Now they become
Infinityand-Infinity, as in postgres.js, and the date serializer turns them back intoinfinityand-infinity. All other values still go throughnew Date.Tested in Node 22:
tests/types.test.tsfail onmainand pass with the change, together with the existing ones (39 in total).