An open source design system that's fully customizable and agent ready
TypeScript
fix(Calendar): compute ISO week numbers without local DST offset leaking in (#6365)
Manohar Paturie63f972837d1
diff --git a/.changeset/iso-week-number-dst.md b/.changeset/iso-week-number-dst.mdnew file mode 100644index 0000000000..4abe5c2337--- /dev/null+++ b/.changeset/iso-week-number-dst.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Compute ISO week numbers (`Calendar hasWeekNumbers`, `getWeekNumber`) on UTC anchors instead of local-time `Date` arithmetic. In timezones whose DST ends after New Year (Sydney, Auckland, Santiago), the local-millisecond difference between the week's Thursday and January 1 carried the DST offset, and in years whose January 1 is a Friday that extra hour rounded a whole week up: the entire ISO week 14 of April 2021 displayed as week 15. Week numbers are now identical in every timezone.++@ManoharPaturidiff --git a/packages/core/src/utils/plainDate.test.ts b/packages/core/src/utils/plainDate.test.tsindex c4db911e68..ff8a37987e 100644--- a/packages/core/src/utils/plainDate.test.ts+++ b/packages/core/src/utils/plainDate.test.ts@@ -1,6 +1,6 @@// Copyright (c) Meta Platforms, Inc. and affiliates.-import {describe, it, expect} from 'vitest';+import {describe, it, expect, afterEach} from 'vitest';import {type PlainDate,plainDateCreate,@@ -711,6 +711,42 @@ describe('plainDateGetWeekNumber', () => {expect(plainDateGetWeekNumber({year: 2026, month: 1, day: 4})).toBe(1);});+ // Regression: the week number must not depend on the host timezone's DST+ // offset. Zones whose DST ends after New Year (southern hemisphere) used to+ // add a phantom hour to the Thursday-vs-Jan-1 day count, and in years whose+ // Jan 1 is a Friday that hour rounded a whole week up.+ describe('DST independence', () => {+ const originalTZ = process.env.TZ;++ afterEach(() => {+ if (originalTZ === undefined) {+ delete process.env.TZ;+ } else {+ process.env.TZ = originalTZ;+ }+ });++ it.each(['Australia/Sydney', 'Pacific/Auckland', 'America/Santiago'])(+ 'does not round week 14 up to 15 in %s (DST ends after New Year)',+ zone => {+ process.env.TZ = zone;+ // 2021-01-01 is a Friday, so every ISO Thursday falls on a whole+ // multiple of 7 days into the year. 2021-04-05..11 is ISO week 14.+ expect(plainDateGetWeekNumber({year: 2021, month: 4, day: 5})).toBe(14);+ expect(plainDateGetWeekNumber({year: 2021, month: 4, day: 8})).toBe(14);+ expect(plainDateGetWeekNumber({year: 2021, month: 4, day: 11})).toBe(+ 14,+ );+ },+ );++ it('keeps late-December weeks correct in Australia/Sydney', () => {+ process.env.TZ = 'Australia/Sydney';+ // 2027-12-30 (Thursday) is ISO week 52, not 53.+ expect(plainDateGetWeekNumber({year: 2027, month: 12, day: 30})).toBe(52);+ });+ });+it('keeps years 1-99 instead of mapping them to 1900-1999', () => {// 0004-01-01 is a Thursday (ISO week 1); 1904-01-01 was a Friday.expect(plainDateGetWeekNumber({year: 4, month: 1, day: 1})).toBe(1);diff --git a/packages/core/src/utils/plainDate.ts b/packages/core/src/utils/plainDate.tsindex 1fbb1d6f81..e956c68788 100644--- a/packages/core/src/utils/plainDate.ts+++ b/packages/core/src/utils/plainDate.ts@@ -282,11 +282,21 @@ export function plainDateSetEndOfWeekExclusive(}export function plainDateGetWeekNumber(pd: PlainDate): number {- const d = plainDateToDate(pd);- const dayNum = d.getDay() || 7;- d.setDate(d.getDate() + 4 - dayNum);- const yearStart = plainDateToDate({year: d.getFullYear(), month: 1, day: 1});- return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);+ // ISO week number: ceil(day-of-year of the week's Thursday / 7). Computed+ // entirely on UTC anchors — the same DST immunity plainDateDiffDays relies+ // on — because mixing local-time instants leaks the host zone's DST offset+ // into the day count. When Jan 1 is a Friday (so every Thursday's day-of-year+ // is an exact multiple of 7), a one-hour offset from a zone whose DST ends+ // after New Year rounds a whole week up: Sydney, Auckland and Santiago users+ // saw week 15 for the week that is ISO week 14. utcTime keeps years 1-99+ // as given, like the other Calendar date helpers.+ const msPerDay = 24 * 60 * 60 * 1000;+ const utcMidnight = utcTime(pd.year, pd.month - 1, pd.day);+ const dayNum = new Date(utcMidnight).getUTCDay() || 7;+ const thursday = utcMidnight + (4 - dayNum) * msPerDay;+ const yearStart = utcTime(new Date(thursday).getUTCFullYear(), 0, 1);+ const dayOfYear = (thursday - yearStart) / msPerDay + 1;+ return Math.ceil(dayOfYear / 7);}// e.g. "Wednesday, May 21, 2026" (locale-dependent)