Repository navigation
zoneinfo.available_timezones() sometimes include 'localtime' #137976
Copy link
Copy link
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Sometimes available_timezones includes localtime. This result is not stable. But localtime should not be included at all.
>>> import zones
>>> zones = zoneinfo.available_timezones()
>>> 'localtime' in zones
TrueInformation about the data folder where this function scans:
❯ ll /usr/share/zoneinfo
Permissions Size User Group Date Modified Name
drwxr-xr-x - root root 3 Jul 20:37 Africa
drwxr-xr-x - root root 3 Jul 20:37 America
drwxr-xr-x - root root 3 Jul 20:37 Antarctica
drwxr-xr-x - root root 3 Jul 20:37 Arctic
drwxr-xr-x - root root 3 Jul 20:37 Asia
drwxr-xr-x - root root 3 Jul 20:37 Atlantic
drwxr-xr-x - root root 3 Jul 20:37 Australia
drwxr-xr-x - root root 3 Jul 20:37 Etc
drwxr-xr-x - root root 3 Jul 20:37 Europe
drwxr-xr-x - root root 3 Jul 20:37 Indian
drwxr-xr-x - root root 3 Jul 20:37 Pacific
.rw-r--r-- 116 root root 22 Apr 16:56 Factory
lrwxrwxrwx - root root 22 Apr 16:56 GMT -> Etc/GMT
.rw-r--r-- 4.8k root root 6 Sep 2023 iso3166.tab
.rw-r--r-- 5.1k root root 7 Jan 18:46 leap-seconds.list
.rw-r--r-- 3.3k root root 22 Apr 16:56 leapseconds
lrwxrwxrwx - root root 22 Apr 16:56 localtime -> /etc/localtime
lrwxrwxrwx - root root 22 Apr 16:56 posixrules -> America/New_York
.rw-r--r-- 114k root root 22 Apr 16:56 tzdata.zi
lrwxrwxrwx - root root 22 Apr 16:56 UTC -> Etc/UTC
.rw-r--r-- 19k root root 20 Mar 18:43 zone.tab
.rw-r--r-- 18k root root 20 Mar 18:43 zone1970.tab
.rw-r--r-- 8.1k root root 22 Mar 20:21 zonenow.tab
/etc/localtime again, is a symlink:
❯ ll /etc/localtime
Permissions Size User Group Date Modified Name
lrwxrwxrwx - root root 8 Jan 12:15 /etc/localtime -> /usr/share/zoneinfo/Asia/Ho_Chi_Minh
I think in this block, the file path should be resolved for symlink:
cpython/Lib/zoneinfo/_tzpath.py
Lines 157 to 167 in d22a745
| fpath = os.path.join(root, file) | |
| key = os.path.relpath(fpath, start=tz_root) | |
| if os.sep != "/": # pragma: nocover | |
| key = key.replace(os.sep, "/") | |
| if not key or key in valid_zones: | |
| continue | |
| if valid_key(fpath): | |
| valid_zones.add(key) |
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
hongquan commentedon Aug 20, 2025
I think it is not correct to just ignore the symlinks. Sometimes, two cities have the same rules, and the data author use symlinks to point these timezones to the same source.
serhiy-storchaka commentedon Aug 20, 2025
Concur with @hongquan. Note also that "GMT" is a symlink -- you don't want to get rid of it, do you? And there is a timezone with strange name "Factory".
hongquan commentedon Aug 20, 2025
I think, to decide which files to take, we can require the folder + file name to follow the scheme "Continent/City". We can ignore those like "localtime", "Etc/xxx", "GMT".
ntindle commentedon Aug 21, 2025
I also experienced this issue today on GitHub Actions runners. The list available on the runners differs from the timezone list pulled on my Mac by only
localtime, which is not an IANA timezone and thus should not be returned based on the mission of PEP 615hongquan commentedon Aug 21, 2025
I think the solution should be future-proof. The Linux distro maintainers may add more file similar to localtime in the future, so excluding only this file like PR #138012 is not enough.
Rectification for my previous comment: "Etc/GMT-xxx" should be taken. By https://data.iana.org/time-zones/tzdb/etcetera, they are "for ships at sea and other miscellany".
gh-137976: Explicitly exclude `localtime` from `available_timezones` (#…
pganssle commentedon Sep 18, 2025
I think we can start by just removing
localtimesince we're already filtering out things likeposixrules. My gut instinct is that maybe it was a mistake to filter outposixrulesin the first place and that we should just list everything that works if you put it intoZoneInfo(...), but that ship has sailed.