Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

3 New Date and Time Libraries for Rubyists

By Peter Cooper / May 5, 2010

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.

To install:

gem install ice_cube

To use:

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.

John has put together a PDF presentation that shows off more usage, and there are some simple examples on the official site too.

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.

To install:

gem install tickle

To use:

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.

To install:

gem install business_time

Note: business_time depends on Active Support (gem: activesupport)

To use:

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"))

Comments

  1. Joshua Hull says:

    I made http://github.com/joshbuddy/spanner the other day because I was shocked to discover Chronic didn't parse time spans. I feel like we should bundle up Chronic, Tickle, Spanner et al and make an all-encompassing natural language time parsing library. There is probably a lot of overlap here.

  2. Nils Jonsson says:

    I can't help but think of the Flight of the Conchords song "Business Time." http://www.youtube.com/watch?v=WGOohBytKTU

  3. Sohan says:

    Your post has been linked at DrinkRails.com as one of the top ruby on rails blogs of the day.

  4. Trans says:

    The state of time and date libs for ruby are quite the tangle. Ruby itself has Time, Date and DateTime, plus standard lib extensions 'time.rb' and 'date.rb', and then there's all the ActiveSupport extensions. Then there is tzinfo and also chronic. And I'm sure there are more. Plus these three libs...

    I really would be nice if someone very detail minded sat down and just created the ultimate time/date library that took care of all this stuff.

  5. Josh says:

    Thanks for the mention. Just FYI to all that I've updated Tickle with a full set of unit tests as well as US Holiday support. I agree with Joshua Hull regarding the overlap and given that Chronic has a number of oddities with it, might be nice to bring them all together. There is a bunch in Tickle, such as the holiday support, that feels like it should be in Chronic as it's more point in time then recurring.

    Latest version is here: http://github.com/noctivityinc/tickle

  6. Ben says:

    Already using Runt (http://runt.rubyforge.org/), but I'll take a peek at ice_cube

Other Posts to Enjoy

Twitter Mentions