cython

Форк
0
129 строк · 4.2 Кб
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

10
from libc.stdint cimport int64_t
11
from cpython.exc cimport PyErr_SetFromErrno
12

13
cdef extern from *:
14
    """
15
    #if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_t)
16
        #define __Pyx_PyTime_t PyTime_t
17
    #else
18
        #define __Pyx_PyTime_t _PyTime_t
19
    #endif
20

21
    #if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_TimeRaw)
22
        static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_TimeRaw(void) {
23
            __Pyx_PyTime_t tic;
24
            (void) PyTime_TimeRaw(&tic);
25
            return tic;
26
        }
27
    #else
28
        #define __Pyx_PyTime_TimeRaw()  _PyTime_GetSystemClock()
29
    #endif
30

31
    #if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_MonotonicRaw)
32
        static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_MonotonicRaw(void) {
33
            __Pyx_PyTime_t tic;
34
            (void) PyTime_MonotonicRaw(&tic);
35
            return tic;
36
        }
37
    #elif CYTHON_COMPILING_IN_PYPY && !defined(_PyTime_GetMonotonicClock)
38
        #define __Pyx_PyTime_MonotonicRaw()  _PyTime_GetSystemClock()
39
    #else
40
        #define __Pyx_PyTime_MonotonicRaw()  _PyTime_GetMonotonicClock()
41
    #endif
42

43
    #if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_PerfCounterRaw)
44
        static CYTHON_INLINE __Pyx_PyTime_t __Pyx_PyTime_PerfCounterRaw(void) {
45
            __Pyx_PyTime_t tic;
46
            (void) PyTime_PerfCounterRaw(&tic);
47
            return tic;
48
        }
49
    #elif CYTHON_COMPILING_IN_PYPY && !defined(_PyTime_GetPerfCounter)
50
        #define __Pyx_PyTime_PerfCounterRaw()  __Pyx_PyTime_MonotonicRaw()
51
    #else
52
        #define __Pyx_PyTime_PerfCounterRaw()  _PyTime_GetPerfCounter()
53
    #endif
54

55
    #if PY_VERSION_HEX >= 0x030d00b1 || defined(PyTime_AsSecondsDouble)
56
        #define __Pyx_PyTime_AsSecondsDouble(t)  PyTime_AsSecondsDouble(t)
57
    #else
58
        #define __Pyx_PyTime_AsSecondsDouble(t)  _PyTime_AsSecondsDouble(t)
59
    #endif
60
    """
61
    ctypedef int64_t PyTime_t "__Pyx_PyTime_t"
62

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
65

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
70

71

72
from libc.time cimport (
73
    tm,
74
    time_t,
75
    localtime as libc_localtime,
76
)
77

78

79
cdef inline double time() noexcept nogil:
80
    cdef PyTime_t tic = PyTime_TimeRaw()
81
    return PyTime_AsSecondsDouble(tic)
82

83

84
cdef inline int64_t time_ns() noexcept nogil:
85
    return <int64_t> PyTime_TimeRaw()
86

87

88
cdef inline double perf_counter() noexcept nogil:
89
    cdef PyTime_t tic = PyTime_PerfCounterRaw()
90
    return PyTime_AsSecondsDouble(tic)
91

92

93
cdef inline int64_t perf_counter_ns() noexcept nogil:
94
    return <int64_t> PyTime_PerfCounterRaw()
95

96

97
cdef inline double monotonic() noexcept nogil:
98
    cdef PyTime_t tic = PyTime_MonotonicRaw()
99
    return PyTime_AsSecondsDouble(tic)
100

101

102
cdef inline int64_t monotonic_ns() noexcept nogil:
103
    return <int64_t> PyTime_MonotonicRaw()
104

105

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.
109

110

111
cdef inline tm localtime() except * nogil:
112
    """
113
    Analogue to the stdlib time.localtime.  The returned struct
114
    has some entries that the stdlib version does not: tm_gmtoff, tm_zone
115
    """
116
    cdef:
117
        time_t tic = <time_t>time()
118
        tm* result
119

120
    result = libc_localtime(&tic)
121
    if result is NULL:
122
        _raise_from_errno()
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
126
    result.tm_mon += 1
127
    result.tm_wday = <int> ((result.tm_wday + 6) % 7)
128
    result.tm_yday += 1
129
    return result[0]
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.