monotonic.py 678 B

1234567891011121314151617181920212223242526272829
  1. # thx to https://stackoverflow.com/a/1205762
  2. import ctypes
  3. # import os
  4. CLOCK_MONOTONIC = 1 # see <linux/time.h>
  5. class _Timespec(ctypes.Structure):
  6. _fields_ = [
  7. ('tv_sec', ctypes.c_long),
  8. ('tv_nsec', ctypes.c_long)
  9. ]
  10. _librt = ctypes.CDLL('librt.so.1', use_errno=True)
  11. _clock_gettime = _librt.clock_gettime
  12. _clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(_Timespec)]
  13. def time():
  14. t = _Timespec()
  15. if _clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
  16. # errno_ = ctypes.get_errno()
  17. # raise OSError(errno_, os.strerror(errno_))
  18. return None
  19. return t.tv_sec + t.tv_nsec * 1e-9
  20. AVAILABLE = bool(time())