Module: Ohai::Mixin::SecondsToHuman

Included in:
DSL::Plugin
Defined in:
lib/ohai/mixin/seconds_to_human.rb

Instance Method Summary collapse

Instance Method Details

#seconds_to_human(seconds) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ohai/mixin/seconds_to_human.rb', line 22

def seconds_to_human(seconds)
  days = seconds.to_i / 86400
  seconds -= 86400 * days

  hours = seconds.to_i / 3600
  seconds -= 3600 * hours

  minutes = seconds.to_i / 60
  seconds -= 60 * minutes

  if days > 1
    return sprintf("%d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
  elsif days == 1
    return sprintf("%d day %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
  elsif hours > 0
    return sprintf("%d hours %02d minutes %02d seconds", hours, minutes, seconds)
  elsif minutes > 0
    return sprintf("%d minutes %02d seconds", minutes, seconds)
  else
    return sprintf("%02d seconds", seconds)
  end
end