Utils: fix usage of iso8601_from_timestamp

The commit Ib384ae8130dcc6cbd47a837d11ca171ce02ef29e replaced
oslo_utils.timeutils.isotime, As oslo_utils.timeutils.isotime()
is deprecated in version '1.6'. So the workaround is to copy
the current implementation from oslo_utils.timeutils.isotime()
to utils.py. However, oslo_utils.timeutils.iso8601_from_timestamp
is also deprecated in version '1.6' and will be removed in a
future version.

So the workaround is to copy the current implementation from
oslo_utils.timeutils.iso8601_from_timestamp() to utils.py

For more informations:
https://docs.openstack.org/oslo.utils/latest/reference/timeutils.html

Change-Id: Iaea0465527329ac4253828fab88998de8eafc031
Partial-Bug: #1715325
This commit is contained in:
chenghuiyu 2017-09-07 09:42:13 +08:00 committed by Luka Peschke
parent 59b68c43c8
commit 07ba0dc644
1 changed files with 22 additions and 1 deletions

View File

@ -106,6 +106,27 @@ def isotime(at=None, subsecond=False):
return st
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()
# 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()
# 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)
def dt2ts(orig_dt):
"""Translate a datetime into a timestamp."""
return calendar.timegm(orig_dt.timetuple())
@ -129,7 +150,7 @@ def ts2iso(timestamp):
"""timestamp to is8601 format."""
if not isinstance(timestamp, float):
timestamp = float(timestamp)
return timeutils.iso8601_from_timestamp(timestamp)
return iso8601_from_timestamp(timestamp)
def dt2iso(orig_dt):