Using iCalendar for semi-predictable but oddball events

RFC reading for fun *and* pragmatism!

Using iCalendar for semi-predictable but oddball events


My girlfriend is a nurse, and around these parts that means she works shift work. Her schedule is a block of shifts (sometimes day shifts, sometimes evening shifts) followed by a block of days off (which also varies as to the number of days), and the entire schedule itself repeats every so many weeks. Basically, picture something like this:

https _bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com_public_images_9fdca9ec-9ad8-4df3-8cb5-bbdde5f5cce0_1260x426.png

...and repeat.

While there is a pattern to it, it essentially means that without access to a copy of her schedule readily at hand, it is literally impossible to know if we are free on some random day next month. So obviously, I want an electronic version at the ready for these types of inquiries.

Unfortunately, it is also incredibly tedious to enter all of these shifts one by one, by hand, into something like Google Calendar, because it requires manually creating dozens of events (“repeats every other Monday” unfortunately does not cut it here :P), and on each of them, manually selecting “Repeats,” manually selecting “Custom…”, manually selecting “every X weeks”, all without introducing any errors. Ugh.

Fortunately, here to save the day… the iCalendar specification!

What is the iCalendar specification?

The iCalendar specification (as in RFC 5545, not to be confused with good ol’ iCal, now known as Apple’s Calendar app) is a “data format for representing and exchanging calendaring and scheduling information such as events, to-dos, journal entries, and free/busy information, independent of any particular calendar service or protocol.”

Basically, it’s a list of rules about how to describe events in computer-friendly language, and if you follow those rules, users can import your events consistently in whatever various calendaring programs they might be using. Glancing through the specification, you’ll see it talks about how to specify all kinds of features you may have seen in various calendaring apps, such as:

  • Specify a location
  • Specify a time zone
  • Repeat an event with a certain frequency
  • Note whether someone’s an optional or required attendee
  • Denote whether it shows up as busy or free time on someone’s calendar
  • Send an alert a few minutes before an event happens

...and much, much more.

If you’ve ever received an email with a .ics file attached, and when you click it you’re prompted to add an event to your calendar, such as below… Congratulations! You officially have experience with iCalendar! :D

https _bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com_public_images_7fc915a4-a284-405f-8f0c-3ef32e207213_926x368.png

Let’s talk about what iCalendar looks like under the hood.

The Basics of iCalendar

The “simplest thing that can possibly work” in terms of iCalendar is a text file, with an .ics extension, that contains the following properties:

  • BEGIN:VCALENDAR … END:VCALENDAR: (required) Marks the beginning and end of the iCalendar object as a whole (similar to how <html> … </html> wraps around a web page).

  • VERSION: (required) The version number of the specification required (generally, “2.0” unless you’re feeling super retro).

  • PRODID: (required) A unique identifier for the product that created the iCalendar object. Some real-world examples are “-//Google Inc//Google Calendar 70.9054//EN” for Google Calendar and “-//Apple Inc.//Mac OS X 10.14.6//EN” for Apple Calendar.

  • BEGIN:VEVENT … BEGIN:VEVENT: One or more of these pairings mark the beginning and end of an event definition.

  • UID: (required) A unique identifier for the event. [buncha-random-chars] and [buncha-random-chars]@[] are both common patterns.

  • DTSTAMP: (required) The date/time that the event was created (not to be confused with when it starts, which is the next one).

  • DTSTART … DTEND: When the event starts and ends.

Here’s a simple example tying all of that together:

PRODID:-//Example Corp//Example Calendar App 1.0//EN
SUMMARY:Party like it’s 1999!

If you save this as “y2k.ics” and import it, you should see a new event appear at the very tail end 1999 that lasts from 11:59:59PM on December 31, 1999 until midnight on January 1, 2020!

How to specify a date/time?

The RFC gets into this at length, but the basic gist is:

  • If it’s a date only, it’s specified in the format of YYYYMMDD.
  • If it’s a date and time, it’s specified in the format of YYYYMMDDTHHMMSS (with HH in 24-hour time). If it’s a UTC time (see below) it also has a Z at the end.

There are three different ways to specify a date and time such as 11:59:59PM on December 31, 1999:

  1. Date with local time: Used when you want an event to be at the same time, regardless of a person’s location.

  2. Date with UTC time: Used when you want an event to be at an absolute time across all geographic locations, and disregarding Daylight Savings Time. (Note: DTSTAMP is always in this format.)

  3. Date with local time and timezone reference: A combination of both; it pegs to a specific time, but within a given time zone, so it takes into consideration things like Daylight Savings Time.

In the above example, we went with the “date with local time” format so that the event would be at the same time regardless of location, since exactly when the “last minute of 1999” is differs across the globe. But for tracking shift schedules, “Date with local time and timezone reference” makes more sense, because if I fly to Europe for work, I don’t want the start times to shift by several hours.

Nope, that semi-colon after DTSTART in the third example is not a typo. “Property parameters” such as TZID are passed in with a semicolon rather than a colon. (See List and Field Separators) This will also come into play with...

Repeating Events

Ok, so we already have enough information to create each shift schedule the first time, but note that it repeats every X weeks. How do we handle that?

Enter Recurrence Rules (RRULE)! This property defines a rule or repeating pattern for recurring events. Here are just a few of the options that it supports:

  • FREQ: How often the event occurs. Some examples are WEEKLY, DAILY, and even SECONDLY.

  • INTERVAL: How many FREQs should there be in between events?

  • COUNT: Repeat the event X times. (Useful if you have a class that runs for 4 weeks, for example)

  • UNTIL: Alternatively, this will repeat the event until a certain date.

  • BYDAY: Allows you to say something only happens on certain days of the week.

The RFC has all sorts of examples.

Putting it All Together

For our purposes, we need events that:

  1. Use a Date with local time and timezone reference because I travel frequently (in non-COVID times) and I want the events to be shown at the proper start/end times regardless of where I am in the world.

  2. Have a FREQ of WEEKLY and an INTERVAL of 8 since the schedule repeats every 8 weeks.

  3. And an UNTIL date of 12/31/2021 because the schedule gets reset every calendar year, and next year will be something different! (Hance my very strong desire to write this process down. ;))

Put it all together, and you get:

PRODID:-//Webchick Inc//Webchick’s Crappy Bash Script 1.0//EN

... (repeat x many more events) ...

SUMMARY:Evening Shift

... (repeat x many more events) ...


And when imported into Apple Calendar:

https _bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com_public_images_9d99d098-3f0a-4e16-b4b7-27cabf5ec3d4_1522x322.png

And there you have it!

So. The next time you get one of those .ics files, try popping it open in a text editor and see how much of it makes sense now!

PS: If you want to see the bash script I used to generate this, it’s at If you want to go it alone, this handy validator is your friend!

(Cover image: "iCal icon replacement" by bertop is licensed under CC BY-NC-SA 2.0)