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

Add timezone to period value types #556

Merged
merged 1 commit into from
Sep 22, 2023
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
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ New features:

Bug fixes:

- ...
- PERIOD values now set the timezone of their start and end. #556

5.0.8 (2023-09-18)
------------------
Expand Down
8 changes: 4 additions & 4 deletions src/icalendar/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def from_ical(cls, ical, timezone=None):
if u.startswith(('P', '-P', '+P')):
return vDuration.from_ical(ical)
if '/' in u:
return vPeriod.from_ical(ical)
return vPeriod.from_ical(ical, timezone=timezone)

if len(ical) in (15, 16):
return vDatetime.from_ical(ical, timezone=timezone)
Expand Down Expand Up @@ -548,11 +548,11 @@ def to_ical(self):
+ vDatetime(self.end).to_ical())

@staticmethod
def from_ical(ical):
def from_ical(ical, timezone=None):
try:
start, end_or_duration = ical.split('/')
start = vDDDTypes.from_ical(start)
end_or_duration = vDDDTypes.from_ical(end_or_duration)
start = vDDDTypes.from_ical(start, timezone=timezone)
end_or_duration = vDDDTypes.from_ical(end_or_duration, timezone=timezone)
return (start, end_or_duration)
except Exception:
raise ValueError(f'Expected period format, got: {ical}')
Expand Down
32 changes: 32 additions & 0 deletions src/icalendar/tests/calendars/period_with_timezone.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME;VALUE=TEXT:Test RDATE
BEGIN:VTIMEZONE
TZID:America/Vancouver
BEGIN:STANDARD
DTSTART:20221106T020000
TZOFFSETFROM:-0700
TZOFFSETTO:-0800
RDATE:20231105T020000
TZNAME:PST
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:20230312T020000
TZOFFSETFROM:-0800
TZOFFSETTO:-0700
RDATE:20240310T020000
TZNAME:PDT
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
UID:1
DESCRIPTION:Test RDATE
DTSTART;TZID=America/Vancouver:20230920T120000
DTEND;TZID=America/Vancouver:20230920T140000
EXDATE;TZID=America/Vancouver:20231220T120000
RDATE;VALUE=PERIOD;TZID=America/Vancouver:20231213T120000/20231213T150000
RRULE:FREQ=MONTHLY;COUNT=9;INTERVAL=1;BYDAY=+3WE;BYMONTH=1,2,3,4,5,9,10,11,
12;WKST=MO
SUMMARY:Test RDATE
END:VEVENT
END:VCALENDAR
17 changes: 16 additions & 1 deletion src/icalendar/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
import pytz
from icalendar.prop import vDDDTypes
import datetime


@pytest.mark.parametrize("calname,tzname,index,period_string", [
Expand All @@ -31,7 +32,7 @@ def test_issue_156_period_list_in_rdate(calendars, calname, tzname, index, perio
calendar = calendars[calname]
rdate = calendar.walk("vevent")[0]["rdate"]
period = rdate.dts[index]
assert period.dt == vDDDTypes.from_ical(period_string, timezone=pytz.timezone(tzname))
assert period.dt == vDDDTypes.from_ical(period_string, timezone=tzname)


def test_duration_properly_parsed(events):
Expand All @@ -46,3 +47,17 @@ def test_duration_properly_parsed(events):
assert period[1].days == 0
assert period[1].seconds == (5 * 60 + 30) * 60
assert period[1] == duration


def test_tzid_is_part_of_the_parameters(calendars):
"""The TZID should be mentioned in the parameters."""
event = list(calendars.period_with_timezone.walk("VEVENT"))[0]
assert event["RDATE"].params["TZID"] == "America/Vancouver"


def test_tzid_is_part_of_the_period_values(calendars):
"""The TZID should be set in the datetime."""
event = list(calendars.period_with_timezone.walk("VEVENT"))[0]
start, end = event["RDATE"].dts[0].dt
assert start == pytz.timezone("America/Vancouver").localize(datetime.datetime(2023, 12, 13, 12))
assert end == pytz.timezone("America/Vancouver").localize(datetime.datetime(2023, 12, 13, 15))