Skip to content

Commit df5a7d7

Browse files
authored
Fix pendulum.tz.timezones() to use system tzdata (#801)
* Fix `pendulum.tz.timezones()` to use system tzdata Fix the `pendulum.tz.available_timezones()` to use `available_timezones()` function instead of iterating over the files in `tzdata` package. This is more in line with PEP 615, as the system timezone functions will operate on system-provided tzdata when available, and use the `tzdata` package only if it's not available. Therefore, the previous code would yield a potentially different list of timezones than the system actually provides. Furthermore, Gentoo provides a dummy `tzdata` package that does not provide any data, since Python always uses system tzdata. This change is necessary to make pendulum work again on Gentoo. Fixes #769 * Fix type hints * Remove unused `_timezones` variable * Remove unused imports * Add caching to `timezones()`
1 parent 6705906 commit df5a7d7

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/pendulum/tz/__init__.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from __future__ import annotations
22

3-
from importlib import resources
4-
from typing import TYPE_CHECKING
5-
from typing import cast
3+
from functools import cache
4+
from zoneinfo import available_timezones
65

76
from pendulum.tz.local_timezone import get_local_timezone
87
from pendulum.tz.local_timezone import set_local_timezone
@@ -12,27 +11,16 @@
1211
from pendulum.tz.timezone import Timezone
1312

1413

15-
if TYPE_CHECKING:
16-
from pathlib import Path
17-
18-
1914
PRE_TRANSITION = "pre"
2015
POST_TRANSITION = "post"
2116
TRANSITION_ERROR = "error"
2217

23-
_timezones = None
24-
2518
_tz_cache: dict[int, FixedTimezone] = {}
2619

2720

28-
def timezones() -> tuple[str, ...]:
29-
global _timezones
30-
31-
if _timezones is None:
32-
with cast("Path", resources.files("tzdata").joinpath("zones")).open() as f:
33-
_timezones = tuple(tz.strip() for tz in f.readlines())
34-
35-
return _timezones
21+
@cache
22+
def timezones() -> set[str]:
23+
return available_timezones()
3624

3725

3826
def fixed_timezone(offset: int) -> FixedTimezone:

0 commit comments

Comments
 (0)