Timeparser
Allows you to parse times from a string using a treetop grammar. It's pretty basic.
Examples
Timeparser.parse("1h20m").to_i # => 80
Timeparser.parse("1h20m").hours # => 1
Timeparser.parse("1h20m").hours(:up) # => 2
Timeparser.parse("1h40m").hours(:down) # => 1
Timeparser.parse("1h20m").minutes # => 80
Installation
Before installing this or another Gem I highly recommend using RVM and that you create a .rvmrc file for your project.
touch .rvmrc && echo "rvm use 1.9.2-p270@yourproject --create" > .rvmrc
Install the gem:
gem install timeparser
or add it to your Gemfile if you are using Bundler (recommended)
gem 'timeparser'
and then run
bundle
Usage
Timeparser.parse('2hours 30minutes')
Timeparser extends String with a method called #parse_time, use it like this:
"1h30m".parse_time
each of these methods will return a Timeparser::Time instance. If you need the accumulated minutes that were parsed out of the string call the #to_i or #minutes methods.
The Time instance also knows about hours, which you can access through the #hours method. #hours takes an argument which allows you to round up the hours based on the minutes. Default is rounding down.
Run the specs
git clone git://github.com/the-architect/timeparser.git
cd timeparser
bundle install
rake
You can view the coverage info generated by simplecov by opening the generated files after you ran the tests.
open coverage/index.html