676e2f1
From cd993d1b04a9c47713d927df757a14d593ca528e Mon Sep 17 00:00:00 2001
676e2f1
From: Dan Radez <dradez@redhat.com>
676e2f1
Date: Thu, 14 Dec 2023 11:22:15 -0500
676e2f1
Subject: [PATCH] Replace deprecated utcnow datetime function
676e2f1
676e2f1
python 3.12 reports:
676e2f1
DeprecationWarning: datetime.datetime.utcnow() is deprecated and
676e2f1
scheduled for removal in a future version. Use timezone-aware
676e2f1
objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
676e2f1
---
676e2f1
 cherrypy/_cplogging.py        | 4 ++--
676e2f1
 cherrypy/lib/locking.py       | 4 ++--
676e2f1
 cherrypy/test/sessiondemo.py  | 8 +++++---
676e2f1
 cherrypy/test/test_logging.py | 2 +-
676e2f1
 4 files changed, 10 insertions(+), 8 deletions(-)
676e2f1
676e2f1
diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py
676e2f1
index bce1c87b..3cf05df5 100644
676e2f1
--- a/cherrypy/_cplogging.py
676e2f1
+++ b/cherrypy/_cplogging.py
676e2f1
@@ -452,6 +452,6 @@ class WSGIErrorHandler(logging.Handler):
676e2f1
 
676e2f1
 class LazyRfc3339UtcTime(object):
676e2f1
     def __str__(self):
676e2f1
-        """Return utcnow() in RFC3339 UTC Format."""
676e2f1
-        iso_formatted_now = datetime.datetime.utcnow().isoformat('T')
676e2f1
+        """Return datetime in RFC3339 UTC Format."""
676e2f1
+        iso_formatted_now = datetime.datetime.now(datetime.UTC).isoformat('T')
676e2f1
         return f'{iso_formatted_now!s}Z'
676e2f1
diff --git a/cherrypy/lib/locking.py b/cherrypy/lib/locking.py
676e2f1
index 317fb58c..44765da9 100644
676e2f1
--- a/cherrypy/lib/locking.py
676e2f1
+++ b/cherrypy/lib/locking.py
676e2f1
@@ -19,10 +19,10 @@ class Timer(object):
676e2f1
         """
676e2f1
         Return a timer that will expire after `elapsed` passes.
676e2f1
         """
676e2f1
-        return cls(datetime.datetime.utcnow() + elapsed)
676e2f1
+        return cls(datetime.datetime.now(datetime.UTC) + elapsed)
676e2f1
 
676e2f1
     def expired(self):
676e2f1
-        return datetime.datetime.utcnow() >= self.expiration
676e2f1
+        return datetime.datetime.now(datetime.UTC) >= self.expiration
676e2f1
 
676e2f1
 
676e2f1
 class LockTimeout(Exception):
676e2f1
diff --git a/cherrypy/test/sessiondemo.py b/cherrypy/test/sessiondemo.py
676e2f1
index 3849a259..040f8bbe 100755
676e2f1
--- a/cherrypy/test/sessiondemo.py
676e2f1
+++ b/cherrypy/test/sessiondemo.py
676e2f1
@@ -2,7 +2,7 @@
676e2f1
 """A session demonstration app."""
676e2f1
 
676e2f1
 import calendar
676e2f1
-from datetime import datetime
676e2f1
+import datetime
676e2f1
 import sys
676e2f1
 
676e2f1
 import cherrypy
676e2f1
@@ -123,9 +123,11 @@ class Root(object):
676e2f1
             'reqcookie': cherrypy.request.cookie.output(),
676e2f1
             'sessiondata': list(cherrypy.session.items()),
676e2f1
             'servertime': (
676e2f1
-                datetime.utcnow().strftime('%Y/%m/%d %H:%M') + ' UTC'
676e2f1
+                datetime.datetime.now(
676e2f1
+                    datetime.UTC).strftime('%Y/%m/%d %H:%M') + ' UTC'
676e2f1
             ),
676e2f1
-            'serverunixtime': calendar.timegm(datetime.utcnow().timetuple()),
676e2f1
+            'serverunixtime': calendar.timegm(
676e2f1
+                datetime.datetime.utcnow(datetime.UTC).timetuple()),
676e2f1
             'cpversion': cherrypy.__version__,
676e2f1
             'pyversion': sys.version,
676e2f1
             'expires': expires,
676e2f1
diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py
676e2f1
index 49d41d0a..ed4991df 100644
676e2f1
--- a/cherrypy/test/test_logging.py
676e2f1
+++ b/cherrypy/test/test_logging.py
676e2f1
@@ -216,7 +216,7 @@ def test_utc_in_timez(monkeypatch):
676e2f1
 
676e2f1
     class mock_datetime:
676e2f1
         @classmethod
676e2f1
-        def utcnow(cls):
676e2f1
+        def now(cls, _):
676e2f1
             return utcoffset8_local_time_in_naive_utc
676e2f1
 
676e2f1
     monkeypatch.setattr('datetime.datetime', mock_datetime)
676e2f1
-- 
676e2f1
2.43.0
676e2f1