]> git.ipfire.org Git - thirdparty/Python/cpython.git/blame - Doc/c-api/time.rst
gh-110850: Add PyTime_TimeRaw() function (#118394)
[thirdparty/Python/cpython.git] / Doc / c-api / time.rst
CommitLineData
879f4546
PV
1.. highlight:: c
2
3PyTime C API
4============
5
6.. versionadded:: 3.13
7
8The clock C API provides access to system clocks.
9It is similar to the Python :mod:`time` module.
10
11For C API related to the :mod:`datetime` module, see :ref:`datetimeobjects`.
12
13
14Types
15-----
16
17.. c:type:: PyTime_t
18
19 A timestamp or duration in nanoseconds, represented as a signed 64-bit
20 integer.
21
22 The reference point for timestamps depends on the clock used. For example,
23 :c:func:`PyTime_Time` returns timestamps relative to the UNIX epoch.
24
25 The supported range is around [-292.3 years; +292.3 years].
26 Using the Unix epoch (January 1st, 1970) as reference, the supported date
27 range is around [1677-09-21; 2262-04-11].
28 The exact limits are exposed as constants:
29
30.. c:var:: PyTime_t PyTime_MIN
31
32 Minimum value of :c:type:`PyTime_t`.
33
34.. c:var:: PyTime_t PyTime_MAX
35
36 Maximum value of :c:type:`PyTime_t`.
37
38
39Clock Functions
40---------------
41
42The following functions take a pointer to a :c:expr:`PyTime_t` that they
43set to the value of a particular clock.
44Details of each clock are given in the documentation of the corresponding
45Python function.
46
47The functions return ``0`` on success, or ``-1`` (with an exception set)
48on failure.
49
50On integer overflow, they set the :c:data:`PyExc_OverflowError` exception and
51set ``*result`` to the value clamped to the ``[PyTime_MIN; PyTime_MAX]``
52range.
53(On current systems, integer overflows are likely caused by misconfigured
54system time.)
55
56As any other C API (unless otherwise specified), the functions must be called
57with the :term:`GIL` held.
58
59.. c:function:: int PyTime_Monotonic(PyTime_t *result)
60
61 Read the monotonic clock.
62 See :func:`time.monotonic` for important details on this clock.
63
64.. c:function:: int PyTime_PerfCounter(PyTime_t *result)
65
66 Read the performance counter.
67 See :func:`time.perf_counter` for important details on this clock.
68
69.. c:function:: int PyTime_Time(PyTime_t *result)
70
71 Read the “wall clock” time.
72 See :func:`time.time` for details important on this clock.
73
74
b52c753e
VS
75Raw Clock Functions
76-------------------
77
78Similar to clock functions, but don't set an exception on error and don't
79require the caller to hold the GIL.
80
81On success, the functions return ``0``.
82
83On failure, they set ``*result`` to ``0`` and return ``-1``, *without* setting
84an exception. To get the cause of the error, acquire the GIL and call the
85regular (non-``Raw``) function. Note that the regular function may succeed after
86the ``Raw`` one failed.
87
88.. c:function:: int PyTime_MonotonicRaw(PyTime_t *result)
89
90 Similar to :c:func:`PyTime_Monotonic`,
91 but don't set an exception on error and don't require holding the GIL.
92
93.. c:function:: int PyTime_PerfCounterRaw(PyTime_t *result)
94
95 Similar to :c:func:`PyTime_PerfCounter`,
96 but don't set an exception on error and don't require holding the GIL.
97
98.. c:function:: int PyTime_TimeRaw(PyTime_t *result)
99
100 Similar to :c:func:`PyTime_Time`,
101 but don't set an exception on error and don't require holding the GIL.
102
103
879f4546
PV
104Conversion functions
105--------------------
106
107.. c:function:: double PyTime_AsSecondsDouble(PyTime_t t)
108
109 Convert a timestamp to a number of seconds as a C :c:expr:`double`.
110
111 The function cannot fail, but note that :c:expr:`double` has limited
112 accuracy for large values.