Method: Time#humanize

Defined in:
lib/doing/time.rb

#humanize(seconds) ⇒ String

Format seconds as a natural language string

Parameters:

  • seconds (Integer)

    number of seconds

Returns:

  • (String)

    Date formatted as "X days, X hours, X minutes, X seconds"



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/doing/time.rb', line 31

def humanize(seconds)
  s = seconds
  m = (s / 60).floor
  s = (s % 60).floor
  h = (m / 60).floor
  m = (m % 60).floor
  d = (h / 24).floor
  h = h % 24

  output = []
  output.push("#{d} #{'day'.to_p(d)}") if d.positive?
  output.push("#{h} #{'hour'.to_p(h)}") if h.positive?
  output.push("#{m} #{'minute'.to_p(m)}") if m.positive?
  output.push("#{s} #{'second'.to_p(s)}") if s.positive?
  output.join(', ')
end