Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Assemble calendar dates from formatToParts for small-ICU builds (#2900)
Luke Vellae735e96815d7
diff --git a/apps/web/src/lib/datetime/utils.test.ts b/apps/web/src/lib/datetime/utils.test.tsindex 037d0fecd..356606b0f 100644--- a/apps/web/src/lib/datetime/utils.test.ts+++ b/apps/web/src/lib/datetime/utils.test.ts@@ -41,6 +41,26 @@ describe("getCalendarDate", () => {getCalendarDate(new Date("2025-07-01T20:00:00Z"), "Asia/Tokyo"),).toBe("2025-07-02");});++ it("returns an ISO date on ICU builds without the requested locale", () => {+ // Small-ICU Node builds (e.g. Alpine's icu-data-en) silently resolve any+ // requested locale to "en", whose date pattern is MM/DD/YYYY.+ const RealDateTimeFormat = Intl.DateTimeFormat;+ class EnOnlyDateTimeFormat extends RealDateTimeFormat {+ constructor(+ _locales?: string | string[],+ options?: Intl.DateTimeFormatOptions,+ ) {+ super("en", options);+ }+ }+ Intl.DateTimeFormat = EnOnlyDateTimeFormat as typeof Intl.DateTimeFormat;+ try {+ expect(getCalendarDate(instant, "UTC")).toBe("2025-07-02");+ } finally {+ Intl.DateTimeFormat = RealDateTimeFormat;+ }+ });});describe("calendarDateToUTCMidnight", () => {diff --git a/apps/web/src/lib/datetime/utils.ts b/apps/web/src/lib/datetime/utils.tsindex 251a04e4b..c65363571 100644--- a/apps/web/src/lib/datetime/utils.ts+++ b/apps/web/src/lib/datetime/utils.ts@@ -14,15 +14,20 @@ export function toISODate(value: DateInput) {/*** The calendar date (YYYY-MM-DD) at the given instant in the given zone.- * en-CA is the locale whose date format is ISO 8601.+ * Assembled from formatToParts rather than a locale's date pattern: small-ICU+ * Node builds (e.g. Alpine's icu-data-en) resolve unavailable locales to "en",+ * whose pattern is MM/DD/YYYY, so no locale can be trusted to format ISO 8601.*/export function getCalendarDate(now: Date, timeZone: string): string {- return new Intl.DateTimeFormat("en-CA", {+ const parts = new Intl.DateTimeFormat("en-CA", {timeZone,year: "numeric",month: "2-digit",day: "2-digit",- }).format(now);+ }).formatToParts(now);+ const get = (type: Intl.DateTimeFormatPartTypes) =>+ parts.find((part) => part.type === type)?.value;+ return `${get("year")}-${get("month")}-${get("day")}`;}/**