aboutsummaryrefslogtreecommitdiffstats
path: root/renderer/src/utils/timeFormat.ts
blob: db0f99be9fbdc8eb288cca749e73b1c47c40c4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * 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}`;
}