Public: anyone can read this; the crew can change it.
Relative times ('3 min ago') carry the exact time as a title and a <time datetime> on every page that prints one #14508
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14487-relative-times-3-min-ago-carry-the-exact → main 6 commits fork 3 threads · 3 unresolved
13 files
changed +82
−45
src/web/pages/claim.ts +2 −1
| @@ -68,6 +68,7 @@ import { | ||
| 68 | 68 | import { pageHead, CAME_FROM_ELSEWHERE } from "../chrome.ts"; |
| 69 | 69 | import { field, readForm } from "../form.ts"; |
| 70 | 70 | import { html, type Html } from "../html.ts"; |
| 71 | import { time } from "../time_element.ts"; | |
| 71 | 72 | import { page, redirect } from "../layout.ts"; |
| 72 | 73 | import { makeOptions, PASSKEY_WORDS, passkeyFields, readPasskey } from "../passkey-ceremony.ts"; |
| 73 | 74 | import { passkeyJs } from "../passkey-client.ts"; |
| @@ -260,7 +261,7 @@ function claimForm( | ||
| 260 | 261 | ${expiresAt === undefined |
| 261 | 262 | ? null |
| 262 | 263 | : html`<p class="mb" id="claim-expiry">This link works until |
| 263 | <time datetime="${expiresAt}">${whenWords(expiresAt)}</time> | |
| 264 | ${time(expiresAt, { text: whenWords(expiresAt) })} | |
| 264 | 265 | (${relativeWords(Date.parse(expiresAt) - Date.now())}). After that, ask |
| 265 | 266 | ${spawner?.username ?? "whoever sent it"} to send you a new one.</p>`} |
| 266 | 267 | <p class="mb" id="claim-access">${accessSentence(spawned, spawner)}</p> |
src/web/pages/home_flow.ts +3 −2
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | * version without being drawn. The page is a static picture with scripts off. |
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | import { time } from "../time_element.ts"; | |
| 18 | 19 | import { requestHolds } from "../../http/conditional.ts"; |
| 19 | 20 | import { setCookie } from "../../auth/cookies.ts"; |
| 20 | 21 | import type { App } from "../../app.ts"; |
| @@ -104,7 +105,7 @@ const waitsOf = (url: URL): boolean => url.searchParams.get("waits") === "me"; | ||
| 104 | 105 | */ |
| 105 | 106 | function age(iso: string, now: number): Html { |
| 106 | 107 | const at = Date.parse(iso); |
| 107 | return html`<time class="hf-age" datetime="${new Date(at).toISOString()}">${span(now - at)}</time>`; | |
| 108 | return time(new Date(at).toISOString(), { class: "hf-age", text: span(now - at) }); | |
| 108 | 109 | } |
| 109 | 110 | |
| 110 | 111 | const TONE_WORD = { |
| @@ -319,7 +320,7 @@ export function stripRegion(picture: LanePicture, viewer: StripViewer, dataDir: | ||
| 319 | 320 | ${fig("Median open to landed", lead === null ? "—" : duration(lead * 3_600_000), lead === null ? "nothing landed in 24 hours" : `median of ${count(strip!.lead.landed)} ${strip!.lead.landed === 1 ? "change" : "changes"}, 24 hours`, "/boards")} |
| 320 | 321 | ${fig("Crew running", b.slots === 0 ? count(b.running) : `${count(b.running)} of ${count(b.slots)}`, b.slots === 0 ? "no builder slots set" : idle === null ? "builders at work now" : `idle: ${idle.word}`, idle === null ? "/settings/crew" : idle.href)} |
| 321 | 322 | ${fig("Week used", week === null ? "—" : week.value, week === null ? "the week is not read yet" : week.note, "/settings/crew", null, week?.attn === true ? "attn" : null)} |
| 322 | ${fig("Live version", version === UNSTAMPED ? "—" : version, html`live <time class="hf-age-ago" datetime="${BOOTED}">${span(now - Date.parse(BOOTED))}</time> · ${proof.text}`, "/releases", null, proof.tone)} | |
| 323 | ${fig("Live version", version === UNSTAMPED ? "—" : version, html`live ${time(BOOTED, { class: "hf-age-ago", text: span(now - Date.parse(BOOTED)) })} · ${proof.text}`, "/releases", null, proof.tone)} | |
| 323 | 324 | </div> |
| 324 | 325 | </section>`; |
| 325 | 326 | } |
src/web/pages/home_table.ts +2 −3
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * and exist for the kind of row are offered. |
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | import { time } from "../time_element.ts"; | |
| 17 | 18 | import { applyFleetIds, previewFleetIds } from "../../actions/fleet.ts"; |
| 18 | 19 | import { |
| 19 | 20 | LANES, |
| @@ -85,9 +86,7 @@ const TONE_DOT = { | ||
| 85 | 86 | function age(iso: string | null, now: number): Html { |
| 86 | 87 | if (iso === null) return html`<span class="muted">—</span>`; |
| 87 | 88 | const at = Date.parse(iso); |
| 88 | return html`<time class="hf-age" datetime="${new Date(at).toISOString()}" | |
| 89 | >${span(now - at)}</time | |
| 90 | >`; | |
| 89 | return time(new Date(at).toISOString(), { class: "hf-age", text: span(now - at) }); | |
| 91 | 90 | } |
| 92 | 91 | |
| 93 | 92 | const HEAD: Record<Column, string> = { |
src/web/pages/pull.ts +1 −1
| @@ -399,7 +399,7 @@ export function redAloneLine(mark: RedMark | undefined): Html { | ||
| 399 | 399 | if (exact === "") return html``; |
| 400 | 400 | return html`<p class="muted small"> |
| 401 | 401 | ${icon("clock")}red alone at |
| 402 | <time datetime="${new Date(mark.at).toISOString()}">${exact}</time>, ${RED_ALONE_WAIT} | |
| 402 | ${time(new Date(mark.at).toISOString(), { text: exact })}, ${RED_ALONE_WAIT} | |
| 403 | 403 | </p>`; |
| 404 | 404 | } |
| 405 | 405 |
src/web/setting_line.ts +2 −1
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | * deeper in a <details>; the mark is drawn as a small bordered button, so it |
| 6 | 6 | * reads as a control without a word that would cost every row a text block. |
| 7 | 7 | */ |
| 8 | import { time } from "./time_element.ts"; | |
| 8 | 9 | import { html, raw, type Html } from "./html.ts"; |
| 9 | 10 | import { icon } from "./icons.ts"; |
| 10 | 11 | import { agoWords } from "../kernel/ago.ts"; |
| @@ -21,7 +22,7 @@ export interface ChangedBy { | ||
| 21 | 22 | */ |
| 22 | 23 | export function changedNote(last: ChangedBy | undefined, now: number = Date.now()): Html | null { |
| 23 | 24 | if (last === undefined) return null; |
| 24 | return html`<span class="muted small setting-changed" data-changed>by ${last.by}, <time datetime="${last.at}">${agoWords(last.at, now)}</time></span>`; | |
| 25 | return html`<span class="muted small setting-changed" data-changed>by ${last.by}, ${time(last.at, { text: agoWords(last.at, now) })}</span>`; | |
| 25 | 26 | } |
| 26 | 27 | |
| 27 | 28 | /** The reason behind a setting, behind its mark. `id` ties it to the control via aria-describedby. */ |
src/web/studio/run.ts +2 −2
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * at `?all=1`. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { time } from "../time_element.ts"; | |
| 11 | 12 | import { wayIn } from "../../actions/blocked_words.ts"; |
| 12 | 13 | import { blockedWay } from "../blocked.ts"; |
| 13 | 14 | import { checkSteps, stepWord, type LookVitals } from "../../checks/summary.ts"; |
| @@ -91,8 +92,7 @@ function stoppedLine(run: Run): Html | undefined { | ||
| 91 | 92 | const at = run.finishedAt === null ? Number.NaN : Date.parse(run.finishedAt); |
| 92 | 93 | if (!Number.isFinite(at)) return html`Stopped by ${name}`; |
| 93 | 94 | const iso = new Date(at).toISOString(); |
| 94 | const exact = `${iso.slice(0, 16).replace("T", " ")} UTC`; | |
| 95 | return html`Stopped by ${name}, <time datetime="${iso}" title="${exact}">${agoWords(iso)}</time>`; | |
| 95 | return html`Stopped by ${name}, ${time(iso, { text: agoWords(iso) })}`; | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
src/web/time_element.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | /** The one place a relative time becomes markup: <time datetime title>, escaped (#14487). */ | |
| 2 | import { html, type Html } from "./html.ts"; | |
| 3 | ||
| 4 | /** A compact relative time: the only clock the UI speaks in. */ | |
| 5 | export function timeAgo(iso: string): string { | |
| 6 | const seconds = Math.max(0, (Date.now() - Date.parse(iso)) / 1000); | |
| 7 | if (!Number.isFinite(seconds)) return ""; | |
| 8 | if (seconds < 60) return "just now"; | |
| 9 | const minutes = seconds / 60; | |
| 10 | if (minutes < 60) return `${Math.floor(minutes)}m ago`; | |
| 11 | const hours = minutes / 60; | |
| 12 | if (hours < 24) return `${Math.floor(hours)}h ago`; | |
| 13 | const days = hours / 24; | |
| 14 | if (days < 30) return `${Math.floor(days)}d ago`; | |
| 15 | const months = days / 30; | |
| 16 | if (months < 12) return `${Math.floor(months)}mo ago`; | |
| 17 | return `${Math.floor(months / 12)}y ago`; | |
| 18 | } | |
| 19 | ||
| 20 | /** | |
| 21 | * A timestamp a machine and a screen reader can both read: relative text for | |
| 22 | * the eye, the exact instant in `datetime` and `title` for everyone else. | |
| 23 | * `date` words it as the calendar day (`2026-12-02`), for an instant ahead | |
| 24 | * of now, which "ago" cannot name. | |
| 25 | */ | |
| 26 | export function time(iso: string, opts: { date?: boolean; text?: string; class?: string } = {}): Html { | |
| 27 | const parsed = Date.parse(iso); | |
| 28 | if (Number.isNaN(parsed)) return html``; | |
| 29 | const at = new Date(parsed); | |
| 30 | const exact = `${at.toISOString().slice(0, 16).replace("T", " ")} UTC`; | |
| 31 | const cls = opts.class === undefined ? html`` : html` class="${opts.class}"`; | |
| 32 | return html`<time${cls} datetime="${at.toISOString()}" title="${exact}" | |
| 33 | >${opts.text ?? (opts.date === true ? at.toISOString().slice(0, 10) : timeAgo(iso))}</time | |
| 34 | >`; | |
| 35 | } |
src/web/ui.ts +2 −31
| @@ -26,6 +26,7 @@ import { icon, type IconName } from "./icons.ts"; | ||
| 26 | 26 | import { crewFace, scene, type SceneName } from "./pixel.ts"; |
| 27 | 27 | import { Html, html, raw, when } from "./html.ts"; |
| 28 | 28 | import { why } from "./setting_line.ts"; |
| 29 | import { time, timeAgo } from "./time_element.ts"; | |
| 29 | 30 | import { PREPARED_WORDS } from "../apps/env_phrase.ts"; |
| 30 | 31 | import { at, countAttr, listAttr, zeroAttr } from "./bind.ts"; |
| 31 | 32 | |
| @@ -35,37 +36,7 @@ export type Tone = "default" | "success" | "danger" | "merged" | "attn"; | ||
| 35 | 36 | |
| 36 | 37 | // ---- text and time ---- |
| 37 | 38 | |
| 38 | /** A compact relative time: the only clock the UI speaks in. */ | |
| 39 | export function timeAgo(iso: string): string { | |
| 40 | const seconds = Math.max(0, (Date.now() - Date.parse(iso)) / 1000); | |
| 41 | if (!Number.isFinite(seconds)) return ""; | |
| 42 | if (seconds < 60) return "just now"; | |
| 43 | const minutes = seconds / 60; | |
| 44 | if (minutes < 60) return `${Math.floor(minutes)}m ago`; | |
| 45 | const hours = minutes / 60; | |
| 46 | if (hours < 24) return `${Math.floor(hours)}h ago`; | |
| 47 | const days = hours / 24; | |
| 48 | if (days < 30) return `${Math.floor(days)}d ago`; | |
| 49 | const months = days / 30; | |
| 50 | if (months < 12) return `${Math.floor(months)}mo ago`; | |
| 51 | return `${Math.floor(months / 12)}y ago`; | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * A timestamp a machine and a screen reader can both read: relative text for | |
| 56 | * the eye, the exact instant in `datetime` and `title` for everyone else. | |
| 57 | * `date` words it as the calendar day (`2026-12-02`), for an instant ahead | |
| 58 | * of now, which "ago" cannot name. | |
| 59 | */ | |
| 60 | export function time(iso: string, opts: { date?: boolean; text?: string } = {}): Html { | |
| 61 | const parsed = Date.parse(iso); | |
| 62 | if (Number.isNaN(parsed)) return html``; | |
| 63 | const at = new Date(parsed); | |
| 64 | const exact = `${at.toISOString().slice(0, 16).replace("T", " ")} UTC`; | |
| 65 | return html`<time datetime="${at.toISOString()}" title="${exact}" | |
| 66 | >${opts.text ?? (opts.date === true ? at.toISOString().slice(0, 10) : timeAgo(iso))}</time | |
| 67 | >`; | |
| 68 | } | |
| 39 | export { time, timeAgo }; | |
| 69 | 40 | |
| 70 | 41 | /** Seconds to one decimal: `3.4s`. A clock that ran backwards reads `0.0s`. */ |
| 71 | 42 | function seconds(ms: number): string { |
test/claim-page-words.test.ts +1 −1
| @@ -80,7 +80,7 @@ describe("the claim page in plain words", () => { | ||
| 80 | 80 | expect(res.status).toBe(200); |
| 81 | 81 | const html = await res.text(); |
| 82 | 82 | expect(html).toContain('id="claim-expiry"'); |
| 83 | expect(html).toMatch(/works until\s*<time datetime="[^"]+">[A-Z][a-z]{2} \d{1,2}, \d\d:\d\d UTC<\/time>\s*\(in (23|24) hours\)/); | |
| 83 | expect(html).toMatch(/works until\s*<time datetime="[^"]+" title="[^"]+"\s*>[A-Z][a-z]{2} \d{1,2}, \d\d:\d\d UTC<\/time\s*>\s*\(in (23|24) hours\)/); | |
| 84 | 84 | expect(html).toContain("alice"); |
| 85 | 85 | }); |
| 86 | 86 |
test/pull-red-alone-line.test.ts +1 −1
| @@ -6,7 +6,7 @@ const mark = { at: "2026-10-26T20:41:30.000Z", head: "a".repeat(40), base: "b".r | ||
| 6 | 6 | test("a pull held as red alone says so, with a <time> element", () => { |
| 7 | 7 | const line = redAloneLine(mark).text; |
| 8 | 8 | expect(line).toContain("red alone at"); |
| 9 | expect(line).toContain('<time datetime="2026-10-26T20:41:30.000Z">2026-10-26 20:41 UTC</time>'); | |
| 9 | expect(line).toMatch(/<time datetime="2026-10-26T20:41:30.000Z" title="2026-10-26 20:41 UTC"\s*>2026-10-26 20:41 UTC<\/time/); | |
| 10 | 10 | expect(line).toContain("waiting for a new head or a new main"); |
| 11 | 11 | }); |
| 12 | 12 |
test/run-stopped-words.test.ts +1 −1
| @@ -83,7 +83,7 @@ describe("a run stopped from the page", () => { | ||
| 83 | 83 | const summary = text.match(/<p class="mt" id="run-summary">(.*?)<\/p>/s)?.[1] ?? ""; |
| 84 | 84 | // The words are the sentence the feed and the terminal say; the time holds the exact instant. |
| 85 | 85 | expect(summary.replace(/<[^>]*>/g, "")).toBe("Stopped by alice, just now"); |
| 86 | const time = summary.match(/<time datetime="([^"]+)" title="([^"]+)">just now<\/time>/); | |
| 86 | const time = summary.match(/<time datetime="([^"]+)" title="([^"]+)"\s*>just now<\/time\s*>/); | |
| 87 | 87 | expect(time).not.toBeNull(); |
| 88 | 88 | expect(Number.isFinite(Date.parse(time![1]!))).toBe(true); |
| 89 | 89 | expect(time![2]).toBe(`${time![1]!.slice(0, 16).replace("T", " ")} UTC`); |
test/settings-last-change.test.ts +1 −1
| @@ -57,7 +57,7 @@ test("a changed setting shows who and when; an untouched one shows nothing", asy | ||
| 57 | 57 | }); |
| 58 | 58 | expect(res.status).toBeLessThan(400); |
| 59 | 59 | const after = await get(); |
| 60 | expect(row(after, "landing")).toMatch(/by olive, <time datetime="[^"]+">just now<\/time>/); | |
| 60 | expect(row(after, "landing")).toMatch(/by olive, <time datetime="[^"]+" title="[^"]+"\s*>just now<\/time\s*>/); | |
| 61 | 61 | expect(row(after, "polishShare")).not.toContain("data-changed"); |
| 62 | 62 | expect(row(after, "requireReview")).not.toContain("data-changed"); |
| 63 | 63 | }); |
test/time-element.test.ts +29
| @@ -98,3 +98,32 @@ describe("rendered pages", () => { | ||
| 98 | 98 | expect(body).toMatch(/data-following-history[\s\S]*?<time datetime="[^"]+" title="[^"]+"\s*>2h ago<\/time/); |
| 99 | 99 | }); |
| 100 | 100 | }); |
| 101 | ||
| 102 | describe("no template builds a relative time by hand (#14487)", () => { | |
| 103 | test("no <time is written by hand in src/web outside the date-only pages", async () => { | |
| 104 | const { Glob } = await import("bun"); | |
| 105 | // Date-only or script-owned elements: no relative phrase, so no title to carry. | |
| 106 | const allowed = new Set([ | |
| 107 | "changelog.ts", "payments.ts", "billing.ts", "frozen.ts", "following.ts", "migrate.ts", | |
| 108 | "upstream-follow.ts", "fleet-graph.ts", "offline.ts", | |
outdated
This regex only matches single-line <time> with a literal call in the text, so pull.ts:402 and claim.ts:263 slip through. Invert it: fail on any |
||
| 109 | "board.ts", // the corner clock: empty, the board script fills it | |
| 110 | "subtitles.ts", // a caption cue's start, not a moment | |
| 111 | "client.ts", "live.ts", // scripts that match <time> in fetched markup | |
| 112 | ]); | |
| 113 | const offenders: string[] = []; | |
| 114 | for await (const file of new Glob("src/web/**/*.ts").scan(".")) { | |
| 115 | const name = file.split("/").pop()!; | |
| 116 | if (name === "time_element.ts" || allowed.has(name)) continue; | |
| 117 | const lines = (await Bun.file(file).text()).split("\n"); | |
| 118 | lines.forEach((l, i) => { | |
| 119 | if (/<time\b/.test(l) && !/^\s*(\*|\/\/|\/\*)/.test(l)) offenders.push(`${file}:${i + 1}: ${l.trim().slice(0, 70)}`); | |
| 120 | }); | |
| 121 | } | |
| 122 | expect(offenders).toEqual([]); | |
| 123 | }); | |
| 124 | test("the helper escapes its words and its class", () => { | |
| 125 | const out = time(at, { text: "<b>x</b>", class: 'a"b' }).text; | |
| 126 | expect(out).not.toContain("<b>"); | |
| 127 | expect(out).toContain('class="a"b"'); | |
| 128 | }); | |
| 129 | }); |
new above
offline
Principle 8/box 1: good single helper. Callers outside the diff (pull.ts:402, claim.ts:263) still hand-build <time>; move them here.