Blob Blame History Raw
From cd993d1b04a9c47713d927df757a14d593ca528e Mon Sep 17 00:00:00 2001
From: Dan Radez <dradez@redhat.com>
Date: Thu, 14 Dec 2023 11:22:15 -0500
Subject: [PATCH] Replace deprecated utcnow datetime function

python 3.12 reports:
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).
---
 cherrypy/_cplogging.py        | 4 ++--
 cherrypy/lib/locking.py       | 4 ++--
 cherrypy/test/sessiondemo.py  | 8 +++++---
 cherrypy/test/test_logging.py | 2 +-
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py
index bce1c87b..3cf05df5 100644
--- a/cherrypy/_cplogging.py
+++ b/cherrypy/_cplogging.py
@@ -452,6 +452,6 @@ class WSGIErrorHandler(logging.Handler):
 
 class LazyRfc3339UtcTime(object):
     def __str__(self):
-        """Return utcnow() in RFC3339 UTC Format."""
-        iso_formatted_now = datetime.datetime.utcnow().isoformat('T')
+        """Return datetime in RFC3339 UTC Format."""
+        iso_formatted_now = datetime.datetime.now(datetime.UTC).isoformat('T')
         return f'{iso_formatted_now!s}Z'
diff --git a/cherrypy/lib/locking.py b/cherrypy/lib/locking.py
index 317fb58c..44765da9 100644
--- a/cherrypy/lib/locking.py
+++ b/cherrypy/lib/locking.py
@@ -19,10 +19,10 @@ class Timer(object):
         """
         Return a timer that will expire after `elapsed` passes.
         """
-        return cls(datetime.datetime.utcnow() + elapsed)
+        return cls(datetime.datetime.now(datetime.UTC) + elapsed)
 
     def expired(self):
-        return datetime.datetime.utcnow() >= self.expiration
+        return datetime.datetime.now(datetime.UTC) >= self.expiration
 
 
 class LockTimeout(Exception):
diff --git a/cherrypy/test/sessiondemo.py b/cherrypy/test/sessiondemo.py
index 3849a259..040f8bbe 100755
--- a/cherrypy/test/sessiondemo.py
+++ b/cherrypy/test/sessiondemo.py
@@ -2,7 +2,7 @@
 """A session demonstration app."""
 
 import calendar
-from datetime import datetime
+import datetime
 import sys
 
 import cherrypy
@@ -123,9 +123,11 @@ class Root(object):
             'reqcookie': cherrypy.request.cookie.output(),
             'sessiondata': list(cherrypy.session.items()),
             'servertime': (
-                datetime.utcnow().strftime('%Y/%m/%d %H:%M') + ' UTC'
+                datetime.datetime.now(
+                    datetime.UTC).strftime('%Y/%m/%d %H:%M') + ' UTC'
             ),
-            'serverunixtime': calendar.timegm(datetime.utcnow().timetuple()),
+            'serverunixtime': calendar.timegm(
+                datetime.datetime.utcnow(datetime.UTC).timetuple()),
             'cpversion': cherrypy.__version__,
             'pyversion': sys.version,
             'expires': expires,
diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py
index 49d41d0a..ed4991df 100644
--- a/cherrypy/test/test_logging.py
+++ b/cherrypy/test/test_logging.py
@@ -216,7 +216,7 @@ def test_utc_in_timez(monkeypatch):
 
     class mock_datetime:
         @classmethod
-        def utcnow(cls):
+        def now(cls, _):
             return utcoffset8_local_time_in_naive_utc
 
     monkeypatch.setattr('datetime.datetime', mock_datetime)
-- 
2.43.0