forked from victronenergy/dbus_generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monotonic_time.py
104 lines (87 loc) · 3.61 KB
/
monotonic_time.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#/usr/bin/python
"""Get monotonic time from the OS using the ctypes module.
Only on Mac OS X is a C module required. Other platforms use a Python-only
implementation.
Copyright 2010, 2011 Gavin Beatty <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
__author__ = 'Gavin Beatty <[email protected]>'
#@VERSION@
__date__ = '2010-01-18'
__all__ = [
'timespec', 'get_monotonic_time_impl', 'monotonic_time'
]
import ctypes
import os
import sys
import errno
class timespec(ctypes.Structure):
_fields_ = [
('tv_sec', ctypes.c_long),
('tv_nsec', ctypes.c_long)
]
def to_seconds_double(self):
return self.tv_sec + self.tv_nsec * 1e-9
def monotonic_time(impl=None):
if impl is None:
impl = get_monotonic_time_impl()
return impl()
def get_monotonic_time_impl():
if sys.platform.startswith("linux"):
return lambda: monotonic_time_unix(1, impl=get_monotonic_time_impl_unix())
elif sys.platform.startswith("freebsd"):
return lambda: monotonic_time_unix(4, impl=get_monotonic_time_impl_unix())
elif sys.platform.startswith("darwin"):
return lambda: monotonic_time_darwin(impl=get_monotonic_time_impl_darwin())
elif sys.platform.startswith("win32"):
return monotonic_time_win32(impl=get_monotonic_time_impl_win32())
else:
raise OSError(errno.ENOSYS, "monotonic_time not supported on your platform.")
def get_monotonic_time_impl_darwin():
return ctypes.CDLL('libmonotonic_time.dylib', use_errno=True).darwin_clock_gettime_MONOTONIC
def monotonic_time_darwin(impl=None):
if impl is None:
impl = get_monotonic_time_impl_darwin()
t = timespec()
if impl(ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerrno(errno_))
return t
def get_monotonic_time_impl_unix():
fxn = ctypes.CDLL('librt.so.1', use_errno=True).clock_gettime
fxn.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
return fxn
def monotonic_time_unix(clock, impl=None):
if impl is None:
impl = get_monotonic_time_impl_unix()
t = timespec()
if impl(clock, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
return t
def get_monotonic_time_impl_win32():
return getattr(ctypes.windll.kernel32, 'GetTickCount64', ctypes.windll.kernel32.GetTickCount)
def monotonic_time_win32(impl=None):
if impl is None:
impl = get_monotonic_time_impl_win32()
ms = impl()
t = timespec()
t.tv_sec = ms / 1000
t.tv_nsec = (ms - (t.tv_sec * 1000)) * 1e6
return t
if __name__ == "__main__":
print(monotonic_time().to_seconds_double())