views:

134

answers:

3

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.

+3  A: 

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.

Amber
Iterating over isn't a bad idea. I'd iterate through all of them once and build a `shelve` file or just store a `dict` in a module or something keyed by the letter code. Then you can just ship the `dict` with the app and not have to perform the search twice. You wouldn't even have to ship `pytz` in that case: you could just use it in the script that builds the `dict`
aaronasterling
Yep. It's something that works well with just pre-calculation (or even just building it on startup; it doesn't take *that* long if you're only doing it once per execution).
Amber
+1  A: 

Check out python-dateutil

py> from dateutil.tz import *
py> ny = gettz('America/New York')
py> ny._filename
'/usr/share/zoneinfo/America/New_York'
py> ny._filename.split('/', 4)[-1]
'America/New_York'
Jesse Dhillon
But that doesn't help me if I have a `tzinfo` object that wasn't obtained from `dateutil.tz.gettz` - for example, one from `pytz` - or if I just call `gettz()` (in which case the filename is `/etc/localtime`). (Still I appreciate the info)
David Zaslavsky
Well, where are these timezone objects coming from? You can store the name of the timezone -- the string argument to `gettz` -- and reconstitute timezones using that argument and `gettz`. I guess I am assuming certain use cases, for example storing a time and the associated timezone string in a database. I would never use `gettz` by itself to get the local timezone, but also in my case the user would be a person behind a web browser.
Jesse Dhillon
A: 

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.

unutbu