Landed
Answer parse-iso-week
Landed as bf199ee · answered parse-iso-week
parseISO reads ISO 8601 week dates (2026-W41-3, the basic 2026W413, and 2026-W41 for the week's Monday) as the UTC midnight of that day, with the same optional time and offset as a calendar date, and gives an invalid date for a week the year doesn't have.
authored by oscar · committed by the platform · parent d88535c · Browse files · 2 files·+83 added−7 removed
This landing
State
Landed as bf199ee · answered parse-iso-week
Credit
Held-back: export 7/7 @ sha256:57d4055d5f282f4ad3c0d7ab56b5adc429956250fbb62f9db2b54172c6042a10; parser 4/4 @ sha256:57d4055d5f282f4ad3c0d7ab56b5adc429956250fbb62f9db2b54172c6042a10
Agents used, disclosed and not credit
- Assisted-by: claude-code_2-1-300_agent (self-reported)
Diff stats
2 files+83 added−7 removed
- territory:+44 added−7 removed
- tests:+39 added−0 removed
Provenance
- Commit
bf199ee- Tree
e97d818- Author
oscar <oscar@users.myr.technology>- Committer
Myr <platform@myr.technology>- Parent
d88535c- Patch id
ccac413- Request-Id
push-4da4d7731c9ccc4ff0c88d07080ee8c3f4e93c51-oscar
2 files changed+83 added−7 removed
modified:src/parse.ts+44 added-7 removed
123423456789101112131415161711 unmodified lines29303132333435363738394041426 unmodified lines4950515253545556575859606162636465666768697071727374757611 unmodified lines88899091929394959697989910010110210348 unmodified lines15215315415512012112212315615715815916016116216316413 unmodified lines/** * Reading ISO 8601 text. `parseISO` reads calendar dates and ordinal dates, * with an optional time and UTC offset, and never throws: text it can't read * gives an invalid date, which `isValid` tells apart. * Reading ISO 8601 text. `parseISO` reads calendar dates, ordinal dates and * week dates, with an optional time and UTC offset, and never throws: text it * can't read gives an invalid date, which `isValid` tells apart. */import { MS_PER_DAY, MS_PER_HOUR, MS_PER_MINUTE, MS_PER_SECOND, daysInMonth, daysInYear, isoWeekday, utcMidnight,} from "./calendar.ts";
/** A date that stands for no instant: what `parseISO` gives for text it can't read. */11 unmodified lines];
const ORDINAL_DATE = /^(?<year>\d{4})-?(?<ordinal>\d{3})$/u;
/** The week date forms, extended and basic; a week with no day is its Monday. */const WEEK_DATES = [ /^(?<year>\d{4})-W(?<week>\d{2})(?:-(?<weekday>\d))?$/u, /^(?<year>\d{4})W(?<week>\d{2})(?<weekday>\d)?$/u,];
const TIME = /^(?<hour>\d{2})(?::(?<minute>\d{2})(?::(?<second>\d{2})(?:[.,](?<fraction>\d{1,9}))?)?)?(?<offset>Z|[+-]\d{2}(?::?\d{2})?)?$/u;
const OFFSET = /^(?<sign>[+-])(?<hours>\d{2}):?(?<minutes>\d{2})?$/u;6 unmodified lines month >= 1 && month <= 12 && day >= 1 && day <= daysInMonth(year, month) ? utcMidnight(year, month, day) : undefined;
/** * The instant ISO week 1 of `year` starts in UTC: the Monday of the week * holding January 4, which is the first week with a Thursday in the year. */const firstWeekMonday = (year: number): number => { const january4 = utcMidnight(year, 1, 4);
return january4 - (isoWeekday(new Date(january4)) - 1) * MS_PER_DAY;};
/** How many ISO weeks `year` has: 52, or 53 when its weeks don't fit in 52. */const weeksInYear = (year: number): number => (firstWeekMonday(year + 1) - firstWeekMonday(year)) / (7 * MS_PER_DAY);
/** A week date, as the instant its day starts in UTC; `undefined` when the year has no such day. */const weekDay = (year: number, week: number, weekday: number): number | undefined => week >= 1 && week <= weeksInYear(year) && weekday >= 1 && weekday <= 7 ? firstWeekMonday(year) + ((week - 1) * 7 + weekday - 1) * MS_PER_DAY : undefined;
/** The start of the day the date part names, in epoch milliseconds. */const readDate = (text: string): number | undefined => { for (const form of CALENDAR_DATES) { const groups = form.exec(text)?.groups;11 unmodified lines
return day >= 1 && day <= daysInYear(year) ? utcMidnight(year, 1, day) : undefined; }
for (const form of WEEK_DATES) { const groups = form.exec(text)?.groups;
if (groups !== undefined) { return weekDay(whole(groups.year), whole(groups.week), whole(groups.weekday, 1)); } }
return undefined;};
/** A UTC offset (`Z`, `+09:00`, `-0330`, `+05`) in minutes east of UTC. */48 unmodified lines};
/** * Reads ISO 8601 text: a date (`2026-10-07`, `20261007`, `2026-10`, `2026`, * or the ordinal `2026-280`), then optionally `T` and a time (`14:30`, * `14:30:15`, `14:30:15.250`) with a UTC offset (`Z`, `+09:00`). Text with * no offset is read as UTC. Anything else, or a day that doesn't exist * (`2026-02-29`), gives an invalid date. * the ordinal `2026-280`, or the week date `2026-W41-3`, `2026W413` or * `2026-W41` for the week's Monday), then optionally `T` and a time * (`14:30`, `14:30:15`, `14:30:15.250`) with a UTC offset (`Z`, `+09:00`). * Text with no offset is read as UTC. Anything else, or a day that doesn't * exist (`2026-02-29`, `2027-W53`), gives an invalid date. */export const parseISO = (text: string): Date => { const [datePart = "", timePart, ...rest] = text.trim().split("T"); const day = readDate(datePart);13 unmodified linesmodified:test/parse.test.ts+39 added
19 unmodified lines20 expect(iso("2026280")).toBe("2026-10-07T00:00:00.000Z");21 expect(iso("2028-366")).toBe("2028-12-31T00:00:00.000Z");22 });23 24+ test("reads a week date as the UTC midnight of its day, and a week as its Monday", () => {25+ // Week 41 of 2026 is Monday, October 5 to Sunday, October 11.26+ expect(iso("2026-W41-3")).toBe("2026-10-07T00:00:00.000Z");27+ expect(iso("2026W413")).toBe("2026-10-07T00:00:00.000Z");28+ expect(iso("2026-W41")).toBe("2026-10-05T00:00:00.000Z");29+ expect(iso("2026W41")).toBe("2026-10-05T00:00:00.000Z");30+ expect(iso("2026-W41-7")).toBe("2026-10-11T00:00:00.000Z");31+ });32+33+ test("puts week 1 around the year's first Thursday, so it can start in the year before", () => {34+ expect(iso("2026-W01-1")).toBe("2025-12-29T00:00:00.000Z");35+ expect(iso("2021-W01-1")).toBe("2021-01-04T00:00:00.000Z");36+ // 2020 starts on a Wednesday and is a leap year, so it has 53 weeks.37+ expect(iso("2020-W53-7")).toBe("2021-01-03T00:00:00.000Z");38+ });39+40+ test("gives a week date the same time and offset as a calendar date", () => {41+ expect(iso("2026-W41-3T14:30Z")).toBe("2026-10-07T14:30:00.000Z");42+ expect(iso("2026-W41-3T09:30:00+02:00")).toBe("2026-10-07T07:30:00.000Z");43+ expect(iso("2026-W41T14:30")).toBe("2026-10-05T14:30:00.000Z");44+ });45+46+ test("reads week 53 only in a year with 53 weeks", () => {47+ // 2026 starts on a Thursday, so it has 53 weeks; 2027 starts on a Friday and has 52.48+ expect(iso("2026-W53-7")).toBe("2027-01-03T00:00:00.000Z");49+ expect(isValid(parseISO("2027-W53-1"))).toBe(false);50+ expect(isValid(parseISO("2027-W53"))).toBe(false);51+ expect(isValid(parseISO("2026-W54-1"))).toBe(false);52+ });53+54 test("reads a time with no offset as UTC", () => {55 expect(iso("2026-10-07T14:30")).toBe("2026-10-07T14:30:00.000Z");56 expect(iso("2026-10-07T14")).toBe("2026-10-07T14:00:00.000Z");57 expect(iso("2026-10-07T14:30:15.250")).toBe("2026-10-07T14:30:15.250Z");30 unmodified lines88 "2026-13-01",89 "2026-10-32",90 "2026-00-10",91 "2026-367",92+ "2026-W00-1",93+ "2026-W41-0",94+ "2026-W41-8",95+ "2026-W41-",96+ "2026-W41-33",97+ "2026-W4-3",98+ "2026-w41-3",99+ "2026-W413",100+ "2026W41-3",101 "2026-10-07T24:00",102 "2026-10-07T14:60",103 "2026-10-07T14:30:00+24:00",104 "2026-10-07T14:30T15:00",13 unmodified lines