1414
1515import calendar
1616import datetime
17+ from datetime import timezone , timedelta
1718
1819import pytest
19- import pytz
2020
2121from google .api_core import datetime_helpers
2222from google .protobuf import timestamp_pb2
2323
2424
25+ UTC = timezone .utc
2526ONE_MINUTE_IN_MICROSECONDS = 60 * 1e6
2627
2728
@@ -31,7 +32,7 @@ def test_utcnow():
3132
3233
3334def test_to_milliseconds ():
34- dt = datetime .datetime (1970 , 1 , 1 , 0 , 0 , 1 , tzinfo = pytz . utc )
35+ dt = datetime .datetime (1970 , 1 , 1 , 0 , 0 , 1 , tzinfo = UTC )
3536 assert datetime_helpers .to_milliseconds (dt ) == 1000
3637
3738
@@ -42,7 +43,7 @@ def test_to_microseconds():
4243
4344
4445def test_to_microseconds_non_utc ():
45- zone = pytz . FixedOffset ( - 1 )
46+ zone = timezone ( timedelta ( minutes = - 1 ) )
4647 dt = datetime .datetime (1970 , 1 , 1 , 0 , 0 , 0 , tzinfo = zone )
4748 assert datetime_helpers .to_microseconds (dt ) == ONE_MINUTE_IN_MICROSECONDS
4849
@@ -56,7 +57,7 @@ def test_to_microseconds_naive():
5657def test_from_microseconds ():
5758 five_mins_from_epoch_in_microseconds = 5 * ONE_MINUTE_IN_MICROSECONDS
5859 five_mins_from_epoch_datetime = datetime .datetime (
59- 1970 , 1 , 1 , 0 , 5 , 0 , tzinfo = pytz . utc
60+ 1970 , 1 , 1 , 0 , 5 , 0 , tzinfo = UTC
6061 )
6162
6263 result = datetime_helpers .from_microseconds (five_mins_from_epoch_in_microseconds )
@@ -78,28 +79,28 @@ def test_from_iso8601_time():
7879def test_from_rfc3339 ():
7980 value = "2009-12-17T12:44:32.123456Z"
8081 assert datetime_helpers .from_rfc3339 (value ) == datetime .datetime (
81- 2009 , 12 , 17 , 12 , 44 , 32 , 123456 , pytz . utc
82+ 2009 , 12 , 17 , 12 , 44 , 32 , 123456 , UTC
8283 )
8384
8485
8586def test_from_rfc3339_nanos ():
8687 value = "2009-12-17T12:44:32.123456Z"
8788 assert datetime_helpers .from_rfc3339_nanos (value ) == datetime .datetime (
88- 2009 , 12 , 17 , 12 , 44 , 32 , 123456 , pytz . utc
89+ 2009 , 12 , 17 , 12 , 44 , 32 , 123456 , UTC
8990 )
9091
9192
9293def test_from_rfc3339_without_nanos ():
9394 value = "2009-12-17T12:44:32Z"
9495 assert datetime_helpers .from_rfc3339 (value ) == datetime .datetime (
95- 2009 , 12 , 17 , 12 , 44 , 32 , 0 , pytz . utc
96+ 2009 , 12 , 17 , 12 , 44 , 32 , 0 , UTC
9697 )
9798
9899
99100def test_from_rfc3339_nanos_without_nanos ():
100101 value = "2009-12-17T12:44:32Z"
101102 assert datetime_helpers .from_rfc3339_nanos (value ) == datetime .datetime (
102- 2009 , 12 , 17 , 12 , 44 , 32 , 0 , pytz . utc
103+ 2009 , 12 , 17 , 12 , 44 , 32 , 0 , UTC
103104 )
104105
105106
@@ -119,7 +120,7 @@ def test_from_rfc3339_nanos_without_nanos():
119120def test_from_rfc3339_with_truncated_nanos (truncated , micros ):
120121 value = "2009-12-17T12:44:32.{}Z" .format (truncated )
121122 assert datetime_helpers .from_rfc3339 (value ) == datetime .datetime (
122- 2009 , 12 , 17 , 12 , 44 , 32 , micros , pytz . utc
123+ 2009 , 12 , 17 , 12 , 44 , 32 , micros , UTC
123124 )
124125
125126
@@ -148,7 +149,7 @@ def test_from_rfc3339_nanos_is_deprecated():
148149def test_from_rfc3339_nanos_with_truncated_nanos (truncated , micros ):
149150 value = "2009-12-17T12:44:32.{}Z" .format (truncated )
150151 assert datetime_helpers .from_rfc3339_nanos (value ) == datetime .datetime (
151- 2009 , 12 , 17 , 12 , 44 , 32 , micros , pytz . utc
152+ 2009 , 12 , 17 , 12 , 44 , 32 , micros , UTC
152153 )
153154
154155
@@ -171,20 +172,20 @@ def test_to_rfc3339():
171172
172173
173174def test_to_rfc3339_with_utc ():
174- value = datetime .datetime (2016 , 4 , 5 , 13 , 30 , 0 , tzinfo = pytz . utc )
175+ value = datetime .datetime (2016 , 4 , 5 , 13 , 30 , 0 , tzinfo = UTC )
175176 expected = "2016-04-05T13:30:00.000000Z"
176177 assert datetime_helpers .to_rfc3339 (value , ignore_zone = False ) == expected
177178
178179
179180def test_to_rfc3339_with_non_utc ():
180- zone = pytz . FixedOffset ( - 60 )
181+ zone = timezone ( timedelta ( minutes = - 60 ) )
181182 value = datetime .datetime (2016 , 4 , 5 , 13 , 30 , 0 , tzinfo = zone )
182183 expected = "2016-04-05T14:30:00.000000Z"
183184 assert datetime_helpers .to_rfc3339 (value , ignore_zone = False ) == expected
184185
185186
186187def test_to_rfc3339_with_non_utc_ignore_zone ():
187- zone = pytz . FixedOffset ( - 60 )
188+ zone = timezone ( timedelta ( minutes = - 60 ) )
188189 value = datetime .datetime (2016 , 4 , 5 , 13 , 30 , 0 , tzinfo = zone )
189190 expected = "2016-04-05T13:30:00.000000Z"
190191 assert datetime_helpers .to_rfc3339 (value , ignore_zone = True ) == expected
@@ -283,7 +284,7 @@ def test_from_rfc3339_w_invalid():
283284 def test_from_rfc3339_wo_fraction ():
284285 timestamp = "2016-12-20T21:13:47Z"
285286 expected = datetime_helpers .DatetimeWithNanoseconds (
286- 2016 , 12 , 20 , 21 , 13 , 47 , tzinfo = pytz . UTC
287+ 2016 , 12 , 20 , 21 , 13 , 47 , tzinfo = UTC
287288 )
288289 stamp = datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (timestamp )
289290 assert stamp == expected
@@ -292,7 +293,7 @@ def test_from_rfc3339_wo_fraction():
292293 def test_from_rfc3339_w_partial_precision ():
293294 timestamp = "2016-12-20T21:13:47.1Z"
294295 expected = datetime_helpers .DatetimeWithNanoseconds (
295- 2016 , 12 , 20 , 21 , 13 , 47 , microsecond = 100000 , tzinfo = pytz . UTC
296+ 2016 , 12 , 20 , 21 , 13 , 47 , microsecond = 100000 , tzinfo = UTC
296297 )
297298 stamp = datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (timestamp )
298299 assert stamp == expected
@@ -301,7 +302,7 @@ def test_from_rfc3339_w_partial_precision():
301302 def test_from_rfc3339_w_full_precision ():
302303 timestamp = "2016-12-20T21:13:47.123456789Z"
303304 expected = datetime_helpers .DatetimeWithNanoseconds (
304- 2016 , 12 , 20 , 21 , 13 , 47 , nanosecond = 123456789 , tzinfo = pytz . UTC
305+ 2016 , 12 , 20 , 21 , 13 , 47 , nanosecond = 123456789 , tzinfo = UTC
305306 )
306307 stamp = datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (timestamp )
307308 assert stamp == expected
@@ -332,7 +333,7 @@ def test_timestamp_pb_wo_nanos_naive():
332333 stamp = datetime_helpers .DatetimeWithNanoseconds (
333334 2016 , 12 , 20 , 21 , 13 , 47 , 123456
334335 )
335- delta = stamp .replace (tzinfo = pytz . UTC ) - datetime_helpers ._UTC_EPOCH
336+ delta = stamp .replace (tzinfo = UTC ) - datetime_helpers ._UTC_EPOCH
336337 seconds = int (delta .total_seconds ())
337338 nanos = 123456000
338339 timestamp = timestamp_pb2 .Timestamp (seconds = seconds , nanos = nanos )
@@ -341,7 +342,7 @@ def test_timestamp_pb_wo_nanos_naive():
341342 @staticmethod
342343 def test_timestamp_pb_w_nanos ():
343344 stamp = datetime_helpers .DatetimeWithNanoseconds (
344- 2016 , 12 , 20 , 21 , 13 , 47 , nanosecond = 123456789 , tzinfo = pytz . UTC
345+ 2016 , 12 , 20 , 21 , 13 , 47 , nanosecond = 123456789 , tzinfo = UTC
345346 )
346347 delta = stamp - datetime_helpers ._UTC_EPOCH
347348 timestamp = timestamp_pb2 .Timestamp (
@@ -351,7 +352,7 @@ def test_timestamp_pb_w_nanos():
351352
352353 @staticmethod
353354 def test_from_timestamp_pb_wo_nanos ():
354- when = datetime .datetime (2016 , 12 , 20 , 21 , 13 , 47 , 123456 , tzinfo = pytz . UTC )
355+ when = datetime .datetime (2016 , 12 , 20 , 21 , 13 , 47 , 123456 , tzinfo = UTC )
355356 delta = when - datetime_helpers ._UTC_EPOCH
356357 seconds = int (delta .total_seconds ())
357358 timestamp = timestamp_pb2 .Timestamp (seconds = seconds )
@@ -361,11 +362,11 @@ def test_from_timestamp_pb_wo_nanos():
361362 assert _to_seconds (when ) == _to_seconds (stamp )
362363 assert stamp .microsecond == 0
363364 assert stamp .nanosecond == 0
364- assert stamp .tzinfo == pytz . UTC
365+ assert stamp .tzinfo == UTC
365366
366367 @staticmethod
367368 def test_from_timestamp_pb_w_nanos ():
368- when = datetime .datetime (2016 , 12 , 20 , 21 , 13 , 47 , 123456 , tzinfo = pytz . UTC )
369+ when = datetime .datetime (2016 , 12 , 20 , 21 , 13 , 47 , 123456 , tzinfo = UTC )
369370 delta = when - datetime_helpers ._UTC_EPOCH
370371 seconds = int (delta .total_seconds ())
371372 timestamp = timestamp_pb2 .Timestamp (seconds = seconds , nanos = 123456789 )
@@ -375,7 +376,7 @@ def test_from_timestamp_pb_w_nanos():
375376 assert _to_seconds (when ) == _to_seconds (stamp )
376377 assert stamp .microsecond == 123456
377378 assert stamp .nanosecond == 123456789
378- assert stamp .tzinfo == pytz . UTC
379+ assert stamp .tzinfo == UTC
379380
380381
381382def _to_seconds (value ):
@@ -387,5 +388,5 @@ def _to_seconds(value):
387388 Returns:
388389 int: Microseconds since the unix epoch.
389390 """
390- assert value .tzinfo is pytz . UTC
391+ assert value .tzinfo is UTC
391392 return calendar .timegm (value .timetuple ())
0 commit comments