|
| 1 | +.. highlight:: c |
| 2 | + |
| 3 | +PyTime API |
| 4 | +========== |
| 5 | + |
| 6 | +.. versionadded:: 3.13 |
| 7 | + |
| 8 | +PyTime API |
| 9 | +---------- |
| 10 | + |
| 11 | +The PyTime_t API is written to use timestamp and timeout values stored in |
| 12 | +various formats and to read clocks. |
| 13 | + |
| 14 | +The :c:type:`PyTime_t` type is an integer to support directly common arithmetic |
| 15 | +operations such as ``t1 + t2``. |
| 16 | + |
| 17 | +The PyTime_t API supports a resolution of ``1`` nanosecond. The |
| 18 | +:c:type:`PyTime_t` type is signed to support negative timestamps. The supported |
| 19 | +range is around [-292.3 years; +292.3 years]. Using the Unix epoch (January |
| 20 | +1st, 1970), the supported date range is around [1677-09-21; 2262-04-11]. |
| 21 | + |
| 22 | +Time formats: |
| 23 | + |
| 24 | +* Seconds. |
| 25 | +* Seconds as a floating pointer number (C :c:type:`double`). |
| 26 | +* Milliseconds (10\ :sup:`-3` seconds). |
| 27 | +* Microseconds (10\ :sup:`-6` seconds). |
| 28 | +* 100 nanoseconds (10\ :sup:`-7` seconds), used on Windows. |
| 29 | +* Nanoseconds (10\ :sup:`-9` seconds). |
| 30 | +* :c:expr:`timeval` structure, 1 microsecond (10\ :sup:`-6` seconds). |
| 31 | +* :c:expr:`timespec` structure, 1 nanosecond (10\ :sup:`-9` seconds). |
| 32 | + |
| 33 | +Integer overflows are detected and raise :exc:`OverflowError`. Conversion to a |
| 34 | +resolution larger than 1 nanosecond is rounded correctly with the requested |
| 35 | +rounding mode. Available rounding modes: |
| 36 | + |
| 37 | +* Round towards minus infinity (-inf). For example, used to read a clock. |
| 38 | +* Round towards infinity (+inf). For example, used for timeout to wait "at |
| 39 | + least" N seconds. |
| 40 | +* Round to nearest with ties going to nearest even integer. For example, used |
| 41 | + to round from a Python float. |
| 42 | +* Round away from zero. For example, used for timeout. |
| 43 | + |
| 44 | +Some functions clamp the result in the range [PyTime_MIN; PyTime_MAX]. The |
| 45 | +caller doesn't have to handle errors and so doesn't need to hold the GIL to |
| 46 | +handle exceptions. For example, ``_PyTime_Add(t1, t2)`` computes ``t1+t2`` and |
| 47 | +clamps the result on overflow. |
| 48 | + |
| 49 | +Clocks: |
| 50 | + |
| 51 | +* System clock |
| 52 | +* Monotonic clock |
| 53 | +* Performance counter |
| 54 | + |
| 55 | +Internally, operations like ``(t * k / q)`` with integers are implemented in a |
| 56 | +way to reduce the risk of integer overflow. Such operation is used to convert a |
| 57 | +clock value expressed in ticks with a frequency to PyTime_t, like |
| 58 | +``QueryPerformanceCounter()`` with ``QueryPerformanceFrequency()`` on Windows. |
| 59 | + |
| 60 | + |
| 61 | +Types |
| 62 | +----- |
| 63 | + |
| 64 | +.. c:type:: PyTime_t |
| 65 | +
|
| 66 | + Timestamp type with subsecond precision: 64-bit signed integer. |
| 67 | + |
| 68 | + This type can be used to store a duration. Indirectly, it can be used to |
| 69 | + store a date relative to a reference date, such as the UNIX epoch. |
| 70 | + |
| 71 | + |
| 72 | +Constants |
| 73 | +--------- |
| 74 | + |
| 75 | +.. c:var:: PyTime_t PyTime_MIN |
| 76 | +
|
| 77 | + Minimum value of the :c:type:`PyTime_t` type. |
| 78 | + :c:macro`PyTime_MIN` nanoseconds is around -292.3 years. |
| 79 | + |
| 80 | +.. c:var:: PyTime_t PyTime_MAX |
| 81 | +
|
| 82 | + Maximum value of the :c:type:`PyTime_t` type. |
| 83 | + :c:macro`PyTime_MAX` nanoseconds is around +292.3 years. |
| 84 | + |
| 85 | + |
| 86 | +Functions |
| 87 | +--------- |
| 88 | + |
| 89 | +.. c:function:: double PyTime_AsSecondsDouble(PyTime_t t) |
| 90 | +
|
| 91 | + Convert a timestamp to a number of seconds as a C :c:type:`double`. |
| 92 | +
|
| 93 | + The function cannot fail. |
| 94 | +
|
| 95 | +
|
| 96 | +.. c:function:: PyTime_t PyTime_GetMonotonicClock(void) |
| 97 | +
|
| 98 | + Get the time of a monotonic clock, i.e. a clock that cannot go backwards. |
| 99 | + The clock is not affected by system clock updates. The reference point of |
| 100 | + the returned value is undefined, so that only the difference between the |
| 101 | + results of consecutive calls is valid. |
| 102 | +
|
| 103 | + If reading the clock fails, silently ignore the error and return 0. |
| 104 | +
|
| 105 | + On integer overflow, silently ignore the overflow and clamp the clock to |
| 106 | + the [PyTime_MIN; PyTime_MAX] range. |
| 107 | +
|
| 108 | + See also the :func:`time.monotonic` function. |
| 109 | + |
| 110 | +.. c:function:: PyTime_t PyTime_GetPerfCounter(void) |
| 111 | +
|
| 112 | + Get the performance counter: clock with the highest available resolution to |
| 113 | + measure a short duration. |
| 114 | +
|
| 115 | + If reading the clock fails, silently ignore the error and return 0. |
| 116 | +
|
| 117 | + On integer overflow, silently ignore the overflow and clamp the time to the |
| 118 | + [PyTime_MIN; PyTime_MAX] range. |
| 119 | +
|
| 120 | + See also the :func:`time.perf_counter` function. |
| 121 | + |
| 122 | + |
| 123 | +.. c:function:: PyTime_t PyTime_GetSystemClock(void) |
| 124 | +
|
| 125 | + Get the current time from the system clock. |
| 126 | +
|
| 127 | + If reading the clock fails, silently ignore the error and return ``0``. |
| 128 | +
|
| 129 | + On integer overflow, silently ignore the overflow and clamp the clock to |
| 130 | + the [PyTime_MIN; PyTime_MAX] range. |
| 131 | +
|
| 132 | + See also the :func:`time.time` function. |
0 commit comments