/** * Format a Unix timestamp (seconds) into a relative time string. * Examples: "now", "5m", "2h", "3d", "1w", "2mo", "1y" */ export function formatRelativeTime(timestamp: number): string { const now = Math.floor(Date.now() / 1000); // Current time in seconds const diffSeconds = now - timestamp; // Handle future timestamps (shouldn't happen, but be defensive) if (diffSeconds < 0) { return "now"; } // Less than 1 minute if (diffSeconds < 60) { return "now"; } // Less than 1 hour (show minutes) if (diffSeconds < 3600) { const minutes = Math.floor(diffSeconds / 60); return `${minutes}m`; } // Less than 24 hours (show hours) if (diffSeconds < 86400) { const hours = Math.floor(diffSeconds / 3600); return `${hours}h`; } // Less than 7 days (show days) if (diffSeconds < 604800) { const days = Math.floor(diffSeconds / 86400); return `${days}d`; } // Less than 4 weeks (show weeks) if (diffSeconds < 2419200) { const weeks = Math.floor(diffSeconds / 604800); return `${weeks}w`; } // Less than 12 months (show months) if (diffSeconds < 31536000) { const months = Math.floor(diffSeconds / 2592000); // Approximate: 30 days per month return `${months}mo`; } // 12 months or more (show years) const years = Math.floor(diffSeconds / 31536000); return `${years}y`; } /** * Format session name with relative time indicator. * Example: "Session Name • 2h" */ export function formatSessionLabel(name: string, updatedAt: number): string { const relativeTime = formatRelativeTime(updatedAt); return `${name} • ${relativeTime}`; }