How can I get a human-readable timezone name in Python?

前端 未结 7 1419
旧时难觅i
旧时难觅i 2020-12-10 12:06

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 syste

7条回答
  •  孤街浪徒
    2020-12-10 12:48

    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..localize() or datetime_object.astimezone(pytz.) 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
    
    >>> 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.

提交回复
热议问题