Skip to content

Commit

Permalink
PYTHON-3744 Fix utcnow deprecation build regressions (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStapp committed Jun 16, 2023
1 parent 374250d commit 82d87dc
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions doc/examples/datetimes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions pymongo/ocsp_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def __setitem__(self, key, value):
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
Expand All @@ -82,7 +86,11 @@ def __getitem__(self, item):
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)
Expand Down
2 changes: 1 addition & 1 deletion pymongo/ocsp_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/test_ocsp_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _create_mock_request(self):
)

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)
Expand Down

0 comments on commit 82d87dc

Please sign in to comment.