What's the best way to model recurring events in a calendar application? [closed]
I'm building a group calendar application that needs to support recurring events, but all the solutions I've come up with to handle these events seem like a hack. I can limit how far ahead one can look, and then generate all the events at once. Or I can store the events as repeating and dynamically display them when one looks ahead on the calendar, but I'll have to convert them to a normal event if someone wants to change the details on a particular instance of the event.
I'm sure there's a better way to do this, but I haven't found it yet. What's the best way to model recurring events, where you can change details of or delete particular event instances?
(I'm using Ruby, but please don't let that constrain your answer. If there's a Ruby-specific library or something, though, that's good to know.)
Solution 1:
I would use a 'link' concept for all future recurring events. They are dynamically displayed in the calendar and link back to a single reference object. When events have taken place the link is broken and the event becomes a standalone instance. If you attempt to edit a recurring event then prompt to change all future items (i.e. change single linked reference) or change just that instance (in which case convert this to a standalone instance and then make change). The latter cased is slightly problematic as you need to keep track in your recurring list of all future events that were converted to single instance. But, this is entirely do-able.
So, in essence, have 2 classes of events - single instances and recurring events.
Solution 2:
I have developed multiple calendar-based applications, and also authored a set of reusable JavaScript calendar components that support recurrence. I wrote up an overview of how to design for recurrence that might be helpful to someone. While there are a few bits that are specific to the library I wrote, the vast majority of the advice offered is general to any calendar implementation.
Some of the key points:
- Store recurrence using the iCal RRULE format -- that's one wheel you really don't want to reinvent
- Do NOT store individual recurring event instances as rows in your database! Always store a recurrence pattern.
- There are many ways to design your event/exception schema, but a basic starting point example is provided
- All date/time values should be stored in UTC and converted to local for display
- The end date stored for a recurring event should always be the end date of the recurrence range (or your platform's "max date" if recurring "forever") and the event duration should be stored separately. This is to ensure a sane way of querying for events later. Read the linked article for more details about this.
- Some discussion around generating event instances and recurrence editing strategies is included
It's a really complicated topic with many, many valid approaches to implementing it. I will say that I've actually implemented recurrence several times successfully, and I would be wary of taking advice on this subject from anyone who hasn't actually done it.
Solution 3:
There can be many problems with recurring events, let me highlight a few that I know of.
Solution 1 - no instances
Store original appointment + recurrence data, do not store all the instances.
Problems:
- You'll have to calculate all the instances in a date window when you need them, costly
- Unable to handle exceptions (ie. you delete one of the instances, or move it, or rather, you can't do this with this solution)
Solution 2 - store instances
Store everything from 1, but also all the instances, linked back to the original appointment.
Problems:
- Takes a lot of space (but space is cheap, so minor)
- Exceptions must be handled gracefully, especially if you go back and edit the original appointment after making an exception. For instance, if you move the third instance one day forward, what if you go back and edit the time of the original appointment, re-insert another on the original day and leave the moved one? Unlink the moved one? Try to change the moved one appropriately?
Of course, if you're not going to do exceptions, then either solution should be fine, and you basically choose from a time/space trade off scenario.
Solution 4:
You may want to look at iCalendar software implementations or the standard itself (RFC 2445 RFC 5545).
Ones to come to mind quickly are the Mozilla projects http://www.mozilla.org/projects/calendar/ A quick search reveals http://icalendar.rubyforge.org/ as well.
Other options can be considered depending on how you're going to store the events. Are you building your own database schema? Using something iCalendar-based, etc.?