diff --git a/news/2154.bugfix b/news/2154.bugfix new file mode 100644 index 000000000..0003bc9f8 --- /dev/null +++ b/news/2154.bugfix @@ -0,0 +1 @@ +Preserve the timezone when parsing jCal TIME properties with a TZID, including through component parsing and serialization. Codex (GPT-6) assisted with reproduction, implementation, and regression tests. diff --git a/src/icalendar/prop/dt/time.py b/src/icalendar/prop/dt/time.py index bed3b10d1..48bbd1726 100644 --- a/src/icalendar/prop/dt/time.py +++ b/src/icalendar/prop/dt/time.py @@ -263,11 +263,14 @@ def from_jcal(cls, jcal_property: list) -> Self: ~icalendar.error.JCalParsingError: If the provided jCal is invalid. """ JCalParsingError.validate_property(jcal_property, cls) + params = Parameters.from_jcal_property(jcal_property) with JCalParsingError.reraise_with_path_added(3): value = cls.parse_jcal_value(jcal_property[3]) + if params.tzid: + value = tzp.localize(value, params.tzid) return cls( value, - params=Parameters.from_jcal_property(jcal_property), + params=params, ) def to_xcal(self, element: Element) -> None: diff --git a/src/icalendar/tests/test_issue_2154_jcal_time_tzid.py b/src/icalendar/tests/test_issue_2154_jcal_time_tzid.py new file mode 100644 index 000000000..92e0b1d17 --- /dev/null +++ b/src/icalendar/tests/test_issue_2154_jcal_time_tzid.py @@ -0,0 +1,37 @@ +"""Preserve TIME timezones through the jCal component parsing entry point.""" + +from copy import deepcopy +from datetime import time + +import pytest + +from icalendar import Event +from icalendar.prop import vTime +from icalendar.timezone.tzid import tzid_from_dt + + +@pytest.mark.parametrize("zone", ["Asia/Dhaka", "Europe/Berlin", "UTC"]) +def test_jcal_time_retains_timezone(tzp, zone): + """Parsing and reserializing a TIME must preserve its timezone semantics.""" + expected = vTime(tzp.localize(time(8, 30), zone)) + property_jcal = expected.to_jcal("x-time") + original = deepcopy(property_jcal) + + parsed = vTime.from_jcal(property_jcal) + assert tzid_from_dt(parsed.dt) == tzid_from_dt(expected.dt) + assert parsed.to_jcal("x-time") == property_jcal + + event_jcal = ["vevent", [property_jcal], []] + event = Event.from_jcal(event_jcal) + assert tzid_from_dt(event["X-TIME"].dt) == tzid_from_dt(expected.dt) + assert event.to_jcal() == event_jcal + assert property_jcal == original + + +def test_jcal_floating_time_stays_floating(): + """A time without a TZID or UTC suffix remains floating.""" + property_jcal = ["x-time", {}, "time", "08:30:00"] + parsed = vTime.from_jcal(property_jcal) + assert parsed.dt == time(8, 30) + assert parsed.dt.tzinfo is None + assert parsed.to_jcal("x-time") == property_jcal