| 1 |
""" |
| 2 |
Cython implementation of (parts of) the standard library time module. |
| 3 |
|
| 4 |
Note: On implementations that lack a C-API for monotonic/perfcounter clocks |
| 5 |
(like PyPy), the fallback code uses the system clock which may return absolute |
| 6 |
time values from a different value range, differing from those provided by |
| 7 |
Python's "time" module. |
| 8 |
|
| 9 |
Note: To remove and use Cython when the cython minimum version of the |
| 10 |
project will be 3.1.0 |
| 11 |
""" |
| 12 |
|
| 13 |
from libc.stdint cimport int64_t |
| 14 |
from cpython.exc cimport PyErr_SetFromErrno |
| 15 |
|
| 16 |
cdef extern from *: |
| 17 |
""" |
| 18 |
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_t) |
| 19 |
#define __Pyx_PyTime_t PyTime_t |
| 20 |
#else |
| 21 |
#define __Pyx_PyTime_t _PyTime_t |
| 22 |
#endif |
| 23 |
|
| 24 |
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_TimeRaw) |
| 25 |
static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_TimeRaw(void) { |
| 26 |
__Pyx_PyTime_t tic; |
| 27 |
(void) PyTime_TimeRaw(&tic); |
| 28 |
return tic; |
| 29 |
} |
| 30 |
#else |
| 31 |
#define __Pyx_PyTime_TimeRaw() _PyTime_GetSystemClock() |
| 32 |
#endif |
| 33 |
|
| 34 |
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_MonotonicRaw) |
| 35 |
static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_MonotonicRaw(void) { |
| 36 |
__Pyx_PyTime_t tic; |
| 37 |
(void) PyTime_MonotonicRaw(&tic); |
| 38 |
return tic; |
| 39 |
} |
| 40 |
#elif CYTHON_COMPILING_IN_PYPY && !defined(_PyTime_GetMonotonicClock) |
| 41 |
#define __Pyx_PyTime_MonotonicRaw() _PyTime_GetSystemClock() |
| 42 |
#else |
| 43 |
#define __Pyx_PyTime_MonotonicRaw() _PyTime_GetMonotonicClock() |
| 44 |
#endif |
| 45 |
|
| 46 |
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_PerfCounterRaw) |
| 47 |
static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_PerfCounterRaw(void) { |
| 48 |
__Pyx_PyTime_t tic; |
| 49 |
(void) PyTime_PerfCounterRaw(&tic); |
| 50 |
return tic; |
| 51 |
} |
| 52 |
#elif CYTHON_COMPILING_IN_PYPY && !defined(_PyTime_GetPerfCounter) |
| 53 |
#define __Pyx_PyTime_PerfCounterRaw() __Pyx_PyTime_MonotonicRaw() |
| 54 |
#else |
| 55 |
#define __Pyx_PyTime_PerfCounterRaw() _PyTime_GetPerfCounter() |
| 56 |
#endif |
| 57 |
|
| 58 |
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_AsSecondsDouble) |
| 59 |
#define __Pyx_PyTime_AsSecondsDouble(t) PyTime_AsSecondsDouble(t) |
| 60 |
#else |
| 61 |
#define __Pyx_PyTime_AsSecondsDouble(t) _PyTime_AsSecondsDouble(t) |
| 62 |
#endif |
| 63 |
""" |
| 64 |
ctypedef int64_t PyTime_t "__Pyx_PyTime_t" |
| 65 |
|
| 66 |
ctypedef PyTime_t _PyTime_t "__Pyx_PyTime_t" # legacy, use "PyTime_t" instead |
| 67 |
PyTime_t PyTime_TimeUnchecked "__Pyx_PyTime_TimeRaw" () noexcept nogil # legacy, use "PyTime_TimeRaw" instead |
| 68 |
|
| 69 |
PyTime_t PyTime_TimeRaw "__Pyx_PyTime_TimeRaw" () noexcept nogil |
| 70 |
PyTime_t PyTime_MonotonicRaw "__Pyx_PyTime_MonotonicRaw" () noexcept nogil |
| 71 |
PyTime_t PyTime_PerfCounterRaw "__Pyx_PyTime_PerfCounterRaw" () noexcept nogil |
| 72 |
double PyTime_AsSecondsDouble "__Pyx_PyTime_AsSecondsDouble" (PyTime_t t) noexcept nogil |
| 73 |
|
| 74 |
|
| 75 |
from libc.time cimport ( |
| 76 |
tm, |
| 77 |
time_t, |
| 78 |
localtime as libc_localtime, |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
cdef inline double time() noexcept nogil: |
| 83 |
cdef PyTime_t tic = PyTime_TimeRaw() |
| 84 |
return PyTime_AsSecondsDouble(tic) |
| 85 |
|
| 86 |
|
| 87 |
cdef inline int64_t time_ns() noexcept nogil: |
| 88 |
return <int64_t> PyTime_TimeRaw() |
| 89 |
|
| 90 |
|
| 91 |
cdef inline double perf_counter() noexcept nogil: |
| 92 |
cdef PyTime_t tic = PyTime_PerfCounterRaw() |
| 93 |
return PyTime_AsSecondsDouble(tic) |
| 94 |
|
| 95 |
|
| 96 |
cdef inline int64_t perf_counter_ns() noexcept nogil: |
| 97 |
return <int64_t> PyTime_PerfCounterRaw() |
| 98 |
|
| 99 |
|
| 100 |
cdef inline double monotonic() noexcept nogil: |
| 101 |
cdef PyTime_t tic = PyTime_MonotonicRaw() |
| 102 |
return PyTime_AsSecondsDouble(tic) |
| 103 |
|
| 104 |
|
| 105 |
cdef inline int64_t monotonic_ns() noexcept nogil: |
| 106 |
return <int64_t> PyTime_MonotonicRaw() |
| 107 |
|
| 108 |
|
| 109 |
cdef inline int _raise_from_errno() except -1 with gil: |
| 110 |
PyErr_SetFromErrno(RuntimeError) |
| 111 |
return <int> -1 # Let the C compiler know that this function always raises. |
| 112 |
|
| 113 |
|
| 114 |
cdef inline tm localtime() except * nogil: |
| 115 |
""" |
| 116 |
Analogue to the stdlib time.localtime. The returned struct |
| 117 |
has some entries that the stdlib version does not: tm_gmtoff, tm_zone |
| 118 |
""" |
| 119 |
cdef: |
| 120 |
time_t tic = <time_t>time() |
| 121 |
tm* result |
| 122 |
|
| 123 |
result = libc_localtime(&tic) |
| 124 |
if result is NULL: |
| 125 |
_raise_from_errno() |
| 126 |
# Fix 0-based date values (and the 1900-based year). |
| 127 |
# See tmtotuple() in https://github.com/python/cpython/blob/master/Modules/timemodule.c |
| 128 |
result.tm_year += 1900 |
| 129 |
result.tm_mon += 1 |
| 130 |
result.tm_wday = <int> ((result.tm_wday + 6) % 7) |
| 131 |
result.tm_yday += 1 |
| 132 |
return result[0] |