Python Time.time() And "daylight Saving Time"
Solution 1:
time.time()
docs state:
Return the time in seconds since the epoch as a floating point number.
The particular epoch referred to here is the Unix epoch, which is Midnight, Jan 1st 1970 UTC.
Since it is always based on UTC, it will not be affected by changing the computer's time zone, nor when the computer's time zone enters or exits daylight saving time.
Though the function does rely on the underlying implementation to provide the value, those implementations always return values in terms of UTC - regardless of operating system.
On Windows in particular, it ultimately calls the GetSystemTimeAsFileTime
OS function, which returns its value in terms of UTC. It is not affected by time zone selection or by DST changes within the time zone.
Solution 2:
Quoting the docs for time.time
,
While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.
What adjusts the value of the system clock is platform dependent. But, per the comments, that shouldn't include changes based on DST, since all OSs that Python supports provide system calls for retrieving the current time in UTC (and DST only affects how local time relates to UTC).
If that isn't a good enough guarantee, you might prefer time.montonic
, which is guaranteed to never go backward - however, note that it comes with this caveat (and decide whether this matters for your use case):
The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
Solution 3:
time.time()
returns the value what the underlying library returns. Python uses time(..)
or gettimeofday(.., nullptr)
(depending whats available on the system).
https://github.com/python-git/python/blob/master/Modules/timemodule.c#L874
In both cases UTC is returned. E.g:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html
The time() function shall return the value of time in seconds since the Epoch
After a valid comment of Matt I have to add the definition of epoch which clearly states that time
returns the UTC time.
Epoch: Historically, the origin of UNIX system time was referred to as "00:00:00 GMT, January 1, 1970". Greenwich Mean Time is actually not a term acknowledged by the international standards community; therefore, this term, "Epoch", is used to abbreviate the reference to the actual standard, Coordinated Universal Time.
To proove the epoch is the same check this:
import time
a = time.gmtime(secs=0)
# time.struct_time(tm_year=2015, tm_mon=9, tm_mday=9, tm_hour=1, tm_min=3, tm_sec=28, tm_wday=2, tm_yday=252, tm_isdst=0)
b = time.localtime(secs=0)
# time.struct_time(tm_year=2015, tm_mon=9, tm_mday=9, tm_hour=3, tm_min=1, tm_sec=28, tm_wday=2, tm_yday=252, tm_isdst=1)
Both functions use the value returned from time.time()
if the parameter secs
is 0. In my case tm_isdst
was set to true for the localtime, and false for the gmtime (which represents the time since the epoch).
Edit: Where have you read that the value will be smaller?
Post a Comment for "Python Time.time() And "daylight Saving Time""