The horrors of internet calendar formats

by Brian Enigma on June 1, 2010 8:27pm

in Code,Dear Diary

I have been work­ing on a lit­tle side-project for a few weeks now.  One of the things I thought I could add easy-peasy, slam-dunk, was a read-only view of a shared cal­en­dar.  I have seen the iCal data for­mat before, and it didn’t strike me as par­tic­u­larly dif­fi­cult, just A BUNCH OF CAPITAL LETTERS and a touch of parsing.

BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:Some Calendar
X-WR-TIMEZONE:UTC
X-WR-CALDESC:Some Calendar
BEGIN:VEVENT
DTSTART:20100526T020000Z
DTEND:20100526T030000Z
DTSTAMP:20100602T023638Z
UID:q2eo6ejpn8scsdb1u2u0bvml6g@google.com
...and so on...

For the most part, it really is quite straight­for­ward.  The state machine for pars­ing is small: read a line at a time, events start with BEGIN:VEVENT and end with END:VEVENT.  For my pur­poses there were exactly three fields I cared about: DTSTART (the start time of the event), SUMMARY (the title of the event), and LOCATION (the loca­tion, duh!).

What gets tricky is repeat­ing events.  The DSTART is the very first event in the sequence that was ever recorded.  You then get one or more RRULE record, which has you dig­ging through RFCs (RFC-2445, to be exact) so that you can cal­cu­late the next occur­rence.  It’s not too bad for yearly events that occur on the same day/month, but gets a lit­tle wig­gily with things like “the sec­ond Mon­day” or “the Tues­day after the first Mon­day in Novem­ber.”  And let’s not get into excep­tion rules (EXRULE): if you have, say, a monthly meet­ing but skip a month or move it to a dif­fer­ent day one month.

What I thought would be a sim­ple exer­cise in pars­ing GMT time­stamps into an array sud­denly bal­loons out to a much larger and com­plex prob­lem of fore­cast­ing days.  At that point, the path of least resis­tance is to embed a Google cal­en­dar (the source of the iCal feeds) in an iframe.  There are a num­ber of open source imple­men­ta­tions for pars­ing the file — as in read­ing the raw strings into an array — but not much out there for under­stand­ing the for­mat — for inter­pret­ing it and doing some­thing intel­li­gi­ble with recur­ring events.  Sin­gle­hand­edly writ­ing a full iCal engine is beyond the scope of this project. 

Oh, and that XML for­mat that Google allows you to export?  It is a cou­ple of nor­mal­ized, yet use­less, fields in XML (cre­ate date, GUID, and what­not) with the meat of the cal­en­dar in a nice, human-readable, non­stan­dard HTML blob, con­tain­ing the first occur­rence and that fact that things recur, but noth­ing allow­ing you to recon­struct the recur­rences.  Good times.

Share and Enjoy:
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Yahoo! Buzz
  • Facebook
  • Google Bookmarks
  • Technorati

If you liked this post, you may also enjoy:

  1. Inter­net cal­en­dar for­mats revisited
  2. 5 Octo­ber 1582
  3. iPhone Impres­sions
  4. What’s a year, anyway?
  5. My Port­land Event Calendar

Leave a Comment

Previous post:

Next post: