PyTime C API

Added in version 3.13.

The clock C API provides access to system clocks.It is similar to the Pythontime module.

For C API related to thedatetime module, seeDateTime Objects.

Types

typePyTime_t

A timestamp or duration in nanoseconds, represented as a signed 64-bitinteger.

The reference point for timestamps depends on the clock used. For example,PyTime_Time() returns timestamps relative to the UNIX epoch.

The supported range is around [-292.3 years; +292.3 years].Using the Unix epoch (January 1st, 1970) as reference, the supported daterange is around [1677-09-21; 2262-04-11].The exact limits are exposed as constants:

PyTime_tPyTime_MIN

Minimum value ofPyTime_t.

PyTime_tPyTime_MAX

Maximum value ofPyTime_t.

Clock Functions

The following functions take a pointer to aPyTime_t that theyset to the value of a particular clock.Details of each clock are given in the documentation of the correspondingPython function.

The functions return0 on success, or-1 (with an exception set)on failure.

On integer overflow, they set thePyExc_OverflowError exception andset*result to the value clamped to the[PyTime_MIN;PyTime_MAX]range.(On current systems, integer overflows are likely caused by misconfiguredsystem time.)

As any other C API (unless otherwise specified), the functions must be calledwith theGIL held.

intPyTime_Monotonic(PyTime_t*result)

Read the monotonic clock.Seetime.monotonic() for important details on this clock.

intPyTime_PerfCounter(PyTime_t*result)

Read the performance counter.Seetime.perf_counter() for important details on this clock.

intPyTime_Time(PyTime_t*result)

Read the “wall clock” time.Seetime.time() for details important on this clock.

Raw Clock Functions

Similar to clock functions, but don’t set an exception on error and don’trequire the caller to hold the GIL.

On success, the functions return0.

On failure, they set*result to0 and return-1,without settingan exception. To get the cause of the error, acquire the GIL and call theregular (non-Raw) function. Note that the regular function may succeed aftertheRaw one failed.

intPyTime_MonotonicRaw(PyTime_t*result)

Similar toPyTime_Monotonic(),but don’t set an exception on error and don’t require holding the GIL.

intPyTime_PerfCounterRaw(PyTime_t*result)

Similar toPyTime_PerfCounter(),but don’t set an exception on error and don’t require holding the GIL.

intPyTime_TimeRaw(PyTime_t*result)

Similar toPyTime_Time(),but don’t set an exception on error and don’t require holding the GIL.

Conversion functions

doublePyTime_AsSecondsDouble(PyTime_tt)

Convert a timestamp to a number of seconds as a Cdouble.

The function cannot fail, but note thatdouble has limitedaccuracy for large values.