From d65053e7662cbf500b2f5708404bf41ed5598de2 Mon Sep 17 00:00:00 2001 From: YASH YEOLE Date: Tue, 17 Nov 2026 22:53:33 +0530 Subject: [PATCH 1/3] Fix dateutil timezone ID detection on Windows --- news/2142.bugfix | 1 + .../test_issue_722_generate_vtimezone.py | 4 +- .../tests/test_timezone_identification.py | 39 +++++++++++++++++++ src/icalendar/timezone/tzid.py | 30 +++++++++++--- 4 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 news/2142.bugfix diff --git a/news/2142.bugfix b/news/2142.bugfix new file mode 100644 index 000000000..06ae2f552 --- /dev/null +++ b/news/2142.bugfix @@ -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 diff --git a/src/icalendar/tests/test_issue_722_generate_vtimezone.py b/src/icalendar/tests/test_issue_722_generate_vtimezone.py index e73c68c4d..0597dfab0 100644 --- a/src/icalendar/tests/test_issue_722_generate_vtimezone.py +++ b/src/icalendar/tests/test_issue_722_generate_vtimezone.py @@ -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() diff --git a/src/icalendar/tests/test_timezone_identification.py b/src/icalendar/tests/test_timezone_identification.py index aed2e3977..74d09f92c 100644 --- a/src/icalendar/tests/test_timezone_identification.py +++ b/src/icalendar/tests/test_timezone_identification.py @@ -32,6 +32,45 @@ def test_can_identify_dateutil(tzid): assert tzid in tzids_from_tzinfo(gettz(tzid)) +@pytest.mark.parametrize( + ("requested_tzid", "resolved_tzid", "expected_alias"), + [ + ("Pacific/Fiji", "Pacific/Fiji", "Pacific/Fiji"), + ("Brazil/West", "America/Manaus", "Brazil/West"), + ("Chile/EasterIsland", "Pacific/Easter", "Chile/EasterIsland"), + ("Arctic/Longyearbyen", "Atlantic/Jan_Mayen", "Arctic/Longyearbyen"), + ], +) +def test_can_identify_dateutil_tzfile_aliases( + requested_tzid, resolved_tzid, expected_alias +): + """Relative dateutil filenames resolve to IDs and their known aliases.""" + from dateutil.tz import gettz + + tzinfo = gettz(requested_tzid) + assert tzinfo is not None + assert tzinfo._filename == resolved_tzid + tzids = tzids_from_tzinfo(tzinfo) + assert resolved_tzid in tzids + assert expected_alias 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) diff --git a/src/icalendar/timezone/tzid.py b/src/icalendar/timezone/tzid.py index 304291ffd..802ca4416 100644 --- a/src/icalendar/timezone/tzid.py +++ b/src/icalendar/timezone/tzid.py @@ -11,6 +11,7 @@ from datetime import date, time, timezone from pathlib import Path from typing import TYPE_CHECKING +from zoneinfo import available_timezones from dateutil.tz import tz @@ -64,13 +65,32 @@ def tzids_from_tzinfo(tzinfo: tzinfo | None) -> tuple[str]: return _get_equivalent_tzids(olson) return _get_equivalent_tzids(tzinfo._name) # noqa: SLF001 if hasattr(tzinfo, "_filename"): # dateutil.tz.tzfile # noqa: SIM102 + path = Path(tzinfo._filename) # noqa: SLF001 + # 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 available_timezones(): + 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 available_timezones(): + 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 + known_tzids = available_timezones() + 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 () From f8f7a6bb2d2c7cd07e7dab39c3f9659de3e60db1 Mon Sep 17 00:00:00 2001 From: YASH YEOLE Date: Tue, 17 Nov 2026 23:25:38 +0530 Subject: [PATCH 2/3] Address timezone identification review feedback --- .../tests/test_timezone_identification.py | 22 +++++++++---------- src/icalendar/timezone/tzid.py | 16 +++++++++----- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/icalendar/tests/test_timezone_identification.py b/src/icalendar/tests/test_timezone_identification.py index 74d09f92c..cbff8343b 100644 --- a/src/icalendar/tests/test_timezone_identification.py +++ b/src/icalendar/tests/test_timezone_identification.py @@ -33,26 +33,24 @@ def test_can_identify_dateutil(tzid): @pytest.mark.parametrize( - ("requested_tzid", "resolved_tzid", "expected_alias"), + ("requested_tzid", "expected_equivalent_tzid"), [ - ("Pacific/Fiji", "Pacific/Fiji", "Pacific/Fiji"), - ("Brazil/West", "America/Manaus", "Brazil/West"), - ("Chile/EasterIsland", "Pacific/Easter", "Chile/EasterIsland"), - ("Arctic/Longyearbyen", "Atlantic/Jan_Mayen", "Arctic/Longyearbyen"), + ("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, resolved_tzid, expected_alias + requested_tzid, expected_equivalent_tzid ): """Relative dateutil filenames resolve to IDs and their known aliases.""" - from dateutil.tz import gettz + from types import SimpleNamespace - tzinfo = gettz(requested_tzid) - assert tzinfo is not None - assert tzinfo._filename == resolved_tzid + tzinfo = SimpleNamespace(_filename=requested_tzid) tzids = tzids_from_tzinfo(tzinfo) - assert resolved_tzid in tzids - assert expected_alias in tzids + assert requested_tzid in tzids + assert expected_equivalent_tzid in tzids def test_dateutil_tzfile_with_absolute_filename_returns_ids(tmp_path): diff --git a/src/icalendar/timezone/tzid.py b/src/icalendar/timezone/tzid.py index 802ca4416..8e467219d 100644 --- a/src/icalendar/timezone/tzid.py +++ b/src/icalendar/timezone/tzid.py @@ -11,7 +11,6 @@ from datetime import date, time, timezone from pathlib import Path from typing import TYPE_CHECKING -from zoneinfo import available_timezones from dateutil.tz import tz @@ -34,6 +33,13 @@ ) +def _get_available_timezones() -> set: + """Get timezone IDs through the zoneinfo provider's shared cache.""" + from .zoneinfo import ZONEINFO # noqa: PLC0415 + + 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. @@ -64,14 +70,15 @@ 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 available_timezones(): + if tzid in known_tzids: return _get_equivalent_tzids(tzid) return () if DATEUTIL_ZONEINFO_PATH is not None: @@ -80,12 +87,11 @@ def tzids_from_tzinfo(tzinfo: tzinfo | None) -> tuple[str]: except ValueError: pass else: - if tzid in available_timezones(): + 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 - known_tzids = available_timezones() for index in range(1, len(parts)): tzid = "/".join(parts[index:]) if tzid in known_tzids: From d37935b5c1b0e0e7e8e5bee1609bf97aa3165d9c Mon Sep 17 00:00:00 2001 From: YASH YEOLE Date: Tue, 17 Nov 2026 23:37:37 +0530 Subject: [PATCH 3/3] Fix timezone identification formatting --- src/icalendar/tests/test_timezone_identification.py | 4 +--- src/icalendar/timezone/tzid.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/icalendar/tests/test_timezone_identification.py b/src/icalendar/tests/test_timezone_identification.py index cbff8343b..20a8e389e 100644 --- a/src/icalendar/tests/test_timezone_identification.py +++ b/src/icalendar/tests/test_timezone_identification.py @@ -41,9 +41,7 @@ def test_can_identify_dateutil(tzid): ("Arctic/Longyearbyen", "Atlantic/Jan_Mayen"), ], ) -def test_can_identify_dateutil_tzfile_aliases( - requested_tzid, expected_equivalent_tzid -): +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 diff --git a/src/icalendar/timezone/tzid.py b/src/icalendar/timezone/tzid.py index 8e467219d..5aa96ea04 100644 --- a/src/icalendar/timezone/tzid.py +++ b/src/icalendar/timezone/tzid.py @@ -35,7 +35,7 @@ def _get_available_timezones() -> set: """Get timezone IDs through the zoneinfo provider's shared cache.""" - from .zoneinfo import ZONEINFO # noqa: PLC0415 + from .zoneinfo import ZONEINFO return ZONEINFO()._available_timezones # noqa: SLF001