From 9829e80486e7ff5652e6361644db284723ee7ea7 Mon Sep 17 00:00:00 2001 From: Martin Eigenmann Date: Mon, 9 Sep 2024 13:40:07 +0200 Subject: [PATCH] chore: add test for per-event timezone where required --- icalevents/icalparser.py | 7 ++-- test/test_data/per_event_timezone.ics | 50 +++++++++++++++++++++++++++ test/test_icalevents.py | 13 +++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/test_data/per_event_timezone.ics diff --git a/icalevents/icalparser.py b/icalevents/icalparser.py index 79aee99..a1f2d56 100644 --- a/icalevents/icalparser.py +++ b/icalevents/icalparser.py @@ -167,11 +167,12 @@ def encode(value: Optional[vText]) -> Optional[str]: return str(value.encode("utf-8")) -def create_event(component, utc_default, strict): +def create_event(component, strict): """ Create an event from its iCal representation. :param component: iCal component + :param strict: :return: event """ @@ -323,11 +324,9 @@ def parse_events( # If there's exactly one timezone in the file, # assume it applies globally, otherwise UTC - utc_default = False if len(timezones) == 1: cal_tz = get_timezone(list(timezones)[0]) else: - utc_default = True cal_tz = UTC # < ========================================== @@ -358,7 +357,7 @@ def parse_events( exceptions[exdate[0:8]] = exdate if component.name == "VEVENT": - e = create_event(component, utc_default, strict) + e = create_event(component, strict) # make rule.between happy and provide from, to points in time that have the same format as dtstart s = component["dtstart"].dt diff --git a/test/test_data/per_event_timezone.ics b/test/test_data/per_event_timezone.ics new file mode 100644 index 0000000..5955786 --- /dev/null +++ b/test/test_data/per_event_timezone.ics @@ -0,0 +1,50 @@ +BEGIN:VCALENDAR +PRODID:-//Google Inc//Google Calendar 70.9054//EN +VERSION:2.0 +CALSCALE:GREGORIAN +METHOD:PUBLISH +X-WR-CALNAME:test +X-WR-TIMEZONE:Europe/Zurich +BEGIN:VTIMEZONE +TZID:Europe/Zurich +X-LIC-LOCATION:Europe/Zurich +BEGIN:DAYLIGHT +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +TZNAME:GMT+2 +DTSTART:19700329T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +TZNAME:GMT+1 +DTSTART:19701025T030000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +DTSTART;TZID=Europe/Zurich:20240325T070000 +DTEND;TZID=Europe/Zurich:20240325T080000 +DTSTAMP:20240908T134310Z +UID:21u17g5sbq791pn23imf7ue0lc@google.com +CREATED:20240908T134239Z +LAST-MODIFIED:20240908T134239Z +SEQUENCE:0 +STATUS:CONFIRMED +SUMMARY:Zürich +TRANSP:OPAQUE +END:VEVENT +BEGIN:VEVENT +DTSTART;TZID=Australia/Perth:20240325T070000 +DTEND;TZID=Australia/Perth:20240325T080000 +DTSTAMP:20240908T134310Z +UID:21u17g5sbq791pn23imf7ue0ld@google.com +CREATED:20240908T134239Z +LAST-MODIFIED:20240908T134239Z +SEQUENCE:0 +STATUS:CONFIRMED +SUMMARY:Perth +TRANSP:OPAQUE +END:VEVENT +END:VCALENDAR diff --git a/test/test_icalevents.py b/test/test_icalevents.py index 6aa01aa..00ce57a 100644 --- a/test/test_icalevents.py +++ b/test/test_icalevents.py @@ -902,6 +902,19 @@ class ICalEventsTests(unittest.TestCase): self.assertEqual(evs[3].start, datetime(2022, 6, 8, 13, 00, 0, tzinfo=tz)) self.assertEqual(evs[3].summary, "Recurring Event") + def test_per_event_timezone(self): + ical = "test/test_data/per_event_timezone.ics" + start = date(2024, 1, 1) + end = date(2024, 12, 30) + + events = icalevents.events(file=ical, start=start, end=end, strict=True) + self.assertEqual( + events[0].start.tzname(), "CET", "check tz as specified in calendar" + ) + self.assertEqual( + events[1].start.tzname(), "AWST", "check tz as specified in calendar" + ) + def test_regression_repeating_events_raise_an_error(self): ical = "test/test_data/recurrence_tzinfo.ics" start = date(2023, 1, 1)