Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified behavior to treat "floating" events found in a calendar with… #7

Merged
merged 1 commit into from
Apr 4, 2022
Merged
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
15 changes: 15 additions & 0 deletions test/calendars/single-event-no-tz.in.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:manually_generated
X-WR-TIMEZONE:Europe/Brussels
X-WR-CALNAME:MyCalendar
X-WR-CALDESC:Description of calendar
BEGIN:VEVENT
UID:match_1025179
DTSTAMP:20210911T014015Z
DESCRIPTION:When X-WR-TIMEZONE is ignored, this is a floating event, when it is interpreted, the event is assumed to be in the Europe/Brussels timezone
DTSTART:20210916T210000
DTEND:20210916T224500
SUMMARY:X-WR-TIMEZONE no timezone
END:VEVENT
END:VCALENDAR
15 changes: 15 additions & 0 deletions test/calendars/single-event-no-tz.out.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:manually_generated
X-WR-TIMEZONE:Europe/Brussels
X-WR-CALNAME:MyCalendar
X-WR-CALDESC:Description of calendar
BEGIN:VEVENT
UID:match_1025179
DTSTAMP:20210911T014015Z
DESCRIPTION:When X-WR-TIMEZONE is ignored, this is a floating event, when it is interpreted, the event is assumed to be in the Europe/Brussels timezone
DTSTART;TZID=Europe/Brussels:20210916T210000
DTEND;TZID=Europe/Brussels:20210916T224500
SUMMARY:X-WR-TIMEZONE no timezone
END:VEVENT
END:VCALENDAR
13 changes: 13 additions & 0 deletions test/test_convert_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ def test_conversion_changes_the_time_zone(to_standard, calendars, calendar_name,
assert_has_line(output_bytes, content, message)


@pytest.mark.parametrize("calendar_name,content,message", [
("single-event-no-tz.in.ics", ("DTSTART", "TZID=Europe/Brussels", ":20210916T210000"), "DTSTART should be updated."),
("single-event-no-tz.in.ics", ("DTEND", "TZID=Europe/Brussels", ":20210916T224500"), "DTEND should be updated."),
("single-event-no-tz.out.ics", ("DTSTART", "TZID=Europe/Brussels", ":20210916T210000"), "DTSTART stays the same in already converted calendar."),
])
def test_conversion_adds_the_time_zone(to_standard, calendars, calendar_name, content, message):
calendar = calendars[calendar_name]
new_calendar = to_standard(calendar.as_icalendar())
output_bytes = new_calendar.to_ical()
print(output_bytes)
assert_has_line(output_bytes, content, message)


@pytest.mark.parametrize("tz,line,message,calendar_name", [
("Europe/Paris", ("DTSTART", "TZID=Europe/Paris" ,"20211223T030000"), "(1) Use string as timezone", "single-events-DTSTART-DTEND.in.ics"),
(pytz.timezone("Europe/Berlin"), ("DTSTART", "TZID=Europe/Berlin", "20211223T030000"), "(2) Use pytz.timezone as timezone", "single-events-DTSTART-DTEND.in.ics"),
Expand Down
7 changes: 7 additions & 0 deletions x_wr_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ def walk_value_datetime(self, dt):

def is_UTC(self, dt):
"""Return whether the time zone is a UTC time zone."""
if dt.tzname() is None:
return False
return dt.tzname().upper() == "UTC"

def is_Floating(self, dt):
return dt.tzname() is None


class UTCChangingWalker(CalendarWalker):
"""Changes the UTC time zone into a new time zone."""
Expand All @@ -125,6 +130,8 @@ def walk_value_datetime(self, dt):
"""Walk along a datetime.datetime object."""
if self.is_UTC(dt):
return dt.astimezone(self.new_timezone)
elif self.is_Floating(dt):
return dt.replace(tzinfo=self.new_timezone)
return dt


Expand Down