31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
/** Time formatting for a screen you look at for three seconds. */
|
|
|
|
export function relTime(ts: number, now: number): string {
|
|
const delta = Math.max(0, now - ts);
|
|
const s = Math.round(delta / 1000);
|
|
if (s < 5) return "now";
|
|
if (s < 60) return `${s}s ago`;
|
|
const m = Math.round(s / 60);
|
|
if (m < 60) return `${m}m ago`;
|
|
const h = Math.round(m / 60);
|
|
if (h < 24) return `${h}h ago`;
|
|
return `${Math.round(h / 24)}d ago`;
|
|
}
|
|
|
|
export function clockTime(ts: number): string {
|
|
return new Date(ts).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
|
}
|
|
|
|
export function duration(ms: number): string {
|
|
if (ms < 1000) return `${ms}ms`;
|
|
if (ms < 60_000) return `${(ms / 1000).toFixed(ms < 10_000 ? 1 : 0)}s`;
|
|
const m = Math.floor(ms / 60_000);
|
|
const s = Math.round((ms % 60_000) / 1000);
|
|
return `${m}m ${s}s`;
|
|
}
|
|
|
|
/** Seconds left, floored at zero, for an approval countdown. */
|
|
export function secondsLeft(expiresAt: number, now: number): number {
|
|
return Math.max(0, Math.ceil((expiresAt - now) / 1000));
|
|
}
|