-
Notifications
You must be signed in to change notification settings - Fork 119
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 deprecated datetime.datetime.utcnow()
#3365
Conversation
4fd7332
to
fc04b59
Compare
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.
most of the changes seems OK at a quick glance, one potential issue spotted
@@ -95,7 +95,7 @@ def drift_check(utc_time_string: str, hours: int = 1) -> bool: | |||
utc_datetime = dateutil.parser.parse(utc_time_string) | |||
# This should not have a timezone, but we know it will be utc. | |||
# We need our timezones to match in order to compare | |||
local_datetime = datetime.datetime.utcnow().replace(tzinfo=utc_datetime.tzinfo) |
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.
be careful here: this local_datetime
will have the tzinfo object not from datetime
but from dateutil
, which helps when comparing it against utc_datetime
(returned by the dateutil.parser.parse()
above)
I'm afraid the tzinfo object still needs to be replaced
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.
Fixed. In theory we could get rid of the whole dateutil
dependency, but that would be a separate card.
fc04b59
to
0efd8d4
Compare
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.
LGTM, I have only few small requests and comments/questions.
# dateutil has its own tzinfo object representing UTC | ||
timestamp = timestamp.replace(tzinfo=datetime.timezone.utc) | ||
|
||
now = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0) |
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.
Small detail... Why do you replace microsecond
with 0
here?
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.
It's to clean up the sub-second delta. The server sends its timestamp with precision in seconds, it doesn't make sense to be more specific than that.
return False | ||
|
||
# The time has format of 'Fri, 12 Jan 2024 08:10:46 GMT' | ||
timestamp: datetime.datetime = dateutil.parser.parse(timestamp) |
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.
Shouldn't we encapsulate dateutil.parser.parse(timestamp)
by try-except since we use drift_check()
only for printing warning message. Server in theory can return any nonsense.
41713ab
to
97e8580
Compare
`utcnow()` is deprecated since Python 3.12. `datetime.datetime.now(datetime.timezone.utc)` is a timezone-aware equivalent. connection.py's `drift_check` logic has been extended a bit.
97e8580
to
8069e5c
Compare
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.
LGTM, Thanks for update
This function is deprecated since Python 3.12.
datetime.datetime.now(datetime.UTC)
is a timezone-aware equivalent.