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
1 change: 1 addition & 0 deletions news/2087.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reject zero-length periods in vPeriod and vPeriod.from_ical when start and end datetimes are identical. @RajGahoi
15 changes: 13 additions & 2 deletions src/icalendar/prop/dt/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class vPeriod(TimeBase):
Raises:
TypeError: If the start is not a date or a datetime, or if the end is
not a date, a datetime, or a duration.
ValueError: If the start is after the end.
ValueError: If the start is after or equal to the end.

Examples:
A period from 18:00:00 UTC on January 1, 1997 to 07:00:00 UTC on
Expand Down Expand Up @@ -163,6 +163,8 @@ def __init__(
duration = normalize_pytz(end - start)
if start > end:
raise ValueError("Start time is greater than end time")
if not isinstance(end_or_duration, timedelta) and start == end:
raise ValueError("Start time is equal to end time")

self.params = Parameters(params or {"value": "PERIOD"})
# set the timezone identifier
Expand Down Expand Up @@ -198,7 +200,16 @@ def from_ical(ical, timezone=None):
end_or_duration = vDDDTypes.from_ical(end_or_duration, timezone=timezone)
except Exception as e:
raise ValueError(f"Expected period format, got: {ical}") from e
return _to_period_datetimes(start, end_or_duration)
start, end_or_duration = _to_period_datetimes(start, end_or_duration)
if isinstance(end_or_duration, timedelta):
end = normalize_pytz(start + end_or_duration)
else:
end = end_or_duration
if start > end:
raise ValueError("Start time is greater than end time")
if not isinstance(end_or_duration, timedelta) and start == end:
raise ValueError("Start time is equal to end time")
return start, end_or_duration

def __repr__(self) -> str:
p = (self.start, self.duration) if self.by_duration else (self.start, self.end)
Expand Down
1 change: 1 addition & 0 deletions src/icalendar/tests/prop/test_vPeriod.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_timezoned_with_timedelta(tzp):
(("20000101T000000", datetime(2000, 1, 2)), TypeError),
((datetime(2000, 1, 1), "20000102T000000"), TypeError),
((datetime(2000, 1, 2), datetime(2000, 1, 1)), ValueError),
((datetime(2000, 1, 1), datetime(2000, 1, 1)), ValueError),
((datetime(2000, 1, 2), timedelta(-1)), ValueError),
],
)
Expand Down
41 changes: 41 additions & 0 deletions src/icalendar/tests/test_issue_2087_zero_length_period.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from datetime import datetime, timezone

import pytest

from icalendar import Calendar
from icalendar.prop import vDDDLists, vPeriod
from icalendar.prop.broken import vBroken


def test_zero_length_period_identical_datetimes_rejected():
start = datetime(2026, 11, 10, 10, 0, tzinfo=timezone.utc)
with pytest.raises(ValueError, match="Start time is equal to end time"):
vPeriod((start, start))


def test_zero_length_period_from_ical_rejected():
with pytest.raises(ValueError, match="Start time is equal to end time"):
vPeriod.from_ical("20261110T100000Z/20261110T100000Z")


def test_zero_length_period_in_vdddlists_rejected():
with pytest.raises(ValueError, match="Start time is equal to end time"):
vDDDLists.from_ical("20261110T100000Z/20261110T100000Z")


def test_zero_length_period_in_calendar_marked_broken():
ics = """BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//example//test//EN
BEGIN:VEVENT
UID:zero-period
DTSTART:20261110T100000Z
RDATE;VALUE=PERIOD:20261110T100000Z/20261110T100000Z
END:VEVENT
END:VCALENDAR
"""
cal = Calendar.from_ical(ics)
rdate = cal.subcomponents[0]["RDATE"]
assert isinstance(rdate, vBroken)
assert isinstance(rdate.parse_error, ValueError)
assert "Start time is equal to end time" in str(rdate.parse_error)
Loading