Get timezone used by datetime.datetime.fromtimestamp()

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

提交回复
热议问题