48

Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork.

Does pytz give me those standard names somewhere, or will i have to manually do this via a table? This could potentially get messy, since for example places are EST in a season and shift to showing EDT during others.

Sidmitra
  • 1,152
  • 2
  • 9
  • 17
  • 6
    Do note that the common name is not unique, and should only be used if it's perfectly clear which country you are in. There is for example an EST in Australia as well. – Lennart Regebro May 25 '11 at 09:13
  • @LennartRegebro: even in the same country, [EST may correspond to different timezones (with different UTC offsets)](http://stackoverflow.com/a/13713813/4279) – jfs Sep 13 '14 at 17:54
  • @LennartRegebro: Really? I try to be aware of these things, so I'd love to know examples. You don't mean states like Queensland (always EST, never EDT) and Arizona (always USMST (first nation territories notwithstanding), never USMDT), do you? – Michael Scheper Mar 14 '17 at 01:58
  • I mean that there are several countries who has time zones called "EST" and that therefore these names does not actually tell you which time zone it is, unless you also know which country. – Lennart Regebro Mar 16 '17 at 13:41

4 Answers4

50

Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork.

If you need this derived from a datetime object localized with pytz...

>>> import pytz as tz
>>> from datetime import datetime as dt
>>> CT = tz.timezone('America/Chicago')
>>>
>>> summer_day = dt(2010, 7, 4, 0, 1, 1)
>>> bar = CT.localize(summer_day, is_dst=None)
>>> bar.tzname()
'CDT'
>>>
>>> christmas = dt(2010, 12, 25, 0, 1, 1)
>>> foo = CT.localize(christmas, is_dst=None)
>>> foo.tzname()
'CST'
>>> 
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • 4
    `.localize` may return wrong result if input naive datetime is ambiguous or non-existent e.g., during DST transitions. Use `is_dst=None` to assert that input time is unambiguous. – jfs Mar 26 '14 at 11:18
  • 6
    Is there a way to go back from `'CDT'` to a timezone object with `'America/Chicago'` timezone? – summerbulb Dec 14 '16 at 11:26
  • @summerbulb your question should be asked as a completely different StackOverflow Q&A – Mike Pennington Dec 23 '22 at 17:09
8

This may not have been around when this question was originally written, but here is a snippet to get the time zone official designation:

>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'

Further, this can be used with a non-naive datetime object (aka a datetime where the actual timezone has been set using pytz.<timezone>.localize(<datetime_object>) or datetime_object.astimezone(pytz.<timezone>) as follows:

>>> import datetime, pytz
>>> todaynow = datetime.datetime.now(tz=pytz.timezone('US/Hawaii'))
>>> todaynow.tzinfo # turned into a string, it can be split/parsed
<DstTzInfo 'US/Hawaii' HST-1 day, 14:00:00 STD>
>>> todaynow.strftime("%Z")
'HST'
>>> todaynow.tzinfo.zone
'US/Hawaii'

This is, of course, for the edification of those search engine users who landed here. ... See more at the pytz module site.

ingyhere
  • 11,818
  • 3
  • 38
  • 52
6

If you are looking for the abbreviations then there are a few ways that come to mind.

First would be:

>>> from pytz import timezone
>>> eastern = timezone('US/Eastern')
>>> eastern._tzname
'EST'

Although since that references a property with the preceding single underscore it may be considered private and might not be the best place to grab it. The other would be from a localized datetime object.

>>> from datetime import datetime
>>> loc_dt = eastern.localize(datetime.now())
>>> loc_dt.strftime('%Z')
'EST'
Philip Southam
  • 15,843
  • 6
  • 28
  • 20
  • 4
    `eastern.localize(datetime.now())` might give you a wrong result during DST transitions, use `datetime.now(eastern)` instead. – jfs Aug 21 '14 at 10:40
5

You can use the tzname() method of tzinfo instances:

>>> tz = timezone('America/St_Johns')
>>> normal = datetime(2009, 9, 1)
>>> tz.tzname(normal, is_dst=False)
'NDT'
Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123