-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.py
75 lines (62 loc) · 2.29 KB
/
clock.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
#!/usr/bin/env python
# Python interface into the posix clock functions
# clock_gettime(), clock_settime()
#
# Motivation is to get access to CLOCK_MONOTONIC, so flight code can not care
# about jumps in system time (in which case CLOCK_REALTIME and python's
# datetime jump).
import ctypes
import os
CLOCK_REALTIME = 0
CLOCK_MONOTONIC = 1
CLOCK_PROCESS_CPUTIME_ID = 2
CLOCK_THREAD_CPUTIME_ID = 3
CLOCK_MONOTONIC_RAW = 4
CLOCK_REALTIME_COARSE = 5
CLOCK_MONOTONIC_COARSE = 6
CLOCK_BOOTTIME = 7
CLOCK_REALTIME_ALARM = 8
CLOCK_BOOTTIME_ALARM = 9
class timespec(ctypes.Structure):
_fields_ = [
("tv_sec", ctypes.c_long),
("tv_nsec", ctypes.c_long)
]
librt = ctypes.CDLL('librt.so.1', use_errno=True)
clock_gettime = librt.clock_gettime
clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
clock_settime = librt.clock_settime
clock_settime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
def gettime(clock_id):
t = timespec()
if clock_gettime(clock_id, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
return t
def gettime_us(clock_id):
t = gettime(clock_id)
return long(t.tv_sec * 1000000 + (t.tv_nsec + 500) / 1000)
def settime(clock_id, t):
if clock_settime(clock_id, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
def settime_us(clock_id, us):
t = timespec(us/1000000, (us%1000000) * 1000)
if clock_settime(clock_id, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
def test(do_set):
rt = gettime(CLOCK_REALTIME)
print "REALTIME: %10d.%09d" % (rt.tv_sec, rt.tv_nsec)
mt = gettime(CLOCK_MONOTONIC)
print "MONOTONIC: %10d.%09d" % (mt.tv_sec, mt.tv_nsec)
if do_set:
# WARNING: this sets the time to zero (1/1/1970)
print "set time..."
os.system("date --set=\"@0\"")
rt = gettime(CLOCK_REALTIME)
print "REALTIME: %10d.%09d" % (rt.tv_sec, rt.tv_nsec)
mt = gettime(CLOCK_MONOTONIC)
print "MONOTONIC: %10d.%09d" % (mt.tv_sec, mt.tv_nsec)
if __name__ == "__main__":
test(False) # True will set your system time to zero