From 2cb8e4557b05f92fbd9f56b7a6a6d4f35c8a883a Mon Sep 17 00:00:00 2001 From: Guang Yee Date: Thu, 28 Feb 2013 14:51:24 -0800 Subject: support ISO 8601 micro-second precision Bug 1134802 Change-Id: I9cc3c9d9324314d293f01f047882eb6be06e02dd --- openstack/common/timeutils.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'openstack') diff --git a/openstack/common/timeutils.py b/openstack/common/timeutils.py index e2c2740..8e40660 100644 --- a/openstack/common/timeutils.py +++ b/openstack/common/timeutils.py @@ -25,18 +25,22 @@ import datetime import iso8601 -TIME_FORMAT = "%Y-%m-%dT%H:%M:%S" -PERFECT_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f" +# ISO 8601 extended time format with microseconds +_ISO8601_TIME_FORMAT_SUBSECOND = '%Y-%m-%dT%H:%M:%S.%f' +_ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S' +PERFECT_TIME_FORMAT = _ISO8601_TIME_FORMAT_SUBSECOND -def isotime(at=None): +def isotime(at=None, subsecond=False): """Stringify time in ISO 8601 format""" if not at: at = utcnow() - str = at.strftime(TIME_FORMAT) + st = at.strftime(_ISO8601_TIME_FORMAT + if not subsecond + else _ISO8601_TIME_FORMAT_SUBSECOND) tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC' - str += ('Z' if tz == 'UTC' else tz) - return str + st += ('Z' if tz == 'UTC' else tz) + return st def parse_isotime(timestr): -- cgit