diff --git a/doc/examples/datetimes.rst b/doc/examples/datetimes.rst index 2dc9c003eb..f9c9fa7a31 100644 --- a/doc/examples/datetimes.rst +++ b/doc/examples/datetimes.rst @@ -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: diff --git a/doc/tutorial.rst b/doc/tutorial.rst index 768b535fe3..e33936363d 100644 --- a/doc/tutorial.rst +++ b/doc/tutorial.rst @@ -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 diff --git a/pymongo/ocsp_cache.py b/pymongo/ocsp_cache.py index b60a24b027..f6ac4bb08c 100644 --- a/pymongo/ocsp_cache.py +++ b/pymongo/ocsp_cache.py @@ -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 @@ -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) diff --git a/pymongo/ocsp_support.py b/pymongo/ocsp_support.py index dda92d0d3b..dd070748a4 100644 --- a/pymongo/ocsp_support.py +++ b/pymongo/ocsp_support.py @@ -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") diff --git a/test/test_ocsp_cache.py b/test/test_ocsp_cache.py index 3740b6b28a..7fff4fd902 100644 --- a/test/test_ocsp_cache.py +++ b/test/test_ocsp_cache.py @@ -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)