简体   繁体   English

python time.time()和“夏令时”

[英]python time.time() and “Daylight Saving Time”

What happens when the clock of the computer running python (Windows or Linux) gets automatically changed and a call time.time() ? 当运行python(Windows或Linux)的计算机的时钟自动更改并调用time.time()什么?

I've read that the value of time.time() will be smaller when the clock is changed manually to some value in the past. 我已经读过,当手动将时钟更改为某个值时, time.time()的值会更小。

time.time() docs state: 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 . 这里提到的特定时代是Unix时代 ,即UTC时间 1970年1月1日午夜。

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. 由于它始终基于UTC,因此不会更改计算机的时区,也不会影响计算机的时区进入或退出夏令时。

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. 尽管该函数确实依赖于底层实现来提供值,但这些实现始终以UTC的形式返回值 - 无论操作系统如何。

On Windows in particular, it ultimately calls the GetSystemTimeAsFileTime OS function, which returns its value in terms of UTC. 特别是在Windows上,它最终调用GetSystemTimeAsFileTime OS函数,该函数以UTC的形式返回其值。 It is not affected by time zone selection or by DST changes within the time zone. 它不受时区选择或时区内DST更改的影响。

Quoting the docs for time.time , 引用的文档进行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). 但是,根据评论,这不应包括基于DST的更改,因为Python支持的所有操作系统都提供系统调用来检索UTC中的当前时间(并且DST仅影响本地时间与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): 如果这不是一个足够好的保证,你可能更喜欢time.montonic ,保证永远不会倒退 - 但是,请注意它伴随着这个警告(并决定这是否对你的用例很重要):

The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid. 返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。

time.time() returns the value what the underlying library returns. time.time()返回底层库返回的值。 Python uses time(..) or gettimeofday(.., nullptr) (depending whats available on the system). Python使用time(..)gettimeofday(.., nullptr) (取决于系统上可用的内容)。

https://github.com/python-git/python/blob/master/Modules/timemodule.c#L874 https://github.com/python-git/python/blob/master/Modules/timemodule.c#L874

In both cases UTC is returned. 在这两种情况下都返回UTC。 Eg: 例如:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html

The time() function shall return the value of time in seconds since the Epoch time()函数应返回自Epoch以来以秒为单位的时间值

After a valid comment of Matt I have to add the definition of epoch which clearly states that time returns the UTC time. 在Matt的有效评论之后,我必须添加epoch的定义,该定义明确指出time返回UTC时间。

Epoch : Historically, the origin of UNIX system time was referred to as "00:00:00 GMT, January 1, 1970". 时代 :从历史上看,UNIX系统时间的起源被称为“GMT,1970年1月1日00:00:00”。 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. 因此,这个术语“Epoch”用于缩写对实际标准协调世界时的参考。

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). 如果参数secs为0,则两个函数都使用从time.time()返回的值。在我的情况下, tm_isdst对于localtime设置为true,对于gmtime设置为false(表示自纪元以来的时间)。

Edit: Where have you read that the value will be smaller? 编辑:你在哪里读到价值会更小?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM