1616import datetime
1717
1818import pytest
19- import pytz
2019
2120from google .api_core import datetime_helpers
2221from google .protobuf import timestamp_pb2
@@ -31,7 +30,7 @@ def test_utcnow():
3130
3231
3332def test_to_milliseconds ():
34- dt = datetime .datetime (1970 ,1 ,1 ,0 ,0 ,1 ,tzinfo = pytz .utc )
33+ dt = datetime .datetime (1970 ,1 ,1 ,0 ,0 ,1 ,tzinfo = datetime . timezone .utc )
3534assert datetime_helpers .to_milliseconds (dt )== 1000
3635
3736
@@ -42,7 +41,7 @@ def test_to_microseconds():
4241
4342
4443def test_to_microseconds_non_utc ():
45- zone = pytz . FixedOffset ( - 1 )
44+ zone = datetime . timezone ( datetime . timedelta ( minutes = - 1 ) )
4645dt = datetime .datetime (1970 ,1 ,1 ,0 ,0 ,0 ,tzinfo = zone )
4746assert datetime_helpers .to_microseconds (dt )== ONE_MINUTE_IN_MICROSECONDS
4847
@@ -56,7 +55,7 @@ def test_to_microseconds_naive():
5655def test_from_microseconds ():
5756five_mins_from_epoch_in_microseconds = 5 * ONE_MINUTE_IN_MICROSECONDS
5857five_mins_from_epoch_datetime = datetime .datetime (
59- 1970 ,1 ,1 ,0 ,5 ,0 ,tzinfo = pytz .utc
58+ 1970 ,1 ,1 ,0 ,5 ,0 ,tzinfo = datetime . timezone .utc
6059 )
6160
6261result = datetime_helpers .from_microseconds (five_mins_from_epoch_in_microseconds )
@@ -78,28 +77,28 @@ def test_from_iso8601_time():
7877def test_from_rfc3339 ():
7978value = "2009-12-17T12:44:32.123456Z"
8079assert datetime_helpers .from_rfc3339 (value )== datetime .datetime (
81- 2009 ,12 ,17 ,12 ,44 ,32 ,123456 ,pytz .utc
80+ 2009 ,12 ,17 ,12 ,44 ,32 ,123456 ,datetime . timezone .utc
8281 )
8382
8483
8584def test_from_rfc3339_nanos ():
8685value = "2009-12-17T12:44:32.123456Z"
8786assert datetime_helpers .from_rfc3339_nanos (value )== datetime .datetime (
88- 2009 ,12 ,17 ,12 ,44 ,32 ,123456 ,pytz .utc
87+ 2009 ,12 ,17 ,12 ,44 ,32 ,123456 ,datetime . timezone .utc
8988 )
9089
9190
9291def test_from_rfc3339_without_nanos ():
9392value = "2009-12-17T12:44:32Z"
9493assert datetime_helpers .from_rfc3339 (value )== datetime .datetime (
95- 2009 ,12 ,17 ,12 ,44 ,32 ,0 ,pytz .utc
94+ 2009 ,12 ,17 ,12 ,44 ,32 ,0 ,datetime . timezone .utc
9695 )
9796
9897
9998def test_from_rfc3339_nanos_without_nanos ():
10099value = "2009-12-17T12:44:32Z"
101100assert datetime_helpers .from_rfc3339_nanos (value )== datetime .datetime (
102- 2009 ,12 ,17 ,12 ,44 ,32 ,0 ,pytz .utc
101+ 2009 ,12 ,17 ,12 ,44 ,32 ,0 ,datetime . timezone .utc
103102 )
104103
105104
@@ -119,7 +118,7 @@ def test_from_rfc3339_nanos_without_nanos():
119118def test_from_rfc3339_with_truncated_nanos (truncated ,micros ):
120119value = "2009-12-17T12:44:32.{}Z" .format (truncated )
121120assert datetime_helpers .from_rfc3339 (value )== datetime .datetime (
122- 2009 ,12 ,17 ,12 ,44 ,32 ,micros ,pytz .utc
121+ 2009 ,12 ,17 ,12 ,44 ,32 ,micros ,datetime . timezone .utc
123122 )
124123
125124
@@ -148,7 +147,7 @@ def test_from_rfc3339_nanos_is_deprecated():
148147def test_from_rfc3339_nanos_with_truncated_nanos (truncated ,micros ):
149148value = "2009-12-17T12:44:32.{}Z" .format (truncated )
150149assert datetime_helpers .from_rfc3339_nanos (value )== datetime .datetime (
151- 2009 ,12 ,17 ,12 ,44 ,32 ,micros ,pytz .utc
150+ 2009 ,12 ,17 ,12 ,44 ,32 ,micros ,datetime . timezone .utc
152151 )
153152
154153
@@ -171,20 +170,20 @@ def test_to_rfc3339():
171170
172171
173172def test_to_rfc3339_with_utc ():
174- value = datetime .datetime (2016 ,4 ,5 ,13 ,30 ,0 ,tzinfo = pytz .utc )
173+ value = datetime .datetime (2016 ,4 ,5 ,13 ,30 ,0 ,tzinfo = datetime . timezone .utc )
175174expected = "2016-04-05T13:30:00.000000Z"
176175assert datetime_helpers .to_rfc3339 (value ,ignore_zone = False )== expected
177176
178177
179178def test_to_rfc3339_with_non_utc ():
180- zone = pytz . FixedOffset ( - 60 )
179+ zone = datetime . timezone ( datetime . timedelta ( minutes = - 60 ) )
181180value = datetime .datetime (2016 ,4 ,5 ,13 ,30 ,0 ,tzinfo = zone )
182181expected = "2016-04-05T14:30:00.000000Z"
183182assert datetime_helpers .to_rfc3339 (value ,ignore_zone = False )== expected
184183
185184
186185def test_to_rfc3339_with_non_utc_ignore_zone ():
187- zone = pytz . FixedOffset ( - 60 )
186+ zone = datetime . timezone ( datetime . timedelta ( minutes = - 60 ) )
188187value = datetime .datetime (2016 ,4 ,5 ,13 ,30 ,0 ,tzinfo = zone )
189188expected = "2016-04-05T13:30:00.000000Z"
190189assert datetime_helpers .to_rfc3339 (value ,ignore_zone = True )== expected
@@ -283,7 +282,7 @@ def test_from_rfc3339_w_invalid():
283282def test_from_rfc3339_wo_fraction ():
284283timestamp = "2016-12-20T21:13:47Z"
285284expected = datetime_helpers .DatetimeWithNanoseconds (
286- 2016 ,12 ,20 ,21 ,13 ,47 ,tzinfo = pytz . UTC
285+ 2016 ,12 ,20 ,21 ,13 ,47 ,tzinfo = datetime . timezone . utc
287286 )
288287stamp = datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (timestamp )
289288assert stamp == expected
@@ -292,7 +291,7 @@ def test_from_rfc3339_wo_fraction():
292291def test_from_rfc3339_w_partial_precision ():
293292timestamp = "2016-12-20T21:13:47.1Z"
294293expected = datetime_helpers .DatetimeWithNanoseconds (
295- 2016 ,12 ,20 ,21 ,13 ,47 ,microsecond = 100000 ,tzinfo = pytz . UTC
294+ 2016 ,12 ,20 ,21 ,13 ,47 ,microsecond = 100000 ,tzinfo = datetime . timezone . utc
296295 )
297296stamp = datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (timestamp )
298297assert stamp == expected
@@ -301,7 +300,7 @@ def test_from_rfc3339_w_partial_precision():
301300def test_from_rfc3339_w_full_precision ():
302301timestamp = "2016-12-20T21:13:47.123456789Z"
303302expected = datetime_helpers .DatetimeWithNanoseconds (
304- 2016 ,12 ,20 ,21 ,13 ,47 ,nanosecond = 123456789 ,tzinfo = pytz . UTC
303+ 2016 ,12 ,20 ,21 ,13 ,47 ,nanosecond = 123456789 ,tzinfo = datetime . timezone . utc
305304 )
306305stamp = datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (timestamp )
307306assert stamp == expected
@@ -332,7 +331,9 @@ def test_timestamp_pb_wo_nanos_naive():
332331stamp = datetime_helpers .DatetimeWithNanoseconds (
3333322016 ,12 ,20 ,21 ,13 ,47 ,123456
334333 )
335- delta = stamp .replace (tzinfo = pytz .UTC )- datetime_helpers ._UTC_EPOCH
334+ delta = (
335+ stamp .replace (tzinfo = datetime .timezone .utc )- datetime_helpers ._UTC_EPOCH
336+ )
336337seconds = int (delta .total_seconds ())
337338nanos = 123456000
338339timestamp = timestamp_pb2 .Timestamp (seconds = seconds ,nanos = nanos )
@@ -341,7 +342,7 @@ def test_timestamp_pb_wo_nanos_naive():
341342@staticmethod
342343def test_timestamp_pb_w_nanos ():
343344stamp = 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 = datetime . timezone . utc
345346 )
346347delta = stamp - datetime_helpers ._UTC_EPOCH
347348timestamp = timestamp_pb2 .Timestamp (
@@ -351,7 +352,9 @@ def test_timestamp_pb_w_nanos():
351352
352353@staticmethod
353354def test_from_timestamp_pb_wo_nanos ():
354- when = datetime .datetime (2016 ,12 ,20 ,21 ,13 ,47 ,123456 ,tzinfo = pytz .UTC )
355+ when = datetime .datetime (
356+ 2016 ,12 ,20 ,21 ,13 ,47 ,123456 ,tzinfo = datetime .timezone .utc
357+ )
355358delta = when - datetime_helpers ._UTC_EPOCH
356359seconds = int (delta .total_seconds ())
357360timestamp = timestamp_pb2 .Timestamp (seconds = seconds )
@@ -361,11 +364,13 @@ def test_from_timestamp_pb_wo_nanos():
361364assert _to_seconds (when )== _to_seconds (stamp )
362365assert stamp .microsecond == 0
363366assert stamp .nanosecond == 0
364- assert stamp .tzinfo == pytz . UTC
367+ assert stamp .tzinfo == datetime . timezone . utc
365368
366369@staticmethod
367370def test_from_timestamp_pb_w_nanos ():
368- when = datetime .datetime (2016 ,12 ,20 ,21 ,13 ,47 ,123456 ,tzinfo = pytz .UTC )
371+ when = datetime .datetime (
372+ 2016 ,12 ,20 ,21 ,13 ,47 ,123456 ,tzinfo = datetime .timezone .utc
373+ )
369374delta = when - datetime_helpers ._UTC_EPOCH
370375seconds = int (delta .total_seconds ())
371376timestamp = timestamp_pb2 .Timestamp (seconds = seconds ,nanos = 123456789 )
@@ -375,7 +380,7 @@ def test_from_timestamp_pb_w_nanos():
375380assert _to_seconds (when )== _to_seconds (stamp )
376381assert stamp .microsecond == 123456
377382assert stamp .nanosecond == 123456789
378- assert stamp .tzinfo == pytz . UTC
383+ assert stamp .tzinfo == datetime . timezone . utc
379384
380385
381386def _to_seconds (value ):
@@ -387,5 +392,5 @@ def _to_seconds(value):
387392 Returns:
388393 int: Microseconds since the unix epoch.
389394 """
390- assert value .tzinfo is pytz . UTC
395+ assert value .tzinfo is datetime . timezone . utc
391396return calendar .timegm (value .timetuple ())