Skip to content

Commit 689bdb8

Browse files
fix(web): usage-limit retry notice shows the timezone (#23029)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 30767c2 commit 689bdb8

5 files changed

Lines changed: 163 additions & 35 deletions

File tree

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,14 @@ const ThreadWorkLogRow = function ThreadWorkLogRow(
937937
const warning = failureItem.failure.class === "usage_limit";
938938
const timestamp = new Date(row.createdAt);
939939
const resetAt = failureItem.failure.resetAt;
940+
// The reset time is the viewer's local time; naming the zone keeps it unambiguous.
940941
const resetTime = resetAt
941-
? new Date(resetAt).toLocaleString(undefined, {
942+
? `${new Date(resetAt).toLocaleString(undefined, {
942943
month: "short",
943944
day: "numeric",
944945
hour: "numeric",
945946
minute: "2-digit",
946-
})
947+
})} (${new Intl.DateTimeFormat().resolvedOptions().timeZone})`
947948
: null;
948949
const label = warning
949950
? `Usage limit reached.${resetTime ? ` Retry after ${resetTime}.` : ""}`

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { flushSync } from "react-dom";
22
import { AudioPreview } from "../files/AudioPreview";
3-
import { page } from "vite-plus/test/browser";
3+
import { cdp, page } from "vite-plus/test/browser";
44
import {
55
ComposerContextId,
66
EnvironmentId,
@@ -565,6 +565,76 @@ describe("MessagesTimeline messages", () => {
565565
});
566566
});
567567

568+
function usageLimitEntries(resetAt: string) {
569+
const threadId = ThreadId.make("usage-limit-thread");
570+
const at = DateTime.makeUnsafe(CREATED_AT);
571+
return deriveTimelineEntriesFromVisibleTurnItems({
572+
visibleTurnItems: [
573+
{
574+
position: 0,
575+
visibility: "local",
576+
sourceThreadId: threadId,
577+
sourceItemId: TurnItemId.make("usage-limit"),
578+
item: {
579+
id: TurnItemId.make("usage-limit"),
580+
threadId,
581+
runId: RunId.make("usage-limit-run"),
582+
nodeId: null,
583+
providerThreadId: null,
584+
providerTurnId: null,
585+
nativeItemRef: null,
586+
parentItemId: null,
587+
ordinal: 0,
588+
title: null,
589+
startedAt: at,
590+
completedAt: at,
591+
updatedAt: at,
592+
type: "error",
593+
status: "failed",
594+
failure: {
595+
class: "usage_limit",
596+
message: "You've hit your usage limit.",
597+
code: null,
598+
retryable: true,
599+
resetAt,
600+
},
601+
},
602+
},
603+
],
604+
optimisticMessages: [],
605+
});
606+
}
607+
608+
describe("MessagesTimeline usage limit notice", () => {
609+
it("labels the retry time with the viewer's timezone", async () => {
610+
await using screen = await renderTimeline(usageLimitEntries("2026-03-17T19:30:00.000Z"));
611+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
612+
expect(screen.text()).toMatch(/Usage limit reached\. Retry after .+\./);
613+
expect(screen.text()).toContain(`(${timeZone})`);
614+
});
615+
616+
it("keeps the retry time in the named zone after the viewer's timezone changes", async () => {
617+
const resetAt = "2026-07-01T19:30:00.000Z";
618+
const session = cdp();
619+
await session.send("Emulation.setTimezoneOverride", { timezoneId: "Europe/Madrid" });
620+
try {
621+
{
622+
await using _madrid = await renderTimeline(usageLimitEntries(resetAt));
623+
}
624+
await session.send("Emulation.setTimezoneOverride", { timezoneId: "America/New_York" });
625+
await using screen = await renderTimeline(usageLimitEntries(resetAt));
626+
const newYorkTime = new Date(resetAt).toLocaleTimeString(undefined, {
627+
hour: "numeric",
628+
minute: "2-digit",
629+
timeZone: "America/New_York",
630+
});
631+
expect(screen.text()).toContain(`${newYorkTime} (America/New_York)`);
632+
} finally {
633+
await session.send("Emulation.setTimezoneOverride", { timezoneId: "" });
634+
}
635+
});
636+
});
637+
568638
const CITATION_REQUEST = {
569639
key: "activation-1",
570640
citation: {

‎apps/web/src/components/chat/TimelineWorkEntryRow.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,12 @@ function WorkEntryLogRow(props: WorkEntryRowProps) {
209209
if (failureItem?.type === "error" && failureItem.status === "failed") {
210210
const warning = failureItem.failure.class === "usage_limit";
211211
const resetAt = failureItem.failure.resetAt;
212-
const resetTime = resetAt ? formatUpcomingTimestamp(resetAt, timestampFormat) : null;
212+
// The reset time is the viewer's local time; naming the zone keeps it
213+
// unambiguous. Both read the zone now, since the device's can change.
214+
const timeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
215+
const resetTime = resetAt
216+
? `${formatUpcomingTimestamp(resetAt, timestampFormat, undefined, timeZone)} (${timeZone})`
217+
: null;
213218
const label = warning
214219
? `Usage limit reached.${resetTime ? ` Retry after ${resetTime}.` : ""}`
215220
: workEntry.label;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { formatDayAwareTimestamp, formatUpcomingTimestamp } from "./timestampFormat";
4+
5+
describe("timestamps in an explicit time zone", () => {
6+
// 23:00 UTC on July 1 is 7 PM on July 1 in New York, whatever the host zone.
7+
const now = Date.parse("2026-07-01T23:00:00.000Z");
8+
const zone = "America/New_York";
9+
10+
it("picks relative days from the calendar in that zone", () => {
11+
expect(formatUpcomingTimestamp("2026-07-02T00:30:00.000Z", "12-hour", now, zone)).not.toMatch(
12+
/tomorrow/,
13+
);
14+
expect(formatUpcomingTimestamp("2026-07-02T05:30:00.000Z", "12-hour", now, zone)).toMatch(
15+
/^tomorrow at /,
16+
);
17+
expect(formatDayAwareTimestamp("2026-07-01T03:30:00.000Z", "12-hour", now, zone)).toMatch(
18+
/^yesterday at /,
19+
);
20+
});
21+
});

‎apps/web/src/timestampFormat.ts‎

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,18 @@ const timestampFormatterCache = new Map<string, Intl.DateTimeFormat>();
7373
function getTimestampFormatter(
7474
timestampFormat: TimestampFormat,
7575
includeSeconds: boolean,
76+
timeZone?: string,
7677
): Intl.DateTimeFormat {
77-
const cacheKey = `${timestampFormat}:${includeSeconds ? "seconds" : "minutes"}`;
78+
const cacheKey = `${timestampFormat}:${includeSeconds ? "seconds" : "minutes"}:${timeZone ?? ""}`;
7879
const cachedFormatter = timestampFormatterCache.get(cacheKey);
7980
if (cachedFormatter) {
8081
return cachedFormatter;
8182
}
8283

83-
const formatter = new Intl.DateTimeFormat(
84-
timestampLocale,
85-
getTimestampFormatOptions(timestampFormat, includeSeconds),
86-
);
84+
const formatter = new Intl.DateTimeFormat(timestampLocale, {
85+
...getTimestampFormatOptions(timestampFormat, includeSeconds),
86+
...(timeZone ? { timeZone } : {}),
87+
});
8788
timestampFormatterCache.set(cacheKey, formatter);
8889
return formatter;
8990
}
@@ -136,58 +137,88 @@ export function formatShortTimestamp(isoDate: string, timestampFormat: Timestamp
136137
return getTimestampFormatter(timestampFormat, false).format(date);
137138
}
138139

139-
const numericDateFormatter = new Intl.DateTimeFormat(timestampLocale, {
140-
month: "numeric",
141-
day: "numeric",
142-
});
143-
const numericDateWithYearFormatter = new Intl.DateTimeFormat(timestampLocale, {
144-
month: "numeric",
145-
day: "numeric",
140+
const numericDateOptions: Intl.DateTimeFormatOptions = { month: "numeric", day: "numeric" };
141+
const numericDateWithYearOptions: Intl.DateTimeFormatOptions = {
142+
...numericDateOptions,
146143
year: "numeric",
147-
});
144+
};
145+
const numericDateFormatter = new Intl.DateTimeFormat(timestampLocale, numericDateOptions);
146+
const numericDateWithYearFormatter = new Intl.DateTimeFormat(
147+
timestampLocale,
148+
numericDateWithYearOptions,
149+
);
150+
151+
interface CalendarDay {
152+
year: number;
153+
/** Days since the epoch, so differences are whole days across DST shifts. */
154+
index: number;
155+
}
156+
157+
function calendarDay(date: Date, timeZone: string | undefined): CalendarDay {
158+
if (!timeZone) {
159+
const year = date.getFullYear();
160+
return { year, index: Date.UTC(year, date.getMonth(), date.getDate()) / 86_400_000 };
161+
}
162+
const parts = new Intl.DateTimeFormat("en-US", {
163+
timeZone,
164+
year: "numeric",
165+
month: "numeric",
166+
day: "numeric",
167+
}).formatToParts(date);
168+
const part = (type: Intl.DateTimeFormatPartTypes) =>
169+
Number(parts.find((entry) => entry.type === type)?.value);
170+
const year = part("year");
171+
return { year, index: Date.UTC(year, part("month") - 1, part("day")) / 86_400_000 };
172+
}
173+
174+
function formatNumericDate(date: Date, sameYear: boolean, timeZone: string | undefined): string {
175+
if (!timeZone) {
176+
return (sameYear ? numericDateFormatter : numericDateWithYearFormatter).format(date);
177+
}
178+
return new Intl.DateTimeFormat(timestampLocale, {
179+
...(sameYear ? numericDateOptions : numericDateWithYearOptions),
180+
timeZone,
181+
}).format(date);
182+
}
148183

149184
export function formatDayAwareTimestamp(
150185
isoDate: string,
151186
timestampFormat: TimestampFormat,
152187
nowMs: number = Date.now(),
188+
timeZone?: string,
153189
): string {
154190
const date = parseTimestampDate(isoDate);
155191
if (!date) return "";
156-
const time = getTimestampFormatter(timestampFormat, false).format(date);
192+
const time = getTimestampFormatter(timestampFormat, false, timeZone).format(date);
157193

158-
const now = new Date(nowMs);
159-
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
160-
const startOfMessageDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
161-
// Round so DST-shifted 23/25 hour days still count as whole days.
162-
const dayDiff = Math.round((startOfToday - startOfMessageDay) / 86_400_000);
194+
const today = calendarDay(new Date(nowMs), timeZone);
195+
const messageDay = calendarDay(date, timeZone);
196+
const dayDiff = today.index - messageDay.index;
163197

164198
if (dayDiff <= 0) return time;
165199
if (dayDiff === 1) return `yesterday at ${time}`;
166-
const dateFormatter =
167-
date.getFullYear() === now.getFullYear() ? numericDateFormatter : numericDateWithYearFormatter;
168-
return `${dateFormatter.format(date)} ${time}`;
200+
return `${formatNumericDate(date, messageDay.year === today.year, timeZone)} ${time}`;
169201
}
170202

203+
/** `timeZone` overrides the zone the formatters cached when they were built. */
171204
export function formatUpcomingTimestamp(
172205
isoDate: string,
173206
timestampFormat: TimestampFormat,
174207
nowMs: number = Date.now(),
208+
timeZone?: string,
175209
): string {
176210
const date = parseTimestampDate(isoDate);
177211
if (!date) return "";
178-
const time = getTimestampFormatter(timestampFormat, false).format(date);
212+
const time = getTimestampFormatter(timestampFormat, false, timeZone).format(date);
179213

180-
const now = new Date(nowMs);
181-
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
182-
const startOfTargetDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
183-
const dayDiff = Math.round((startOfTargetDay - startOfToday) / 86_400_000);
214+
const today = calendarDay(new Date(nowMs), timeZone);
215+
const targetDay = calendarDay(date, timeZone);
216+
const dayDiff = targetDay.index - today.index;
184217

185-
if (dayDiff < 0) return formatDayAwareTimestamp(isoDate, timestampFormat, nowMs);
218+
if (dayDiff < 0) return formatDayAwareTimestamp(isoDate, timestampFormat, nowMs, timeZone);
186219
if (dayDiff === 0) return time;
187220
if (dayDiff === 1) return `tomorrow at ${time}`;
188-
const dateFormatter =
189-
date.getFullYear() === now.getFullYear() ? numericDateFormatter : numericDateWithYearFormatter;
190-
return `${dateFormatter.format(date)} ${time}`;
221+
return `${formatNumericDate(date, targetDay.year === today.year, timeZone)} ${time}`;
191222
}
192223

193224
type RelativeTimeParts = { value: string; suffix: string | null };

0 commit comments

Comments
 (0)