Class: Fixnum
- Inherits:
-
Object
- Object
- Fixnum
- Defined in:
- lib/zena/core_ext/fixnum.rb
Instance Method Summary collapse
-
#as_duration ⇒ Object
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’.
- #fmt(format) ⇒ Object
Instance Method Details
#as_duration ⇒ Object
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.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/zena/core_ext/fixnum.rb', line 3 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 |
#fmt(format) ⇒ Object
21 22 23 |
# File 'lib/zena/core_ext/fixnum.rb', line 21 def fmt(format) # TODO: Better strftime with thousand separator end |