Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix datetime deprecation warning #3372

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions botocore/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def signature(self, string_to_sign, request):
def add_auth(self, request):
if self.credentials is None:
raise NoCredentialsError()
datetime_now = datetime.datetime.utcnow()
datetime_now = datetime.datetime.now(datetime.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 @@ -559,7 +559,7 @@ class S3ExpressPostAuth(S3ExpressAuth):
REQUIRES_IDENTITY_CACHE = True

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

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

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

fields = {}
Expand Down
4 changes: 2 additions & 2 deletions botocore/crt/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def add_auth(self, request):

# Use utcnow() because that's what gets mocked by tests, but set
# timezone because CRT assumes naive datetime is local time.
datetime_now = datetime.datetime.utcnow().replace(
datetime_now = datetime.datetime.now(datetime.UTC).replace(
tzinfo=datetime.timezone.utc
)

Expand Down Expand Up @@ -253,7 +253,7 @@ def add_auth(self, request):

# Use utcnow() because that's what gets mocked by tests, but set
# timezone because CRT assumes naive datetime is local time.
datetime_now = datetime.datetime.utcnow().replace(
datetime_now = datetime.datetime.now(datetime.UTC).replace(
tzinfo=datetime.timezone.utc
)

Expand Down
4 changes: 2 additions & 2 deletions botocore/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def prepare_request(self, request):
def _calculate_ttl(
self, response_received_timestamp, date_header, read_timeout
):
local_timestamp = datetime.datetime.utcnow()
local_timestamp = datetime.datetime.now(datetime.UTC)
date_conversion = datetime.datetime.strptime(
date_header, "%a, %d %b %Y %H:%M:%S %Z"
)
Expand All @@ -167,7 +167,7 @@ def _set_ttl(self, retries_context, read_timeout, success_response):
has_streaming_input = retries_context.get('has_streaming_input')
if response_date_header and not has_streaming_input:
try:
response_received_timestamp = datetime.datetime.utcnow()
response_received_timestamp = datetime.datetime.now(datetime.UTC)
retries_context['ttl'] = self._calculate_ttl(
response_received_timestamp,
response_date_header,
Expand Down
2 changes: 1 addition & 1 deletion botocore/signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ def generate_presigned_post(
policy = {}

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

Expand Down
2 changes: 1 addition & 1 deletion botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def _evaluate_expiration(self, credentials):
)
jitter = random.randint(120, 600) # Between 2 to 10 minutes
refresh_interval_with_jitter = refresh_interval + jitter
current_time = datetime.datetime.utcnow()
current_time = datetime.datetime.now(datetime.UTC)
refresh_offset = datetime.timedelta(
seconds=refresh_interval_with_jitter
)
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ class FreezeTime(ContextDecorator):

def __init__(self, module, date=None):
if date is None:
date = datetime.datetime.utcnow()
date = datetime.datetime.now(datetime.UTC)
self.date = date
self.datetime_patcher = mock.patch.object(
module, 'datetime', mock.Mock(wraps=datetime.datetime)
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def test_expiration_in_datetime_format(self):
self.assertEqual(response, expected_response)

def test_retrieves_from_cache(self):
date_in_future = datetime.utcnow() + timedelta(seconds=1000)
date_in_future = datetime.now(datetime.UTC) + timedelta(seconds=1000)
utc_timestamp = date_in_future.isoformat() + 'Z'
cache_key = '793d6e2f27667ab2da104824407e486bfec24a47'
cache = {
Expand Down Expand Up @@ -749,7 +749,7 @@ def test_no_cache(self):
self.assertEqual(response, expected_response)

def test_retrieves_from_cache(self):
date_in_future = datetime.utcnow() + timedelta(seconds=1000)
date_in_future = datetime.now(datetime.UTC) + timedelta(seconds=1000)
utc_timestamp = date_in_future.isoformat() + 'Z'
cache_key = '793d6e2f27667ab2da104824407e486bfec24a47'
cache = {
Expand Down Expand Up @@ -867,7 +867,7 @@ def test_assume_role_with_no_cache(self):
mock_loader_cls.assert_called_with('/some/path/token.jwt')

def test_assume_role_retrieves_from_cache(self):
date_in_future = datetime.utcnow() + timedelta(seconds=1000)
date_in_future = datetime.now(datetime.UTC) + timedelta(seconds=1000)
utc_timestamp = date_in_future.isoformat() + 'Z'

cache_key = 'c29461feeacfbed43017d20612606ff76abc073d'
Expand Down Expand Up @@ -2037,7 +2037,7 @@ def test_assume_role_refresher_serializes_datetime(self):
self.assertEqual(expiry_time, '2016-11-06T01:30:00UTC')

def test_assume_role_retrieves_from_cache(self):
date_in_future = datetime.utcnow() + timedelta(seconds=1000)
date_in_future = datetime.now(datetime.UTC) + timedelta(seconds=1000)
utc_timestamp = date_in_future.isoformat() + 'Z'
self.fake_config['profiles']['development']['role_arn'] = 'myrole'

Expand Down Expand Up @@ -2066,7 +2066,7 @@ def test_assume_role_retrieves_from_cache(self):
self.assertEqual(creds.token, 'baz-cached')

def test_chain_prefers_cache(self):
date_in_future = datetime.utcnow() + timedelta(seconds=1000)
date_in_future = datetime.now(datetime.UTC) + timedelta(seconds=1000)
utc_timestamp = date_in_future.isoformat() + 'Z'

# The profile we will be using has a cache entry, but the profile it
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,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.utcnow()
dt = datetime.datetime.now(datetime.UTC)
if offset is not None:
dt = offset_func(dt, offset)

Expand Down