Duration.new recieves a string describing a duration of time, and converts it into: seconds, and a standard readable format.
Or, you can pass Duration.new an integer representing seconds, and receive the same standard readable format.
Usage:
Passing a String
duration = Duration.new("2weeks 8 hr 30m")
duration.seconds # => 1240200
duration.readable # => "2 weeks, 8 hours and 30 mins"
Passing an Fixnum (of seconds)
duration = Duration.new(1240200) duration.readable # => "2 weeks, 8 hours and 30 mins"
Example valid input strings:
“2 weeks”, “2wks”, “2w”,
“8h”, “8 hrs”,”8 hours”,
“30m”, “0:30″, “30 minutes”, “30min”,
“2 weeks, 8 hours and 30 minutes”, “2w 8h 30m”, “2w 8:30″,
“4 hours 30 minutes”, “4 hours and 30m”, “4h 30min”, “4:30″, “4.5″, “4.50″, “4h, 30 min”, “4.5 hours”, “4.50h”
ahabman added functionality, extended and mashed-up this stack overflow article and english friendly timespan
One Trackback
[...] to install the following gems (”sudo gem install FOO”): map_by_method, what_methods, duration, and wirble. Most options should be self-explanatory and there’s plenty of [...]