-
Notifications
You must be signed in to change notification settings - Fork 11
/
monotonic.py
44 lines (38 loc) · 1.31 KB
/
monotonic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# From: http://bugs.python.org/file19461/monotonic.py
# Bug: http://bugs.python.org/issue10278
import platform
if platform.system() not in ('Windows', 'Darwin'):
from ctypes import Structure, c_long, CDLL, c_int, POINTER, byref
from ctypes.util import find_library
if platform.system() == 'FreeBSD':
CLOCK_MONOTONIC = 4
else:
CLOCK_MONOTONIC = 1
class timespec(Structure):
_fields_ = [
('tv_sec', c_long),
('tv_nsec', c_long)
]
librt_filename = find_library('rt')
if not librt_filename:
# On Debian Lenny (Python 2.5.2), find_library() is unable
# to locate /lib/librt.so.1
librt_filename = 'librt.so.1'
librt = CDLL(librt_filename)
_clock_gettime = librt.clock_gettime
_clock_gettime.argtypes = (c_int, POINTER(timespec))
def monotonic_time():
"""
Clock that cannot be set and represents monotonic time since some
unspecified starting point. The unit is a second.
"""
t = timespec()
_clock_gettime(CLOCK_MONOTONIC, byref(t))
return t.tv_sec + t.tv_nsec / 1e9
else:
try:
from win32api import GetTickCount
def monotonic_time():
return GetTickCount / 1000.0
except ImportError:
from time import time as monotonic_time