Skip to content

Commit

Permalink
use datetime.timezone.utc instead of datetime.UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
azliu0 committed Mar 26, 2024
1 parent 76a1fbf commit 5c193b3
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 42 deletions.
6 changes: 3 additions & 3 deletions botocore/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def signature(self, string_to_sign, request):
def add_auth(self, request):
if self.credentials is None:
raise NoCredentialsError()
datetime_now = datetime.datetime.now(datetime.UTC)
datetime_now = datetime.datetime.now(datetime.timezone.utc)
request.context['timestamp'] = datetime_now.strftime(SIGV4_TIMESTAMP)
# This could be a retry. Make sure the previous
# authorization header is removed first.
Expand Down Expand Up @@ -554,7 +554,7 @@ class S3ExpressPostAuth(S3ExpressAuth):
REQUIRES_IDENTITY_CACHE = True

def add_auth(self, request):
datetime_now = datetime.datetime.now(datetime.UTC)
datetime_now = datetime.datetime.now(datetime.timezone.utc)
request.context['timestamp'] = datetime_now.strftime(SIGV4_TIMESTAMP)

fields = {}
Expand Down Expand Up @@ -813,7 +813,7 @@ class S3SigV4PostAuth(SigV4Auth):
"""

def add_auth(self, request):
datetime_now = datetime.datetime.now(datetime.UTC)
datetime_now = datetime.datetime.now(datetime.timezone.utc)
request.context['timestamp'] = datetime_now.strftime(SIGV4_TIMESTAMP)

fields = {}
Expand Down
8 changes: 4 additions & 4 deletions botocore/crt/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def add_auth(self, request):
if self.credentials is None:
raise NoCredentialsError()

datetime_now = datetime.datetime.now(datetime.UTC).replace(
tzinfo=datetime.UTC
datetime_now = datetime.datetime.now(datetime.timezone.utc).replace(
tzinfo=datetime.timezone.utc
)

# Use existing 'X-Amz-Content-SHA256' header if able
Expand Down Expand Up @@ -249,8 +249,8 @@ def add_auth(self, request):
if self.credentials is None:
raise NoCredentialsError()

datetime_now = datetime.datetime.now(datetime.UTC).replace(
tzinfo=datetime.UTC
datetime_now = datetime.datetime.now(datetime.timezone.utc).replace(
tzinfo=datetime.timezone.utc
)

# Use existing 'X-Amz-Content-SHA256' header if able
Expand Down
6 changes: 3 additions & 3 deletions botocore/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ def prepare_request(self, request):
def _calculate_ttl(
self, response_received_timestamp, date_header, read_timeout
):
local_timestamp = datetime.datetime.now(datetime.UTC)
local_timestamp = datetime.datetime.now(datetime.timezone.utc)
date_conversion = datetime.datetime.strptime(
date_header, "%a, %d %b %Y %H:%M:%S %Z"
).replace(tzinfo=datetime.UTC)
).replace(tzinfo=datetime.timezone.utc)
estimated_skew = date_conversion - response_received_timestamp
ttl = (
local_timestamp
Expand All @@ -170,7 +170,7 @@ def _set_ttl(self, retries_context, read_timeout, success_response):
if response_date_header and not has_streaming_input:
try:
response_received_timestamp = datetime.datetime.now(
datetime.UTC
datetime.timezone.utc
)
retries_context['ttl'] = self._calculate_ttl(
response_received_timestamp,
Expand Down
2 changes: 1 addition & 1 deletion botocore/signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ def generate_presigned_post(
policy = {}

# Create an expiration date for the policy
datetime_now = datetime.datetime.now(datetime.UTC)
datetime_now = datetime.datetime.now(datetime.timezone.utc)
expire_date = datetime_now + datetime.timedelta(seconds=expires_in)
policy['expiration'] = expire_date.strftime(botocore.auth.ISO8601)

Expand Down
4 changes: 2 additions & 2 deletions botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,13 @@ def _evaluate_expiration(self, credentials):
try:
expiration = datetime.datetime.strptime(
expiration, "%Y-%m-%dT%H:%M:%SZ"
).replace(tzinfo=datetime.UTC)
).replace(tzinfo=datetime.timezone.utc)
refresh_interval = self._config.get(
"ec2_credential_refresh_window", 60 * 10
)
jitter = random.randint(120, 600) # Between 2 to 10 minutes
refresh_interval_with_jitter = refresh_interval + jitter
current_time = datetime.datetime.now(datetime.UTC)
current_time = datetime.datetime.now(datetime.timezone.utc)
refresh_offset = datetime.timedelta(
seconds=refresh_interval_with_jitter
)
Expand Down
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,12 @@ class FreezeTime(ContextDecorator):
:param module: reference to imported module to patch (e.g. botocore.auth.datetime)
:type date: datetime.datetime
:param date: datetime object specifying the output for now(datetime.UTC)
:param date: datetime object specifying the output for now(datetime.timezone.utc)
"""

def __init__(self, module, date=None):
if date is None:
date = datetime.datetime.now(datetime.UTC)
date = datetime.datetime.now(datetime.timezone.utc)
self.date = date
self.datetime_patcher = mock.patch.object(
module, 'datetime', mock.Mock(wraps=datetime.datetime)
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def setUp(self):
'<snapshotId>%s</snapshotId>\n'
'</CopySnapshotResponse>\n'
)
self.now = datetime.datetime(2011, 9, 9, 23, 36, tzinfo=datetime.UTC)
self.now = datetime.datetime(
2011, 9, 9, 23, 36, tzinfo=datetime.timezone.utc
)
self.datetime_patch = mock.patch.object(
botocore.auth.datetime,
'datetime',
Expand Down
52 changes: 38 additions & 14 deletions tests/functional/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,50 @@ def _retry_headers_test_cases(self):
# now is called in SigV4 signing.
now_side_effects = [
[
datetime.datetime(2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 1, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 1, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 2, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.UTC),
datetime.datetime(
2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 1, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 1, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 2, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc
),
],
[
datetime.datetime(2020, 6, 1, 0, 0, 0, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 5, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 6, 0, tzinfo=datetime.UTC),
datetime.datetime(2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.UTC),
datetime.datetime(
2019, 6, 1, 0, 0, 11, 0, tzinfo=datetime.UTC
2020, 6, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 5, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 6, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 11, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 12, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(
2019, 6, 1, 0, 0, 12, 0, tzinfo=datetime.UTC
2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc
),
datetime.datetime(2019, 6, 1, 0, 0, 0, 0, tzinfo=datetime.UTC),
],
]
expected_headers = [
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
unittest,
)

DATE = datetime.datetime(2021, 8, 27, 0, 0, 0, tzinfo=datetime.UTC)
DATE = datetime.datetime(2021, 8, 27, 0, 0, 0, tzinfo=datetime.timezone.utc)


class TestS3BucketValidation(unittest.TestCase):
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/auth/test_signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class BaseTestWithFixedDate(unittest.TestCase):
def setUp(self):
self.fixed_date = datetime.datetime(2014, 3, 10, 17, 2, 55, 0).replace(
tzinfo=datetime.UTC
tzinfo=datetime.timezone.utc
)
self.datetime_patch = mock.patch('botocore.auth.datetime.datetime')
self.datetime_mock = self.datetime_patch.start()
Expand Down Expand Up @@ -527,7 +527,7 @@ def test_thread_safe_timestamp(self):
) as mock_datetime:
mock_datetime.now.side_effect = now_patcher_factory(
datetime.datetime(2014, 1, 1, 0, 0).replace(
tzinfo=datetime.UTC
tzinfo=datetime.timezone.utc
)
)
# Go through the add_auth process once. This will attach
Expand All @@ -539,7 +539,7 @@ def test_thread_safe_timestamp(self):
# Now suppose the utc time becomes the next day all of a sudden
mock_datetime.now.side_effect = now_patcher_factory(
datetime.datetime(2014, 1, 2, 0, 0).replace(
tzinfo=datetime.UTC
tzinfo=datetime.timezone.utc
)
)
# Smaller methods like the canonical request and string_to_sign
Expand Down Expand Up @@ -807,7 +807,7 @@ def setUp(self):
)
mocked_datetime = self.datetime_patcher.start()
mocked_datetime.now.side_effect = now_patcher_factory(
datetime.datetime(2014, 1, 1, 0, 0, tzinfo=datetime.UTC)
datetime.datetime(2014, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
)

def tearDown(self):
Expand Down Expand Up @@ -1111,7 +1111,7 @@ def setUp(self):
)
mocked_datetime = self.datetime_patcher.start()
mocked_datetime.now.side_effect = now_patcher_factory(
datetime.datetime(2014, 1, 1, 0, 0, tzinfo=datetime.UTC)
datetime.datetime(2014, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
)

def tearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/auth/test_sigv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

SECRET_KEY = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"
ACCESS_KEY = 'AKIDEXAMPLE'
DATE = datetime.datetime(2015, 8, 30, 12, 36, 0, tzinfo=datetime.UTC)
DATE = datetime.datetime(2015, 8, 30, 12, 36, 0, tzinfo=datetime.timezone.utc)
SERVICE = 'service'
REGION = 'us-east-1'

Expand Down
8 changes: 5 additions & 3 deletions tests/unit/test_signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,13 @@ def setUp(self):
self.datetime_patch = mock.patch('botocore.signers.datetime')
self.datetime_mock = self.datetime_patch.start()
self.fixed_date = datetime.datetime(
2014, 3, 10, 17, 2, 55, 0, tzinfo=datetime.UTC
2014, 3, 10, 17, 2, 55, 0, tzinfo=datetime.timezone.utc
)
self.fixed_delta = datetime.timedelta(seconds=3600)
self.datetime_mock.datetime.now.side_effect = now_patcher_factory(
self.fixed_date
)
self.datetime_mock.UTC = datetime.UTC
self.datetime_mock.timezone.utc = datetime.timezone.utc
self.datetime_mock.timedelta.return_value = self.fixed_delta

def tearDown(self):
Expand Down Expand Up @@ -1148,7 +1148,9 @@ def test_generate_db_auth_token(self):
hostname = 'prod-instance.us-east-1.rds.amazonaws.com'
port = 3306
username = 'someusername'
clock = datetime.datetime(2016, 11, 7, 17, 39, 33, tzinfo=datetime.UTC)
clock = datetime.datetime(
2016, 11, 7, 17, 39, 33, tzinfo=datetime.timezone.utc
)

with mock.patch('datetime.datetime') as dt:
dt.now.side_effect = now_patcher_factory(clock)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
)
from tests import FreezeTime, RawResponse, create_session, mock, unittest

DATE = datetime.datetime(2021, 12, 10, 00, 00, 00, tzinfo=datetime.UTC)
DATE = datetime.datetime(2021, 12, 10, 00, 00, 00, tzinfo=datetime.timezone.utc)
DT_FORMAT = "%Y-%m-%dT%H:%M:%SZ"


Expand Down Expand Up @@ -3051,7 +3051,7 @@ def test_v1_disabled_by_config(self):

def _get_datetime(self, dt=None, offset=None, offset_func=operator.add):
if dt is None:
dt = datetime.datetime.now(datetime.UTC)
dt = datetime.datetime.now(datetime.timezone.utc)
if offset is not None:
dt = offset_func(dt, offset)

Expand Down

0 comments on commit 5c193b3

Please sign in to comment.