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>
This commit is contained in:
Takashi Natsume 2024-10-02 23:47:55 +09:00
parent b438cffeb5
commit 568cd86eee
9 changed files with 42 additions and 24 deletions

View File

@ -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,

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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,7 +134,8 @@ 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(
created_iso = datetime.datetime.fromtimestamp(
created, tz=datetime.timezone.utc).replace(tzinfo=None).strftime(
'%Y-%m-%dT%H:%M:%SZ')
return {
'id': msg_id,

View File

@ -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

View File

@ -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,

View File

@ -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:

View File

@ -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]