Replace deprecated datetime.utcfromtimestamp

It was deprecated in Python 3.12 due to datetime.fromtimestamp[1].

[1] https://docs.python.org/3/library/datetime.html#datetime.datetime.utcfromtimestamp

Change-Id: I939080262e095e4f151f804c8364b8fa6a99162a
This commit is contained in:
Takashi Kajinami
2025-06-17 00:16:18 +09:00
parent ab97bb5ff6
commit 092dd09ddb

View File

@@ -67,21 +67,24 @@ def iso8601_from_timestamp(timestamp, microsecond=False):
"""Returns an iso8601 formatted date from timestamp"""
# Python provides a similar instance method for datetime.datetime
# objects called isoformat() and utcfromtimestamp(). The format
# of the strings generated by isoformat() and utcfromtimestamp()
# objects called isoformat() and fromtimestamp(). The format
# of the strings generated by isoformat() and fromtimestamp()
# have a couple of problems:
# 1) The method iso8601_from_timestamp in oslo_utils is realized
# by isotime, the strings generated by isotime are used in
# tokens and other public APIs that we can't change without a
# deprecation period. The strings generated by isoformat are
# not the same format, so we can't just change to it.
# 2) The strings generated by isoformat() and utcfromtimestamp()
# 2) The strings generated by isoformat() and fromtimestamp()
# do not include the microseconds if the value happens to be 0.
# This will likely show up as random failures as parsers may be
# written to always expect microseconds, and it will parse
# correctly most of the time.
return isotime(datetime.datetime.utcfromtimestamp(timestamp), microsecond)
return isotime(
datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc),
microsecond
)
def dt2ts(orig_dt):
@@ -100,7 +103,8 @@ def ts2dt(timestamp):
"""timestamp to datetime format."""
if not isinstance(timestamp, float):
timestamp = float(timestamp)
return datetime.datetime.utcfromtimestamp(timestamp)
return datetime.datetime.fromtimestamp(
timestamp, tz=datetime.timezone.utc).replace(tzinfo=None)
def ts2iso(timestamp):