Search code examples
icalendarrecurrencerrule

iCalendar Spec RRULE Multiple Times?


I'm developing some software which interfaces with the Google Calendar API, and one feature my users will be able to take advantage of is setting multiple event times for a single "class" (i.e. the class is 2-5 on some days and 3-6 on others).

Is it possible for a recurrence rule to specify a different time for different days, allowing me to create one repeating event instead of multiple?

Thank you!


Solution

  • Is it possible for a recurrence rule to specify a different time for different days, allowing me to create one repeating event instead of multiple?

    No, not really, but you can achieve this result with other methods.

    • If there is a pattern, you can combine multiple RRULE. For example one repeat every other days starting Monday at 2, the second one repeat every other days starting Tuesday at 3. So the combined result will be Mon at 2, Tue at 3, Wed at 2, and so on. Though note that according to the RFC you SHOULD NOT define more than one RRULE (see https://www.rfc-editor.org/rfc/rfc5545#section-3.8.5.3)

    it SHOULD NOT be specified more than once. The recurrence set generated with multiple "RRULE" properties is undefined.

    So the behavior depends on the actual implementation and I do not know about Google Calendar API. Most libraries I know of do support multiple RRULE though, so you should give it a try.

    • Again, if there is a pattern, you can also use BYSETPOS. It's a bit complicated to wrap your head around this one, but basically you need to generate a set of occurrences (for example, over a week) and then cherry pick the ones that are valid. Try something like this (multi-lines for clarity):
    DTSTART=20160711T140000
    FREQ=WEEKLY
    BYDAY=MO,TU,WE,TH,FR
    BYHOUR=14,15
    BYSETPOS=1,4,5,8,9
    

    This will alternate Monday at 2, Tuesday at 3, Wednesday at 2, and so on. Again, you need a clear repetitive pattern for this one to work.

    • If there is no pattern and you just want to have some days with a different time, combine your RRULE with RDATE and EXDATE to add or remove special occurrences.