Files
minstrel/web/src/lib/utils/relativeTime.ts
T
bvandeusenandClaude Opus 5.5 10d50dccbd docs: web comments say what the app does, not how it came to be (#6277)
Rewrites the web app's comments to rule 32. Task, milestone, rule and
spec ids are gone; regression stories are restated as the trap they
guard (public auth routes, the menu's stopPropagation, universal loads
under adapter-static, metric deltas with their margin). Flutter and
Tauri mentions now name the Android app or a native shell, operator
quotes and dates are dropped, and one library's counts no longer
appear outside test fixtures. Verified with a TypeScript-scanner token
comparison (template literals and regex literals re-scanned): only
comment text changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:42:12 -04:00

53 lines
2.4 KiB
TypeScript

/**
* Coarse "how long ago" label for an ISO timestamp — "3d ago", "5h ago",
* "12m ago", or "just now" under a minute.
*
* The single source for the admin/triage surfaces, which had three
* byte-identical private copies of this before it was extracted (admin
* quarantine, admin playback-errors, library/hidden). Each showed the age
* of something an operator is deciding about, so they must read the same
* — a row that says "2d ago" on one screen and "2 days" on another makes
* the reader wonder whether they mean different things.
*
* Deliberately NOT the formatter used by listening history. HistoryRow
* shows a weekday and clock time for anything under a week ("Tue 21:40")
* because when you played something is more useful than how long ago,
* and it floors at "1m ago" rather than "just now". That is a different
* intent, not a copy that drifted, and it stays where it is.
*/
export function relativeTime(iso: string): string {
const ms = Date.now() - new Date(iso).getTime();
const days = Math.floor(ms / (24 * 3_600_000));
if (days >= 1) return `${days}d ago`;
const hours = Math.floor(ms / 3_600_000);
if (hours >= 1) return `${hours}h ago`;
const minutes = Math.floor(ms / 60_000);
if (minutes >= 1) return `${minutes}m ago`;
return 'just now';
}
/**
* Forward-looking companion to [relativeTime]: "in 4h", "in 2d", or "any
* moment" once the moment has passed.
*
* Separate function rather than a sign-aware relativeTime, because the two
* read differently in a sentence ("last tried 3d ago, next in 4h") and
* because relativeTime deliberately collapses future timestamps to "just
* now" — that is the right answer for a clock-skewed past event and the
* wrong one for a scheduled future one.
*/
export function timeUntil(iso: string): string {
const ms = new Date(iso).getTime() - Date.now();
if (ms < 60_000) return 'any moment';
// Rounds where relativeTime floors, and the difference matters. Flooring an
// elapsed time is honest — "3h ago" for 3h59m means "at least three hours".
// Flooring a countdown is not: it would show "in 3h" for something 3h59m
// away, so an admin comes back an hour early and finds nothing has
// happened.
const minutes = Math.round(ms / 60_000);
if (minutes < 60) return `in ${minutes}m`;
const hours = Math.round(ms / 3_600_000);
if (hours < 24) return `in ${hours}h`;
return `in ${Math.round(ms / (24 * 3_600_000))}d`;
}