Generating recurring events #541
Replies: 3 comments
-
This is hard. We did something for khal, which seems to work fine now but is pretty ugly (but MIT licensed), see here: Feel free to ping me if you have any questions. |
Beta Was this translation helpful? Give feedback.
-
Thanks for this @geier, I've bolted something together that copes with my tests. The complexities of this are too much for my brain to handle :) For anybody else struggling with this. I removed the timezone info from the dates that I pass into dateutil, I'm also using to I'm then storing the UID of the expanded events so that I can replace them with matching expanding events (i.e. when you change the third event in a recurrence you don't want to see two events). I'm not explaining myself well, here's some updated code: def get_recurrence_lines(event):
recurrence_lines = ['RRULE', 'EXRULE', 'RDATE', 'EXDATE']
for rl in recurrence_lines:
values = event.get(rl)
if values:
if isinstance(values, list):
for value in values:
value = value.to_ical().decode('utf-8')
yield f'{rl}:{value}'
else:
value = values.to_ical().decode('utf-8')
yield f'{rl}:{value}'
def expand_event(event):
if 'RRULE' in event:
event_start = event['DTSTART'].dt
tz = getattr(event_start, 'tzinfo', None)
event_start = event_start.replace(tzinfo=None)
rrule = '\n'.join(get_recurrence_lines(event))
ruleset = rrulestr(rrule, dtstart=event_start, forceset=True, ignoretz=True)
for event_dt_start in ruleset.before(datetime.now()):
yield event_dt_start.replace(tzinfo=tz), False
else:
if event['DTSTART'].dt < datetime.now():
yield event['DTSTART'].dt, True
ical = icalendar.Calendar.from_ical(data)
events = {}
for ev in ical.walk(name='VEVENT'):
for event, preferred in expand_event(ev):
key = (event['UID'], event['DTSTART'])
if key not in events or preferred:
events[key] = event
for e in events.values():
print(e) |
Beta Was this translation helpful? Give feedback.
-
Thanks William for putting your example online. I adapted some of it in my ical to html script and I hope that's OK. |
Beta Was this translation helpful? Give feedback.
-
I'm struggling to get my head around this one. I want to read in a calendar and walk through the events. When an event has a recurrence rule, I want to expand them out.
I understand that expanding the events isn't considered to be inside this project's scope? And that I should use dateutil instead? Can you point me in the right direction for doing this?
Here's what I'm trying so far:
I've tried various iterations of this code. Mostly I'm getting errors about not being able to compare timezone naive and aware datetimes.
The above code is simplified, in my actual code I'm returning events between two UTC datetimes.
How do I get this to work? Accounting for timezones and all kinds of combinations of RRULEs.
Beta Was this translation helpful? Give feedback.
All reactions