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..20a8e389e 100644 --- a/src/icalendar/tests/test_timezone_identification.py +++ b/src/icalendar/tests/test_timezone_identification.py @@ -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) diff --git a/src/icalendar/timezone/tzid.py b/src/icalendar/timezone/tzid.py index 304291ffd..5aa96ea04 100644 --- a/src/icalendar/timezone/tzid.py +++ b/src/icalendar/timezone/tzid.py @@ -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. @@ -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 ()