python 的 time.time() 方法是如何工作的?

How does python's time.time() method work?

time.time() 方法为您提供时间戳,它基本上是自纪元以来经过的时间(采用 UTC)。

datetime.fromtimestamp() 声明如果未提供 tz 参数,则返回本地日期时间对象。

我知道 tz 信息基本上被视为 utc 的偏移量以获取当地时间。

如果是这种情况,则必须从平台中提取 UTC 格式的当前时间。

一天中的当前时间是如何从底层平台提取的,无论是本地时间还是 UTC?

在(当前)CPython time.time() 调用中
floattime(NULL) 调用
_PyTime_gettimeofday_info() 调用
pygettimeofday()
其中 pygettimeofday() may use GetSystemTimeAsFileTime(), clock_gettime(CLOCK_REALTIME)gettimeofday() 功能取决于平台。


不相关:time.time() returns "seconds since the epoch". It is POSIX time on most systems supported by Python (Linux, OS X, Windows). POSIX timestamp 而不是 1970-01-01T00:00:00Z(纪元)以来经过的 SI 秒数。虽然非常接近(从 Epoch 算起在 0.000003% 以内)。它计算非闰 SI 秒数或 UT1(平均太阳)秒数。

UTC 不是相对于 TAI、GPS 时间尺度的线性时间尺度(参见 the picture). You won't get the exact elapsed SI seconds due to leap seconds (UTC is kept within ±0.9 seconds of UT1 (Earth rotation)). See also, Is a day always 86,400 epoch seconds long?

某些操作系统可以配置为使用非POSIX "right" zoneinfo。在这种情况下,time.time() 不需要 return POSIX 时间戳。 "right" 时区计算闰秒,因此 "right" 时间戳和 POSIX 时间戳之间的差异不是恒定的,例如,它将在 July 2015 due to the introduction of the positive leap second.

中增加

原则上,"the epoch" 可能与 POSIX 纪元不同,尽管 Python doesn't support such systems.