forked from closeio/ciso8601
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
162 lines (146 loc) · 4.95 KB
/
tests.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
import ciso8601
import datetime
import pytz
import unittest
class CISO8601TestCase(unittest.TestCase):
def test_formats(self):
expected = datetime.datetime(2014, 2, 3)
self.assertEqual(
ciso8601.parse_datetime('20140203'),
expected
)
self.assertEqual(
ciso8601.parse_datetime('2014-02-03'),
expected
)
self.assertEqual(
ciso8601.parse_datetime('2014-02'),
datetime.datetime(2014, 2, 1)
)
self.assertEqual(
ciso8601.parse_datetime('2014-02T05'),
datetime.datetime(2014, 2, 1, 5)
)
self.assertEqual(
ciso8601.parse_datetime('20140203T1035'),
datetime.datetime(2014, 2, 3, 10, 35)
)
self.assertEqual(
ciso8601.parse_datetime('20140203T10:35'),
datetime.datetime(2014, 2, 3, 10, 35)
)
self.assertEqual(
ciso8601.parse_datetime('20140203T103527'),
datetime.datetime(2014, 2, 3, 10, 35, 27)
)
self.assertEqual(
ciso8601.parse_datetime('20140203T10:35:27'),
datetime.datetime(2014, 2, 3, 10, 35, 27)
)
self.assertEqual(
ciso8601.parse_datetime('20140203T10:35:27.234'),
datetime.datetime(2014, 2, 3, 10, 35, 27, 234000)
)
self.assertEqual(
ciso8601.parse_datetime('20140203T103527,234567'),
datetime.datetime(2014, 2, 3, 10, 35, 27, 234567)
)
self.assertEqual(
ciso8601.parse_datetime('20140203T103527.234567891234'),
datetime.datetime(2014, 2, 3, 10, 35, 27, 234567)
)
for leap_year in (1600, 2000, 2016):
self.assertEqual(
ciso8601.parse_datetime_unaware('{}-02-29'.format(leap_year)),
datetime.datetime(leap_year, 2, 29, 0, 0, 0, 0)
)
def test_aware_utc(self):
expected = datetime.datetime(2014, 12, 5, 12, 30, 45, 123456, pytz.UTC)
self.assertEqual(
ciso8601.parse_datetime('2014-12-05T12:30:45.123456Z'),
expected
)
self.assertEqual(
ciso8601.parse_datetime('2014-12-05T12:30:45.123456+00:00'),
expected,
)
self.assertEqual(
ciso8601.parse_datetime('2014-12-05T12:30:45.123456-00:00'),
expected,
)
def test_aware_offset(self):
self.assertEqual(
ciso8601.parse_datetime('2014-12-05T12:30:45.123456+05:30'),
datetime.datetime(2014, 12, 5, 12, 30, 45, 123456, pytz.FixedOffset(330))
)
self.assertEqual(
ciso8601.parse_datetime('2014-12-05T12:30:45.123456-05:30'),
datetime.datetime(2014, 12, 5, 12, 30, 45, 123456, pytz.FixedOffset(-330))
)
self.assertEqual(
ciso8601.parse_datetime('2014-12-05T12:30:45.123456-06:00'),
datetime.datetime(2014, 12, 5, 12, 30, 45, 123456, pytz.FixedOffset(-360))
)
def test_unaware(self):
expected = datetime.datetime(2014, 12, 5, 12, 30, 45, 123456)
self.assertEqual(
ciso8601.parse_datetime('2014-12-05T12:30:45.123456'),
expected
)
# parse_datetime_unaware ignores tz offset
self.assertEqual(
ciso8601.parse_datetime_unaware('2014-12-05T12:30:45.123456Z'),
expected
)
self.assertEqual(
ciso8601.parse_datetime_unaware('2014-12-05T12:30:45.123456+00:00'),
expected,
)
self.assertEqual(
ciso8601.parse_datetime_unaware('2014-12-05T12:30:45.123456-05:00'),
expected,
)
def test_invalid(self):
self.assertEqual(
ciso8601.parse_datetime_unaware('asdf'),
None,
)
self.assertEqual(
ciso8601.parse_datetime_unaware('2014-99-03'),
None,
)
self.assertEqual(
ciso8601.parse_datetime_unaware('2014-13-03'),
None,
)
self.assertEqual(
ciso8601.parse_datetime('20140203T24:35:27'),
None,
)
self.assertEqual(
ciso8601.parse_datetime('20140203T23:60:27'),
None,
)
self.assertEqual(
ciso8601.parse_datetime('20140203T23:35:61'),
None,
)
self.assertEqual(
ciso8601.parse_datetime_unaware('2014-01-32'),
None,
)
for non_leap_year in (1700, 1800, 1900, 2014):
self.assertEqual(
ciso8601.parse_datetime_unaware('{}-02-29'.format(non_leap_year)),
None,
)
self.assertEqual(
ciso8601.parse_datetime_unaware('Z'),
None,
)
self.assertEqual(
ciso8601.parse_datetime('2014-12-05asdfasdf'),
datetime.datetime(2014, 12, 5)
)
if __name__ == '__main__':
unittest.main()