Ruby Duration

DOWNLOAD Duration (44.42 KB zip with documentation)

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”

http://ahabman.com added functionallity, extended and mashed-up http://stackoverflow.com/questions/657309/how-to-parse-days-hours-minutes-seconds-in-ruby and http://www.postal-code.com/binarycode/2007/04/04/english-friendly-timespan/

June 12, 2009 • Posted in: Uncategorized

One Response to “Ruby Duration”

  1. Improved irb configuration | tail -f findings.out - July 18, 2009

    [...] 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 [...]

Leave a Reply