From 7fe9b5294a99bf82f7c6fea6e0243cbfc32c594f Mon Sep 17 00:00:00 2001 From: Rahul Ranjan <11793455+rahul05ranjan@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:05:23 +0000 Subject: [PATCH 1/3] fix(prop): reject a zero-length period vPeriod raised ValueError only when the start came after the end, so a period whose end equaled its start - written either as two equal date-times or as a zero duration - was accepted and serialized back out. RFC 5545 section 3.3.9 requires the end of a period to come after its start, so a period with no length is as invalid as a reversed one. Reject start == end with the same ValueError the reversed case gets. The message now says the start is not before the end, which covers both cases, and the one test matching the old message is updated. Fixes #2087 Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- news/2087.bugfix | 1 + src/icalendar/prop/dt/period.py | 4 ++-- src/icalendar/tests/prop/test_vPeriod.py | 16 ++++++++++++++++ src/icalendar/tests/test_issue_1633.py | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 news/2087.bugfix diff --git a/news/2087.bugfix b/news/2087.bugfix new file mode 100644 index 000000000..0b23f2e36 --- /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. diff --git a/src/icalendar/prop/dt/period.py b/src/icalendar/prop/dt/period.py index 1deb428cf..7ce97b5e1 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 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/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")) From edc7f7649a4fb9ca78e083257316eef2789c5269 Mon Sep 17 00:00:00 2001 From: Rahul Ranjan <11793455+rahul05ranjan@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:20:39 +0000 Subject: [PATCH 2/3] fix(prop): reject a zero duration like a negative one in the xCal path RFC 5545 section 3.3.9 defines the period-start form with a positive duration, so a zero duration is as invalid as a negative one. The xCal parser's check now matches, raising the same "Expected positive duration" XCalParsingError for P0D, and the xCal test fixtures move the zero-duration specs from the valid round-trip lists to the invalid ones, where they already belonged: the constructor's start >= end rejection from the previous commit caught them first. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- src/icalendar/prop/dt/period.py | 2 +- src/icalendar/tests/rfc_6321_xcal/prop/test_period.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/icalendar/prop/dt/period.py b/src/icalendar/prop/dt/period.py index 7ce97b5e1..2ce0cd183 100644 --- a/src/icalendar/prop/dt/period.py +++ b/src/icalendar/prop/dt/period.py @@ -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/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), From 6b4f970eef1cd3186efdc8e6647511feeaf907dd Mon Sep 17 00:00:00 2001 From: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:17:39 +0000 Subject: [PATCH 3/3] docs(news): add GitHub handle to news/2087.bugfix Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- news/2087.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/2087.bugfix b/news/2087.bugfix index 0b23f2e36..36bfd53e5 100644 --- a/news/2087.bugfix +++ b/news/2087.bugfix @@ -1 +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. +: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