PyTime C API

Added in version 3.13.

时钟 C API 提供对系统时钟的访问。它类似于 Python time 模块。

有关与 datetime 模块相关的 C API,请参阅 DateTime 对象

类型

type PyTime_t

以纳秒为单位的时间戳或持续时间,表示为带符号的 64 位整数。

时间戳的参考点取决于所使用的时钟。 例如,PyTime_Time() 返回相对于 UNIX 纪元的时间戳。

支持的范围约为[-292.3年; +292.3 年]。以Unix纪元(1970年1月1日)为参考,支持的日期范围约为[1677-09-21; 2262-04-11]。确切的限制以常数形式公开:

PyTime_t PyTime_MIN

PyTime_t 的最小值。

PyTime_t PyTime_MAX

PyTime_t 的最大值。

时钟函数

以下函数采用指向 PyTime_t 的指针,并将其设置为特定时钟的值。 每个时钟的详细信息在相应的 Python 函数的文档中给出。

成功时函数返回``0``,失败时返回``-1``(设置一个异常)。

在整数溢出时,他们设置 PyExc_OverflowError 异常,并将``*result``设置为``[PyTime_MIN; PyTime_MAX]``范围内的值。 (在当前系统上,整数溢出可能是由于系统时间配置错误引起的。)

As any other C API (unless otherwise specified), the functions must be called with an attached thread state.

int PyTime_Monotonic(PyTime_t *result)

读取单调时间。有关该时钟的重要详细信息,请参阅 time.monotonic()

int PyTime_PerfCounter(PyTime_t *result)

读取性能计数器。有关该时钟的重要详细信息,请参阅 time.perf_counter()

int PyTime_Time(PyTime_t *result)

读取“Wall Clock” 时间。有关该时钟的重要详细信息,请参阅 time.time`()

原始时钟函数

Similar to clock functions, but don't set an exception on error and don't require the caller to have an attached thread state.

成功时,函数返回 0

On failure, they set *result to 0 and return -1, without setting an exception. To get the cause of the error, attach a thread state, and call the regular (non-Raw) function. Note that the regular function may succeed after the Raw one failed.

int PyTime_MonotonicRaw(PyTime_t *result)

Similar to PyTime_Monotonic(), but don't set an exception on error and don't require an attached thread state.

int PyTime_PerfCounterRaw(PyTime_t *result)

Similar to PyTime_PerfCounter(), but don't set an exception on error and don't require an attached thread state.

int PyTime_TimeRaw(PyTime_t *result)

Similar to PyTime_Time(), but don't set an exception on error and don't require an attached thread state.

转换函数

double PyTime_AsSecondsDouble(PyTime_t t)

将时间戳转换为 C:c:expr:double 形式的秒数。

该函数不会失败,但请注意 double 对于大值的精度有限。