Skip to content
Open
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
40 changes: 40 additions & 0 deletions packages/lib/timeFormat.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
16 changes: 8 additions & 8 deletions packages/lib/timeFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Comment thread
coderabbitai[bot] marked this conversation as resolved.

setIs24hClockInLocalStorage(is24h);
return is24h;
};

/**
Expand Down
Loading