views:

202

answers:

3

I have an interesting problem trying to represent complex schedule data in a database. As a guideline, I need to be able to represent the entirety of what the iCalendar -- ics -- format can represent, but in a database. I'm not actually implementing anything relating to ics, but it gives a good scope of the type of rules I need to be able to model for my particular project.

I need to allow allow representation of a single event or a recurring event based on multiple times per day, days of the week, week of a month, month, year, or some combination of those. For example, the third Thursday in November annually, or the 25th of December annually, or every two weeks starting November 2 and continuing until September 8 the following year.

I don't care about insertion efficiency but query efficiency is critical. The operation I will be doing most often is providing either a single date/time or a date/time range, and trying to determine if the defined schedule matches any part of the date/time range. Other operations can be slower. For example, given January 15, 2010 at 10:00 AM through January 15, 2010 at 11:00 AM, find all schedules that match at least part of that time. (i.e. a schedule that covers 10:30 - 11:00 still matches.)

Any suggestions? I looked at http://stackoverflow.com/questions/1016170/how-would-one-represent-scheduled-events-in-an-rdbms but it doesn't cover the scope of the type of recurrence rules I'd like to model.

+1  A: 

The way I did something similar to this was to have two tables. If an event had no recurring pattern, then just store the date, start time, and end time. Your query checks if the time your searching for is greater than the start time of any entry and less than or equal to the end time of that same event.

For recurring events, I'm not too familiar with how iCalendar stores recurrences, but if you store each event by day of the week (you might have to have multiple rows for a single event if it repeats on more than one day a week), then search it almost the same way as the above table. For stranger recurrences like the third Tuesday of the week, you could have an extra column describing the specific condition. I might be able to give you a better answer for this if you could tell me more about how ics represents that kind of recurrence.

I hope that helps. I don't have much time right now. You can contact me later if you want to discuss this. I'm currently in Missouri so my availability for the next week is going to be erratic.

Frank Riccobono
A: 

This might be a trivial solution, but what would be the drawbacks of adding a column that defines the recurrence of the event (i.e. every x weeks, annually, weekly, etc) and using that as the result criterion?

Carlos Nunez
A: 

In the end, this post was most helpful:

http://stackoverflow.com/questions/1054201/ical-field-list-for-database-schema-based-on-ical-standard

We decided to follow the iCal model pretty exactly since the guys who wrote that standard had a great feel for the problem domain.

David Pfeffer