5

pytz provides list of timezones in formats like America/Chicago, America/Los_Angeles, Asia/Kolkata or the tz abbreviation.

I want the full name for timezones.

  • Central Standard Time
  • Pacific Standard Time
  • Indian Standard Time

Is this possible in Python?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
MavWolverine
  • 846
  • 1
  • 9
  • 24
  • 1
    pytz is based on the [tz database](http://en.wikipedia.org/wiki/Tz_database). The "full name" timezones you listed go against the tz database naming conventions and seem to have no standard that organizes them. It looks like you may be on your own. – Andrew Johnson Aug 09 '14 at 20:25
  • 1
    @AndrewJohnson - They are organized by Unicode in the CLDR. See my answer for details. – Matt Johnson-Pint Aug 09 '14 at 21:01

1 Answers1

8

The Unicode CLDR project contains many localized strings, including the human-readable names of time zones in many different languages.

A quick search for Python implementations of CLDR found the Babel library. An example shown in this part of the documentation is as follows:

>>> british = get_timezone('Europe/London')
>>> format_datetime(dt, 'H:mm zzzz', tzinfo=british, locale='en_US')
u'16:30 British Summer Time'

Here we can see that at the date and time specified by the dt variable, and the IANA time zone (as used by pytz) of Europe/London, the English display name is "British Summer Time".

There's also an example of how to get just the generic time zone name, without regard to a specific date:

>>> from babel import Locale
>>> from babel.dates import get_timezone_name, get_timezone

>>> tz = get_timezone('Europe/Berlin')
>>> get_timezone_name(tz, locale=Locale.parse('pt_PT'))
u'Hor\xe1rio Alemanha'

That is, the Europe/Berlin time zone has the Portugese name of "Horário Alemanha".

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks Matt, That surely helps. But is it also possible to get the timezone from that name? I am porting a PHP application to python, which has tz stored in fullname. And I can't just move them away since this is partial port. – MavWolverine Aug 10 '14 at 23:41
  • I'm not sure what you mean. PHP also uses IANA time zones. If you mean you want to translate from "Eastern Standard Time" to "America/New_York", then no - because there's also an "Eastern Standard Time" in Australia. It's a one-way conversion. – Matt Johnson-Pint Aug 11 '14 at 02:04
  • Yes thats what I was looking for. Whoever wrote the PHP code, stores "Eastern Standard Time" in the DB. As long as we are stuck with parts of PHP code, I guess I will just have to write my own wrapper for now. Thanks. – MavWolverine Aug 11 '14 at 04:28
  • `get_timezone_name()` returns different name on my machine: `u'Hora da Europa Central'` (Central European Time). `babel.__version__ == 1.3` – jfs Aug 31 '14 at 16:35
  • @J.F.Sebastian - I assume that if you don't pass a locale, your machine's current locale is used. That would be expected. – Matt Johnson-Pint Aug 31 '14 at 18:41
  • @MattJohnson: the code passes the locale explicitly: `locale=Locale.parse('pt_PT')` (it is **the exact same** code as in your answer) – jfs Aug 31 '14 at 18:49
  • @J.F.Sebastian - Ok, I can see that in the latest CLDR 25, that `Europe/Berlin` is in the `Europe_Central` [metazone](http://unicode.org/repos/cldr/trunk/common/supplemental/metaZones.xml), and `Europe_Central` has [these translations](http://www.unicode.org/cldr/charts/25/by_type/timezones.europe.html#Europe_Central), of which the `pt_PT` locale does indeed have "hora da Europa Central". It would seem that the samples in the Babel docs are from an older version of CLDR that didn't use metazones, as the country name would be part of a fall-back pattern. – Matt Johnson-Pint Aug 31 '14 at 22:53