From 88bc274573966e1a47cc1a18cb3e87cae7f6237b Mon Sep 17 00:00:00 2001 From: maximilliangrand <214999687+maximilliangrand@users.noreply.github.com> Date: Sun, 16 Aug 2026 12:24:57 +0200 Subject: [PATCH] Parse negative UNIX timestamps for %Q and %s 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 #87 Co-Authored-By: Claude Opus 4.8 --- src/locale.js | 5 +++-- test/utcParse-test.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/locale.js b/src/locale.js index cc0dac2..d855a8b 100644 --- a/src/locale.js +++ b/src/locale.js @@ -387,6 +387,7 @@ export default function formatLocale(locale) { var pads = {"-": "", "_": " ", "0": "0"}, numberRe = /^\s*\d+/, // note: ignores next directive + signedNumberRe = /^\s*-?\d+/, // for UNIX timestamps, which may be negative percentRe = /^%/, requoteRe = /[\\^$*+?|[\]().{}]/g; @@ -500,12 +501,12 @@ function parseLiteralPercent(d, string, i) { } function parseUnixTimestamp(d, string, i) { - var n = numberRe.exec(string.slice(i)); + var n = signedNumberRe.exec(string.slice(i)); return n ? (d.Q = +n[0], i + n[0].length) : -1; } function parseUnixTimestampSeconds(d, string, i) { - var n = numberRe.exec(string.slice(i)); + var n = signedNumberRe.exec(string.slice(i)); return n ? (d.s = +n[0], i + n[0].length) : -1; } diff --git a/test/utcParse-test.js b/test/utcParse-test.js index 482e9bd..11f217d 100644 --- a/test/utcParse-test.js +++ b/test/utcParse-test.js @@ -189,12 +189,23 @@ it("utcParse(\"%Q\")(date) parses UNIX timestamps", () => { assert.deepStrictEqual(p("631152000000"), utc(1990, 0, 1)); }); +it("utcParse(\"%Q\")(date) parses negative UNIX timestamps", () => { + const p = utcParse("%Q"); + assert.deepStrictEqual(p("-1"), new Date(-1)); + assert.deepStrictEqual(p("-631152000000"), utc(1950, 0, 1)); +}); + it("utcParse(\"%s\")(date) parses UNIX timestamps in seconds", () => { const p = utcParse("%s"); assert.deepStrictEqual(p("0"), utc(1970, 0, 1)); assert.deepStrictEqual(p("631152000"), utc(1990, 0, 1)); }); +it("utcParse(\"%s\")(date) parses negative UNIX timestamps in seconds", () => { + const p = utcParse("%s"); + assert.deepStrictEqual(p("-631152000"), utc(1950, 0, 1)); +}); + it("utcParse(\"%s.%L\")(date) parses UNIX timetamps in seconds and milliseconds", () => { const p = utcParse("%s.%L"); assert.deepStrictEqual(p("631152000.123"), utc(1990, 0, 1, 0, 0, 0, 123));