openpilot/common/clock.pyx

23 lines
545 B
Cython
Raw Normal View History

2020-02-06 14:51:42 -07:00
from posix.time cimport clock_gettime, timespec, CLOCK_MONOTONIC_RAW, clockid_t
2019-06-05 22:38:45 -06:00
2020-02-06 14:51:42 -07: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-05 22:38:45 -06:00
2020-02-06 14:51:42 -07:00
cdef double readclock(clockid_t clock_id):
cdef timespec ts
cdef double current
2019-06-05 22:38:45 -06:00
2020-02-06 14:51:42 -07:00
clock_gettime(clock_id, &ts)
current = ts.tv_sec + (ts.tv_nsec / 1000000000.)
return current
2019-06-05 22:38:45 -06:00
def monotonic_time():
2020-02-06 14:51:42 -07:00
return readclock(CLOCK_MONOTONIC_RAW)
2019-06-05 22:38:45 -06:00
def sec_since_boot():
2020-02-06 14:51:42 -07:00
return readclock(CLOCK_BOOTTIME)