Skip to content

Commit 82d87dc

Browse files
authored
PYTHON-3744 Fix utcnow deprecation build regressions (#1244)
1 parent 374250d commit 82d87dc

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

doc/examples/datetimes.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ time into MongoDB:
2626
.. doctest::
2727

2828
>>> result = db.objects.insert_one(
29-
... {"last_modified": datetime.datetime.now(tz=timezone.utc)}
29+
... {"last_modified": datetime.datetime.now(tz=datetime.timezone.utc)}
3030
... )
3131

32-
Always use :meth:`datetime.datetime.now(tz=timezone.utc)`, which explicitly returns the current time in
32+
Always use :meth:`datetime.datetime.now(tz=datetime.timezone.utc)`, which explicitly returns the current time in
3333
UTC, instead of :meth:`datetime.datetime.now`, with no arguments, which returns the current local
3434
time. Avoid doing this:
3535

doc/tutorial.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ post:
109109
... "author": "Mike",
110110
... "text": "My first blog post!",
111111
... "tags": ["mongodb", "python", "pymongo"],
112-
... "date": datetime.datetime.now(tz=timezone.utc),
112+
... "date": datetime.datetime.now(tz=datetime.timezone.utc),
113113
... }
114114

115115
Note that documents can contain native Python types (like

pymongo/ocsp_cache.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ def __setitem__(self, key, value):
6161
return
6262

6363
# Do nothing if the response is invalid.
64-
if not (value.this_update <= _datetime.now(tz=timezone.utc) < value.next_update):
64+
if not (
65+
value.this_update
66+
<= _datetime.now(tz=timezone.utc).replace(tzinfo=None)
67+
< value.next_update
68+
):
6569
return
6670

6771
# Cache new response OR update cached response if new response
@@ -82,7 +86,11 @@ def __getitem__(self, item):
8286
value = self._data[cache_key]
8387

8488
# Return cached response if it is still valid.
85-
if value.this_update <= _datetime.now(tz=timezone.utc) < value.next_update:
89+
if (
90+
value.this_update
91+
<= _datetime.now(tz=timezone.utc).replace(tzinfo=None)
92+
< value.next_update
93+
):
8694
return value
8795

8896
self._data.pop(cache_key, None)

pymongo/ocsp_support.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _verify_response(issuer, response):
220220

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

test/test_ocsp_cache.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _create_mock_request(self):
6161
)
6262

6363
def _create_mock_response(self, this_update_delta_seconds, next_update_delta_seconds):
64-
now = datetime.now(tz=timezone.utc)
64+
now = datetime.now(tz=timezone.utc).replace(tzinfo=None)
6565
this_update = now + timedelta(seconds=this_update_delta_seconds)
6666
if next_update_delta_seconds is not None:
6767
next_update = now + timedelta(seconds=next_update_delta_seconds)

0 commit comments

Comments
 (0)