Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 24 additions & 0 deletions test/zone-position-test.js
Original file line number Diff line number Diff line change
@@ -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"));
});
}