Skip to content

Parse negative UNIX timestamps for %Q and %s - #131

Open
maximilliangrand wants to merge 1 commit into
d3:mainfrom
maximilliangrand:fix/parse-negative-unix
Open

Parse negative UNIX timestamps for %Q and %s#131
maximilliangrand wants to merge 1 commit into
d3:mainfrom
maximilliangrand:fix/parse-negative-unix

Conversation

@maximilliangrand

Copy link
Copy Markdown

timeFormat("%Q") / timeFormat("%s") emit negative output for pre-epoch dates, but the matching parsers reject a leading minus and return null, so the parse(format(date)) round-trip breaks:

const f = utcFormat("%Q"), p = utcParse("%Q");
f(new Date(-1)); // "-1"
p("-1");         // null  (expected new Date(-1))

Cause: both UNIX parsers use the generic numberRe = /^\s*\d+/, which has no sign. As noted in #87, a minus doesn't make sense for the other numeric fields, so this uses a dedicated signedNumberRe = /^\s*-?\d+/ for the two UNIX timestamp parsers only, leaving every other field unchanged.

Round-trip now holds for %Q, %s, and %s.%L across negative and positive values. Added two focused parse tests; full suite (148) and eslint pass.

Closes #87

The generic numberRe rejects a leading minus sign, so timeParse("%Q")
and timeParse("%s") returned null for any pre-epoch value even though
timeFormat produces negative output for those dates, breaking the
parse(format(date)) round-trip. Use a dedicated signed regex for the two
UNIX timestamp parsers only, leaving the other numeric fields unchanged.

Closes d3#87

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Parsing Negative Unix Values Fail

1 participant