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

given the number of seconds return a day/hours/minutes/seconds human form

Parameters:

  • seconds (Integer)

Returns:

  • String



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ohai/mixin/seconds_to_human.rb', line 27

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
    sprintf("%d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
  elsif days == 1
    sprintf("%d day %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds)
  elsif hours > 0
    sprintf("%d hours %02d minutes %02d seconds", hours, minutes, seconds)
  elsif minutes > 0
    sprintf("%d minutes %02d seconds", minutes, seconds)
  else
    sprintf("%02d seconds", seconds)
  end
end