Open History to see the last commit
1import { type TimestampFormat } from "@v12/contracts/settings";
2
3export function getTimestampFormatOptions(
4 timestampFormat: TimestampFormat,
5 includeSeconds: boolean,
6): Intl.DateTimeFormatOptions {
7 const baseOptions: Intl.DateTimeFormatOptions = {
8 hour: "numeric",
9 minute: "2-digit",
10 ...(includeSeconds ? { second: "2-digit" } : {}),
11 };
12
13 if (timestampFormat === "locale") {
14 return baseOptions;
15 }
16
17 return {
18 ...baseOptions,
19 hour12: timestampFormat === "12-hour",
20 };
21}
22
23const timestampFormatterCache = new Map<string, Intl.DateTimeFormat>();
24
25function getTimestampFormatter(
26 timestampFormat: TimestampFormat,
27 includeSeconds: boolean,
28): Intl.DateTimeFormat {
29 const cacheKey = `${timestampFormat}:${includeSeconds ? "seconds" : "minutes"}`;
30 const cachedFormatter = timestampFormatterCache.get(cacheKey);
31 if (cachedFormatter) {
32 return cachedFormatter;
33 }
34
35 const formatter = new Intl.DateTimeFormat(
36 undefined,
37 getTimestampFormatOptions(timestampFormat, includeSeconds),
38 );
39 timestampFormatterCache.set(cacheKey, formatter);
40 return formatter;
41}
42
43export function formatTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {
44 return getTimestampFormatter(timestampFormat, true).format(new Date(isoDate));
45}
46
47const monthNameFormatter = new Intl.DateTimeFormat(undefined, { month: "long" });
48
49function ordinalSuffix(day: number): string {
50 const lastTwo = day % 100;
51 if (lastTwo >= 11 && lastTwo <= 13) return "th";
52 switch (day % 10) {
53 case 1:
54 return "st";
55 case 2:
56 return "nd";
57 case 3:
58 return "rd";
59 default:
60 return "th";
61 }
62}
63
64/**
65 * Long-form tooltip label, e.g. `12:04, 4th June`.
66 * Renders the wall-clock time without seconds followed by the ordinal day and month name.
67 */
68export function formatChatTimestampTooltip(
69 isoDate: string,
70 timestampFormat: TimestampFormat,
71): string {
72 const date = new Date(isoDate);
73 if (Number.isNaN(date.getTime())) return "";
74 const time = formatShortTimestamp(isoDate, timestampFormat);
75 const day = date.getDate();
76 const month = monthNameFormatter.format(date);
77 const year = date.getFullYear();
78 return `${time}, ${day}${ordinalSuffix(day)} ${month} ${year}`;
79}
80
81export function formatShortTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {
82 return getTimestampFormatter(timestampFormat, false).format(new Date(isoDate));
83}
84
85/**
86 * Format a relative time string from an ISO date.
87 * Returns `{ value: "20s", suffix: "ago" }` or `{ value: "just now", suffix: null }`
88 * so callers can style the numeric portion independently.
89 */
90export function formatRelativeTime(isoDate: string): { value: string; suffix: string | null } {
91 const diffMs = Date.now() - new Date(isoDate).getTime();
92 if (diffMs < 0) return { value: "just now", suffix: null };
93 const seconds = Math.floor(diffMs / 1000);
94 if (seconds < 60) return { value: "just now", suffix: null };
95 const minutes = Math.floor(seconds / 60);
96 if (minutes < 60) return { value: `${minutes}m`, suffix: "ago" };
97 const hours = Math.floor(minutes / 60);
98 if (hours < 24) return { value: `${hours}h`, suffix: "ago" };
99 const days = Math.floor(hours / 24);
100 return { value: `${days}d`, suffix: "ago" };
101}
102
103export function formatRelativeTimeLabel(isoDate: string) {
104 const relative = formatRelativeTime(isoDate);
105 return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
106}
107
108/**
109 * Relative elapsed duration since an ISO instant, without an "ago" suffix.
110 * Useful for labels like "Connected for 3m".
111 */
112export function formatElapsedDurationLabel(isoDate: string, nowMs: number = Date.now()): string {
113 const diffMs = nowMs - new Date(isoDate).getTime();
114 if (diffMs <= 0) return "just now";
115
116 const seconds = Math.floor(diffMs / 1000);
117 if (seconds < 5) return "just now";
118 if (seconds < 60) return `${seconds}s`;
119
120 const minutes = Math.floor(seconds / 60);
121 if (minutes < 60) return `${minutes}m`;
122
123 const hours = Math.floor(minutes / 60);
124 if (hours < 24) return `${hours}h`;
125
126 const days = Math.floor(hours / 24);
127 return `${days}d`;
128}
129
130/**
131 * Relative time until an ISO instant (e.g. expiry). Mirrors {@link formatRelativeTime} but for future times.
132 */
133export function formatRelativeTimeUntil(isoDate: string): { value: string; suffix: string | null } {
134 const diffMs = new Date(isoDate).getTime() - Date.now();
135 if (diffMs <= 0) return { value: "Expired", suffix: null };
136 const seconds = Math.floor(diffMs / 1000);
137 if (seconds < 5) return { value: "Soon", suffix: null };
138 if (seconds < 60) return { value: `${seconds}s`, suffix: "left" };
139 const minutes = Math.floor(seconds / 60);
140 if (minutes < 60) return { value: `${minutes}m`, suffix: "left" };
141 const hours = Math.floor(minutes / 60);
142 if (hours < 24) return { value: `${hours}h`, suffix: "left" };
143 const days = Math.floor(hours / 24);
144 return { value: `${days}d`, suffix: "left" };
145}
146
147export function formatRelativeTimeUntilLabel(isoDate: string): string {
148 const relative = formatRelativeTimeUntil(isoDate);
149 return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
150}
151
152/**
153 * Countdown for a future instant (e.g. link expiry): "Expires in 4m 12s", with second precision under one hour.
154 * Pass `nowMs` when a parent tick drives re-renders so the diff matches that snapshot.
155 */
156export function formatExpiresInLabel(isoDate: string, nowMs: number = Date.now()): string {
157 const diffMs = new Date(isoDate).getTime() - nowMs;
158 if (diffMs <= 0) return "Expired";
159
160 const totalSeconds = Math.floor(diffMs / 1000);
161 if (totalSeconds < 5) return "Expires in a moment";
162 if (totalSeconds < 60) return `Expires in ${totalSeconds}s`;
163
164 if (totalSeconds < 3600) {
165 const minutes = Math.floor(totalSeconds / 60);
166 const seconds = totalSeconds % 60;
167 return seconds === 0 ? `Expires in ${minutes}m` : `Expires in ${minutes}m ${seconds}s`;
168 }
169
170 if (totalSeconds < 86_400) {
171 const hours = Math.floor(totalSeconds / 3600);
172 const rem = totalSeconds % 3600;
173 const minutes = Math.floor(rem / 60);
174 const seconds = rem % 60;
175 const parts = [`${hours}h`];
176 if (minutes > 0) parts.push(`${minutes}m`);
177 if (seconds > 0) parts.push(`${seconds}s`);
178 return `Expires in ${parts.join(" ")}`;
179 }
180
181 const days = Math.floor(totalSeconds / 86_400);
182 const remAfterDays = totalSeconds % 86_400;
183 if (remAfterDays === 0) return `Expires in ${days}d`;
184 const hours = Math.floor(remAfterDays / 3600);
185 const rem = remAfterDays % 3600;
186 const minutes = Math.floor(rem / 60);
187 const seconds = rem % 60;
188 const tail: string[] = [];
189 if (hours > 0) tail.push(`${hours}h`);
190 if (minutes > 0) tail.push(`${minutes}m`);
191 if (seconds > 0) tail.push(`${seconds}s`);
192 return tail.length > 0 ? `Expires in ${days}d ${tail.join(" ")}` : `Expires in ${days}d`;
193}