Addition of a "list of available time zones" function to zoneinfo #84716
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = 'https://github.com/pganssle' closed_at = <Date 2020-09-14.18:08:27.166> created_at = <Date 2020-05-06.17:19:51.795> labels = ['type-feature', 'library', '3.10'] title = 'Addition of a "list of available time zones" function to zoneinfo' updated_at = <Date 2020-09-14.18:08:27.165> user = 'https://github.com/pganssle'
activity = <Date 2020-09-14.18:08:27.165> actor = 'p-ganssle' assignee = 'p-ganssle' closed = True closed_date = <Date 2020-09-14.18:08:27.166> closer = 'p-ganssle' components = ['Library (Lib)'] creation = <Date 2020-05-06.17:19:51.795> creator = 'p-ganssle' dependencies =  files =  hgrepos =  issue_num = 40536 keywords = ['patch'] message_count = 12.0 messages = ['368285', '368530', '368799', '369143', '369148', '369162', '369163', '369209', '369243', '373595', '376889', '376891'] nosy_count = 7.0 nosy_names = ['lemburg', 'belopolsky', 'vstinner', 'lukasz.langa', 'p-ganssle', 'gaborjbernat', 'FFY00'] pr_nums = ['20158'] priority = 'critical' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue40536' versions = ['Python 3.10']
The text was updated successfully, but these errors were encountered:
One thing that I sort of overlooked in PEP-615 that I think will be a common feature request for zoneinfo is the ability to get a list of time zones available on the current TZPATH.
This is more complicated to implement than it sounds like, but luckily I already did it in order to implement the property tests for the zoneinfo module:
The biggest complication is that there are files on TZPATH that are not necessarily time zones, and when I looked I couldn't easily find a list of all available time zones, so the only way to tell which ones are time zones and which ones aren't is to open each one and read the first 4 bytes. However,
The common cases will be that
I don't know that the PEP needs to be amended – if this were a feature request for Python 3.10 I don't think it would need a PEP to go through, but I don't mind amending the PEP to document it.
Design decisions (with my initial answers, loosely held):
I'm inclined towards free-standing function because zoneinfo.TZPATH is at the module level and not the class level.
I think frozenset() or set(); a sorted list is also a reasonable option, but for people who just want to use "in" or show some subset of available zones, that would be wasteful.
We should go with frozenset() if we want there to be some possibility of caching the result in the future.
I would say no, at least for now. It would not be super easy to get the cache invalidation right in the general case, and not many people are likely to be sensitive to the performance of this operation – the people who are can easily create their own cache.
Naming things is hard. Options:
From some discussion on the reference implementation PR, it seems that this may be a more complicated feature than I had bargained for: pganssle/zoneinfo#60
The issue is that the current implementation picks up the posix/ and right/ directories as well as the posixrules file, none of which is wrong — they are available time zones, after all — but they're not really canonical zones. In
I'm considering postponing this feature to Python 3.10 so that we can have more time to figure out a decent API for this.
Łukasz: Question for you as release manager — would you prefer that we put this in for 3.9 with the understanding that before the final release we may end up needing to remove it as a nuisance or change what it returns (and possibly flags that would be passed to it), or would that be too disruptive in the beta period? I'm confident that we can make a final decision before October, just not confident that we can make a final decision before Monday.
I've opened a PR adding this feature and tagged this as release blocker, since I'd like to make sure this is in the beta if Łukasz does not object.
The concerns about the stability of the function I expressed earlier have not changed much, though we did get some feedback from the tz database list that at least make me think that the new approach (excluding posix/, right/ and posixrules) is *probably* the right way to go.
I think semantically the correct expression would be available timezones, free function, set and no cache. Document the operation is expensive and users should cache if they want repeated access or provide an available timezones cached function 👍 my 2c
def valid_key(fpath): try: with open(fpath, "rb") as f: return f.read(4) == b"TZif" except Exception: # pragma: nocover return False
Why not only catching "except OSError:" rather than any exception?
Or even "except (FileNotFoundError, PermissionError):" if you want to be pedantic :-p