Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3c4402b
fix: reconcile matching DTEND and DURATION
aspire488 Nov 1, 2026
8724452
test: cover matching and conflicting event end values
aspire488 Nov 1, 2026
92f4287
test: update VEVENT and VTODO duration conflict expectations
aspire488 Nov 1, 2026
fdc741d
news: document DTEND and DURATION reconciliation
aspire488 Nov 1, 2026
d7cdff2
Fix changelog format and disclose AI assistance
aspire488 Nov 5, 2026
eab96fe
Merge branch 'main' into fix/start-end-duration-mismatch
angatha Nov 5, 2026
02c4022
fix(attr): normalize pytz after duration arithmetic
aspire488 Nov 6, 2026
c43c79b
test: cover pytz duration across DST transition
aspire488 Nov 9, 2026
be65db3
test: assert DST-aware validation result only
aspire488 Nov 9, 2026
44b2a10
style: format DST regression test
aspire488 Nov 9, 2026
0684353
Clarify DURATION precedence rationale (AI-assisted)
aspire488 Nov 11, 2026
da8743f
Add explicit mismatched end and duration test (AI-assisted)
aspire488 Nov 11, 2026
b8b1944
Fix test formatting reported by CI (AI-assisted)
aspire488 Nov 11, 2026
2fdaa3b
style: fix extra blank line in test imports
aspire488 Nov 12, 2026
b54c7fe
docs: explain DURATION precedence for issue 1796
aspire488 Nov 14, 2026
deb7d72
fix: prefer duration when end properties conflict
aspire488 Nov 14, 2026
8c48e9f
test: cover duration precedence and negative fallback
aspire488 Nov 14, 2026
545b903
style: format duration precedence condition
aspire488 Nov 14, 2026
ba7c608
fix: remove unused timezone helper import
aspire488 Nov 14, 2026
74b15fc
fix: validate matching end and duration values
aspire488 Nov 19, 2026
15ab08c
test: expect InvalidCalendar when DTEND and DURATION disagree
aspire488 Nov 20, 2026
24ec69b
docs: clarify DTSTART/DTEND/DURATION resolution
aspire488 Nov 21, 2026
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/1796.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Accepted matching VEVENT DTEND and DURATION values while rejecting conflicting end times; AI was used to assist with this change. @aspire488
32 changes: 20 additions & 12 deletions src/icalendar/attr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from icalendar.prop.conference import Conference
from icalendar.prop.image import Image
from icalendar.timezone import tzp
from icalendar.tools import is_date
from icalendar.tools import is_date, normalize_pytz

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -1776,16 +1776,7 @@ def get_start_end_duration_with_validation(
end = getattr(component, end_property, None)
duration = component.DURATION

# RFC 5545: Only one of end property and DURATION may be present
if duration is not None and end is not None:
end_name = "DTEND" if end_property == "DTEND" else "DUE"
msg = (
f"Only one of {end_name} and DURATION "
f"may be in a {component_name}, not both."
)
raise InvalidCalendar(msg)

# RFC 5545: When DTSTART is a date, DURATION must be of days or weeks
# RFC 5545: When DTSTART is a date, DURATION must be of days or weeks.
if (
start is not None
and is_date(start)
Expand All @@ -1795,14 +1786,31 @@ def get_start_end_duration_with_validation(
msg = "When DTSTART is a date, DURATION must be of days or weeks."
raise InvalidCalendar(msg)

# RFC 5545: DTSTART and end property must be of the same type
# RFC 5545: DTSTART and end property must be of the same type.
if start is not None and end is not None and is_date(start) != is_date(end):
end_name = "DTEND" if end_property == "DTEND" else "DUE"
msg = (
f"DTSTART and {end_name} must be of the same type, either date or datetime."
)
raise InvalidCalendar(msg)

# Issue #1796: real-world VEVENTs can contain DTSTART, DTEND, and DURATION.
# Prefer a non-negative DURATION; when it is negative but DTEND is valid
# (DTEND >= DTSTART), retain DTEND. Reject conflicting non-negative values.
if duration is not None and end is not None:
end_name = "DTEND" if end_property == "DTEND" else "DUE"
if duration < timedelta(0) and start is not None and end >= start:
duration = None
elif start is None:
msg = f"Cannot validate both {end_name} and DURATION without DTSTART."
raise InvalidCalendar(msg)
else:
expected_end = normalize_pytz(start + duration)
if end != expected_end:
msg = f"{end_name} and DURATION specify different end times."
raise InvalidCalendar(msg)
end = None

return start, end, duration


Expand Down
95 changes: 90 additions & 5 deletions src/icalendar/tests/test_consolidation_attr_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ def test_consolidated_functions_maintain_identical_behavior(self) -> None:
assert event1.duration == event2.duration


def test_matching_duration_and_end_normalize_pytz_across_dst() -> None:
"""Matching DTEND and DURATION must account for pytz DST normalization."""
pytz = pytest.importorskip("pytz")
timezone = pytz.timezone("Europe/Berlin")
start = timezone.localize(datetime(2026, 3, 29, 1, 30))
end = timezone.localize(datetime(2026, 3, 29, 3, 30))

event = Event()
event.add("UID", "test-pytz-dst-duration")
event.add("DTSTART", start)
event.add("DTEND", end)
event.add("DURATION", timedelta(hours=1))

validated_start, validated_end, duration = get_start_end_duration_with_validation(
event,
"DTSTART",
"DTEND",
"VEVENT",
)

assert validated_start == start
assert validated_end is None
assert duration == timedelta(hours=1)


class TestConsolidatedPropertyGetters:
"""Test consolidated property getter functions."""

Expand Down Expand Up @@ -274,23 +299,83 @@ def test_get_start_end_duration_validation_success(self) -> None:
assert end == datetime(2026, 1, 1, 15, 0)
assert duration is None

def test_get_start_end_duration_validation_errors(self) -> None:
"""Test get_start_end_duration_with_validation error cases."""
# Test both DTEND and DURATION
def test_duration_and_end_do_not_match(self) -> None:
"""If DTEND and DURATION describe different end times, the
calendar is ambiguous and validation must still fail.

See https://github.com/collective/icalendar/issues/1796.
"""
event = Event()
event.add("UID", "test-both")
event.add("UID", "test-both-mismatch")
event.add("DTSTART", datetime(2026, 1, 1, 12, 0))
event.add("DTEND", datetime(2026, 1, 1, 15, 0))
event.add("DURATION", timedelta(hours=2))

with pytest.raises(InvalidCalendar, match="Only one of DTEND and DURATION"):
with pytest.raises(InvalidCalendar, match="different end times"):
get_start_end_duration_with_validation(
event,
"DTSTART",
"DTEND",
"VEVENT",
)

def test_negative_duration_uses_valid_explicit_end(self) -> None:
"""A negative DURATION must not override a valid DTEND after DTSTART."""
event = Event()
event.add("UID", "test-negative-duration")
event.add("DTSTART", datetime(2026, 1, 1, 12, 0))
event.add("DTEND", datetime(2026, 1, 1, 15, 0))
event.add("DURATION", -timedelta(hours=2))

start, end, duration = get_start_end_duration_with_validation(
event,
"DTSTART",
"DTEND",
"VEVENT",
)

assert start == datetime(2026, 1, 1, 12, 0)
assert end == datetime(2026, 1, 1, 15, 0)
assert duration is None
assert event.end == datetime(2026, 1, 1, 15, 0)

def test_get_start_end_duration_validation_errors(self) -> None:
"""Test get_start_end_duration_with_validation error cases."""
# Matching DTEND and DURATION are accepted, with DURATION effective.
event = Event()
event.add("UID", "test-both-matching")
event.add("DTSTART", datetime(2026, 1, 1, 12, 0))
event.add("DTEND", datetime(2026, 1, 1, 14, 0))
event.add("DURATION", timedelta(hours=2))

start, end, duration = get_start_end_duration_with_validation(
event,
"DTSTART",
"DTEND",
"VEVENT",
)
assert start == datetime(2026, 1, 1, 12, 0)
assert end is None
assert duration == timedelta(hours=2)
assert event.end == datetime(2026, 1, 1, 14, 0)

# Conflicting end values remain invalid.
event_mismatch = Event()
event_mismatch.add("UID", "test-both-mismatch")
event_mismatch.add("DTSTART", datetime(2026, 1, 1, 12, 0))
event_mismatch.add("DTEND", datetime(2026, 1, 1, 15, 0))
event_mismatch.add("DURATION", timedelta(hours=2))

with pytest.raises(
InvalidCalendar, match="DTEND and DURATION specify different end times"
):
get_start_end_duration_with_validation(
event_mismatch,
"DTSTART",
"DTEND",
"VEVENT",
)

# Test invalid duration for date DTSTART
event2 = Event()
event2.add("UID", "test-date-duration")
Expand Down
4 changes: 2 additions & 2 deletions src/icalendar/tests/test_issue_662_component_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def test_start_and_duration(start_end_component, dtstart, duration):
),
(
invalid_event_end_3,
"Only one of DTEND and DURATION may be in a VEVENT, not both.",
"DTEND and DURATION specify different end times.",
),
(
invalid_event_end_4,
Expand All @@ -319,7 +319,7 @@ def test_start_and_duration(start_end_component, dtstart, duration):
),
(
invalid_todo_end_3,
"Only one of DUE and DURATION may be in a VTODO, not both.",
"DUE and DURATION specify different end times.",
),
(
invalid_todo_end_4,
Expand Down