Skip to content

Commit

Permalink
replaced all usage of utcnow with .now(datetime.timezone.utc)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jibola committed Jan 10, 2024
1 parent fc84d28 commit 41240cf
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion motor/aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _set_standard_headers(self, path, resp, gridout, checksum):

if cache_time > 0:
resp.headers["Expires"] = (
datetime.datetime.utcnow() + datetime.timedelta(seconds=cache_time)
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=cache_time)
).strftime("%a, %d %b %Y %H:%M:%S GMT")

resp.headers["Cache-Control"] = "max-age=" + str(cache_time)
Expand Down
2 changes: 1 addition & 1 deletion motor/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async def get(self, path, include_body=True):

if cache_time > 0:
self.set_header(
"Expires", datetime.datetime.utcnow() + datetime.timedelta(seconds=cache_time)
"Expires", datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=cache_time)
)
self.set_header("Cache-Control", "max-age=" + str(cache_time))
else:
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ filterwarnings = [
"ignore: The fetch_next property is deprecated:DeprecationWarning",
"ignore:The next_object method is deprecated:DeprecationWarning",
"ignore:unclosed <ssl.SSLSocket:ResourceWarning",
"ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning",
"ignore:datetime.datetime.utcnow:DeprecationWarning",
]

[tool.ruff]
Expand Down
8 changes: 4 additions & 4 deletions test/asyncio_tests/test_aiohttp_gridfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ def setUpClass(cls):
cls.contents = b"Jesse" * 100 * 1024

# Record when we created the file, to check the Last-Modified header
cls.put_start = datetime.datetime.utcnow().replace(microsecond=0)
cls.put_start = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
file_id = "id"
cls.file_id = file_id
cls.fs.delete(cls.file_id)
cls.fs.put(cls.contents, _id=file_id, filename="foo", content_type="my type")
item = cls.fs.get(file_id)
cls.contents_hash = _hash_gridout(item)
cls.put_end = datetime.datetime.utcnow().replace(microsecond=0)
cls.put_end = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
cls.app = cls.srv = cls.app_handler = None

@classmethod
Expand Down Expand Up @@ -232,7 +232,7 @@ class AIOHTTPTZAwareGridFSHandlerTest(AIOHTTPGridFSHandlerTestBase):
async def test_tz_aware(self):
client = self.asyncio_client(tz_aware=True)
await self.start_app(AIOHTTPGridFS(client.motor_test))
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
ago = now - datetime.timedelta(minutes=10)
hence = now + datetime.timedelta(minutes=10)

Expand Down Expand Up @@ -276,7 +276,7 @@ def extras(response, gridout):
self.assertRegex(cache_control, r"max-age=\d+")
self.assertEqual(10, int(cache_control.split("=")[1]))
expiration = parse_date(response.headers["Expires"])
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)

# It should expire about 10 seconds from now
self.assertTrue(
Expand Down
8 changes: 4 additions & 4 deletions test/tornado_tests/test_motor_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def setUp(self):
self.contents = b"Jesse" * 100 * 1024

# Record when we created the file, to check the Last-Modified header
self.put_start = datetime.datetime.utcnow().replace(microsecond=0)
self.put_start = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
file_id = "id"
self.file_id = file_id
self.fs.delete(self.file_id)
self.fs.put(self.contents, _id=file_id, filename="foo", content_type="my type")

item = self.fs.get(file_id)
self.contents_hash = _hash_gridout(item)
self.put_end = datetime.datetime.utcnow().replace(microsecond=0)
self.put_end = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
self.assertTrue(self.fs.get_last_version("foo"))

def motor_db(self, **kwargs):
Expand Down Expand Up @@ -174,7 +174,7 @@ def motor_db(self):
return super().motor_db(tz_aware=True)

def test_tz_aware(self):
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
ago = now - datetime.timedelta(minutes=10)
hence = now + datetime.timedelta(minutes=10)

Expand Down Expand Up @@ -219,7 +219,7 @@ def test_get_gridfs_file(self):
# It should expire about 10 seconds from now
self.assertTrue(
datetime.timedelta(seconds=8)
< expires - datetime.datetime.utcnow()
< expires - datetime.datetime.now(datetime.timezone.utc)
< datetime.timedelta(seconds=12)
)

Expand Down

0 comments on commit 41240cf

Please sign in to comment.