Sha256: 85d3c5569cf07fcc3576d94edd58857c6da06fe3403e70caa8dab52f34df07a3

Contents?: true

Size: 1015 Bytes

Versions: 26

Compression:

Stored size: 1015 Bytes

Contents

class Fixnum
  # Convert a number of seconds to a string representation of a duration as '3 days', '2 hours', '1 day 2 hours 5 minutes 3 seconds'. See String::to_duration for the reverse conversion. A month is 30 days.
  def as_duration
    rest    = self
    years   = self / 31536000
    months  = (rest -= years  * 31536000) / 2592000
    days    = (rest -= months * 2592000)  / 86400
    hours   = (rest -= days   * 86400)    / 3600
    minutes = (rest -= hours  * 3600)     / 60
    seconds =  rest - minutes * 60
    res = []
    res << "#{years  } year#{  years   == 1 ? '' : 's'}" if years   != 0
    res << "#{months } month#{ months  == 1 ? '' : 's'}" if months  != 0
    res << "#{days   } day#{   days    == 1 ? '' : 's'}" if days    != 0
    res << "#{hours  } hour#{  hours   == 1 ? '' : 's'}" if hours   != 0
    res << "#{minutes} minute#{minutes == 1 ? '' : 's'}" if minutes != 0
    res << "#{seconds} second#{seconds == 1 ? '' : 's'}" if seconds != 0
    res == [] ? '0' : res.join(' ')
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
zena-0.16.2 lib/core_ext/fixnum.rb
zena-0.16.1 lib/core_ext/fixnum.rb
zena-0.16.0 lib/core_ext/fixnum.rb
zena-0.15.2 lib/core_ext/fixnum.rb
zena-0.15.1 lib/core_ext/fixnum.rb
zena-0.15.0 lib/core_ext/fixnum.rb