Skip to content

Commit d8fc08b

Browse files
KurisuLabsclaude
andcommitted
fix(web): Usage hourly labels follow the Time format setting
The Past 24 hours window label, chart axis, chart tooltip, breakdown table and model dialog chart now use the Time format setting. 24-hour labels read `14:00`. Mobile and other callers keep the 12-hour labels. Fixes #15570 Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent eac52f0 commit d8fc08b

7 files changed

Lines changed: 121 additions & 19 deletions

File tree

‎apps/web/src/components/usage/UsageModelDialog.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface UsageChartWindow {
3131
readonly resolution: "day" | "hour";
3232
readonly timeZone: string;
3333
readonly referenceTime: string | undefined;
34+
readonly hour12: boolean;
3435
}
3536

3637
/**
@@ -117,6 +118,7 @@ export function UsageModelDialog({
117118
referenceTime={chartWindow.referenceTime}
118119
resolution={chartWindow.resolution}
119120
timeZone={chartWindow.timeZone}
121+
hour12={chartWindow.hour12}
120122
/>
121123

122124
<div className="grid gap-x-10 gap-y-8 sm:grid-cols-2">

‎apps/web/src/components/usage/UsagePage.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from "@t3tools/shared/usageMerge";
3131

3232
import { isElectron } from "../../env";
33+
import { usePrimarySettings } from "../../hooks/useSettings";
3334
import { cn } from "../../lib/utils";
3435
import { environmentPresentations } from "../../state/presentation";
3536
import { primaryServerKeybindingsAtom, serverEnvironment } from "../../state/server";
@@ -38,6 +39,7 @@ import { isModelPickerOpen } from "../../modelPickerVisibility";
3839
import { shortcutLabelForCommand } from "../../keybindings";
3940
import { useUsage, type EnvironmentUsageStatus } from "../../state/usage";
4041
import { useAtomCommand } from "../../state/use-atom-command";
42+
import { resolveHour12 } from "../../timestampFormat";
4143
import {
4244
enumerateDays,
4345
enumerateHourStarts,
@@ -113,6 +115,7 @@ export function UsagePage() {
113115
const [preferences, setPreferences] = useState(readUsagePagePreferences);
114116
useEscapeToGoBack();
115117
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
118+
const hour12 = usePrimarySettings((settings) => resolveHour12(settings.timestampFormat));
116119
const shortcutTitle = (
117120
option: (typeof METRIC_OPTIONS)[number] | (typeof WINDOW_OPTIONS)[number],
118121
) => {
@@ -321,7 +324,7 @@ export function UsagePage() {
321324

322325
const windowLabel =
323326
isPast24Hours && window.sinceTime !== undefined && window.untilTime !== undefined
324-
? `${formatDateTimeShort(window.sinceTime, window.timeZone)} to ${formatDateTimeShort(window.untilTime, window.timeZone)}`
327+
? `${formatDateTimeShort(window.sinceTime, window.timeZone, hour12)} to ${formatDateTimeShort(window.untilTime, window.timeZone, hour12)}`
325328
: `${formatDayShort(window.sinceDay)} to ${formatDayShort(window.untilDay)}`;
326329
const topbarContent = (
327330
<div className="grid w-full min-w-0 grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 gap-y-2 py-2 xl:flex">
@@ -623,6 +626,7 @@ export function UsagePage() {
623626
referenceTime={window.untilTime}
624627
resolution={isPast24Hours ? "hour" : "day"}
625628
timeZone={window.timeZone}
629+
hour12={hour12}
626630
/>
627631
</div>
628632
</section>
@@ -812,7 +816,7 @@ export function UsagePage() {
812816
>
813817
<td className="py-2 text-foreground">
814818
{"hourStart" in period
815-
? formatHourShort(period.hourStart, window.timeZone)
819+
? formatHourShort(period.hourStart, window.timeZone, hour12)
816820
: formatDayShort(period.day)}
817821
</td>
818822
{activeProviders.map((provider) => (
@@ -852,6 +856,7 @@ export function UsagePage() {
852856
resolution: isPast24Hours ? "hour" : "day",
853857
timeZone: window.timeZone,
854858
referenceTime: window.untilTime,
859+
hour12,
855860
}}
856861
onSetPrice={() => {
857862
setSelectedModelKey(null);

‎apps/web/src/components/usage/UsageProviderChart.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface UsageProviderChartProps {
2929
readonly referenceTime: string | undefined;
3030
readonly resolution: "day" | "hour";
3131
readonly timeZone: string;
32+
readonly hour12: boolean;
3233
}
3334

3435
/** One day's per-provider values, shared by the paths and the hover readout. */
@@ -180,6 +181,7 @@ export function UsageProviderChart({
180181
referenceTime,
181182
resolution,
182183
timeZone,
184+
hour12,
183185
}: UsageProviderChartProps) {
184186
const periods = resolution === "hour" ? hours : days;
185187
const byPeriod = useMemo(
@@ -309,10 +311,10 @@ export function UsageProviderChart({
309311
const hoveredPeriod = hoverIndex === null ? undefined : periods[hoverIndex];
310312
const hoveredColumn = hoverIndex === null ? undefined : series[hoverIndex];
311313
const formatPeriod = (period: string) =>
312-
resolution === "hour" ? formatHourShort(period, timeZone) : formatDayShort(period);
314+
resolution === "hour" ? formatHourShort(period, timeZone, hour12) : formatDayShort(period);
313315
const formatTooltipPeriod = (period: string) =>
314316
resolution === "hour" && referenceTime !== undefined
315-
? formatRelativeHourShort(period, referenceTime, timeZone)
317+
? formatRelativeHourShort(period, referenceTime, timeZone, hour12)
316318
: formatPeriod(period);
317319

318320
return (

‎apps/web/src/timestampFormat.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ describe("formatShortTimestamp", () => {
5656
});
5757
});
5858

59+
describe("resolveHour12", () => {
60+
afterEach(() => {
61+
vi.unstubAllGlobals();
62+
vi.resetModules();
63+
});
64+
65+
it.each([
66+
["en-GB", false],
67+
["en-US", true],
68+
])(
69+
"follows the %s clock for System default and the explicit settings",
70+
async (locale, hour12) => {
71+
vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => locale } });
72+
vi.resetModules();
73+
const { resolveHour12: resolve } = await import("./timestampFormat");
74+
expect(resolve("locale")).toBe(hour12);
75+
expect(resolve("12-hour")).toBe(true);
76+
expect(resolve("24-hour")).toBe(false);
77+
},
78+
);
79+
});
80+
5981
describe("resolveWeekStartsOn", () => {
6082
it.each([
6183
["en-US", 0],

‎apps/web/src/timestampFormat.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ function readHostSystemLocale(): string | null {
5353

5454
const timestampLocale = resolveTimestampLocale(readHostSystemLocale());
5555

56+
const localeUsesHour12 =
57+
new Intl.DateTimeFormat(timestampLocale, { hour: "numeric" }).resolvedOptions().hour12 ?? false;
58+
59+
/**
60+
* Whether a time format setting shows a 12-hour clock. "System default"
61+
* follows the same locale as the other timestamps. For formatters that take
62+
* an explicit `hour12`, such as the usage page's hourly labels.
63+
*/
64+
export function resolveHour12(timestampFormat: TimestampFormat): boolean {
65+
if (timestampFormat === "locale") return localeUsesHour12;
66+
return timestampFormat === "12-hour";
67+
}
68+
5669
const WEEKDAY_INDEXES = [0, 1, 2, 3, 4, 5, 6] as const;
5770
type WeekdayIndex = (typeof WEEKDAY_INDEXES)[number];
5871

‎packages/shared/src/usageFormat.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ describe("hourly usage formatting", () => {
6565
expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
6666
});
6767

68+
it("follows an explicit hour cycle and keeps the 12-hour default without one", () => {
69+
const afternoon = "2026-08-11T14:37:00.000Z";
70+
const midnight = "2026-08-11T00:37:00.000Z";
71+
72+
expect(formatHourShort(afternoon, "UTC")).toBe("2 PM");
73+
expect(formatHourShort(afternoon, "UTC", true)).toBe("2 PM");
74+
expect(formatHourShort(afternoon, "UTC", false)).toBe("14:00");
75+
expect(formatHourShort(midnight, "UTC", true)).toBe("12 AM");
76+
expect(formatHourShort(midnight, "UTC", false)).toBe("00:00");
77+
78+
expect(formatDateTimeShort(afternoon, "UTC")).toBe("Aug 11, 2 PM");
79+
expect(formatDateTimeShort(afternoon, "UTC", true)).toBe("Aug 11, 2 PM");
80+
expect(formatDateTimeShort(afternoon, "UTC", false)).toBe("Aug 11, 14:00");
81+
82+
const windowEnd = "2026-08-11T15:37:00.000Z";
83+
expect(formatRelativeHourShort(afternoon, windowEnd, "UTC")).toBe("2 PM today");
84+
expect(formatRelativeHourShort(afternoon, windowEnd, "UTC", true)).toBe("2 PM today");
85+
expect(formatRelativeHourShort(afternoon, windowEnd, "UTC", false)).toBe("14:00 today");
86+
expect(formatRelativeHourShort("2026-08-10T17:37:00.000Z", windowEnd, "UTC", false)).toBe(
87+
"17:00 yesterday",
88+
);
89+
expect(formatRelativeHourShort("2026-08-08T17:37:00.000Z", windowEnd, "UTC", false)).toBe(
90+
"Aug 8, 17:00",
91+
);
92+
});
93+
94+
it("disambiguates repeated hours in the requested hour cycle", () => {
95+
const daylight = "2026-11-01T05:37:00.000Z";
96+
const standard = "2026-11-01T06:37:00.000Z";
97+
98+
expect(formatHourShort(daylight, "America/New_York", true)).toBe("1 AM EDT");
99+
expect(formatHourShort(standard, "America/New_York", true)).toBe("1 AM EST");
100+
expect(formatHourShort(daylight, "America/New_York", false)).toBe("01:00 EDT");
101+
expect(formatHourShort(standard, "America/New_York", false)).toBe("01:00 EST");
102+
});
103+
68104
it("makes hourly tooltip dates relative to the window in its requested time zone", () => {
69105
const windowEnd = "2026-08-11T14:37:00.000Z";
70106

‎packages/shared/src/usageFormat.ts‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,38 @@ export function enumerateHourStarts(sinceTime: string, untilTime: string): reado
126126
return starts;
127127
}
128128

129+
/** Hour fields for the hourly labels. Without `hour12` they keep the `en-US` 12-hour clock. */
130+
function hourOptions(hour12: boolean | undefined): Intl.DateTimeFormatOptions {
131+
if (hour12 === undefined) return { hour: "numeric" };
132+
return hour12 ? { hour: "numeric", hourCycle: "h12" } : { hour: "2-digit", hourCycle: "h23" };
133+
}
134+
135+
/**
136+
* Formats an hour label. Like `2 PM`, a 24-hour label names the whole hour, so
137+
* it reads `14:00` even when the rolling bucket starts at 14:37. A bare `14`
138+
* does not read as a time.
139+
*/
140+
function formatHour(format: Intl.DateTimeFormat, date: Date, hour12: boolean | undefined): string {
141+
if (hour12 !== false) return format.format(date);
142+
return format
143+
.formatToParts(date)
144+
.map((part) => (part.type === "hour" ? `${part.value}:00` : part.value))
145+
.join("");
146+
}
147+
129148
/**
130149
* A rolling bucket start rendered in the viewer's requested time zone.
131150
*
132151
* Repeated wall-clock hours during a fall-back transition include their short
133152
* zone name so the two distinct buckets remain distinguishable.
134153
*/
135-
export function formatHourShort(hourStart: string, timeZone?: string): string {
154+
export function formatHourShort(hourStart: string, timeZone?: string, hour12?: boolean): string {
136155
const instant = new Date(hourStart);
137156
if (Number.isNaN(instant.getTime())) return hourStart;
138157
const options = timeZone === undefined ? {} : { timeZone };
139158
const hourFormat = dateTimeFormatter("en-US", {
140159
...options,
141-
hour: "numeric",
160+
...hourOptions(hour12),
142161
});
143162
const wallHourFormat = dateTimeFormatter("en-CA", {
144163
...options,
@@ -153,36 +172,39 @@ export function formatHourShort(hourStart: string, timeZone?: string): string {
153172
(offset) => wallHourFormat.format(new Date(instant.getTime() + offset)) === wallHour,
154173
);
155174

156-
if (!isRepeatedHour) return hourFormat.format(instant);
157-
return dateTimeFormatter("en-US", {
158-
...(timeZone === undefined ? {} : { timeZone }),
159-
hour: "numeric",
175+
if (!isRepeatedHour) return formatHour(hourFormat, instant, hour12);
176+
const zonedHourFormat = dateTimeFormatter("en-US", {
177+
...options,
178+
...hourOptions(hour12),
160179
timeZoneName: "short",
161-
}).format(instant);
180+
});
181+
return formatHour(zonedHourFormat, instant, hour12);
162182
}
163183

164-
/** `2026-08-11T14:37:00Z` to `Aug 11, 2 PM` in the requested zone. */
165-
export function formatDateTimeShort(instant: string, timeZone?: string): string {
184+
/** `2026-08-11T14:37:00Z` to `Aug 11, 2 PM` (or `Aug 11, 14:00`) in the requested zone. */
185+
export function formatDateTimeShort(instant: string, timeZone?: string, hour12?: boolean): string {
166186
const date = new Date(instant);
167187
if (Number.isNaN(date.getTime())) return instant;
168-
return dateTimeFormatter("en-US", {
188+
const dateHourFormat = dateTimeFormatter("en-US", {
169189
...(timeZone === undefined ? {} : { timeZone }),
170190
month: "short",
171191
day: "numeric",
172-
hour: "numeric",
173-
}).format(date);
192+
...hourOptions(hour12),
193+
});
194+
return formatHour(dateHourFormat, date, hour12);
174195
}
175196

176197
/** An hourly tooltip label relative to the rolling window's end date. */
177198
export function formatRelativeHourShort(
178199
hourStart: string,
179200
relativeTo: string,
180201
timeZone?: string,
202+
hour12?: boolean,
181203
): string {
182204
const instant = new Date(hourStart);
183205
const reference = new Date(relativeTo);
184206
if (Number.isNaN(instant.getTime()) || Number.isNaN(reference.getTime())) {
185-
return formatDateTimeShort(hourStart, timeZone);
207+
return formatDateTimeShort(hourStart, timeZone, hour12);
186208
}
187209

188210
const dayFormat = dateTimeFormatter("en-CA", {
@@ -194,11 +216,11 @@ export function formatRelativeHourShort(
194216
const instantDay = Date.parse(`${dayFormat.format(instant)}T00:00:00Z`);
195217
const referenceDay = Date.parse(`${dayFormat.format(reference)}T00:00:00Z`);
196218
const calendarDaysAgo = Math.round((referenceDay - instantDay) / (24 * HOUR_MS));
197-
const hour = formatHourShort(hourStart, timeZone);
219+
const hour = formatHourShort(hourStart, timeZone, hour12);
198220

199221
if (calendarDaysAgo === 0) return `${hour} today`;
200222
if (calendarDaysAgo === 1) return `${hour} yesterday`;
201-
return formatDateTimeShort(hourStart, timeZone);
223+
return formatDateTimeShort(hourStart, timeZone, hour12);
202224
}
203225

204226
/**

0 commit comments

Comments
 (0)