In a Python project I'm working on, I'd like to be able to get a "human-readable" timezone name of the form America/New_York, corresponding to the system local timezone, to display to the user. Every piece of code I've seen that accesses timezone information only returns either a numeric offset (-0400) or a letter code (EDT) or sometimes both. Is there some Python library that can access this information, or if not that, convert the offset/letter code into a human-readable name?
If there's more than one human-readable name corresponding to a particular timezone, either a list of the possible results or any one of them is fine, and if there is no human-readable name corresponding to the current time zone, I'll take either an exception or None
or []
or whatever.
A clarification: I don't remember exactly what I had in mind when I originally wrote this question, but I think what I really wanted was a way to turn a timezone into a human-readable name. I don't think this question was meant to focus on how to get the system local timezone specifically, but for the specific use case I had in mind, it just happened that the local timezone was the one I wanted the name for. I'm not editing the bit about the local timezone out of the question because there are answers focusing on both aspects.
The following generates a defaultdict mapping timezone offsets (e.g. '-0400') and abbreviations (e.g. 'EDT') to common geographic timezone names (e.g. 'America/New_York').
import os import dateutil.tz as dtz import pytz import datetime as dt import collections result=collections.defaultdict(list) for name in pytz.common_timezones: timezone=dtz.gettz(name) now=dt.datetime.now(timezone) offset=now.strftime('%z') abbrev=now.strftime('%Z') result[offset].append(name) result[abbrev].append(name) print(result)
Note that timezone abbreviations can have vastly different meanings. For example, 'EST' could stand for Eastern Summer Time (UTC+10) in Australia, or Eastern Standard Time (UTC-5) in North America.
Also, the offsets and abbreviations may change for regions that use daylight standard time. So saving the static dict may not provide the correct timezone name 365 days a year.
I'd like to be able to get a "human-readable" timezone name of the form America/New_York, corresponding to the system local timezone, to display to the user.
There is tzlocal
module that returns a pytz
tzinfo object (before tzlocal 3.0
version) that corresponds to the system local timezone:
#!/usr/bin/env python import tzlocal # $ pip install tzlocal print(tzlocal.get_localzone().zone) # display "human-readable" name (tzid) # -> Europe/Moscow
To answer the question in the title (for people from google), you could use %Z%z
to print the local time zone info:
#!/usr/bin/env python import time print(time.strftime('%Z%z')) # -> MSK+0300
It prints the current timezone abbreviation and the utc offset corresponding to your local timezone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With