Replace usage of removed timeutils.isotime()

timeutils.isotime() is no longer available in oslo_utils.

The error message says to use datetime.datetime.isoformat()
instead, but the format of the string generated by isoformat isn't
the same as the format of the string generated by isotime.

This change adds a local isotime() helper copying the
implementation of oslo_utils.timeutils.isotime() to
sysinv.objects.utils.isotime().

Test Plan:
PASS  AIO-SX

Story: 2011360

Change-Id: Ibad1cb3a49d39b451d999f11f37ca87347065589
Signed-off-by: sharang bhardwaj <sharang.bhardwaj@windriver.com>
This commit is contained in:
sharang bhardwaj
2026-03-03 01:51:14 -05:00
committed by Sai Lakshmi Teja Vanka
parent 6042024831
commit 768879121a
2 changed files with 54 additions and 4 deletions
@@ -0,0 +1,10 @@
#
# Copyright (c) 2026 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# OS type
OS_RELEASE_FILE = '/etc/os-release'
OS_DEBIAN_BULLSEYE = 'bullseye'
OS_DEBIAN_TRIXIE = 'trixie'
@@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
#
# Copyright (c) 2013-2014 Wind River Systems, Inc.
# Copyright (c) 2013-2014,2026 Wind River Systems, Inc.
#
@@ -24,9 +24,29 @@ import iso8601
import netaddr
import six
from functools import lru_cache
from sm_api.common import constants
from sm_api.openstack.common import timeutils
@lru_cache(maxsize=None)
def get_debian_codename():
"""Returns the Debian codename, e.g., 'bullseye', 'trixie'."""
try:
with open(constants.OS_RELEASE_FILE) as f:
for line in f:
if line.startswith("VERSION_CODENAME="):
return line.strip().split("=")[1]
except FileNotFoundError:
return None
return None
def is_debian_bullseye():
"""Returns True if the current OS is Debian Bullseye."""
return get_debian_codename() == constants.OS_DEBIAN_BULLSEYE
def datetime_or_none(dt):
"""Validate a datetime or None value."""
if dt is None:
@@ -103,14 +123,34 @@ def nested_object_or_none(objclass):
return validator
_ISO8601_TIME_FORMAT_SUBSECOND = '%Y-%m-%dT%H:%M:%S.%f'
_ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
def isotime(at=None, subsecond=False):
"""Stringify time in ISO 8601 format."""
if not at:
at = timeutils.utcnow()
st = at.strftime(_ISO8601_TIME_FORMAT
if not subsecond
else _ISO8601_TIME_FORMAT_SUBSECOND)
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
st += ('Z' if (tz == 'UTC' or tz == 'UTC+00:00') else tz)
return st
def dt_serializer(name):
"""Return a datetime serializer for a named attribute."""
def serializer(self, name=name):
if getattr(self, name) is not None:
return timeutils.isotime(getattr(self, name))
else:
value = getattr(self, name)
if value is None:
return None
if is_debian_bullseye():
return timeutils.isotime(value)
return isotime(at=value, subsecond=True)
return serializer