Get timezone used by datetime.datetime.fromtimestamp()
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 isNone
or not specified, the timestamp is converted to the platform’s local date and time, and the returneddatetime
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 totz.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.
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.