diff --git a/news/2087.bugfix b/news/2087.bugfix new file mode 100644 index 000000000..36bfd53e5 --- /dev/null +++ b/news/2087.bugfix @@ -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 diff --git a/src/icalendar/prop/dt/period.py b/src/icalendar/prop/dt/period.py index 1deb428cf..2ce0cd183 100644 --- a/src/icalendar/prop/dt/period.py +++ b/src/icalendar/prop/dt/period.py @@ -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 @@ -334,7 +334,7 @@ def from_xcal(cls, parser: XCalParser, params: Parameters) -> Self: if duration_element is None: raise XCalParsingError("Expected or ", 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(), diff --git a/src/icalendar/tests/prop/test_vPeriod.py b/src/icalendar/tests/prop/test_vPeriod.py index b11eb8f05..9f3fe967a 100644 --- a/src/icalendar/tests/prop/test_vPeriod.py +++ b/src/icalendar/tests/prop/test_vPeriod.py @@ -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): @@ -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)) diff --git a/src/icalendar/tests/rfc_6321_xcal/prop/test_period.py b/src/icalendar/tests/rfc_6321_xcal/prop/test_period.py index 87c9eb4a3..12859f987 100644 --- a/src/icalendar/tests/rfc_6321_xcal/prop/test_period.py +++ b/src/icalendar/tests/rfc_6321_xcal/prop/test_period.py @@ -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), diff --git a/src/icalendar/tests/test_issue_1633.py b/src/icalendar/tests/test_issue_1633.py index 14ca7f9f2..9c35a79c4 100644 --- a/src/icalendar/tests/test_issue_1633.py +++ b/src/icalendar/tests/test_issue_1633.py @@ -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"))