From 092dd09ddb77a28fa1e73d9464fe1b1b2caa8f29 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Tue, 17 Jun 2025 00:16:18 +0900 Subject: [PATCH] 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 --- cloudkitty/utils/__init__.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cloudkitty/utils/__init__.py b/cloudkitty/utils/__init__.py index f3170af6..3af69f6f 100644 --- a/cloudkitty/utils/__init__.py +++ b/cloudkitty/utils/__init__.py @@ -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):