-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Remove datetime.datetime.utcnow #3145
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -54,9 +54,7 @@ def add_auth(self, request): | |||||||||
if self.credentials is None: | ||||||||||
raise NoCredentialsError() | ||||||||||
|
||||||||||
# 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.timezone.utc).replace( | ||||||||||
tzinfo=datetime.timezone.utc | ||||||||||
) | ||||||||||
Comment on lines
+57
to
59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to call
Suggested change
|
||||||||||
|
||||||||||
|
@@ -251,9 +249,7 @@ def add_auth(self, request): | |||||||||
if self.credentials is None: | ||||||||||
raise NoCredentialsError() | ||||||||||
|
||||||||||
# 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.timezone.utc).replace( | ||||||||||
tzinfo=datetime.timezone.utc | ||||||||||
) | ||||||||||
Comment on lines
+252
to
254
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above.
Suggested change
|
||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -152,10 +152,10 @@ 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.timezone.utc) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted above, this can be simplified to avoid fundamentally changing
Suggested change
|
||||||
date_conversion = datetime.datetime.strptime( | ||||||
date_header, "%a, %d %b %Y %H:%M:%S %Z" | ||||||
) | ||||||
).replace(tzinfo=datetime.timezone.utc) | ||||||
estimated_skew = date_conversion - response_received_timestamp | ||||||
ttl = ( | ||||||
local_timestamp | ||||||
|
@@ -169,7 +169,9 @@ 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.timezone.utc | ||||||
) | ||||||
retries_context['ttl'] = self._calculate_ttl( | ||||||
response_received_timestamp, | ||||||
response_date_header, | ||||||
|
@@ -298,9 +300,9 @@ def _do_get_response(self, request, operation_model, context): | |||||
) | ||||||
|
||||||
http_response_record_dict = response_dict.copy() | ||||||
http_response_record_dict[ | ||||||
'streaming' | ||||||
] = operation_model.has_streaming_output | ||||||
http_response_record_dict['streaming'] = ( | ||||||
operation_model.has_streaming_output | ||||||
) | ||||||
history_recorder.record('HTTP_RESPONSE', http_response_record_dict) | ||||||
|
||||||
protocol = operation_model.metadata['protocol'] | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The minimal change here is:
The code as it currently stands makes
datetime_now
timezone-aware. The change I'm suggesting above resolves theDeprecationWarning
without fundamentally alteringdatetime_now
in the process.You can see how
datetime_now
was affected by looking in the test suite, where all of the existing tests had to be modified to suddenly be timezone-aware.