Skip to content

Parse and serialize timestamp infinity as Infinity - fixes #728 - #1184

Open
spokodev wants to merge 1 commit into
porsager:masterfrom
spokodev:fix/timestamp-infinity
Open

Parse and serialize timestamp infinity as Infinity - fixes #728#1184
spokodev wants to merge 1 commit into
porsager:masterfrom
spokodev:fix/timestamp-infinity

Conversation

@spokodev

Copy link
Copy Markdown

timestamptz/timestamp/date columns can hold the special values infinity and -infinity — e.g. expires timestamptz DEFAULT 'infinity' for "never expires". Postgres.js currently parses these through new Date('infinity'), which yields an Invalid Date: .getTime() is NaN, so every subsequent date comparison silently breaks. Reported in #728 (with a second report confirming it).

node-postgres maps these to JS Infinity / -Infinity (via postgres-date). This brings postgres.js in line.

Change (src/types.js, date type)

  • parse: 'infinity' → Infinity, '-infinity' → -Infinity, otherwise new Date(x) as before.
  • serialize: Infinity → 'infinity', -Infinity → '-infinity', otherwise unchanged. Without this half, writing a parsed Infinity back through the date serializer hits new Date(Infinity).toISOString(), which throws RangeError: Invalid time value — the two halves together let the value round-trip.

Behavior change (intentional)

Reading an infinity timestamp now returns the number Infinity instead of an Invalid Date. Code doing row.col.getTime() was already getting NaN; it now gets a value that compares correctly (Infinity > anyDate). This matches node-postgres and the underlying Postgres semantics.

Tests

Added alongside the existing Date cases — parse of 'infinity'/'-infinity', plus round-trip via sql.typed(±Infinity, 1184). Verified failing on main (parse → Invalid Date, round-trip → RangeError) and passing after the change; the normal Date path is unaffected.

Refs: Postgres date/time special values, node-postgres / postgres-date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant