Skip to content

Commit 88a027e

Browse files
committed
add monotonic time func for py2 (https://stackoverflow.com/a/1205762)
1 parent f6c2f52 commit 88a027e

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# thx to https://stackoverflow.com/a/1205762
2+
import ctypes
3+
# import os
4+
5+
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>
6+
7+
8+
class _Timespec(ctypes.Structure):
9+
_fields_ = [
10+
('tv_sec', ctypes.c_long),
11+
('tv_nsec', ctypes.c_long)
12+
]
13+
14+
15+
_librt = ctypes.CDLL('librt.so.1', use_errno=True)
16+
_clock_gettime = _librt.clock_gettime
17+
_clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(_Timespec)]
18+
19+
20+
def time():
21+
t = _Timespec()
22+
if _clock_gettime(CLOCK_MONOTONIC_RAW, ctypes.pointer(t)) != 0:
23+
# errno_ = ctypes.get_errno()
24+
# raise OSError(errno_, os.strerror(errno_))
25+
return None
26+
return t.tv_sec + t.tv_nsec * 1e-9
27+
28+
29+
AVAILABLE = bool(time())

0 commit comments

Comments
 (0)