diff --git a/src/locale.js b/src/locale.js index cc0dac2..58924c9 100644 --- a/src/locale.js +++ b/src/locale.js @@ -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, @@ -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; @@ -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; @@ -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(); @@ -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; diff --git a/test/iso-calendar-years-test.js b/test/iso-calendar-years-test.js new file mode 100644 index 0000000..49ba64c --- /dev/null +++ b/test/iso-calendar-years-test.js @@ -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); + } + }); +}