2
Cython implementation of (parts of) the standard library time module.
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
10
from libc.stdint cimport int64_t
11
from cpython.exc cimport PyErr_SetFromErrno
15
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_t)
16
#define __Pyx_PyTime_t PyTime_t
18
#define __Pyx_PyTime_t _PyTime_t
21
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_TimeRaw)
22
static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_TimeRaw(void) {
24
(void) PyTime_TimeRaw(&tic);
28
#define __Pyx_PyTime_TimeRaw() _PyTime_GetSystemClock()
31
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_MonotonicRaw)
32
static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_MonotonicRaw(void) {
34
(void) PyTime_MonotonicRaw(&tic);
37
#elif CYTHON_COMPILING_IN_PYPY && !defined(_PyTime_GetMonotonicClock)
38
#define __Pyx_PyTime_MonotonicRaw() _PyTime_GetSystemClock()
40
#define __Pyx_PyTime_MonotonicRaw() _PyTime_GetMonotonicClock()
43
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_PerfCounterRaw)
44
static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_PerfCounterRaw(void) {
46
(void) PyTime_PerfCounterRaw(&tic);
49
#elif CYTHON_COMPILING_IN_PYPY && !defined(_PyTime_GetPerfCounter)
50
#define __Pyx_PyTime_PerfCounterRaw() __Pyx_PyTime_MonotonicRaw()
52
#define __Pyx_PyTime_PerfCounterRaw() _PyTime_GetPerfCounter()
55
#if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_AsSecondsDouble)
56
#define __Pyx_PyTime_AsSecondsDouble(t) PyTime_AsSecondsDouble(t)
58
#define __Pyx_PyTime_AsSecondsDouble(t) _PyTime_AsSecondsDouble(t)
61
ctypedef int64_t PyTime_t "__Pyx_PyTime_t"
63
ctypedef PyTime_t _PyTime_t "__Pyx_PyTime_t" # legacy, use "PyTime_t" instead
64
PyTime_t PyTime_TimeUnchecked "__Pyx_PyTime_TimeRaw" () noexcept nogil # legacy, use "PyTime_TimeRaw" instead
66
PyTime_t PyTime_TimeRaw "__Pyx_PyTime_TimeRaw" () noexcept nogil
67
PyTime_t PyTime_MonotonicRaw "__Pyx_PyTime_MonotonicRaw" () noexcept nogil
68
PyTime_t PyTime_PerfCounterRaw "__Pyx_PyTime_PerfCounterRaw" () noexcept nogil
69
double PyTime_AsSecondsDouble "__Pyx_PyTime_AsSecondsDouble" (PyTime_t t) noexcept nogil
72
from libc.time cimport (
75
localtime as libc_localtime,
79
cdef inline double time() noexcept nogil:
80
cdef PyTime_t tic = PyTime_TimeRaw()
81
return PyTime_AsSecondsDouble(tic)
84
cdef inline int64_t time_ns() noexcept nogil:
85
return <int64_t> PyTime_TimeRaw()
88
cdef inline double perf_counter() noexcept nogil:
89
cdef PyTime_t tic = PyTime_PerfCounterRaw()
90
return PyTime_AsSecondsDouble(tic)
93
cdef inline int64_t perf_counter_ns() noexcept nogil:
94
return <int64_t> PyTime_PerfCounterRaw()
97
cdef inline double monotonic() noexcept nogil:
98
cdef PyTime_t tic = PyTime_MonotonicRaw()
99
return PyTime_AsSecondsDouble(tic)
102
cdef inline int64_t monotonic_ns() noexcept nogil:
103
return <int64_t> PyTime_MonotonicRaw()
106
cdef inline int _raise_from_errno() except -1 with gil:
107
PyErr_SetFromErrno(RuntimeError)
108
return <int> -1 # Let the C compiler know that this function always raises.
111
cdef inline tm localtime() except * nogil:
113
Analogue to the stdlib time.localtime. The returned struct
114
has some entries that the stdlib version does not: tm_gmtoff, tm_zone
117
time_t tic = <time_t>time()
120
result = libc_localtime(&tic)
123
# Fix 0-based date values (and the 1900-based year).
124
# See tmtotuple() in https://github.com/python/cpython/blob/master/Modules/timemodule.c
125
result.tm_year += 1900
127
result.tm_wday = <int> ((result.tm_wday + 6) % 7)