Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/test_convert_examples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""This test converts example calendars"""
import datetime
import icalendar
import pytest
import x_wr_timezone
import zoneinfo
Expand Down Expand Up @@ -113,3 +114,29 @@ def test_timezone_parameter(calendars, tz, line, message, calendar_name):
new_calendar = x_wr_timezone.to_standard(calendar.as_icalendar(), timezone=tz)
output_bytes = new_calendar.to_ical()
assert_has_line(output_bytes, line, message)


def test_invalid_x_wr_timezone_does_not_fail():
calendar = icalendar.Calendar.from_ical(b"""BEGIN:VCALENDAR
VERSION:2.0
X-WR-TIMEZONE:d6708488efb330c0
BEGIN:VEVENT
UID:invalid-x-wr-timezone
DTSTART:20260101T120000Z
DTEND:20260101T130000Z
SUMMARY:Invalid X-WR-TIMEZONE
END:VEVENT
END:VCALENDAR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR!

I think, we have calendar files for the tests. Please put this in there to make sure that the other tests pick them up.

""")

new_calendar = x_wr_timezone.to_standard(calendar)

assert new_calendar is calendar
assert new_calendar.to_ical() == calendar.to_ical()


def test_invalid_timezone_parameter_still_fails(calendars):
calendar = calendars["single-events-DTSTART-DTEND.in.ics"].as_icalendar()

with pytest.raises(zoneinfo.ZoneInfoNotFoundError):
x_wr_timezone.to_standard(calendar, timezone="d6708488efb330c0")
11 changes: 9 additions & 2 deletions x_wr_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,17 @@ def to_standard(

add_timezone_component: whether to add a VTIMEZONE component to the result.
"""
if timezone is None:
timezone_from_calendar = timezone is None
if timezone_from_calendar:
timezone = calendar.get(X_WR_TIMEZONE, None)
if timezone is not None and not isinstance(timezone, datetime.tzinfo):
timezone = zoneinfo.ZoneInfo(str(timezone))
try:
timezone = zoneinfo.ZoneInfo(str(timezone))
except zoneinfo.ZoneInfoNotFoundError:
if timezone_from_calendar:
timezone = None
else:
raise
result : icalendar.Calendar = calendar
del calendar
if timezone is not None:
Expand Down