51๐
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'
>>>
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.
- [Django]-Django multiple and dynamic databases
- [Django]-How to redo a migration on django 1.8 after using โfake
- [Django]-How do you use the django-filter package with a list of parameters?
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'
- [Django]-Get the list of checkbox post in django views
- [Django]-Separating form input and model validation in Django?
- [Django]-Django Programming error column does not exist even after running migrations
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'
- [Django]-Get Primary Key after Saving a ModelForm in Django
- [Django]-'RelatedManager' object is not iterable Django
- [Django]-How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead."