PYTHON-3744 Fix utcnow deprecation build regressions (#1244)

This commit is contained in:
Noah Stapp 2023-06-16 13:30:54 -07:00 committed by GitHub
parent 374250d549
commit 82d87dc173
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 7 deletions

View File

@ -26,10 +26,10 @@ time into MongoDB:
.. doctest::
>>> result = db.objects.insert_one(
... {"last_modified": datetime.datetime.now(tz=timezone.utc)}
... {"last_modified": datetime.datetime.now(tz=datetime.timezone.utc)}
... )
Always use :meth:`datetime.datetime.now(tz=timezone.utc)`, which explicitly returns the current time in
Always use :meth:`datetime.datetime.now(tz=datetime.timezone.utc)`, which explicitly returns the current time in
UTC, instead of :meth:`datetime.datetime.now`, with no arguments, which returns the current local
time. Avoid doing this:

View File

@ -109,7 +109,7 @@ post:
... "author": "Mike",
... "text": "My first blog post!",
... "tags": ["mongodb", "python", "pymongo"],
... "date": datetime.datetime.now(tz=timezone.utc),
... "date": datetime.datetime.now(tz=datetime.timezone.utc),
... }
Note that documents can contain native Python types (like

View File

@ -61,7 +61,11 @@ class _OCSPCache:
return
# Do nothing if the response is invalid.
if not (value.this_update <= _datetime.now(tz=timezone.utc) < value.next_update):
if not (
value.this_update
<= _datetime.now(tz=timezone.utc).replace(tzinfo=None)
< value.next_update
):
return
# Cache new response OR update cached response if new response
@ -82,7 +86,11 @@ class _OCSPCache:
value = self._data[cache_key]
# Return cached response if it is still valid.
if value.this_update <= _datetime.now(tz=timezone.utc) < value.next_update:
if (
value.this_update
<= _datetime.now(tz=timezone.utc).replace(tzinfo=None)
< value.next_update
):
return value
self._data.pop(cache_key, None)

View File

@ -220,7 +220,7 @@ def _verify_response(issuer, response):
# Note that we are not using a "tolerance period" as discussed in
# https://tools.ietf.org/rfc/rfc5019.txt?
now = _datetime.now(tz=timezone.utc)
now = _datetime.now(tz=timezone.utc).replace(tzinfo=None)
# RFC6960, Section 3.2, Number 5
if response.this_update > now:
_LOGGER.debug("thisUpdate is in the future")

View File

@ -61,7 +61,7 @@ class TestOcspCache(unittest.TestCase):
)
def _create_mock_response(self, this_update_delta_seconds, next_update_delta_seconds):
now = datetime.now(tz=timezone.utc)
now = datetime.now(tz=timezone.utc).replace(tzinfo=None)
this_update = now + timedelta(seconds=this_update_delta_seconds)
if next_update_delta_seconds is not None:
next_update = now + timedelta(seconds=next_update_delta_seconds)