Recurring Dates in Ruby iCalendar Using Runt, Ep. 1

Posted by Larry Karnowski Fri, 14 Mar 2008 20:00:00 GMT

What began as my "learn to write Ruby" project almost a year ago is finally taking shape thanks to Open Source Fridays at my new place of gainful employment -- Relevance!

iCalendar is the open standard for those ubiquitous *.ics files. These are the files we read and write with Apple's iCal, Google Calendars, and other standards-based calendaring programs. (Read that as "all calendars except Outlook.")

Ruby has a partial implementation of the specification at the Rubyforge iCalendar project. However, it's missing one important feature -- recurring dates.

For example, that 1pm Monday meeting that you have every week? The Ruby iCalendar project doesn't support it. Now, don't give the iCalendar folks a hard time -- recurring rules are damn hard to implement, and they've spent their time doing the most important stuff -- reading and writing the *.ics files. Also, since most webapps nowadays just export important dates as one-off dates anyway -- the Ruby implementation of iCalendar just hasn't really needed a recurring date implementation. (But we really want one!)

I started by brute forcing my way through the massive list of examples in the RFC 2445. I foolishly thought it was just a matter of hacking out an example using TDD and then refactoring all previous examples. No... I've found out it's just not that simple. (And let's face it, if that's all it took then the Ruby iCalendar folks would've added it ages ago! Poor, naive Larry.)

Then at one of the Raleigh.rb hack nights, Nathaniel Talbott recommended that I look at a little-known Ruby toolkit called Runt. This toolkit implements something called temporal expressions. You remember that weekly 1pm Monday meeting you have? Come to find out that with Runt they look like this:

  DIWeek.new(Mon) & REDay.new(13, 00,14, 00)

You read this as: "day in week: Monday, and range each day: from 1pm to 2pm". You can use this temporal expression either to generate a list of matching DateTimes or to check if a given DateTime matches. Very powerful stuff, incredibly powerful. (By the way, this all comes from an idea by Martin Fowler and implemented by Matthew Lipper.)

So, now my problem looks like this:

  • Take the iCalendar recurring rule (called a RRULE) syntax and convert it into an appropriate Runt temporal expression.
  • Let Runt do all the work.

I'm home free! Or... well, I'm not. Come to find out, Runt only implemented about 60% of what I needed to implement the full iCalendar spec. The biggest missing piece was the ability to handle dates at a "week precision". That is, I need to be able to know if a given day and another given day are in the same week. I finally got that feature in today!

So now with the weekly precision and a few other temporal expressions I added, I now have all but 14 of the 41 examples working as Test::Unit tests in Runt! That 65% may sound a little low, but it is actually something like 80% of the total iCalendar functionality. It's definitely all the key features. It definitely supports recurring rules based on a given day and month (mark November 15th on your calendar! it's my birthday!), and it definitely supports week-based recurring rules like the 1pm Monday meeting above. What else do you need?

Here's the patch for Runt's current Subversion trunk. I'm now working on my old iCalendar code. I'm going to do the same trick of using the RFC examples as Test::Unit tests.

I'm hoping to have more next week!

Tags , , , , , ,  | no comments