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/2142.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed dateutil timezone identification on Windows when timezone objects expose relative IANA filenames and the zoneinfo root paths are unavailable. I used Claude Sonnet 5.5 to investigate the dateutil behavior and assist with implementation and regression tests, and ChatGPT GPT-6 to review the implementation, investigate platform-specific behavior, and validate the changes. @yash-yeole
4 changes: 3 additions & 1 deletion src/icalendar/tests/test_issue_722_generate_vtimezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,10 @@ def test_dateutil_timezone_is_matched_with_tzname(tzname):
event.start = datetime(2024, 11, 12, tzinfo=gettz(tzname))
print(dir(event.start.tzinfo))
cal.add_component(event)
assert cal.get_missing_tzids() == {tzname}
stored_tzid = event["DTSTART"].params["TZID"]
assert cal.get_missing_tzids() == {stored_tzid}
cal.add_missing_timezones()
assert [timezone.tz_name for timezone in cal.timezones] == [stored_tzid]
assert cal.get_missing_tzids() == set()


Expand Down
35 changes: 35 additions & 0 deletions src/icalendar/tests/test_timezone_identification.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ def test_can_identify_dateutil(tzid):
assert tzid in tzids_from_tzinfo(gettz(tzid))


@pytest.mark.parametrize(
("requested_tzid", "expected_equivalent_tzid"),
[
("Pacific/Fiji", "Pacific/Fiji"),
("Brazil/West", "America/Manaus"),
("Chile/EasterIsland", "Pacific/Easter"),
("Arctic/Longyearbyen", "Atlantic/Jan_Mayen"),
],
)
def test_can_identify_dateutil_tzfile_aliases(requested_tzid, expected_equivalent_tzid):
"""Relative dateutil filenames resolve to IDs and their known aliases."""
from types import SimpleNamespace

tzinfo = SimpleNamespace(_filename=requested_tzid)
tzids = tzids_from_tzinfo(tzinfo)
assert requested_tzid in tzids
assert expected_equivalent_tzid in tzids


def test_dateutil_tzfile_with_absolute_filename_returns_ids(tmp_path):
from types import SimpleNamespace

filename = tmp_path / "zoneinfo" / "America" / "Manaus"
tzinfo = SimpleNamespace(_filename=str(filename))
tzids = tzids_from_tzinfo(tzinfo)
assert "America/Manaus" in tzids
assert "Brazil/West" in tzids


def test_dateutil_tzfile_with_unrecognized_filename_returns_no_ids():
from types import SimpleNamespace

assert tzids_from_tzinfo(SimpleNamespace(_filename="not/a/timezone")) == ()


def test_utc_is_identified(utc):
"""Test UTC because it is handled in a special way."""
assert "UTC" in tzids_from_tzinfo(utc)
Expand Down
38 changes: 32 additions & 6 deletions src/icalendar/timezone/tzid.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
)


def _get_available_timezones() -> set:
"""Get timezone IDs through the zoneinfo provider's shared cache."""
from .zoneinfo import ZONEINFO

return ZONEINFO()._available_timezones # noqa: SLF001


def tzids_from_tzinfo(tzinfo: tzinfo | None) -> tuple[str]:
"""Get several timezone ids if we can identify the timezone.

Expand Down Expand Up @@ -63,14 +70,33 @@ def tzids_from_tzinfo(tzinfo: tzinfo | None) -> tuple[str]:
if olson is not None:
return _get_equivalent_tzids(olson)
return _get_equivalent_tzids(tzinfo._name) # noqa: SLF001
if hasattr(tzinfo, "_filename"): # dateutil.tz.tzfile # noqa: SIM102
if hasattr(tzinfo, "_filename"): # dateutil.tz.tzfile
path = Path(tzinfo._filename) # noqa: SLF001
known_tzids = _get_available_timezones()
# dateutil can retain a relative TZID (notably when using the tzdata
# package), or an absolute path into a system zoneinfo directory.
# Resolve the latter against the known directory when possible.
if not path.is_absolute():
tzid = path.as_posix()
if tzid in known_tzids:
return _get_equivalent_tzids(tzid)
return ()
if DATEUTIL_ZONEINFO_PATH is not None:
# tzfile('/usr/share/zoneinfo/Europe/Berlin')
path = tzinfo._filename # noqa: SLF001
if path.startswith(str(DATEUTIL_ZONEINFO_PATH)):
tzid = str(Path(path).relative_to(DATEUTIL_ZONEINFO_PATH))
try:
tzid = path.relative_to(DATEUTIL_ZONEINFO_PATH).as_posix()
except ValueError:
pass
else:
if tzid in known_tzids:
return _get_equivalent_tzids(tzid)
# Some dateutil builds do not expose the zoneinfo root. Find a valid
# TZID suffix in the absolute path, independent of path separators.
parts = path.parts
for index in range(1, len(parts)):
tzid = "/".join(parts[index:])
if tzid in known_tzids:
return _get_equivalent_tzids(tzid)
return _get_equivalent_tzids(path)
return ()
if isinstance(tzinfo, tz.tzutc):
return _get_equivalent_tzids("UTC")
return ()
Expand Down
Loading