Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/iso8601/test_iso8601.py
7813 views
1
# coding=UTF-8
2
from __future__ import absolute_import
3
4
import copy
5
import datetime
6
import pickle
7
8
import hypothesis
9
import hypothesis.extra.pytz
10
import hypothesis.strategies
11
import pytest
12
13
from . import iso8601
14
15
16
def test_iso8601_regex() -> None:
17
assert iso8601.ISO8601_REGEX.match("2006-10-11T00:14:33Z")
18
19
20
def test_fixedoffset_eq() -> None:
21
# See https://bitbucket.org/micktwomey/pyiso8601/issues/19
22
datetime.tzinfo() == iso8601.FixedOffset(2, 0, "+2:00")
23
24
25
def test_parse_no_timezone_different_default() -> None:
26
tz = iso8601.FixedOffset(2, 0, "test offset")
27
d = iso8601.parse_date("2007-01-01T08:00:00", default_timezone=tz)
28
assert d == datetime.datetime(2007, 1, 1, 8, 0, 0, 0, tz)
29
assert d.tzinfo == tz
30
31
32
def test_parse_utc_different_default() -> None:
33
"""Z should mean 'UTC', not 'default'."""
34
tz = iso8601.FixedOffset(2, 0, "test offset")
35
d = iso8601.parse_date("2007-01-01T08:00:00Z", default_timezone=tz)
36
assert d == datetime.datetime(2007, 1, 1, 8, 0, 0, 0, iso8601.UTC)
37
38
39
@pytest.mark.parametrize(
40
"invalid_date, error_string",
41
[
42
("2013-10-", "Unable to parse date string"),
43
("2013-", "Unable to parse date string"),
44
("", "Unable to parse date string"),
45
("wibble", "Unable to parse date string"),
46
("23", "Unable to parse date string"),
47
("131015T142533Z", "Unable to parse date string"),
48
("131015", "Unable to parse date string"),
49
("20141", "Unable to parse date string"),
50
("201402", "Unable to parse date string"),
51
(
52
"2007-06-23X06:40:34.00Z",
53
"Unable to parse date string",
54
), # https://code.google.com/p/pyiso8601/issues/detail?id=14
55
(
56
"2007-06-23 06:40:34.00Zrubbish",
57
"Unable to parse date string",
58
), # https://code.google.com/p/pyiso8601/issues/detail?id=14
59
("20114-01-03T01:45:49", "Unable to parse date string"),
60
],
61
)
62
def test_parse_invalid_date(invalid_date: str, error_string: str) -> None:
63
with pytest.raises(iso8601.ParseError) as exc:
64
iso8601.parse_date(invalid_date)
65
assert exc.errisinstance(iso8601.ParseError)
66
assert str(exc.value).startswith(error_string)
67
68
69
@pytest.mark.parametrize(
70
"valid_date,expected_datetime,isoformat",
71
[
72
(
73
"2007-06-23 06:40:34.00Z",
74
datetime.datetime(2007, 6, 23, 6, 40, 34, 0, iso8601.UTC),
75
"2007-06-23T06:40:34+00:00",
76
), # Handle a separator other than T
77
(
78
"1997-07-16T19:20+01:00",
79
datetime.datetime(
80
1997, 7, 16, 19, 20, 0, 0, iso8601.FixedOffset(1, 0, "+01:00")
81
),
82
"1997-07-16T19:20:00+01:00",
83
), # Parse with no seconds
84
(
85
"2007-01-01T08:00:00",
86
datetime.datetime(2007, 1, 1, 8, 0, 0, 0, iso8601.UTC),
87
"2007-01-01T08:00:00+00:00",
88
), # Handle timezone-less dates. Assumes UTC. http://code.google.com/p/pyiso8601/issues/detail?id=4
89
(
90
"2006-10-20T15:34:56.123+02:30",
91
datetime.datetime(
92
2006, 10, 20, 15, 34, 56, 123000, iso8601.FixedOffset(2, 30, "+02:30")
93
),
94
None,
95
),
96
(
97
"2006-10-20T15:34:56Z",
98
datetime.datetime(2006, 10, 20, 15, 34, 56, 0, iso8601.UTC),
99
"2006-10-20T15:34:56+00:00",
100
),
101
(
102
"2007-5-7T11:43:55.328Z",
103
datetime.datetime(2007, 5, 7, 11, 43, 55, 328000, iso8601.UTC),
104
"2007-05-07T11:43:55.328000+00:00",
105
), # http://code.google.com/p/pyiso8601/issues/detail?id=6
106
(
107
"2006-10-20T15:34:56.123Z",
108
datetime.datetime(2006, 10, 20, 15, 34, 56, 123000, iso8601.UTC),
109
"2006-10-20T15:34:56.123000+00:00",
110
),
111
(
112
"2013-10-15T18:30Z",
113
datetime.datetime(2013, 10, 15, 18, 30, 0, 0, iso8601.UTC),
114
"2013-10-15T18:30:00+00:00",
115
),
116
(
117
"2013-10-15T22:30+04",
118
datetime.datetime(
119
2013, 10, 15, 22, 30, 0, 0, iso8601.FixedOffset(4, 0, "+04:00")
120
),
121
"2013-10-15T22:30:00+04:00",
122
), # <time>±hh:mm
123
(
124
"2013-10-15T1130-0700",
125
datetime.datetime(
126
2013, 10, 15, 11, 30, 0, 0, iso8601.FixedOffset(-7, 0, "-07:00")
127
),
128
"2013-10-15T11:30:00-07:00",
129
), # <time>±hhmm
130
(
131
"2013-10-15T1130+0700",
132
datetime.datetime(
133
2013, 10, 15, 11, 30, 0, 0, iso8601.FixedOffset(+7, 0, "+07:00")
134
),
135
"2013-10-15T11:30:00+07:00",
136
), # <time>±hhmm
137
(
138
"2013-10-15T1130+07",
139
datetime.datetime(
140
2013, 10, 15, 11, 30, 0, 0, iso8601.FixedOffset(+7, 0, "+07:00")
141
),
142
"2013-10-15T11:30:00+07:00",
143
), # <time>±hh
144
(
145
"2013-10-15T1130-07",
146
datetime.datetime(
147
2013, 10, 15, 11, 30, 0, 0, iso8601.FixedOffset(-7, 0, "-07:00")
148
),
149
"2013-10-15T11:30:00-07:00",
150
), # <time>±hh
151
(
152
"2013-10-15T15:00-03:30",
153
datetime.datetime(
154
2013, 10, 15, 15, 0, 0, 0, iso8601.FixedOffset(-3, -30, "-03:30")
155
),
156
"2013-10-15T15:00:00-03:30",
157
),
158
(
159
"2013-10-15T183123Z",
160
datetime.datetime(2013, 10, 15, 18, 31, 23, 0, iso8601.UTC),
161
"2013-10-15T18:31:23+00:00",
162
), # hhmmss
163
(
164
"2013-10-15T1831Z",
165
datetime.datetime(2013, 10, 15, 18, 31, 0, 0, iso8601.UTC),
166
"2013-10-15T18:31:00+00:00",
167
), # hhmm
168
(
169
"2013-10-15T18Z",
170
datetime.datetime(2013, 10, 15, 18, 0, 0, 0, iso8601.UTC),
171
"2013-10-15T18:00:00+00:00",
172
), # hh
173
(
174
"2013-10-15",
175
datetime.datetime(2013, 10, 15, 0, 0, 0, 0, iso8601.UTC),
176
"2013-10-15T00:00:00+00:00",
177
), # YYYY-MM-DD
178
(
179
"20131015T18:30Z",
180
datetime.datetime(2013, 10, 15, 18, 30, 0, 0, iso8601.UTC),
181
"2013-10-15T18:30:00+00:00",
182
), # YYYYMMDD
183
(
184
"2012-12-19T23:21:28.512400+00:00",
185
datetime.datetime(
186
2012, 12, 19, 23, 21, 28, 512400, iso8601.FixedOffset(0, 0, "+00:00")
187
),
188
"2012-12-19T23:21:28.512400+00:00",
189
), # https://code.google.com/p/pyiso8601/issues/detail?id=21
190
(
191
"2006-10-20T15:34:56.123+0230",
192
datetime.datetime(
193
2006, 10, 20, 15, 34, 56, 123000, iso8601.FixedOffset(2, 30, "+02:30")
194
),
195
"2006-10-20T15:34:56.123000+02:30",
196
), # https://code.google.com/p/pyiso8601/issues/detail?id=18
197
(
198
"19950204",
199
datetime.datetime(1995, 2, 4, tzinfo=iso8601.UTC),
200
"1995-02-04T00:00:00+00:00",
201
), # https://code.google.com/p/pyiso8601/issues/detail?id=1
202
(
203
"2010-07-20 15:25:52.520701+00:00",
204
datetime.datetime(
205
2010, 7, 20, 15, 25, 52, 520701, iso8601.FixedOffset(0, 0, "+00:00")
206
),
207
"2010-07-20T15:25:52.520701+00:00",
208
), # https://code.google.com/p/pyiso8601/issues/detail?id=17
209
(
210
"2010-06-12",
211
datetime.datetime(2010, 6, 12, tzinfo=iso8601.UTC),
212
"2010-06-12T00:00:00+00:00",
213
), # https://code.google.com/p/pyiso8601/issues/detail?id=16
214
(
215
"1985-04-12T23:20:50.52-05:30",
216
datetime.datetime(
217
1985, 4, 12, 23, 20, 50, 520000, iso8601.FixedOffset(-5, -30, "-05:30")
218
),
219
"1985-04-12T23:20:50.520000-05:30",
220
), # https://bitbucket.org/micktwomey/pyiso8601/issue/8/015-parses-negative-timezones-incorrectly
221
(
222
"1997-08-29T06:14:00.000123Z",
223
datetime.datetime(1997, 8, 29, 6, 14, 0, 123, iso8601.UTC),
224
"1997-08-29T06:14:00.000123+00:00",
225
), # https://bitbucket.org/micktwomey/pyiso8601/issue/9/regression-parsing-microseconds
226
(
227
"2014-02",
228
datetime.datetime(2014, 2, 1, 0, 0, 0, 0, iso8601.UTC),
229
"2014-02-01T00:00:00+00:00",
230
), # https://bitbucket.org/micktwomey/pyiso8601/issue/14/regression-yyyy-mm-no-longer-parses
231
(
232
"2014",
233
datetime.datetime(2014, 1, 1, 0, 0, 0, 0, iso8601.UTC),
234
"2014-01-01T00:00:00+00:00",
235
), # YYYY
236
(
237
"1997-08-29T06:14:00,000123Z",
238
datetime.datetime(1997, 8, 29, 6, 14, 0, 123, iso8601.UTC),
239
"1997-08-29T06:14:00.000123+00:00",
240
), # Use , as decimal separator
241
],
242
)
243
def test_parse_valid_date(
244
valid_date: str, expected_datetime: datetime.datetime, isoformat: str
245
) -> None:
246
parsed = iso8601.parse_date(valid_date)
247
assert parsed.year == expected_datetime.year
248
assert parsed.month == expected_datetime.month
249
assert parsed.day == expected_datetime.day
250
assert parsed.hour == expected_datetime.hour
251
assert parsed.minute == expected_datetime.minute
252
assert parsed.second == expected_datetime.second
253
assert parsed.microsecond == expected_datetime.microsecond
254
assert parsed.tzinfo == expected_datetime.tzinfo
255
assert parsed == expected_datetime
256
assert parsed.isoformat() == expected_datetime.isoformat()
257
copy.deepcopy(parsed) # ensure it's deep copy-able
258
pickle.dumps(parsed) # ensure it pickles
259
if isoformat:
260
assert parsed.isoformat() == isoformat
261
assert iso8601.parse_date(parsed.isoformat()) == parsed # Test round trip
262
263
264
@hypothesis.given(s=hypothesis.strategies.datetimes())
265
def test_hypothesis_valid_naive_datetimes(s: datetime.datetime) -> None:
266
as_string = s.isoformat()
267
parsed = iso8601.parse_date(as_string, default_timezone=None)
268
print(f"{s!r} {as_string!r} {parsed!r}")
269
assert s == parsed
270
271
272
@hypothesis.given(
273
s=hypothesis.strategies.datetimes(timezones=hypothesis.extra.pytz.timezones())
274
)
275
def test_hypothesis_valid_datetimes_with_timezone(s: datetime.datetime) -> None:
276
as_string = s.isoformat()
277
parsed = iso8601.parse_date(as_string)
278
print(f"{s!r} {as_string!r} {parsed!r}")
279
assert s == parsed
280
281