From 3d757b2de242c5286a385e4b02876d3e22a6e5bb Mon Sep 17 00:00:00 2001 From: magiccwarss Date: Sat, 7 Nov 2026 17:10:39 +0400 Subject: [PATCH] Anchor both alternatives when parsing time zones --- src/locale.js | 2 +- test/zone-position-test.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/zone-position-test.js diff --git a/src/locale.js b/src/locale.js index cc0dac2..9929950 100644 --- a/src/locale.js +++ b/src/locale.js @@ -445,7 +445,7 @@ function parseYear(d, string, i) { } function parseZone(d, string, i) { - var n = /^(Z)|([+-]\d\d)(?::?(\d\d))?/.exec(string.slice(i, i + 6)); + var n = /^(?:(Z)|([+-]\d\d)(?::?(\d\d))?)/.exec(string.slice(i, i + 6)); return n ? (d.Z = n[1] ? 0 : -(n[2] + (n[3] || "00")), i + n[0].length) : -1; } diff --git a/test/zone-position-test.js b/test/zone-position-test.js new file mode 100644 index 0000000..fa37172 --- /dev/null +++ b/test/zone-position-test.js @@ -0,0 +1,24 @@ +import assert from "node:assert"; +import {it} from "vitest"; +import {timeParse, utcParse} from "d3-time-format"; + +for (const [name, parse] of [["timeParse", timeParse], ["utcParse", utcParse]]) { + it(`${name} requires a time zone at the current input position`, () => { + const p = parse("%Y-%m-%d %Z%M"); + for (const zone of ["x+0100", "x-0100", "ab+01", "ab-01"]) { + assert.strictEqual(p(`2020-01-01 ${zone}`), null, zone); + } + }); + + it(`${name} still parses every supported time zone syntax at the current position`, () => { + const p = parse("%Y-%m-%d %Z|%M"); + for (const zone of ["+01", "+0100", "+01:00"]) { + assert.deepStrictEqual(p(`2020-01-01 ${zone}|30`), new Date("2019-12-31T23:30:00Z")); + } + for (const zone of ["-01", "-0100", "-01:00"]) { + assert.deepStrictEqual(p(`2020-01-01 ${zone}|30`), new Date("2020-01-01T01:30:00Z")); + } + assert.deepStrictEqual(p("2020-01-01 Z|30"), new Date("2020-01-01T00:30:00Z")); + assert.deepStrictEqual(parse("%Y-%m-%d %Z%M")("2020-01-01 +010030"), new Date("2019-12-31T23:30:00Z")); + }); +}