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 @@
:class:`~icalendar.prop.dt.period.vPeriod` now rejects a zero-length period whose end equals its start, as :rfc:`5545#section-3.3.9` requires the end to come after the start. @rahul05ranjan
6 changes: 3 additions & 3 deletions src/icalendar/prop/dt/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def __init__(
else:
end = end_or_duration
duration = normalize_pytz(end - start)
if start > end:
raise ValueError("Start time is greater than end time")
if start >= end:
raise ValueError("Start time is not before end time")

self.params = Parameters(params or {"value": "PERIOD"})
# set the timezone identifier
Expand Down Expand Up @@ -334,7 +334,7 @@ def from_xcal(cls, parser: XCalParser, params: Parameters) -> Self:
if duration_element is None:
raise XCalParsingError("Expected <end> or <duration>", None, element)
end = vDuration.from_xcal(duration_element).td
if end < timedelta(0):
if end <= timedelta(0):
raise XCalParsingError(
"Expected positive duration",
duration_element.get_xsd_token(),
Expand Down
16 changes: 16 additions & 0 deletions src/icalendar/tests/prop/test_vPeriod.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def test_timezoned_with_timedelta(tzp):
((datetime(2000, 1, 1), "20000102T000000"), TypeError),
((datetime(2000, 1, 2), datetime(2000, 1, 1)), ValueError),
((datetime(2000, 1, 2), timedelta(-1)), ValueError),
# a zero-length period has no end after its start (#2087)
((datetime(2000, 1, 1), datetime(2000, 1, 1)), ValueError),
((datetime(2000, 1, 1), timedelta(0)), ValueError),
],
)
def test_invalid_parameters(period, error):
Expand All @@ -63,6 +66,19 @@ def test_invalid_parameters(period, error):
vPeriod(period)


def test_a_zero_length_period_is_rejected():
"""A period whose end equals its start is rejected, like a reversed one.

:rfc:`5545#section-3.3.9` requires the end to come after the start, so
a period with no length is as invalid as one that runs backwards (#2087).
"""
start = datetime(2026, 11, 10, 10, 0)
with pytest.raises(ValueError, match="Start time is not before end time"):
vPeriod((start, start))
with pytest.raises(ValueError, match="Start time is not before end time"):
vPeriod((start, timedelta(0)))


def test_ical_value_with_end():
"""ical_value property returns the start/end tuple."""
value = (datetime(2000, 1, 1), datetime(2000, 1, 2))
Expand Down
6 changes: 4 additions & 2 deletions src/icalendar/tests/rfc_6321_xcal/prop/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ def tzid(request) -> str | None:

PERIODS_DURATION_UTC = [
(START_UTC, DURATION_POSITIVE),
(START_UTC, DURATION_0),
]
PERIODS_DURATION_FLOATING = [
(START_FLOATING, DURATION_POSITIVE),
(START_FLOATING, DURATION_0),
]
PERIODS_DURATION_INVALID = [
(START_FLOATING, DURATION_NEGATIVE),
(START_UTC, DURATION_NEGATIVE),
# RFC 5545 requires a positive duration, so a zero duration is as
# invalid as a negative one (#2087).
(START_FLOATING, DURATION_0),
(START_UTC, DURATION_0),
]
PERIODS_END_UTC = [
(START_UTC, END_UTC),
Expand Down
2 changes: 1 addition & 1 deletion src/icalendar/tests/test_issue_1633.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,5 @@ def test_an_end_date_before_the_start_is_rejected():
the start, so the period is invalid. Reading the end as the end of that
day would invent a duration the calendar never stated.
"""
with pytest.raises(ValueError, match="Start time is greater than end time"):
with pytest.raises(ValueError, match="Start time is not before end time"):
vPeriod(vPeriod.from_ical("19970102T120000Z/19970102"))
Loading