diff --git a/packages/lib/timeFormat.test.ts b/packages/lib/timeFormat.test.ts new file mode 100644 index 00000000000000..6e1a4f77e52670 --- /dev/null +++ b/packages/lib/timeFormat.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; + +vi.mock("@calcom/lib/webstorage", () => ({ + localStorage: { + getItem: vi.fn().mockReturnValue(null), + setItem: vi.fn(), + }, +})); + +import { isBrowserLocale24h } from "./timeFormat"; + +describe("isBrowserLocale24h", () => { + beforeEach(() => { + vi.restoreAllMocks(); + }); + + it("returns true for 24h hourCycle", () => { + vi.spyOn(Intl, "DateTimeFormat").mockImplementation( + function (): Intl.DateTimeFormat { + return { + resolvedOptions: () => ({ hourCycle: "h23" }), + } as unknown as Intl.DateTimeFormat; + } + ); + + expect(isBrowserLocale24h()).toBe(true); + }); + + it("returns false for 12h hourCycle", () => { + vi.spyOn(Intl, "DateTimeFormat").mockImplementation( + function (): Intl.DateTimeFormat { + return { + resolvedOptions: () => ({ hourCycle: "h12" }), + } as unknown as Intl.DateTimeFormat; + } + ); + + expect(isBrowserLocale24h()).toBe(false); + }); +}); \ No newline at end of file diff --git a/packages/lib/timeFormat.ts b/packages/lib/timeFormat.ts index 4864df4119b7bb..a3fb70569abcf5 100644 --- a/packages/lib/timeFormat.ts +++ b/packages/lib/timeFormat.ts @@ -41,14 +41,14 @@ export const isBrowserLocale24h = () => { } else if (localStorageTimeFormat === false) { return false; } - // Intl.DateTimeFormat with value=undefined uses local browser settings. - if (!!new Intl.DateTimeFormat(undefined, { hour: "numeric" }).format(0).match(/M/i)) { - setIs24hClockInLocalStorage(false); - return false; - } else { - setIs24hClockInLocalStorage(true); - return true; - } + + const formatter = new Intl.DateTimeFormat(undefined, { hour: "numeric" }); + const hourCycle = (formatter.resolvedOptions() as { hourCycle?: string }).hourCycle; + + const is24h = hourCycle === "h23" || hourCycle === "h24"; + + setIs24hClockInLocalStorage(is24h); + return is24h; }; /**