From 2b802835db538a94c2c6ea7da41e981bf4012b4b Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 6 Mar 2026 13:42:49 -0500 Subject: [PATCH] Extract shared relativeTime helper from timeAgo and timeUntil Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/arc-web/app/lib/time.ts | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/apps/arc-web/app/lib/time.ts b/apps/arc-web/app/lib/time.ts index 631b18c71..decaf06fe 100644 --- a/apps/arc-web/app/lib/time.ts +++ b/apps/arc-web/app/lib/time.ts @@ -1,29 +1,25 @@ +function relativeTime(seconds: number, past: boolean): string { + if (seconds < 60) return past ? "just now" : "in <1m"; + const minutes = Math.floor(seconds / 60); + if (minutes < 60) return past ? `${minutes}m ago` : `in ${minutes}m`; + const hours = Math.floor(minutes / 60); + if (hours < 24) return past ? `${hours}h ago` : `in ${hours}h`; + const days = Math.floor(hours / 24); + return past ? `${days}d ago` : `in ${days}d`; +} + /** - * Format an ISO 8601 timestamp as a relative time string (e.g. "2h ago", "3d ago"). + * Format an ISO 8601 timestamp as a relative past time string (e.g. "2h ago", "3d ago"). */ export function timeAgo(iso: string): string { - const seconds = Math.floor((Date.now() - new Date(iso).getTime()) / 1000); - if (seconds < 60) return "just now"; - const minutes = Math.floor(seconds / 60); - if (minutes < 60) return `${minutes}m ago`; - const hours = Math.floor(minutes / 60); - if (hours < 24) return `${hours}h ago`; - const days = Math.floor(hours / 24); - return `${days}d ago`; + return relativeTime(Math.floor((Date.now() - new Date(iso).getTime()) / 1000), true); } /** * Format an ISO 8601 timestamp as a relative future time string (e.g. "in 2h", "in 3d"). */ export function timeUntil(iso: string): string { - const seconds = Math.floor((new Date(iso).getTime() - Date.now()) / 1000); - if (seconds < 60) return "in <1m"; - const minutes = Math.floor(seconds / 60); - if (minutes < 60) return `in ${minutes}m`; - const hours = Math.floor(minutes / 60); - if (hours < 24) return `in ${hours}h`; - const days = Math.floor(hours / 24); - return `in ${days}d`; + return relativeTime(Math.floor((new Date(iso).getTime() - Date.now()) / 1000), false); } /**