Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

timestampFormat.ts

159 lines5.8 KB

1import { type TimestampFormat } from "@t3tools/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 };
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;
42​
43export function formatTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {
44 return getTimestampFormatter(timestampFormat, true).format(new Date(isoDate));
46​
47export function formatShortTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {
48 return getTimestampFormatter(timestampFormat, false).format(new Date(isoDate));
50​
51/**
52 * Format a relative time string from an ISO date.
53 * Returns `{ value: "20s", suffix: "ago" }` or `{ value: "just now", suffix: null }`
54 * so callers can style the numeric portion independently.
55 */
56export function formatRelativeTime(isoDate: string): { value: string; suffix: string | null } {
57 const diffMs = Date.now() - new Date(isoDate).getTime();
58 if (diffMs < 0) return { value: "just now", suffix: null };
59 const seconds = Math.floor(diffMs / 1000);
60 if (seconds < 60) return { value: "just now", suffix: null };
61 const minutes = Math.floor(seconds / 60);
62 if (minutes < 60) return { value: `${minutes}m`, suffix: "ago" };
63 const hours = Math.floor(minutes / 60);
64 if (hours < 24) return { value: `${hours}h`, suffix: "ago" };
65 const days = Math.floor(hours / 24);
66 return { value: `${days}d`, suffix: "ago" };
68​
69export function formatRelativeTimeLabel(isoDate: string) {
70 const relative = formatRelativeTime(isoDate);
71 return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
73​
74/**
75 * Relative elapsed duration since an ISO instant, without an "ago" suffix.
76 * Useful for labels like "Connected for 3m".
77 */
78export function formatElapsedDurationLabel(isoDate: string, nowMs: number = Date.now()): string {
79 const diffMs = nowMs - new Date(isoDate).getTime();
80 if (diffMs <= 0) return "just now";
81​
82 const seconds = Math.floor(diffMs / 1000);
83 if (seconds < 5) return "just now";
84 if (seconds < 60) return `${seconds}s`;
85​
86 const minutes = Math.floor(seconds / 60);
87 if (minutes < 60) return `${minutes}m`;
88​
89 const hours = Math.floor(minutes / 60);
90 if (hours < 24) return `${hours}h`;
91​
92 const days = Math.floor(hours / 24);
93 return `${days}d`;
95​
96/**
97 * Relative time until an ISO instant (e.g. expiry). Mirrors {@link formatRelativeTime} but for future times.
98 */
99export function formatRelativeTimeUntil(isoDate: string): { value: string; suffix: string | null } {
100 const diffMs = new Date(isoDate).getTime() - Date.now();
101 if (diffMs <= 0) return { value: "Expired", suffix: null };
102 const seconds = Math.floor(diffMs / 1000);
103 if (seconds < 5) return { value: "Soon", suffix: null };
104 if (seconds < 60) return { value: `${seconds}s`, suffix: "left" };
105 const minutes = Math.floor(seconds / 60);
106 if (minutes < 60) return { value: `${minutes}m`, suffix: "left" };
107 const hours = Math.floor(minutes / 60);
108 if (hours < 24) return { value: `${hours}h`, suffix: "left" };
109 const days = Math.floor(hours / 24);
110 return { value: `${days}d`, suffix: "left" };
112​
113export function formatRelativeTimeUntilLabel(isoDate: string): string {
114 const relative = formatRelativeTimeUntil(isoDate);
115 return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
117​
118/**
119 * Countdown for a future instant (e.g. link expiry): "Expires in 4m 12s", with second precision under one hour.
120 * Pass `nowMs` when a parent tick drives re-renders so the diff matches that snapshot.
121 */
122export function formatExpiresInLabel(isoDate: string, nowMs: number = Date.now()): string {
123 const diffMs = new Date(isoDate).getTime() - nowMs;
124 if (diffMs <= 0) return "Expired";
125​
126 const totalSeconds = Math.floor(diffMs / 1000);
127 if (totalSeconds < 5) return "Expires in a moment";
128 if (totalSeconds < 60) return `Expires in ${totalSeconds}s`;
129​
130 if (totalSeconds < 3600) {
131 const minutes = Math.floor(totalSeconds / 60);
132 const seconds = totalSeconds % 60;
133 return seconds === 0 ? `Expires in ${minutes}m` : `Expires in ${minutes}m ${seconds}s`;
134 }
135​
136 if (totalSeconds < 86_400) {
137 const hours = Math.floor(totalSeconds / 3600);
138 const rem = totalSeconds % 3600;
139 const minutes = Math.floor(rem / 60);
140 const seconds = rem % 60;
141 const parts = [`${hours}h`];
142 if (minutes > 0) parts.push(`${minutes}m`);
143 if (seconds > 0) parts.push(`${seconds}s`);
144 return `Expires in ${parts.join(" ")}`;
145 }
146​
147 const days = Math.floor(totalSeconds / 86_400);
148 const remAfterDays = totalSeconds % 86_400;
149 if (remAfterDays === 0) return `Expires in ${days}d`;
150 const hours = Math.floor(remAfterDays / 3600);
151 const rem = remAfterDays % 3600;
152 const minutes = Math.floor(rem / 60);
153 const seconds = rem % 60;
154 const tail: string[] = [];
155 if (hours > 0) tail.push(`${hours}h`);
156 if (minutes > 0) tail.push(`${minutes}m`);
157 if (seconds > 0) tail.push(`${seconds}s`);
158 return tail.length > 0 ? `Expires in ${days}d ${tail.join(" ")}` : `Expires in ${days}d`;