Is it possible to get a zoneinfo.ZoneInfo key (i.e.: IANA tzdb identifier) for the current system timezone, in Python? `datetime.now(timezone.utc).astimezone().` gives a potentially ambiguous 'tzname' and 'utcoffset' which make it difficult to communicate long-term time intervals to other systems

#python #datetime #timezones

Follow

@glyph There's no standard library way to do it, but IIRC `tzlocal` migrated to use `zoneinfo` a while back, so if you trust their heuristics you can use that to get the name.

@glyph As for *why* you can't do this with the standard library, it's half "haven't gotten around to trying yet" and half "it's really complicated and there is neither a consistent API for retrieving this across different platforms *nor* a guarantee that your system is even set to a specific IANA zone", with a dash of, "There are a lot of versions of this kind of API that are very close to an attractive nuisance, so it is necessary to take care in its design."

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.