Replace oslo_utils.timeutils.isotime

Function 'oslo_utils.timeutils.isotime()' is deprecated in version '1.6'
and will be removed in a future version. We cant't use
datetime.datetime.isoformat() instead. Because the format of the string
generated by isoformat isn't the same as the format of the string
generated by isotime. The string is used in tokens and other public
APIs and we can't change it without potentially breaking clients.

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

For more informations:
http://docs.openstack.org/developer/oslo.utils/api/timeutils.html#oslo_utils.timeutils.isotime

Change-Id: Ib384ae8130dcc6cbd47a837d11ca171ce02ef29e
Closes-Bug: #1461251
This commit is contained in:
Luong Anh Tuan 2016-11-07 09:01:58 +07:00 committed by Tuan Luong-Anh
parent 981b256b61
commit 932b5feb7c
1 changed files with 30 additions and 1 deletions

View File

@ -30,6 +30,35 @@ from six import moves
from stevedore import extension
_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."""
# Python provides a similar instance method for datetime.datetime objects
# called isoformat(). The format of the strings generated by isoformat()
# have a couple of problems:
# 1) 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 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.
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' else tz)
return st
def dt2ts(orig_dt):
"""Translate a datetime into a timestamp."""
return calendar.timegm(orig_dt.timetuple())
@ -58,7 +87,7 @@ def ts2iso(timestamp):
def dt2iso(orig_dt):
"""datetime to is8601 format."""
return timeutils.isotime(orig_dt)
return isotime(orig_dt)
def utcnow():