public
Description: A simple Ruby natural language parser for elapsed time
Homepage:
Clone URL: git://github.com/hpoydar/chronic_duration.git
name age message
file .gitignore Sun Feb 22 18:51:00 -0800 2009 Fixed RDoc build with missing .rdoc extension [olauzon]
file MIT-LICENSE Thu Jan 15 07:54:42 -0800 2009 Renamed license file [hpoydar]
file README.rdoc Mon Oct 05 11:08:44 -0700 2009 Upated README [hpoydar]
file Rakefile Mon Oct 05 12:50:30 -0700 2009 Added gemcutter rake tasks [hpoydar]
file VERSION Mon Oct 05 11:01:20 -0700 2009 Added jeweler management [hpoydar]
file chronic_duration.gemspec Mon Oct 05 12:50:30 -0700 2009 Added gemcutter rake tasks [hpoydar]
directory lib/ Mon Feb 23 19:20:23 -0800 2009 Updated documentation [hpoydar]
directory spec/ Mon Oct 05 11:01:20 -0700 2009 Added jeweler management [hpoydar]
README.rdoc

Chronic Duration

A simple Ruby natural language parser for elapsed time. (For example, 4 hours and 30 minutes, 6 minutes 4 seconds, 3 days, etc.) Returns all results in seconds. Will return an integer unless you get tricky and need a float. (4 minutes and 13.47 seconds, for example.)

The reverse can also be accomplished with the output method. So pass in seconds and you can get strings like 4 mins 31.51 secs (default format), 4h 3m 30s, or 4:01:29.

Installation

    $ sudo gem sources -a http://gemcutter.org
    $ sudo gem install chronic_duration

Usage

    >> require 'chronic_duration'
    => true
    >> ChronicDuration.parse('4 minutes and 30 seconds')
    => 270
    >> ChronicDuration.output(270)
    => 4 mins 30 secs
    >> ChronicDuration.output(270, :format => :short)
    => 4m 30s
    >> ChronicDuration.output(270, :format => :long)
    => 4 minutes 30 seconds
    >> ChronicDuration.output(270, :format => :chrono)
    => 4:30

Nil is returned if the string can’t be parsed

Examples of parse-able strings:

  • ‘12.4 secs’
  • ‘1:20’
  • ‘1:20.51’
  • ‘4:01:01’
  • ‘3 mins 4 sec’
  • ‘2 hrs 20 min’
  • ‘2h20min’
  • ‘6 mos 1 day’
  • ‘47 yrs 6 mos and 4d’
  • ‘two hours and twenty minutes’

Contributors

jduff, olauzon

TODO

  • Benchmark, optimize
  • Context specific matching (E.g., for ‘4m30s’, assume ‘m’ is minutes not months)
  • Smartly parse vacation-like durations (E.g., ‘4 days and 3 nights’)
  • :chrono output option should probably change to something like 4 days 4:00:12 instead of 4:04:00:12
  • Other locale support