Last active
May 14, 2016 11:11
-
-
Save pgcd/2f2e880e64044c1d86f8d50c0b6f235b to your computer and use it in GitHub Desktop.
Generate iCal VTIMEZONE block with DAYLIGHT and STANDARD components, based on pytz zoneinfo data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytz | |
import icalendar | |
import datetime | |
def generate_vtimezone(timezone): | |
if not timezone: # UTC as a fallback doesn't work, since it has no transition info | |
return None | |
z = pytz.timezone(timezone) | |
utc = pytz.timezone('utc') | |
dst1, std1, dst2, std2 = filter(lambda x: x[0].year in (datetime.now().year, datetime.now().year + 1), | |
zip(z._utc_transition_times, z._transition_info)) | |
vtz = icalendar.Timezone(tzid=timezone) | |
dl = icalendar.TimezoneDaylight() | |
utcoffset, dstoffset, tzname = dst1[1] | |
offsetfrom = std1[1][0] | |
dl.add('dtstart', dst1[0] + offsetfrom) | |
dl.add('rdate', dst1[0] + offsetfrom) | |
dl.add('rdate', dst2[0] + offsetfrom) | |
dl.add('tzoffsetfrom', offsetfrom) | |
dl.add('tzoffsetto', utcoffset) | |
dl.add('tzname', tzname) | |
vtz.add_component(dl) | |
dl = icalendar.TimezoneStandard() | |
utcoffset, dstoffset, tzname = std1[1] | |
offsetfrom = dst1[1][0] | |
dl.add('dtstart', std1[0] + offsetfrom) | |
dl.add('rdate', std1[0] + offsetfrom) | |
dl.add('rdate', std2[0] + offsetfrom) | |
dl.add('tzoffsetfrom', offsetfrom) | |
dl.add('tzoffsetto', utcoffset) | |
dl.add('tzname', tzname) | |
vtz.add_component(dl) | |
return vtz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment