How to get the common name for a pytz timezone eg. EST/EDT for America/New_York

后端 未结 4 1733
梦谈多话
梦谈多话 2020-12-09 14:40

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 see

4条回答
  •  一生所求
    2020-12-09 15:32

    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'
    

提交回复
热议问题