This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_03_time_util.py
260 lines (196 loc) · 6.59 KB
/
test_03_time_util.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
# pylint: disable=missing-docstring
import calendar
import time
from datetime import datetime
import pytest
from oidcmsg.time_util import TimeUtilError
from oidcmsg.time_util import a_while_ago
from oidcmsg.time_util import add_duration
from oidcmsg.time_util import after
from oidcmsg.time_util import before
from oidcmsg.time_util import f_quotient
from oidcmsg.time_util import in_a_while
from oidcmsg.time_util import instant
from oidcmsg.time_util import later_than
from oidcmsg.time_util import modulo
from oidcmsg.time_util import not_before
from oidcmsg.time_util import not_on_or_after
from oidcmsg.time_util import parse_duration
from oidcmsg.time_util import shift_time
from oidcmsg.time_util import str_to_time
from oidcmsg.time_util import time_a_while_ago
from oidcmsg.time_util import utc_time_sans_frac
from oidcmsg.time_util import valid
__author__ = "rohe0002"
def test_f_quotient():
assert f_quotient(-1, 3) == -1
assert f_quotient(0, 3) == 0
assert f_quotient(1, 3) == 0
assert f_quotient(2, 3) == 0
assert f_quotient(3, 3) == 1
assert f_quotient(3.123, 3) == 1
def test_modulo():
assert modulo(-1, 3) == 2
assert modulo(0, 3) == 0
assert modulo(1, 3) == 1
assert modulo(2, 3) == 2
assert modulo(3, 3) == 0
x = 3.123
assert modulo(3.123, 3) == x - 3
def test_f_quotient_2():
assert f_quotient(0, 1, 13) == -1
for i in range(1, 13):
assert f_quotient(i, 1, 13) == 0
assert f_quotient(13, 1, 13) == 1
assert f_quotient(13.123, 1, 13) == 1
def test_modulo_2():
assert modulo(0, 1, 13) == 12
for i in range(1, 13):
assert modulo(i, 1, 13) == i
assert modulo(13, 1, 13) == 1
# x = 0.123
# assert modulo(13+x, 1, 13) == 1+x
def test_parse_duration():
(sign, d) = parse_duration("P1Y3M5DT7H10M3.3S")
assert sign == "+"
assert d["tm_sec"] == 3.3
assert d["tm_mon"] == 3
assert d["tm_hour"] == 7
assert d["tm_mday"] == 5
assert d["tm_year"] == 1
assert d["tm_min"] == 10
def test_add_duration_1():
# 2000-01-12T12:13:14Z P1Y3M5DT7H10M3S 2001-04-17T19:23:17Z
t = add_duration(str_to_time("2000-01-12T12:13:14Z"), "P1Y3M5DT7H10M3S")
assert t.tm_year == 2001
assert t.tm_mon == 4
assert t.tm_mday == 17
assert t.tm_hour == 19
assert t.tm_min == 23
assert t.tm_sec == 17
def test_add_duration_2():
# 2000-01-12 PT33H 2000-01-13
t = add_duration(str_to_time("2000-01-12T00:00:00Z"), "PT33H")
assert t.tm_year == 2000
assert t.tm_mon == 1
assert t.tm_mday == 14
assert t.tm_hour == 9
assert t.tm_min == 0
assert t.tm_sec == 0
def test_add_duration_3():
# 2000-01-12 PT33H 2000-01-13
t = add_duration(str_to_time("2000-01-12T00:00:00Z"), "P32D")
assert t.tm_year == 2000
assert t.tm_mon == 2
assert t.tm_mday == 12
assert t.tm_hour == 0
assert t.tm_min == 0
assert t.tm_sec == 0
assert t.tm_wday == 5
assert t.tm_wday == 5
assert t.tm_yday == 43
assert t.tm_isdst == 0
def test_add_duration_4():
# 2000-01-12 PT33H 2000-01-13
t = add_duration(str_to_time("2000-01-12T00:00:00Z"), "-P32D")
assert t is None
def test_str_to_time():
t = calendar.timegm(str_to_time("2000-01-12T00:00:00Z"))
assert t == 947635200
def test_instant():
inst = str_to_time(instant())
now = time.gmtime()
assert now >= inst
def test_valid():
assert valid("2000-01-12T00:00:00Z") is False
current_year = datetime.today().year
assert valid("%d-01-12T00:00:00Z" % (current_year + 1)) is True
this_instance = instant()
assert valid(this_instance) is False # unless on a very fast machine :-)
soon = in_a_while(seconds=10)
assert valid(soon) is True
def test_timeout():
soon = in_a_while(seconds=-1, time_format="")
assert valid(soon) is False
def test_before():
current_year = datetime.today().year
assert before("%d-01-01T00:00:00Z" % current_year) is False
assert before("%d-01-01T00:00:00Z" % (current_year + 1)) is True
def test_after():
current_year = datetime.today().year
assert after("%d-01-01T00:00:00Z" % (current_year + 1)) is False
assert after("%d-01-01T00:00:00Z" % current_year) is True
def test_not_before():
current_year = datetime.today().year
assert not_before("%d-01-01T00:00:00Z" % (current_year + 1)) is False
assert not_before("%d-01-01T00:00:00Z" % current_year) is True
def test_not_on_or_after():
current_year = datetime.today().year
assert not_on_or_after("%d-01-01T00:00:00Z" % (current_year + 1)) is True
assert not_on_or_after("%d-01-01T00:00:00Z" % current_year) is False
def test_parse_duration_1():
(sign, d) = parse_duration("-P1Y3M5DT7H10M3.3S")
assert sign == "-"
assert d["tm_sec"] == 3.3
assert d["tm_mon"] == 3
assert d["tm_hour"] == 7
assert d["tm_mday"] == 5
assert d["tm_year"] == 1
assert d["tm_min"] == 10
@pytest.mark.parametrize(
"duration",
[
"-P1Y-3M5DT7H10M3.3S",
"-P1Y3M5DU7H10M3.3S",
"-P1Y3M5DT",
"-P1Y3M5DU7H10M3.S",
"-P1Y3M5DT7H10MxS",
"-P1Y4M4DT7H10.5M3S",
],
)
def test_parse_duration_error(duration):
with pytest.raises(TimeUtilError):
parse_duration(duration)
def test_time_a_while_ago():
dt = datetime.utcnow()
t = time_a_while_ago(seconds=10)
delta = dt - t # slightly less than 10
assert (delta.seconds == 9 and delta.microseconds > 0) or delta.seconds == 10
def test_a_while_ago():
dt = time.mktime(time.gmtime())
then = a_while_ago(seconds=10)
t = time.mktime(str_to_time(then))
delta = dt - t # slightly less than 10
assert 9 <= delta <= 10
def test_shift_time():
dt = datetime.utcnow()
t = shift_time(dt, 10)
delta = t - dt # exactly 10
assert delta.seconds == 10
def test_str_to_time_str_error():
with pytest.raises(AttributeError):
str_to_time("2000-01-12T00:00:00ZABC")
def test_str_to_time_1():
t = str_to_time("")
assert t == 0
# def test_utc_time_sans_frac():
# t1 = utc_time_sans_frac()
# t2 = int("%d" % time.time())
# assert t1 != t2
def test_before_0():
assert before("")
assert before(0)
def test_before_int():
now_local = int(time.time())
assert before(now_local - 1) is False
assert before(now_local + 2)
def test_later_than_int():
now_local = int(time.time())
assert later_than(now_local, now_local - 1)
assert later_than(now_local - 1, now_local) is False
def test_later_than_str():
a = in_a_while(seconds=10)
b = in_a_while(seconds=20)
assert later_than(b, a)
assert later_than(a, b) is False