Class: SwarmCLI::UI::Formatters::Time

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_cli/ui/formatters/time.rb

Overview

Time and duration formatting utilities

Class Method Summary collapse

Class Method Details

.duration(seconds) ⇒ Object

Format duration in human-readable form 0.5 → "500ms" 2.3 → "2.3s" 65 → "1m 5s" 3665 → "1h 1m 5s"



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/swarm_cli/ui/formatters/time.rb', line 32

def duration(seconds)
  return "0ms" if seconds.nil? || seconds.zero?

  if seconds < 1
    "#{(seconds * 1000).round}ms"
  elsif seconds < 60
    "#{seconds.round(2)}s"
  elsif seconds < 3600
    minutes = (seconds / 60).floor
    secs = (seconds % 60).round
    "#{minutes}m #{secs}s"
  else
    hours = (seconds / 3600).floor
    minutes = ((seconds % 3600) / 60).floor
    secs = (seconds % 60).round
    "#{hours}h #{minutes}m #{secs}s"
  end
end

.relative(time) ⇒ Object

Format relative time (future enhancement) Time.now - 120 → "2 minutes ago"



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/swarm_cli/ui/formatters/time.rb', line 53

def relative(time)
  return "" if time.nil?

  seconds_ago = ::Time.now - time

  case seconds_ago
  when 0...60
    "#{seconds_ago.round}s ago"
  when 60...3600
    "#{(seconds_ago / 60).round}m ago"
  when 3600...86400
    "#{(seconds_ago / 3600).round}h ago"
  else
    "#{(seconds_ago / 86400).round}d ago"
  end
end

.timestamp(time) ⇒ Object

Format timestamp as [HH:MM:SS] Time.now → "[12:34:56]"



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/swarm_cli/ui/formatters/time.rb', line 11

def timestamp(time)
  return "" if time.nil?

  case time
  when ::Time
    time.strftime("[%H:%M:%S]")
  when String
    parsed = ::Time.parse(time)
    parsed.strftime("[%H:%M:%S]")
  else
    ""
  end
rescue StandardError
  ""
end