-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrace_timestamp_class.py
More file actions
executable file
·136 lines (110 loc) · 3.46 KB
/
strace_timestamp_class.py
File metadata and controls
executable file
·136 lines (110 loc) · 3.46 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from functools import total_ordering
@total_ordering
class Timestamp:
# TODO: Implement some init verification
def __init__(self, ts_string):
tmp = ts_string.split(":")
self.hour = int(tmp[0])
self.minute = int(tmp[1])
tmpsec = tmp[2].split(".")
self.second = int(tmpsec[0])
self.subsec = int(tmpsec[1])
def __str__(self):
return f"{self.hour:02d}:{self.minute:02d}:{self.second:02d}.{self.subsec:06d}"
def __add__(self, y):
of = 0
subsec = self.subsec + y.subsec
while subsec >= 1000000:
of += 1
subsec -= 1000000
second = self.second + y.second + of
of = 0
while second >= 60:
of += 1
second -= 60
minute = self.minute + y.minute + of
of = 0
while minute >= 60:
of += 1
minute -= 60
hour = self.hour + y.hour + of
return Timestamp(f"{hour}:{minute}:{second}.{subsec}")
def __sub__(self, y):
of = 0
if self.subsec >= y.subsec:
subsec = self.subsec - y.subsec
else:
of = 1
subsec = 1000000 + self.subsec
subsec -= y.subsec
if self.second - of >= y.second:
second = self.second - y.second - of
of = 0
else:
second = 60 + self.second - of
second -= y.second
of = 1
if self.minute - of >= y.minute:
minute = self.minute - y.minute - of
of = 0
else:
minute = 60 + self.minute - of
minute -= y.minute
of = 1
if self.hour - of >= y.hour:
hour = self.hour - y.hour - of
of = 0
else:
#return Timestamp("0:0:0:0") ## TODO: Handle this exception.. somehow :P
raise Exception("Subtraction into negative.")
return Timestamp(f"{hour}:{minute}:{second}.{subsec}")
# TODO: Comparison
def __eq__(self, y):
if (self.hour == y.hour and
self.minute == y.minute and
self.second == y.second and
self.subsec == y.subsec):
return True
else:
return False
def __ne__(self, y):
if (self.hour != y.hour or
self.minute != y.minute or
self.second != y.second or
self.subsec != y.subsec):
return True
else:
return False
def __lt__(self, y):
if self.hour == y.hour:
if self.minute == y.minute:
if self.second == y.second:
if self.subsec == y.subsec:
return False
else:
return self.subsec < y.subsec
else:
return self.second < y.second
else:
return self.minute < y.minute
else:
return self.hour < y.hour
def add(self, x):
self.subsec += x.subsec
self.second += x.second
self.minute += x.minute
self.hour += x.hour
def us(self):
us = self.subsec
us += self.second*1000000
us += self.minute*60*1000000
us += self.hour*60*60*1000000
return us
## Testing:
#ts1 = Timestamp("21:22:33.000100")
#ts2 = Timestamp("22:22:33.000100")
#ts3 = Timestamp("13:20:34.999999")
#ts4 = Timestamp("21:22:33.010100")
#print(ts1)
#print(ts2)
#print(ts4-ts3)