3 New Date and Time Libraries for Rubyists
In the UK there's a cliché that goes: "You wait hours for a bus, and then three come along at once!" So it went with these three Ruby date and time libraries. They all made an appearance on RubyFlow last week and are all useful in their own ways, depending on how you're working with dates and times.
ice_cube - Fast querying and expansion of event recurrence rules
ice_cube is a library by John Crepezzi that provides "fast querying and expansion of recurrence rules in Ruby." What this means is that you can create schedules powered by date recurrence rules that can be quite complex (e.g. every 4 years on a Tuesday in the first week of November). Rules like these are defined by chaining methods together, rather than using natural language.
gem install ice_cube
require 'ice_cube' rule = IceCube::Rule.yearly(4).month_of_year(:november).day(:tuesday).day_of_month(2, 3, 4, 5, 6, 7, 8) schedule = IceCube::Schedule.new(Time.now) schedule.add_recurrence_rule rule schedule.first(3) # => [Tue Nov 02 05:04:38 +0000 2010, Tue Nov 04 05:04:38 +0000 2014, Tue Nov 06 05:04:38 +0000 2018]
ice_cube also supports exporting rules into iCal and YAML formats as well as a natural language equivalent.
tickle - A natural language parser for recurring events
tickle is a natural language parser for recurring events by Joshua Lippiner that stands in contrast to ice_cube's method driven approach. It depends on the popular chronic natural language date parser and appears (through my experience) to be for Ruby 1.9+ only.
tickle lets you throw it things like
every 4 days starting next saturday,
every other week,
the tenth of the month and similar (there are a lot of examples on tickle's GitHub page). You pass these to the
Tickle.parse method and you get the next occurrence of the rule.
gem install tickle
require 'tickle' Tickle.parse('every 4 days starting next saturday') # => 2010-05-01 12:00:00 +0000
tickle isn't particularly mature yet and it only makes it easy to get the next occurrence of your rule, but the developer suggests that once an event has occurred, you automatically run Tickle again to get the next date. In this way, it seems tickle is well suited for situations where only the next occurrence needs to be stored and the rule can be kept in a separate database column or similar.
business_time - Time and date offsets based on "business time/hours"
business_time is a new library that works with the concept of "business time" or "business hours." Rather than just letting you perform operations on dates by absolute numbers of days or hours, you can now work with business days and hours of your own definition. business_time depends heavily on Active Support.
gem install business_time
Note: business_time depends on Active Support (gem:
require 'active_support' require 'business_time' # Examples "from now" 4.business_hours.from_now 5.business_days.from_now # Using user supplied dates my_birthday = Date.parse("August 4th, 2010") 10.business_days.before(my_birthday) # Add a day to not count as a business day BusinessTime::Config.holidays << my_birthday # Overlapping days are OK 6.business_hours.after(Time.parse("August 3rd, 3:00pm"))