summaryrefslogtreecommitdiff
path: root/keystoneclient/utils.py
diff options
context:
space:
mode:
authorBrant Knudson <bknudson@us.ibm.com>2015-08-06 09:37:43 -0500
committerBrant Knudson <bknudson@us.ibm.com>2015-08-06 09:37:43 -0500
commit4e498a54d0034b2ce5c87130f080ff580d241600 (patch)
treed21d11091373e80bf1a4f70ecf1ee54ff82e0ff8 /keystoneclient/utils.py
parentd5c5423d6de3710e3480e47062333b33e8de0713 (diff)
parenteae8e83f5a7a170b98ef2d74a4ffd9eac7cc47ba (diff)
downloadpython-keystoneclient-feature/keystoneauth_integration.tar.gz
Merge remote-tracking branch 'origin/master' into merge-branchfeature/keystoneauth_integration
Conflicts: keystoneclient/exceptions.py keystoneclient/fixture/discovery.py keystoneclient/fixture/v2.py keystoneclient/fixture/v3.py keystoneclient/middleware/auth_token.py keystoneclient/middleware/s3_token.py keystoneclient/tests/unit/test_auth_token_middleware.py keystoneclient/tests/unit/test_memcache_crypt.py keystoneclient/tests/unit/test_s3_token_middleware.py requirements.txt test-requirements.txt Change-Id: Ib51acebaac7966bf37c1562fa15b9061df6a7aa5
Diffstat (limited to 'keystoneclient/utils.py')
-rw-r--r--keystoneclient/utils.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/keystoneclient/utils.py b/keystoneclient/utils.py
index 300ca54..5017431 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,37 @@ 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
+
+
+def strtime(at=None):
+ at = at or timeutils.utcnow()
+ return at.strftime(timeutils.PERFECT_TIME_FORMAT)