Module: TimeSupport

Defined in:
lib/bumbleworks/gui/lib/time_support.rb

Class Method Summary collapse

Class Method Details

.seconds_to_units(seconds, include_zeros: false, round_seconds: true) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/bumbleworks/gui/lib/time_support.rb', line 3

def seconds_to_units(seconds, include_zeros: false, round_seconds: true)
  seconds = seconds.to_i if round_seconds
  units = Hash[
    [:days, :hours, :minutes, :seconds].zip(
      [60, 60, 24].inject([seconds]) {|result, unitsize|
        result.unshift(*result.shift.divmod(unitsize))
        result
      }
    )
  ]
  units.reject! { |k,v| v == 0 } unless include_zeros
  units
end

.seconds_to_units_in_words(seconds, **options) ⇒ Object



17
18
19
20
21
# File 'lib/bumbleworks/gui/lib/time_support.rb', line 17

def seconds_to_units_in_words(seconds, **options)
  seconds_to_units(seconds, options).map { |unit, num|
    "#{num} #{unit}"
  }.join(', ')
end