From eec935ed36a6b8c18576e78bac34942f9f63347a Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 3 Nov 2024 20:54:54 -0800 Subject: [PATCH] Fix deprecated datetime.datetime.utcnow() usage DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC). Signed-off-by: Zac Medico --- lib/portage/repository/storage/hardlink_rcu.py | 8 ++++++-- lib/portage/sync/modules/rsync/rsync.py | 6 +++++- lib/portage/tests/sync/test_sync_local.py | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/portage/repository/storage/hardlink_rcu.py b/lib/portage/repository/storage/hardlink_rcu.py index 6f464c8d05..e40c190853 100644 --- a/lib/portage/repository/storage/hardlink_rcu.py +++ b/lib/portage/repository/storage/hardlink_rcu.py @@ -266,7 +266,11 @@ async def garbage_collection(self): st = os.stat(snap_path) except OSError: continue - snap_timestamp = datetime.datetime.utcfromtimestamp(st.st_mtime) - if (datetime.datetime.utcnow() - snap_timestamp) < snap_ttl: + snap_timestamp = datetime.datetime.fromtimestamp( + st.st_mtime, tz=datetime.timezone.utc + ) + if ( + datetime.datetime.now(datetime.timezone.utc) - snap_timestamp + ) < snap_ttl: continue await self._check_call(["rm", "-rf", snap_path]) diff --git a/lib/portage/sync/modules/rsync/rsync.py b/lib/portage/sync/modules/rsync/rsync.py index e89221ebc9..29fee0a71a 100644 --- a/lib/portage/sync/modules/rsync/rsync.py +++ b/lib/portage/sync/modules/rsync/rsync.py @@ -437,7 +437,11 @@ def update(self): raise RuntimeError("Timestamp not found in Manifest") if ( self.max_age != 0 - and (datetime.datetime.utcnow() - ts.ts).days > self.max_age + and ( + datetime.datetime.now(datetime.timezone.utc) + - ts.ts.replace(tzinfo=datetime.timezone.utc) + ).days + > self.max_age ): out.quiet = False out.ewarn( diff --git a/lib/portage/tests/sync/test_sync_local.py b/lib/portage/tests/sync/test_sync_local.py index 7e6158ee45..284c117777 100644 --- a/lib/portage/tests/sync/test_sync_local.py +++ b/lib/portage/tests/sync/test_sync_local.py @@ -141,7 +141,7 @@ def bump_timestamp(): ) ) - bump_timestamp.timestamp = datetime.datetime.utcnow() + bump_timestamp.timestamp = datetime.datetime.now(datetime.timezone.utc) bump_timestamp_cmds = ((homedir, bump_timestamp),)