Get timezone used by datetime.datetime.fromtimestamp()

前端 未结 4 1495
清酒与你
清酒与你 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:40

    If you know the timezone of the timestamp you want to convert, you can simply send it in while calling fromtimestamp:

    >>> from datetime import datetime
    >>> import pytz
    >>>
    >>> datetime.fromtimestamp(1562684265, pytz.timezone("Europe/Stockholm"))
    datetime.datetime(2019, 7, 9, 16, 57, 45, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>)
    >>>
    >>> datetime.fromtimestamp(1562684265, pytz.timezone("UTC"))
    datetime.datetime(2019, 7, 9, 14, 57, 45, tzinfo=<UTC>)
    
    0 讨论(0)
  • 2020-12-09 02:41

    datetime.fromtimestamp(ts) converts "seconds since the epoch" to a naive datetime object that represents local time. tzinfo is always None in this case.

    Local timezone may have had a different UTC offset in the past. On some systems that provide access to a historical timezone database, fromtimestamp() may take it into account.

    To get the UTC offset used by fromtimestamp():

    utc_offset = fromtimestamp(ts) - utcfromtimestamp(ts)
    

    See also, Getting computer's utc offset in Python.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-09 02:58

    From the Python documentation:

    classmethod datetime.fromtimestamp(timestamp, tz=None)

    Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

    Else tz must be an instance of a class tzinfo subclass, and the timestamp is converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz)).

    The key part of this description as it relates to your question is that when you don't specify a time zone, not only does it use the local time zone, but the result is naive. You seem to want it to be aware.

    This is a particular distinction made by Python, and is discussed right at the very top of the datetime documentation.

    If what you want is a datetime that is aware of the local time zone, try the tzlocal library. It is focused on that particular problem. See also this question.

    0 讨论(0)
提交回复
热议问题