summaryrefslogtreecommitdiff
path: root/keystoneclient/utils.py
diff options
context:
space:
mode:
authorBrant Knudson <bknudson@us.ibm.com>2015-06-07 11:20:44 -0500
committerBrant Knudson <bknudson@us.ibm.com>2015-06-30 12:58:55 -0500
commit97c2c690d8983fd1d929a4eae3b0d62bbcb2cf6a (patch)
tree1d21dbde7b1e77c839c749e3a658d461332385d7 /keystoneclient/utils.py
parentaa9e413a6def86c58b19495ac6ac439e61d04a8b (diff)
downloadpython-keystoneclient-97c2c690d8983fd1d929a4eae3b0d62bbcb2cf6a.tar.gz
Switch from deprecated isotime
oslo_utils.timeutils.isotime() is deprecated as of 1.6 so we need to stop using it. The deprecation 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. 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 keystone.common.utils.isotime(). Change-Id: I34b12b96de3ea21beaf935ed8a9f6bae2fe0d0bc Closes-Bug: 1461251
Diffstat (limited to 'keystoneclient/utils.py')
-rw-r--r--keystoneclient/utils.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/keystoneclient/utils.py b/keystoneclient/utils.py
index 300ca54..3565565 100644
--- a/keystoneclient/utils.py
+++ b/keystoneclient/utils.py
@@ -18,6 +18,7 @@ import logging
import sys
from oslo_utils import encodeutils
+from oslo_utils import timeutils
import prettytable
import six
@@ -336,3 +337,32 @@ class positional(object):
return func(*args, **kwargs)
return inner
+
+
+_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