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));