class Rufus::Scheduler

def self.to_duration(seconds, options={})


from the result
* :drop_seconds, if set to true, seconds and milliseconds will be trimmed
account when building up the result
* :months, if set to true, months (M) of 30 days will be taken into

Options are :

(this behaviour mirrors the one found for parse_time_string()).

Rufus.to_duration 0.120 + 30 * 24 * 3600 + 1 # => "4w2d1s120"
Rufus.to_duration 7.051 # => "7s51"
Rufus.to_duration 0.051 # => "51"

'marker'
If a Float value is passed, milliseconds will be displayed without

Rufus.to_duration 30 * 24 * 3600 + 1, true # => "1M1s"

method can be set to true.
For 30 days months to be counted, the second parameter of this

are of variable length). Weeks are counted.
It goes from seconds to the year. Months are not counted (as they

Rufus.to_duration 30 * 24 * 3600 + 1 # => "4w2d1s"
Rufus.to_duration 7 * 24 * 3600 # => '1w'
Rufus.to_duration 3661 # => '1h1m1s'
Rufus.to_duration 60 # => '1m'
Rufus.to_duration 0 # => '0s'

Turns a number of seconds into a a time string
def self.to_duration(seconds, options={})
  h = to_duration_hash(seconds, options)
  return (options[:drop_seconds] ? '0m' : '0s') if h.empty?
  s =
    DU_KEYS.inject('') { |r, key|
      count = h[key]
      count = nil if count == 0
      r << "#{count}#{key}" if count
      r
    }
  ms = h[:ms]
  s << ms.to_s if ms
  s
end