TimeInterval
Did you know that the ISO8601 defines time interval standards?
Find out all about it here: http://en.wikipedia.org/wiki/ISO_8601#Time_intervals
# A one month period
interval = TimeInterval.parse "2007-03-01T00:00:00Z/P1M"
interval.start_time # => 2007-03-01 00:00:00 UTC
interval.end_time # => 2007-04-01 00:00:00 UTC
# A period defined by two time points
interval = TimeInterval.parse "2007-03-01T00:00:00Z/2007-09-01T00:00:00Z"
interval.start_time # => 2007-03-01 00:00:00 UTC
interval.end_time # => 2007-09-01 00:00:00 UTC
# A repeating five minute period
repeating_interval = TimeInterval.parse "R/2007-03-01T00:00:00Z/PT5M"
periods = repeating_interval.take(100) # => using take due to infinite nature of repeating interval
periods.last.start_time # => 2007-03-01 08:15:00 UTC
periods.last.end_time # => 2007-03-01 08:20:00 UTC
# A two week period repeated two times
repeating_interval = TimeInterval.parse "R2/2007-03-01T00:00:00Z/P2W"
periods = repeating_interval.to_a
periods.length # => 2
periods[0].start_time # => 2007-03-01 00:00:00 UTC
periods[0].end_time # => 2007-03-15 00:00:00 UTC
periods[1].start_time # => 2007-03-15 00:00:00 UTC
periods[1].end_time # => 2007-03-29 00:00:00 UTC
periods[2] # => nil
Installation
Add this line to your application's Gemfile:
gem 'time-interval'
And then execute:
$ bundle
Or install it yourself as:
$ gem install time-interval
Usage
TimeInterval.parse(iso8601)
will parse ISO8601 string into a time, interval, or repeating interval.
TimeInterval.repeating?(iso8601)
will check if ISO8601 string is a repeating interval.
TimeInterval.interval?(iso8601)
will check if ISO8601 string is an interval.
Contributing
- Fork it ( https://github.com/SebastianEdwards/time-interval/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request