UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

49 lines (48 loc) • 1.55 kB
import "./src-H3RMXyFL.mjs"; import { t as bucketRelativeTimeMs } from "./format-KwhqGIVR.mjs"; //#region src/infra/format-time/format-relative.ts /** * Format a duration (in ms) as a human-readable relative time. * * Input: how many milliseconds ago something happened. * * With suffix (default): "just now", "5m ago", "3h ago", "2d ago" * Without suffix: "0s", "5m", "3h", "2d" */ function formatTimeAgo(e, t) { const n = t?.suffix !== false; const r = t?.fallback ?? "unknown"; if (e == null || !Number.isFinite(e) || e < 0) return r; const i = Math.round(e / 1e3); const a = Math.round(i / 60); if (a < 1) return n ? "just now" : `${i}s`; const o = Math.round(a / 60); const s = a < 60 ? `${a}m` : o < 48 ? `${o}h` : `${Math.round(o / 24)}d`; return n ? `${s} ago` : s; } /** * Format an epoch timestamp relative to now. * * Handles both past ("5m ago") and future ("in 5m") timestamps. * Falls back to a short date for timestamps older than 7 days. */ function formatRelativeTimestamp(e) { if (e == null || !Number.isFinite(e)) return ""; const t = Date.now() - e; const n = Math.abs(t); const r = t >= 0; const { value: i, unit: a } = bucketRelativeTimeMs(n); if (a === "second") return r ? "just now" : "in <1m"; const o = r ? `${i}${a[0]} ago` : `in ${i}${a[0]}`; if (a !== "day" || i <= 7) return o; try { return new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric" }).format(new Date(e)); } catch { return o; } } //#endregion export { formatTimeAgo as n, formatRelativeTimestamp as t };