Class: Formatters::TimeFormatter
- Inherits:
-
Object
- Object
- Formatters::TimeFormatter
- Defined in:
- lib/formatters/time_formatter.rb
Class Method Summary collapse
-
.humanize(secs) ⇒ String
format ‘secs` into a human readable output - X days Y hours Z minutes S seconds 0 will return “”.
Class Method Details
.humanize(secs) ⇒ String
format ‘secs` into a human readable output - X days Y hours Z minutes S seconds 0 will return “”
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 |