Skip to content
Merged
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
10 changes: 10 additions & 0 deletions package/src/resolve-first-week.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ describe("resolveFirstWeekSpec", () => {
const result = resolveFirstWeekSpec(spec, weekStartDay, T);
expect(result.toString()).toBe(expected);
});

it("converts a non-ISO-calendar PlainDate to ISO before snapping", () => {
// Buddhist 2026-03-15 (a Sunday) — its calendar fields read year 2569.
// Pre-fix the PlainDate fell through to the {month,year,day} branch and
// those fields were reinterpreted as ISO → ~543 years off.
const buddhist = T.PlainDate.from("2026-03-15").withCalendar("buddhist");
const result = resolveFirstWeekSpec(buddhist, 0, T);
expect(result.toString()).toBe("2026-03-15");
expect(result.calendarId).toBe("iso8601");
});
});

describe("resolveFirstWeek", () => {
Expand Down
12 changes: 9 additions & 3 deletions package/src/resolve-first-week.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ export function resolveFirstWeekSpec(
T: TemporalNamespace,
timeZone?: string,
): Temporal.PlainDate {
// 1. PlainDate — Temporal objects have a "calendar" property
if ("calendar" in spec) {
return snapToWeekStart(spec as Temporal.PlainDate, weekStartDay);
// 1. Temporal.PlainDate — identified by its `calendarId` (modern Temporal
// and @js-temporal/polyfill 0.5+ expose `calendarId`, not `calendar`).
// Convert non-ISO calendars to ISO so the rest of the pipeline — which
// is ISO-only — stays consistent; the week snap itself is
// calendar-independent.
if ("calendarId" in spec) {
const pd = spec as Temporal.PlainDate;
const iso = pd.calendarId === "iso8601" ? pd : pd.withCalendar("iso8601");
return snapToWeekStart(iso, weekStartDay);
}

// 2. Native Date
Expand Down
Loading