forked from mawei/dp
1
0
Fork 0
dp/common/clock.pyx

25 lines
602 B
Cython
Raw Permalink Normal View History

2020-11-25 05:53:25 +08:00
# distutils: language = c++
2020-10-21 21:33:00 +08:00
# cython: language_level = 3
2020-02-07 05:51:42 +08:00
from posix.time cimport clock_gettime, timespec, CLOCK_MONOTONIC_RAW, clockid_t
2019-06-06 12:38:45 +08:00
2020-02-07 05:51:42 +08:00
IF UNAME_SYSNAME == "Darwin":
# Darwin doesn't have a CLOCK_BOOTTIME
CLOCK_BOOTTIME = CLOCK_MONOTONIC_RAW
ELSE:
from posix.time cimport CLOCK_BOOTTIME
2019-06-06 12:38:45 +08:00
2020-02-07 05:51:42 +08:00
cdef double readclock(clockid_t clock_id):
cdef timespec ts
cdef double current
2019-06-06 12:38:45 +08:00
2020-02-07 05:51:42 +08:00
clock_gettime(clock_id, &ts)
current = ts.tv_sec + (ts.tv_nsec / 1000000000.)
return current
2019-06-06 12:38:45 +08:00
def monotonic_time():
2020-02-07 05:51:42 +08:00
return readclock(CLOCK_MONOTONIC_RAW)
2019-06-06 12:38:45 +08:00
def sec_since_boot():
2020-02-07 05:51:42 +08:00
return readclock(CLOCK_BOOTTIME)