python 的 time.time() 方法是如何工作的? [英] How does python's time.time() method work?

查看:46
本文介绍了python 的 time.time() 方法是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The time.time() method provides you the timestamp which is basically the time elapsed since epoch(which is in UTC).

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

The datetime.fromtimestamp() states that if the tz param isn't provided then a local datetime object is returned.

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

I am aware that the tz info is basically treated as an offset to utc in order to get the local time.

如果是这样,则必须从平台中提取 utc 中的当前时间.

If that's the case, the current time in utc had to be extracted from the platform.

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

How is the current time of the day, be it local or UTC, extracted from the underlying platform?

推荐答案

在(当前)CPython time.time() 调用
floattime(NULL) 调用
_PyTime_gettimeofday_info()其中调用
pygettimeofday()
其中 pygettimeofday() 可以使用 GetSystemTimeAsFileTime(), clock_gettime(CLOCK_REALTIME), gettimeofday() 函数取决于平台.

In (the current) CPython time.time() calls
floattime(NULL) which calls
_PyTime_gettimeofday_info() which calls
pygettimeofday()
where pygettimeofday() may use GetSystemTimeAsFileTime(), clock_gettime(CLOCK_REALTIME), gettimeofday() functions depending on the platform.

无关:time.time() 返回自纪元以来的秒数".在 Python 支持的大多数系统(Linux、OS X、Windows)上,现在是 POSIX 时间.POSIX 时间戳不是1970-01-01T00:00:00Z(纪元)以来经过的 SI 秒数.虽然它非常接近(从 Epoch 算起在 0.000003% 以内).统计非闰秒的秒数或UT1(平均太阳)秒数.

Unrelated: time.time() returns "seconds since the epoch". It is POSIX time on most systems supported by Python (Linux, OS X, Windows). POSIX timestamp is not the number of elapsed SI seconds since 1970-01-01T00:00:00Z (Epoch). Though it is very close (within 0.000003% counting from Epoch). It counts the number of non-leap SI seconds or the number of UT1 (mean-solar) seconds.

UTC 不是相对于 TAI、GPS 时间标度的线性时间标度(参见图片).由于闰秒(UTC 保持在 UT1(地球自转)的 ±0.9 秒内),您将无法获得精确的 SI 秒.另请参阅,一天总是 86,400 个纪元秒吗?

UTC is not a linear time scale relative to TAI, GPS time scales (see 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 的正确"区域信息.在这种情况下,time.time() 不需要返回 POSIX 时间戳.正确"时区计算闰秒,因此正确"时间戳和 POSIX 时间戳之间的差异不是恒定的,例如,它会在 2015 年 7 月,由于引入了正闰秒.

Some OSes can be configured to use non-POSIX "right" zoneinfo. time.time() is not required to return POSIX timestamp in this case. "right" timezone counts leap seconds and therefore the difference between "right" timestamps and POSIX timestamps is not constant e.g., it will increase in July 2015 due to the introduction of the positive leap second.

原则上,时代"可能与 POSIX 时代不同,尽管 Python 不支持此类系统.

In principle, "the epoch" may differ from POSIX Epoch, though Python doesn't support such systems.

这篇关于python 的 time.time() 方法是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆