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
20 changes: 17 additions & 3 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export default function formatLocale(locale) {
"d": parseDayOfMonth,
"e": parseDayOfMonth,
"f": parseMicroseconds,
"g": parseYear,
"G": parseFullYear,
"g": parseYearISO,
"G": parseFullYearISO,
"H": parseHour24,
"I": parseHour24,
"j": parseDayOfYear,
Expand Down Expand Up @@ -197,7 +197,7 @@ export default function formatLocale(locale) {

function newParse(specifier, Z) {
return function(string) {
var d = newDate(1900, undefined, 1),
var d = newDate(undefined, undefined, 1),
i = parseSpecifier(d, specifier, string += "", 0),
week, day;
if (i != string.length) return null;
Expand All @@ -206,6 +206,9 @@ export default function formatLocale(locale) {
if ("Q" in d) return new Date(d.Q);
if ("s" in d) return new Date(d.s * 1000 + ("L" in d ? d.L : 0));

// Use the ISO year as a fallback when no calendar year was specified.
if (d.y === undefined) d.y = "G" in d ? d.G : 1900;

// If this is utcParse, never use the local timezone.
if (Z && !("Z" in d)) d.Z = 0;

Expand All @@ -218,6 +221,7 @@ export default function formatLocale(locale) {
// Convert day-of-week and week-of-year to day-of-year.
if ("V" in d) {
if (d.V < 1 || d.V > 53) return null;
if ("G" in d) d.y = d.G;
if (!("w" in d)) d.w = 1;
if ("Z" in d) {
week = utcDate(newDate(d.y, 0, 1)), day = week.getUTCDay();
Expand Down Expand Up @@ -444,6 +448,16 @@ function parseYear(d, string, i) {
return n ? (d.y = +n[0] + (+n[0] > 68 ? 1900 : 2000), i + n[0].length) : -1;
}

function parseFullYearISO(d, string, i) {
var n = numberRe.exec(string.slice(i, i + 4));
return n ? (d.G = +n[0], i + n[0].length) : -1;
}

function parseYearISO(d, string, i) {
var n = numberRe.exec(string.slice(i, i + 2));
return n ? (d.G = +n[0] + (+n[0] > 68 ? 1900 : 2000), i + n[0].length) : -1;
}

function parseZone(d, string, i) {
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
41 changes: 41 additions & 0 deletions test/iso-calendar-years-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import assert from "node:assert";
import {it} from "vitest";
import {timeFormat, timeParse, utcFormat, utcParse} from "d3-time-format";

for (const [name, parse, format, suffix] of [
["timeParse", timeParse, timeFormat, ""],
["utcParse", utcParse, utcFormat, "Z"]
]) {
it(`${name} distinguishes ISO years from calendar years in calendar dates`, () => {
for (const year of ["%Y", "%y"]) {
for (const isoYear of ["%G", "%g"]) {
for (const specifier of [`${year}-%m-%d ${isoYear}`, `${isoYear} ${year}-%m-%d`]) {
for (const string of ["2018-12-31", "2019-12-30", "2021-01-01", "2021-01-04"]) {
const date = new Date(`${string}T00:00:00${suffix}`);
assert.deepStrictEqual(parse(specifier)(format(specifier)(date)), date, specifier);
}
}
}
}
});

it(`${name} uses the ISO year for ISO weeks when a calendar year is also present`, () => {
for (const year of ["%Y", "%y"]) {
for (const isoYear of ["%G", "%g"]) {
for (const specifier of [`${year} ${isoYear}-W%V-%w`, `${isoYear}-W%V-%w ${year}`]) {
for (const string of ["2018-12-31", "2019-12-30", "2021-01-01", "2021-01-04"]) {
const date = new Date(`${string}T00:00:00${suffix}`);
assert.deepStrictEqual(parse(specifier)(format(specifier)(date)), date, specifier);
}
}
}
}
});

it(`${name} retains ISO year parsing when no calendar year is present`, () => {
for (const specifier of ["%G-%m-%d", "%g-%m-%d", "%G-W%V-%w", "%g-W%V-%w"]) {
const date = new Date(`2021-01-04T00:00:00${suffix}`);
assert.deepStrictEqual(parse(specifier)(format(specifier)(date)), date);
}
});
}