fix(shared): degrade an unknown system time zone to UTC in usage windows (#6670)
This commit is contained in:
1 parent
40ab7bf32a
commit
684d703b0a
2 files changed
+36
-8
No files matched your search
@@ -1,5 +1,5 @@
|
||||
// @effect-diagnostics globalDate:off -- A fixed instant keeps calendar-window assertions deterministic.
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import { describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
enumerateHourStarts,
|
||||
@@ -54,4 +54,20 @@ describe("hourly usage formatting", () => {
|
||||
expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
|
||||
expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
|
||||
});
|
||||
|
||||
it("degrades an unknown resolved zone to UTC instead of crashing", () => {
|
||||
const resolved = new Intl.DateTimeFormat().resolvedOptions();
|
||||
const resolvedOptions = vi
|
||||
.spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions")
|
||||
.mockReturnValue({ ...resolved, timeZone: "Etc/Unknown" });
|
||||
|
||||
try {
|
||||
const now = new Date("2026-08-11T12:37:42.123Z");
|
||||
|
||||
expect(makeWindow(1, now, "hour").timeZone).toBe("UTC");
|
||||
expect(makeWindow(30, now).timeZone).toBe("UTC");
|
||||
} finally {
|
||||
resolvedOptions.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -179,13 +179,25 @@ export function makeWindow(
|
||||
now = new Date(),
|
||||
resolution: UsageResolution = "day",
|
||||
): UsageSummaryInput {
|
||||
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
||||
const format = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
});
|
||||
let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
||||
let format: Intl.DateTimeFormat;
|
||||
try {
|
||||
format = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
});
|
||||
} catch {
|
||||
// An unknown zone should degrade to UTC rather than crash the page.
|
||||
timeZone = "UTC";
|
||||
format = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone: "UTC",
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
});
|
||||
}
|
||||
const untilDay = format.format(now);
|
||||
if (resolution === "hour") {
|
||||
// Minute-aligned bounds keep labels readable while still representing an
|
||||
|
||||
Reference in new issue
Block a user