How can I get a human-readable timezone name in Python?

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

# display "human-readable" name (tzid)
print(tzlocal.get_localzone_name())

# Example Results:
# -> Europe/Moscow
# -> America/Chicago

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'))

# Example Results:
# -> MSK+0300 
# -> CDT-0500

It prints the current timezone abbreviation and the utc offset corresponding to your local timezone.


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)

for k, v in result.items():
    print(k, v)

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.


http://pytz.sourceforge.net/ may be of help. If nothing else, you may be able to grab a list of all of the timezones and then iterate through until you find one that matches your offset.