Class: Formatters::TimeFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/formatters/time_formatter.rb

Class Method Summary collapse

Class Method Details

.humanize(secs) ⇒ String

format ‘secs` into a human readable output - X days Y hours Z minutes S seconds 0 will return “”

Parameters:

  • secs (Float|Integer)

    the number of seconds to format, eg: 940913.38729661

Returns:

  • (String)

    the human readable string



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/formatters/time_formatter.rb', line 13

def self.humanize(secs)
  [[60, :seconds], [60, :minutes], [24, :hours], [Float::INFINITY, :days]].map do |count, name|
    if secs.positive?
      secs, num = secs.divmod(count)

      num = num.to_i
      unit = num > 1 ? name : name.to_s.chomp('s')
      "#{num} #{unit}" unless num.zero?
    end
  end.compact.reverse.join(' ')
end