12
12
13
13
# 3rd party
14
14
import pytz
15
+ import pytest
15
16
16
17
# this package
17
18
from domdf_python_tools import dates
18
19
19
20
test_date = datetime .datetime (1996 , 10 , 13 , 2 , 20 ).replace (tzinfo = pytz .utc )
20
21
today = datetime .datetime .utcnow ().replace (tzinfo = pytz .utc ) # make sure UTC
21
22
23
+
22
24
# TODO: travis matrix to test without pytz installed
23
25
# TODO: test get_timezone
24
26
@@ -29,7 +31,7 @@ def test_utc_offset():
29
31
assert dates .get_utc_offset ("Europe/London" , test_date ) == datetime .timedelta (0 , 3600 )
30
32
assert dates .get_utc_offset ("Africa/Algiers" , test_date ) == datetime .timedelta (0 , 3600 )
31
33
# TODO: Finish
32
-
34
+
33
35
# Check that the correct UTC offsets are given for common timezones for today
34
36
assert dates .get_utc_offset ("US/Pacific" , today ) == datetime .timedelta (- 1 , 61200 )
35
37
assert dates .get_utc_offset ("Europe/London" , today ) == datetime .timedelta (0 , 3600 )
@@ -39,36 +41,39 @@ def test_utc_offset():
39
41
assert dates .get_utc_offset ("US/Pacific" ) == datetime .timedelta (- 1 , 61200 )
40
42
assert dates .get_utc_offset ("Europe/London" ) == datetime .timedelta (0 , 3600 )
41
43
assert dates .get_utc_offset ("Africa/Algiers" ) == datetime .timedelta (0 , 3600 )
42
- # TODO: Finish
44
+
45
+
46
+ # TODO: Finish
43
47
44
48
45
49
def test_converting_timezone ():
46
50
# No matter what timezone we convert to the timestamp should be the same
47
51
for tz in pytz .all_timezones :
48
- assert test_date .astimezone (dates .get_timezone (tz , test_date )).timestamp () == test_date .timestamp () == 845173200.0
49
-
52
+ assert test_date .astimezone (
53
+ dates .get_timezone (tz , test_date )).timestamp () == test_date .timestamp () == 845173200.0
54
+
50
55
if dates .get_utc_offset (tz , test_date ): # otherwise the timezone stayed as UTC
51
56
assert test_date .astimezone (dates .get_timezone (tz , test_date )).hour != test_date .hour
52
57
53
58
# And again with today's date
54
59
assert today .astimezone (dates .get_timezone (tz , today )).timestamp () == today .timestamp ()
55
60
if dates .get_utc_offset (tz , today ): # otherwise the timezone stayed as UTC
56
61
assert today .astimezone (dates .get_timezone (tz , today )).hour != today .hour
57
-
58
-
62
+
63
+
59
64
def test_utc_timestamp_to_datetime ():
60
65
# Going from a datetime object to timezone and back should give us the same object
61
66
for tz in pytz .all_timezones :
62
67
tzinfo = dates .get_timezone (tz , test_date )
63
68
dt = test_date .astimezone (tzinfo )
64
69
assert dates .utc_timestamp_to_datetime (dt .timestamp (), tzinfo ) == dt
65
-
70
+
66
71
# And again with today's date
67
72
tzinfo = dates .get_timezone (tz , today )
68
73
dt = today .astimezone (tzinfo )
69
74
assert dates .utc_timestamp_to_datetime (dt .timestamp (), tzinfo ) == dt
70
75
71
-
76
+
72
77
def test_set_timezone ():
73
78
# Setting the timezone should change the timestamp
74
79
for tz in pytz .all_timezones :
@@ -79,7 +84,7 @@ def test_set_timezone():
79
84
80
85
# Difference between "today" and the new timezone should be the timezone difference
81
86
assert \
82
- dates .set_timezone (today , dates .get_timezone (tz , today )).timestamp () + \
87
+ dates .set_timezone (today , dates .get_timezone (tz , today )).timestamp () + \
83
88
dates .get_utc_offset (tz , today ).total_seconds () \
84
89
== today .timestamp ()
85
90
@@ -104,6 +109,102 @@ def test_set_timezone():
104
109
#
105
110
# )
106
111
assert \
107
- dates .set_timezone (test_date , dates .get_timezone (tz , test_date )).timestamp () + \
112
+ dates .set_timezone (test_date , dates .get_timezone (tz , test_date )).timestamp () + \
108
113
dates .get_utc_offset (tz , test_date ).total_seconds () \
109
114
== test_date .timestamp ()
115
+
116
+
117
+ months = [
118
+ "January" ,
119
+ "February" ,
120
+ "March" ,
121
+ "April" ,
122
+ "May" ,
123
+ "June" ,
124
+ "July" ,
125
+ "August" ,
126
+ "September" ,
127
+ "October" ,
128
+ "November" ,
129
+ "December" ,
130
+ ]
131
+
132
+
133
+ def test_parse_month ():
134
+ for month_idx , month in enumerate (months ):
135
+
136
+ month_idx += 1 # to make 1-indexed
137
+
138
+ for i in range (3 , len (month )):
139
+ assert dates .parse_month (month .lower ()[:i ]) == month
140
+ assert dates .parse_month (month .upper ()[:i ]) == month
141
+ assert dates .parse_month (month .capitalize ()[:i ]) == month
142
+
143
+ assert dates .parse_month (month_idx ) == month
144
+
145
+ for value in ["abc" , 0 , "0" , - 1 , "-1" , 13 , "13" ]:
146
+ with pytest .raises (ValueError ):
147
+ dates .parse_month (value )
148
+
149
+
150
+ def test_get_month_number ():
151
+ for month_idx , month in enumerate (months ):
152
+
153
+ month_idx += 1 # to make 1-indexed
154
+
155
+ for i in range (3 , len (month )):
156
+ assert dates .get_month_number (month .lower ()[:i ]) == month_idx
157
+ assert dates .get_month_number (month .upper ()[:i ]) == month_idx
158
+ assert dates .get_month_number (month .capitalize ()[:i ]) == month_idx
159
+
160
+ assert dates .get_month_number (month ) == month_idx
161
+
162
+ for month_idx in range (1 , 13 ):
163
+ assert dates .get_month_number (month_idx ) == month_idx
164
+
165
+ for value in ["abc" , 0 , "0" , - 1 , "-1" , 13 , "13" ]:
166
+ with pytest .raises (ValueError ):
167
+ dates .get_month_number (value )
168
+
169
+
170
+ def test_check_date ():
171
+ for month_idx , month in enumerate (months ):
172
+
173
+ month_idx += 1 # to make 1-indexed
174
+
175
+ if month_idx in {9 , 4 , 6 , 11 }:
176
+ max_day = 30
177
+ elif month_idx == 2 :
178
+ max_day = 28
179
+ else :
180
+ max_day = 31
181
+
182
+ for day in range (- 5 , 36 ):
183
+ if month_idx == 2 and day == 29 :
184
+ for i in range (3 , len (month )):
185
+ assert dates .check_date (month .lower ()[:i ], 29 )
186
+ assert dates .check_date (month .upper ()[:i ], 29 )
187
+ assert dates .check_date (month .capitalize ()[:i ], 29 )
188
+
189
+ assert not dates .check_date (month .lower ()[:i ], 29 , False )
190
+ assert not dates .check_date (month .upper ()[:i ], 29 , False )
191
+ assert not dates .check_date (month .capitalize ()[:i ], 29 , False )
192
+
193
+ assert dates .check_date (month , 29 )
194
+ assert not dates .check_date (month , 29 , False )
195
+
196
+ elif 0 < day <= max_day :
197
+ for i in range (3 , len (month )):
198
+ assert dates .check_date (month .lower ()[:i ], day )
199
+ assert dates .check_date (month .upper ()[:i ], day )
200
+ assert dates .check_date (month .capitalize ()[:i ], day )
201
+
202
+ assert dates .check_date (month , day )
203
+
204
+ else :
205
+ for i in range (3 , len (month )):
206
+ assert not dates .check_date (month .lower ()[:i ], day )
207
+ assert not dates .check_date (month .upper ()[:i ], day )
208
+ assert not dates .check_date (month .capitalize ()[:i ], day )
209
+
210
+ assert not dates .check_date (month , day )
0 commit comments