Get timezone used by datetime.datetime.fromtimestamp()

前端 未结 4 1508
清酒与你
清酒与你 2020-12-09 02:12

Is it possible, and if yes, how, to get the time zone (i.e. the UTC offset or a datetime.timezone instance with that offset) that is used by datetime.date

4条回答
  •  执笔经年
    2020-12-09 02:44

    Using time.gmtime you can extract the timezone as described in this previous answer: Get TZ information of the system in Python?.

    >>> from __future__ import print_function
    >>> from time import gmtime, strftime
    >>> print(strftime("%z", gmtime()))
    -0600
    

    Prints -06:00 for my CST laptop in both python-2.7 and python-3.3 You can also use localtime() to get a local time struct.

    >>> from __future__ import print_function
    >>> from time import localtime
    >>> lt = localtime()
    >>> print(lt.tm_zone)
    "CDT"
    >>> print(lt.tm_gmtoff/(60*60))
    -5.0
    >>> print(lt.tm_gmtoff/(60*60) - (1 if lt.tm_isdst == 1 else 0)) # Adjusted for DST
    -6.0
    

    Hope this helps

提交回复
热议问题