From 070cf2a2d552f075b42bcf85ddfd7520ba40bcb1 Mon Sep 17 00:00:00 2001 From: Shanjidul Hasan Rohan <125924303+shrohan2003@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:39:25 +0600 Subject: [PATCH] Preserve TZID when parsing jCal TIME properties Localize the parsed time before constructing vTime so its constructor retains the supplied TZID. Cover direct property and Event round trips with pytz and zoneinfo, along with UTC and floating times. Codex (GPT-6) assisted with investigation, implementation, and testing. The user requested additional verified open-source bug fixes and PRs. Validation: four regression cases failed before the fix; the full suite passes with 22425 passed, 43 skipped, and 577 xfailed. Ruff passes. --- news/2154.bugfix | 1 + src/icalendar/prop/dt/time.py | 5 ++- .../tests/test_issue_2154_jcal_time_tzid.py | 37 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 news/2154.bugfix create mode 100644 src/icalendar/tests/test_issue_2154_jcal_time_tzid.py 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