From 568cd86eee052182903d62ed639225e350cf9a52 Mon Sep 17 00:00:00 2001
From: Takashi Natsume <takanattie@gmail.com>
Date: Wed, 2 Oct 2024 23:47:55 +0900
Subject: [PATCH] Replace deprecated datetime.utcfromtimestamp()

The datetime.utcfromtimestamp() is deprecated in Python 3.12.
Replace datetime.utcfromtimestamp() with datetime.fromtimestamp().

Change-Id: I495e783c3a7c37e1b328c5d0920553d0d1eb9352
Signed-off-by: Takashi Natsume <takanattie@gmail.com>
---
 zaqar/storage/mongodb/claims.py            | 16 ++++++++++------
 zaqar/storage/mongodb/messages.py          |  6 ++++--
 zaqar/storage/mongodb/subscriptions.py     |  6 ++++--
 zaqar/storage/mongodb/topic_messages.py    |  6 ++++--
 zaqar/storage/mongodb/utils.py             |  8 +++++---
 zaqar/storage/redis/models.py              |  5 +++--
 zaqar/storage/swift/messages.py            | 10 ++++++----
 zaqar/transport/validation.py              |  3 ++-
 zaqar/transport/wsgi/v2_0/subscriptions.py |  6 ++++--
 9 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/zaqar/storage/mongodb/claims.py b/zaqar/storage/mongodb/claims.py
index 9ec996db1..d9aea1532 100644
--- a/zaqar/storage/mongodb/claims.py
+++ b/zaqar/storage/mongodb/claims.py
@@ -142,11 +142,13 @@ class ClaimController(storage.Claim):
 
         now = timeutils.utcnow_ts()
         claim_expires = now + ttl
-        claim_expires_dt = datetime.datetime.utcfromtimestamp(claim_expires)
+        claim_expires_dt = datetime.datetime.fromtimestamp(
+            claim_expires, tz=datetime.timezone.utc).replace(tzinfo=None)
 
         message_ttl = ttl + grace
-        message_expiration = datetime.datetime.utcfromtimestamp(
-            claim_expires + grace)
+        message_expiration = datetime.datetime.fromtimestamp(
+            claim_expires + grace, tz=datetime.timezone.utc).replace(
+                tzinfo=None)
 
         meta = {
             'id': oid,
@@ -293,10 +295,12 @@ class ClaimController(storage.Claim):
         grace = metadata['grace']
         ttl = metadata['ttl']
         claim_expires = now + ttl
-        claim_expires_dt = datetime.datetime.utcfromtimestamp(claim_expires)
+        claim_expires_dt = datetime.datetime.fromtimestamp(
+            claim_expires, tz=datetime.timezone.utc).replace(tzinfo=None)
         message_ttl = ttl + grace
-        message_expires = datetime.datetime.utcfromtimestamp(
-            claim_expires + grace)
+        message_expires = datetime.datetime.fromtimestamp(
+            claim_expires + grace, tz=datetime.timezone.utc).replace(
+                tzinfo=None)
 
         msg_ctrl = self.driver.message_controller
         claimed = msg_ctrl._claimed(queue, cid, expires=now,
diff --git a/zaqar/storage/mongodb/messages.py b/zaqar/storage/mongodb/messages.py
index 2084ea4be..0fdfa04b4 100644
--- a/zaqar/storage/mongodb/messages.py
+++ b/zaqar/storage/mongodb/messages.py
@@ -668,7 +668,8 @@ class MessageController(storage.Message):
         # is an upsert.
         self._get_counter(queue_name, project)
         now = timeutils.utcnow_ts()
-        now_dt = datetime.datetime.utcfromtimestamp(now)
+        now_dt = datetime.datetime.fromtimestamp(
+            now, tz=datetime.timezone.utc).replace(tzinfo=None)
         collection = self._collection(queue_name, project)
 
         messages = list(messages)
@@ -854,7 +855,8 @@ class FIFOMessageController(MessageController):
         # is an upsert.
         self._get_counter(queue_name, project)
         now = timeutils.utcnow_ts()
-        now_dt = datetime.datetime.utcfromtimestamp(now)
+        now_dt = datetime.datetime.fromtimestamp(
+            now, tz=datetime.timezone.utc).replace(tzinfo=None)
         collection = self._collection(queue_name, project)
 
         # Set the next basis marker for the first attempt.
diff --git a/zaqar/storage/mongodb/subscriptions.py b/zaqar/storage/mongodb/subscriptions.py
index ee345d7a7..921443846 100644
--- a/zaqar/storage/mongodb/subscriptions.py
+++ b/zaqar/storage/mongodb/subscriptions.py
@@ -104,7 +104,8 @@ class SubscriptionController(base.Subscription):
     def create(self, queue, subscriber, ttl, options, project=None):
         source = queue
         now = timeutils.utcnow_ts()
-        now_dt = datetime.datetime.utcfromtimestamp(now)
+        now_dt = datetime.datetime.fromtimestamp(
+            now, tz=datetime.timezone.utc).replace(tzinfo=None)
         expires = now_dt + datetime.timedelta(seconds=ttl)
         confirmed = False
 
@@ -138,7 +139,8 @@ class SubscriptionController(base.Subscription):
         new_ttl = fields.get('t')
         if new_ttl is not None:
             now = timeutils.utcnow_ts()
-            now_dt = datetime.datetime.utcfromtimestamp(now)
+            now_dt = datetime.datetime.fromtimestamp(
+                now, tz=datetime.timezone.utc).replace(tzinfo=None)
             expires = now_dt + datetime.timedelta(seconds=new_ttl)
             fields['e'] = expires
 
diff --git a/zaqar/storage/mongodb/topic_messages.py b/zaqar/storage/mongodb/topic_messages.py
index af9023010..fcfa37b73 100644
--- a/zaqar/storage/mongodb/topic_messages.py
+++ b/zaqar/storage/mongodb/topic_messages.py
@@ -565,7 +565,8 @@ class MessageController(storage.Message):
         # is an upsert.
         self._get_counter(topic_name, project)
         now = timeutils.utcnow_ts()
-        now_dt = datetime.datetime.utcfromtimestamp(now)
+        now_dt = datetime.datetime.fromtimestamp(
+            now, tz=datetime.timezone.utc).replace(tzinfo=None)
         collection = self._collection(topic_name, project)
 
         messages = list(messages)
@@ -753,7 +754,8 @@ class FIFOMessageController(MessageController):
         # is an upsert.
         self._get_counter(topic_name, project)
         now = timeutils.utcnow_ts()
-        now_dt = datetime.datetime.utcfromtimestamp(now)
+        now_dt = datetime.datetime.fromtimestamp(
+            now, tz=datetime.timezone.utc).replace(tzinfo=None)
         collection = self._collection(topic_name, project)
 
         # Set the next basis marker for the first attempt.
diff --git a/zaqar/storage/mongodb/utils.py b/zaqar/storage/mongodb/utils.py
index b3626953b..a1adfdfa6 100644
--- a/zaqar/storage/mongodb/utils.py
+++ b/zaqar/storage/mongodb/utils.py
@@ -31,7 +31,8 @@ from zaqar.storage import errors as storage_errors
 
 # BSON ObjectId gives TZ-aware datetime, so we generate a
 # TZ-aware UNIX epoch for convenience.
-EPOCH = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=tz_util.utc)
+EPOCH = datetime.datetime.fromtimestamp(
+    0, tz=datetime.timezone.utc).replace(tzinfo=tz_util.utc)
 
 # NOTE(cpp-cabrera): the authoritative form of project/queue keys.
 PROJ_QUEUE_KEY = 'p_q'
@@ -133,8 +134,9 @@ def stat_message(message, now):
     msg_id = message['id']
     created = oid_ts(to_oid(msg_id))
     age = now - created
-    created_iso = datetime.datetime.utcfromtimestamp(created).strftime(
-        '%Y-%m-%dT%H:%M:%SZ')
+    created_iso = datetime.datetime.fromtimestamp(
+        created, tz=datetime.timezone.utc).replace(tzinfo=None).strftime(
+            '%Y-%m-%dT%H:%M:%SZ')
     return {
         'id': msg_id,
         'age': int(age),
diff --git a/zaqar/storage/redis/models.py b/zaqar/storage/redis/models.py
index 774e88a3a..f0fe4e4b5 100644
--- a/zaqar/storage/redis/models.py
+++ b/zaqar/storage/redis/models.py
@@ -237,8 +237,9 @@ class Message(MessageEnvelope):
         }
 
         if include_created:
-            created_iso = datetime.datetime.utcfromtimestamp(
-                self.created).strftime('%Y-%m-%dT%H:%M:%SZ')
+            created_iso = datetime.datetime.fromtimestamp(
+                self.created, tz=datetime.timezone.utc).replace(
+                    tzinfo=None).strftime('%Y-%m-%dT%H:%M:%SZ')
             basic_msg['created'] = created_iso
         if self.checksum:
             basic_msg['checksum'] = self.checksum
diff --git a/zaqar/storage/swift/messages.py b/zaqar/storage/swift/messages.py
index a40e1de31..183a30aaf 100644
--- a/zaqar/storage/swift/messages.py
+++ b/zaqar/storage/swift/messages.py
@@ -352,8 +352,9 @@ class MessageQueueHandler(object):
                     raise
             else:
                 created = float(headers['x-timestamp'])
-                created_iso = datetime.datetime.utcfromtimestamp(
-                    created).strftime('%Y-%m-%dT%H:%M:%SZ')
+                created_iso = datetime.datetime.fromtimestamp(
+                    created, tz=datetime.timezone.utc).replace(
+                        tzinfo=None).strftime('%Y-%m-%dT%H:%M:%SZ')
                 newest = {
                     'id': obj['name'],
                     'age': now - created,
@@ -442,8 +443,9 @@ class MessageTopicHandler(object):
                     raise
             else:
                 created = float(headers['x-timestamp'])
-                created_iso = datetime.datetime.utcfromtimestamp(
-                    created).strftime('%Y-%m-%dT%H:%M:%SZ')
+                created_iso = datetime.datetime.fromtimestamp(
+                    created, tz=datetime.timezone.utc).replace(
+                        tzinfo=None).strftime('%Y-%m-%dT%H:%M:%SZ')
                 newest = {
                     'id': obj['name'],
                     'age': now - created,
diff --git a/zaqar/transport/validation.py b/zaqar/transport/validation.py
index ce3ed28ad..7530c9990 100644
--- a/zaqar/transport/validation.py
+++ b/zaqar/transport/validation.py
@@ -598,7 +598,8 @@ class Validator(object):
             # NOTE(flwang): By this change, technically, user can set a very
             # big TTL so as to get a very long subscription.
             now = timeutils.utcnow_ts()
-            now_dt = datetime.datetime.utcfromtimestamp(now)
+            now_dt = datetime.datetime.fromtimestamp(
+                now, tz=datetime.timezone.utc).replace(tzinfo=None)
             msg = _(u'The TTL seconds for a subscription plus current time'
                     ' must be less than {0}.')
             try:
diff --git a/zaqar/transport/wsgi/v2_0/subscriptions.py b/zaqar/transport/wsgi/v2_0/subscriptions.py
index e5619d90f..45b97a819 100644
--- a/zaqar/transport/wsgi/v2_0/subscriptions.py
+++ b/zaqar/transport/wsgi/v2_0/subscriptions.py
@@ -206,7 +206,8 @@ class CollectionResource(object):
             raise wsgi_errors.HTTPServiceUnavailable(description)
 
         now = timeutils.utcnow_ts()
-        now_dt = datetime.datetime.utcfromtimestamp(now)
+        now_dt = datetime.datetime.fromtimestamp(
+            now, tz=datetime.timezone.utc).replace(tzinfo=None)
         expires = now_dt + datetime.timedelta(seconds=ttl)
         api_version = req.path.split('/')[1]
         if created:
@@ -272,7 +273,8 @@ class ConfirmResource(object):
                                                   confirmed=confirmed)
             if confirmed is False:
                 now = timeutils.utcnow_ts()
-                now_dt = datetime.datetime.utcfromtimestamp(now)
+                now_dt = datetime.datetime.fromtimestamp(
+                    now, tz=datetime.timezone.utc).replace(tzinfo=None)
                 ttl = self._conf.transport.default_subscription_ttl
                 expires = now_dt + datetime.timedelta(seconds=ttl)
                 api_version = req.path.split('/')[1]