The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 01:45 UTC
fix(web): Usage hourly labels follow the Time format setting (#15575)
Co-authored-by: kurisu <kurisu@nerv.no> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Martin Bergo committed
Oct 19, 2026 at 19:30 UTC
6cfd82fe1f342aa42f61248c7bae2332d5ed7f48
7 files changed
+82
-22
apps/web/src/components/usage/UsageModelDialog.tsx
+2
@@ -30,6 +30,7 @@ export interface UsageChartWindow {
30
readonly resolution: "day" | "hour";
31
readonly timeZone: string;
32
readonly referenceTime: string | undefined;
33
+ readonly hour12: boolean;
34
}
35
36
export function UsageModelDialog({
@@ -107,6 +108,7 @@ export function UsageModelDialog({
108
referenceTime={chartWindow.referenceTime}
109
resolution={chartWindow.resolution}
110
timeZone={chartWindow.timeZone}
111
+ hour12={chartWindow.hour12}
112
/>
113
114
<div className="grid gap-x-10 gap-y-8 sm:grid-cols-2">
apps/web/src/components/usage/UsagePage.tsx
+10
-2
@@ -35,6 +35,8 @@ import {
35
36
import { isElectron } from "../../env";
37
import { cn } from "../../lib/utils";
38
+import { usePrimarySettings } from "../../hooks/useSettings";
39
+import { resolveHour12 } from "../../timestampFormat";
40
import { appAtomRegistry } from "~/rpc/atomRegistry";
41
import { environmentPresentations } from "../../state/presentation";
42
import { primaryServerKeybindingsAtom, serverEnvironment } from "../../state/server";
@@ -180,6 +182,7 @@ export function UsagePage() {
182
const [preferences, setPreferences] = useState(readUsagePagePreferences);
183
useEscapeToGoBack();
184
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
185
+ const hour12 = usePrimarySettings((settings) => resolveHour12(settings.timestampFormat));
186
const shortcutTitle = (
187
option: (typeof METRIC_OPTIONS)[number] | (typeof WINDOW_OPTIONS)[number],
188
) => {
@@ -356,7 +359,7 @@ export function UsagePage() {
359
360
const windowLabel =
361
shownHourly && shownWindow.sinceTime !== undefined && shownWindow.untilTime !== undefined
359
- ? `${formatDateTimeShort(shownWindow.sinceTime, shownWindow.timeZone)} to ${formatDateTimeShort(shownWindow.untilTime, shownWindow.timeZone)}`
362
+ ? `${formatDateTimeShort(shownWindow.sinceTime, shownWindow.timeZone, hour12)} to ${formatDateTimeShort(shownWindow.untilTime, shownWindow.timeZone, hour12)}`
363
: `${formatDayShort(shownWindow.sinceDay)} to ${formatDayShort(shownWindow.untilDay)}`;
364
const topbarContent = (
365
<div className="grid w-full min-w-0 grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 py-2 xl:flex">
@@ -546,6 +549,7 @@ export function UsagePage() {
549
<UsageOverview
550
merged={merged}
551
shownWindow={shownWindow}
552
+ hour12={hour12}
553
days={days}
554
hours={hours}
555
showingKept={showingKept}
@@ -576,6 +580,7 @@ export function UsagePage() {
580
resolution: shownHourly ? "hour" : "day",
581
timeZone: shownWindow.timeZone,
582
referenceTime: shownWindow.untilTime,
583
+ hour12,
584
}}
585
onSetPrice={(model) => setPriceDialog({ model })}
586
/>
@@ -597,6 +602,7 @@ export function UsagePage() {
602
function UsageOverview({
603
merged,
604
shownWindow,
605
+ hour12,
606
days,
607
hours,
608
showingKept,
@@ -611,6 +617,7 @@ function UsageOverview({
617
}: {
618
readonly merged: MergedUsage;
619
readonly shownWindow: UsageSummaryInput;
620
+ readonly hour12: boolean;
621
readonly days: readonly string[];
622
readonly hours: readonly string[];
623
readonly showingKept: boolean;
@@ -826,6 +833,7 @@ function UsageOverview({
833
referenceTime={shownWindow.untilTime}
834
resolution={shownHourly ? "hour" : "day"}
835
timeZone={shownWindow.timeZone}
836
+ hour12={hour12}
837
/>
838
</div>
839
</section>
@@ -1027,7 +1035,7 @@ function UsageOverview({
1035
>
1036
<td className="py-2 text-foreground">
1037
{"hourStart" in period
1030
- ? formatHourShort(period.hourStart, shownWindow.timeZone)
1038
+ ? formatHourShort(period.hourStart, shownWindow.timeZone, hour12)
1039
: formatDayShort(period.day)}
1040
</td>
1041
{activeProviders.map((provider) => (
apps/web/src/components/usage/UsageProviderChart.tsx
+4
-2
@@ -33,6 +33,7 @@ interface UsageProviderChartProps {
33
readonly referenceTime: string | undefined;
34
readonly resolution: "day" | "hour";
35
readonly timeZone: string;
36
+ readonly hour12: boolean;
37
}
38
39
export interface DayColumn {
@@ -417,6 +418,7 @@ export function UsageProviderChart({
418
referenceTime,
419
resolution,
420
timeZone,
421
+ hour12,
422
}: UsageProviderChartProps) {
423
const periods = resolution === "hour" ? hours : days;
424
const totals: readonly (DailyTotals | HourlyTotals)[] = resolution === "hour" ? hourly : daily;
@@ -436,10 +438,10 @@ export function UsageProviderChart({
438
cn("transition-opacity", loading && "opacity-40 delay-150");
439
const format = metric === "tokens" ? formatTokens : formatUsd;
440
const formatPeriod = (period: string) =>
439
- resolution === "hour" ? formatHourShort(period, timeZone) : formatDayShort(period);
441
+ resolution === "hour" ? formatHourShort(period, timeZone, hour12) : formatDayShort(period);
442
const formatTooltipPeriod = (period: string) =>
443
resolution === "hour" && referenceTime !== undefined
442
- ? formatRelativeHourShort(period, referenceTime, timeZone)
444
+ ? formatRelativeHourShort(period, referenceTime, timeZone, hour12)
445
: formatPeriod(period);
446
447
return (
apps/web/src/timestampFormat.test.ts
+8
-4
@@ -1,3 +1,4 @@
1
+import { formatHourShort } from "@t3tools/shared/usageFormat";
2
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4
import {
@@ -40,17 +41,20 @@ describe("formatShortTimestamp", () => {
41
});
42
43
it.each([
43
- ["en-GB", "15:44"],
44
- ["en-US", "3:44 PM"],
45
- ])("honors %s and the explicit hour-cycle settings", async (locale, localTime) => {
44
+ ["en-GB", "15:44", "15:00"],
45
+ ["en-US", "3:44 PM", "3 PM"],
46
+ ])("honors %s and the explicit hour-cycle settings", async (locale, localTime, localHour) => {
47
vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => locale } });
48
vi.resetModules();
48
- const { formatShortTimestamp: format } = await import("./timestampFormat");
49
+ const { formatShortTimestamp: format, resolveHour12 } = await import("./timestampFormat");
50
const date = new Date(2026, 3, 7, 15, 44).toISOString();
51
// ICU can separate the day period with a narrow no-break space.
52
expect(format(date, "locale").replace(/[ ]/g, " ")).toBe(localTime);
53
expect(format(date, "12-hour").replace(/[ ]/g, " ")).toMatch(/^3:44 [ap]m$/i);
54
expect(format(date, "24-hour")).toBe("15:44");
55
+ expect(formatHourShort(date, undefined, resolveHour12("locale"))).toBe(localHour);
56
+ expect(formatHourShort(date, undefined, resolveHour12("12-hour"))).toBe("3 PM");
57
+ expect(formatHourShort(date, undefined, resolveHour12("24-hour"))).toBe("15:00");
58
});
59
});
60
apps/web/src/timestampFormat.ts
+4
@@ -93,6 +93,10 @@ function getTimestampFormatter(
93
return formatter;
94
}
95
96
+export function resolveHour12(timestampFormat: TimestampFormat): boolean {
97
+ return getTimestampFormatter(timestampFormat, false).resolvedOptions().hour12 ?? false;
98
+}
99
+
100
export function parseTimestampDate(isoDate: string): Date | null {
101
const date = new Date(isoDate);
102
return Number.isNaN(date.getTime()) ? null : date;
packages/shared/src/usageFormat.test.ts
+24
@@ -63,6 +63,30 @@ describe("hourly usage formatting", () => {
63
it("disambiguates repeated hours during a fall-back transition", () => {
64
expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
65
expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
66
+ expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York", false)).toBe(
67
+ "01:00 EDT",
68
+ );
69
+ expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York", false)).toBe(
70
+ "01:00 EST",
71
+ );
72
+ });
73
+
74
+ it.each([
75
+ [true, "2 PM", "12 AM"],
76
+ [false, "14:00", "00:00"],
77
+ ])("formats hourly usage labels with hour12=%s", (hour12, afternoon, midnight) => {
78
+ const instant = "2026-08-11T14:37:00.000Z";
79
+
80
+ expect(formatHourShort(instant, "UTC", hour12)).toBe(afternoon);
81
+ expect(formatHourShort("2026-08-11T00:37:00.000Z", "UTC", hour12)).toBe(midnight);
82
+ expect(formatDateTimeShort(instant, "UTC", hour12)).toBe(`Aug 11, ${afternoon}`);
83
+ expect(formatRelativeHourShort(instant, instant, "UTC", hour12)).toBe(`${afternoon} today`);
84
+ expect(formatRelativeHourShort(instant, "2026-08-12T15:37:00.000Z", "UTC", hour12)).toBe(
85
+ `${afternoon} yesterday`,
86
+ );
87
+ expect(formatRelativeHourShort(instant, "2026-08-14T15:37:00.000Z", "UTC", hour12)).toBe(
88
+ `Aug 11, ${afternoon}`,
89
+ );
90
});
91
92
it("makes hourly tooltip dates relative to the window in its requested time zone", () => {
packages/shared/src/usageFormat.ts
+30
-14
@@ -114,17 +114,30 @@ export function enumerateHourStarts(sinceTime: string, untilTime: string): reado
114
return starts;
115
}
116
117
+function hourOptions(hour12: boolean): Intl.DateTimeFormatOptions {
118
+ return hour12 ? { hour: "numeric" } : { hour: "2-digit", hourCycle: "h23" };
119
+}
120
+
121
+function formatHour(formatter: Intl.DateTimeFormat, date: Date, hour12: boolean): string {
122
+ if (hour12) return formatter.format(date);
123
+ // Rolling buckets can start between whole hours, but their labels always name the hour.
124
+ return formatter
125
+ .formatToParts(date)
126
+ .map((part) => (part.type === "hour" ? `${part.value}:00` : part.value))
127
+ .join("");
128
+}
129
+
130
/**
131
* Repeated wall-clock hours during a fall-back transition include their short zone name so the
132
* two distinct buckets remain distinguishable.
133
*/
121
-export function formatHourShort(hourStart: string, timeZone?: string): string {
134
+export function formatHourShort(hourStart: string, timeZone?: string, hour12 = true): string {
135
const instant = new Date(hourStart);
136
if (Number.isNaN(instant.getTime())) return hourStart;
137
const options = timeZone === undefined ? {} : { timeZone };
138
const hourFormat = dateTimeFormatter("en-US", {
139
...options,
127
- hour: "numeric",
140
+ ...hourOptions(hour12),
141
});
142
const wallHourFormat = dateTimeFormatter("en-CA", {
143
...options,
@@ -139,34 +152,37 @@ export function formatHourShort(hourStart: string, timeZone?: string): string {
152
(offset) => wallHourFormat.format(new Date(instant.getTime() + offset)) === wallHour,
153
);
154
142
- if (!isRepeatedHour) return hourFormat.format(instant);
143
- return dateTimeFormatter("en-US", {
144
- ...(timeZone === undefined ? {} : { timeZone }),
145
- hour: "numeric",
155
+ if (!isRepeatedHour) return formatHour(hourFormat, instant, hour12);
156
+ const zonedHourFormat = dateTimeFormatter("en-US", {
157
+ ...options,
158
+ ...hourOptions(hour12),
159
timeZoneName: "short",
147
- }).format(instant);
160
+ });
161
+ return formatHour(zonedHourFormat, instant, hour12);
162
}
163
150
-export function formatDateTimeShort(instant: string, timeZone?: string): string {
164
+export function formatDateTimeShort(instant: string, timeZone?: string, hour12 = true): string {
165
const date = new Date(instant);
166
if (Number.isNaN(date.getTime())) return instant;
153
- return dateTimeFormatter("en-US", {
167
+ const dateHourFormat = dateTimeFormatter("en-US", {
168
...(timeZone === undefined ? {} : { timeZone }),
169
month: "short",
170
day: "numeric",
157
- hour: "numeric",
158
- }).format(date);
171
+ ...hourOptions(hour12),
172
+ });
173
+ return formatHour(dateHourFormat, date, hour12);
174
}
175
176
export function formatRelativeHourShort(
177
hourStart: string,
178
relativeTo: string,
179
timeZone?: string,
180
+ hour12 = true,
181
): string {
182
const instant = new Date(hourStart);
183
const reference = new Date(relativeTo);
184
if (Number.isNaN(instant.getTime()) || Number.isNaN(reference.getTime())) {
169
- return formatDateTimeShort(hourStart, timeZone);
185
+ return formatDateTimeShort(hourStart, timeZone, hour12);
186
}
187
188
const dayFormat = dateTimeFormatter("en-CA", {
@@ -178,11 +194,11 @@ export function formatRelativeHourShort(
194
const instantDay = Date.parse(`${dayFormat.format(instant)}T00:00:00Z`);
195
const referenceDay = Date.parse(`${dayFormat.format(reference)}T00:00:00Z`);
196
const calendarDaysAgo = Math.round((referenceDay - instantDay) / (24 * HOUR_MS));
181
- const hour = formatHourShort(hourStart, timeZone);
197
+ const hour = formatHourShort(hourStart, timeZone, hour12);
198
199
if (calendarDaysAgo === 0) return `${hour} today`;
200
if (calendarDaysAgo === 1) return `${hour} yesterday`;
185
- return formatDateTimeShort(hourStart, timeZone);
201
+ return formatDateTimeShort(hourStart, timeZone, hour12);
202
}
203
204
export function makeWindow(