Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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/2154.bugfix
Original file line number Diff line number Diff line change
@@ -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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Preserved the timezone when parsing jCal TIME properties, including when parsing components and serializing them. Codex (GPT-6) assisted with reproducing the issue, implementing the fix, and writing regression tests. :user:`shrohan2003`

5 changes: 4 additions & 1 deletion src/icalendar/prop/dt/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines 267 to +270

@SemTiOne SemTiOne Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reject TZID when the jCal TIME value ends in Z, as vTime.from_xcal() does. zoneinfo silently replaces UTC with the local timezone. Also add a regression test for both providers.

Suggested change
with JCalParsingError.reraise_with_path_added(3):
value = cls.parse_jcal_value(jcal_property[3])
if params.tzid:
value = tzp.localize(value, params.tzid)
with JCalParsingError.reraise_with_path_added(3):
value = cls.parse_jcal_value(jcal_property[3])
if params.tzid:
if is_utc(value):
raise JCalParsingError(
f"Cannot mix {params.tzid} with UTC", cls
)
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:
Expand Down
37 changes: 37 additions & 0 deletions src/icalendar/tests/test_issue_2154_jcal_time_tzid.py
Original file line number Diff line number Diff line change
@@ -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