Module: FriendlyNumbers::SecondsToTime

Defined in:
lib/friendly_numbers/seconds_to_time.rb

Constant Summary collapse

DEFAULTS =
{
  format: ->(hours, minutes, seconds) {
    fields = []
    fields.push("#{hours}h") unless hours.zero?
    fields.push("#{minutes}m") unless minutes.zero?
    fields.push("#{seconds}s") if seconds

    fields.join(" ")
  }
}

Class Method Summary collapse

Class Method Details

.convert(seconds, options) ⇒ Object

:nodoc:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/friendly_numbers/seconds_to_time.rb', line 16

def convert(seconds, options) # :nodoc:
  options = DEFAULTS.merge(options)

  seconds = seconds.to_i

  minutes, seconds  = seconds.divmod(60)
  hours, minutes    = minutes.divmod(60)

  if options[:format].respond_to?(:call)
    options[:format].call(hours, minutes, seconds)
  end
end