-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_timeutils.py
More file actions
208 lines (188 loc) · 5.7 KB
/
Copy pathtest_timeutils.py
File metadata and controls
208 lines (188 loc) · 5.7 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
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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/commoncode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from datetime import datetime
from commoncode.testcase import FileBasedTesting
from commoncode.timeutils import UTC
from commoncode.timeutils import time2tstamp
from commoncode.timeutils import tstamp2time
class TestTimeStamp(FileBasedTesting):
def test_time2tstamp_is_path_safe_and_file_is_writable(self):
ts = time2tstamp()
tf = self.get_temp_file(extension="ext", dir_name=ts, file_name=ts)
fd = open(tf, "w")
fd.write("a")
fd.close()
def test_time2tstamp_accepts_existing_datetimes(self):
ts = time2tstamp()
tf = self.get_temp_file(extension="ext", dir_name=ts, file_name=ts)
fd = open(tf, "w")
fd.write("a")
fd.close()
def test_time2tstamp_raises_on_non_datetime(self):
self.assertRaises(AttributeError, time2tstamp, "some")
self.assertRaises(AttributeError, time2tstamp, 1)
def test_time2tstamp_tstamp2time_is_idempotent(self):
dt = datetime.utcnow()
ts = time2tstamp(dt)
dt_from_ts = tstamp2time(ts)
assert dt_from_ts == dt
def test_tstamp2time_format(self):
import re
ts = time2tstamp()
pat = r"^20\d\d-[0-1][0-9]-[0-3]\dT[0-2]\d[0-6]\d[0-6]\d.\d\d\d\d\d\d$"
assert re.match(pat, ts)
def test_tstamp2time(self):
dt_from_ts = tstamp2time("2010-11-12T131415.000016")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=16,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time2(self):
dt_from_ts = tstamp2time("20101112T131415.000016")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=16,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time3(self):
dt_from_ts = tstamp2time("20101112T131415.000016Z")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=16,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time4(self):
dt_from_ts = tstamp2time("2010-11-12T131415")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=0,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time5(self):
dt_from_ts = tstamp2time("2010-11-12T13:14:15")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=0,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time6(self):
dt_from_ts = tstamp2time("20101112T13:14:15")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=0,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time7(self):
dt_from_ts = tstamp2time("20101112T13:14:15Z")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=0,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time8(self):
dt_from_ts = tstamp2time("20101112T13:14:15Z")
assert (
datetime(
year=2010,
month=11,
day=12,
hour=13,
minute=14,
second=15,
microsecond=0,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time9(self):
dt_from_ts = tstamp2time("2010-06-30T21:26:40.000Z")
assert (
datetime(
year=2010,
month=6,
day=30,
hour=21,
minute=26,
second=40,
microsecond=0,
tzinfo=UTC(),
)
== dt_from_ts
)
def test_tstamp2time_raise(self):
self.assertRaises(ValueError, tstamp2time, "201011A12T13:14:15Z")
def test_tstamp2time_scales_fractional_seconds(self):
for fraction, expected in (
("1", 100000),
("12", 120000),
("123", 123000),
("001", 1000),
("123456", 123456),
("1234567", 123456),
):
for prefix in ("2010-11-12T13:14:15", "20101112T131415"):
with self.subTest(fraction=fraction, prefix=prefix):
result = tstamp2time(f"{prefix}.{fraction}Z")
assert result.microsecond == expected