// public/utils/simple-time-ago.js export function simpleTimeAgo(input) { if (!input) return 'never'; let date; try { if (input instanceof Date) { date = input; } else if (typeof input === 'number') { date = new Date(input); } else if (typeof input === 'string') { let cleaned = input.trim(); if (!/[Z+-]/.test(cleaned)) { cleaned += 'Z'; } date = new Date(cleaned); } else { return 'invalid'; } if (isNaN(date.getTime())) return 'invalid'; const nowMs = Date.now(); let diffMs = nowMs - date.getTime(); // Cap any "future" time within 24 hours as "recent" (handles timezone skew in static tests) if (diffMs < 0) { if (diffMs > -24 * 60 * 60 * 1000) { diffMs = 0; // treat as now / very recent } else { return 'in the future'; // rare case } } const seconds = Math.floor(diffMs / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (seconds < 60) return `${seconds} seconds ago`; if (minutes < 60) return `${minutes} minutes ago`; if (hours < 24) return `${hours} hours ago`; if (days < 30) return `${days} days ago`; return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); } catch (err) { return 'invalid'; } }