Fixing time offsets in iCalendar / .ics files

Description of the Problem

Wanting to add a iCalendar (.ics) file to your calendar can become a nightmare if the times are incorrectly stated for a different time zones. I recently encountered this problem with a timetable calendar file of a conference. It contained 160 calendar events, all stated in an incorrect timezone resulting in unusable calendar entries when importing to the Apple Calendar application or to Google Calendar. So I had to find a way to fix the file.

Python Solution

#!/usr/bin/env python

from datetime import timedelta
from ics import Calendar #  ← install with:  pip install ics

with open('twepp15.ics', 'r') as f:
    c = Calendar(f.read())

# A time shift of +1h:
offset = timedelta(seconds=3600)

for e in c.events:
    print('------')
    print("Before correction: Event '{}' starts {} ends {}".format(e.name, e.begin, e.end))
    e.end += offset
    e.begin += offset
    print("After correction: Event '{}' starts {} ends {}".format(e.name, e.begin, e.end))

with open('twepp15_fixed.ics', 'w') as f:
    f.write(str(c))

This makes use of the great ics Python package (documentation on rtd). The type of e.begin and e.end is an Arrow object.

A more elaborate variant is to be found in this Github gist.

Solutions by setting the correct timezone in the .ics file

Maybe the problem can also be fixed by specifying the correct time zone globally in the .ics file? I didn't follow this more closely, but here are the hints that I collected about this:

Using VTIMEZONE?

Using X-WR-TIMEZONE:

X-WR-TIMEZONE:US/Pacific
X-WR-TIMEZONE:Europe/Vienna
X-WR-TIMEZONE:Europe/Brussels

(See timezoned.ics)

Resources