From c30193fbf5c0f2e77b09a44803246732c10e211d Mon Sep 17 00:00:00 2001 From: Eoghan Glynn Date: Wed, 15 Feb 2012 16:48:50 +0000 Subject: Support non-UTC timestamps in changes-since filter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes nova aspect of lp 837464 Prevously only Zulu time was supported in the changes-since filter, i.e. timestamps formatted as %Y-%m-%dT%H:%M:%SZ We now support arbitrary timezones, with the offset from UTC expressed via the ISO 8601 ±hh:mm notation. Microsecond accurracy is also optionally supported in timestamps. Notes: - nova.utils.parse_isotime(), isotime() & normalized_time() are prime candidates for promotion to openstack-common, as these methods were duplicated from my corresponding glance patch: https://review.openstack.org/#change,4198 - this patch introduces a new dependency on python-iso8601, which has already been packaged for Fedora, EPEL and Ubuntu/Debian. Change-Id: I89b45f4f3d910606c578d927420f78cea94f4e3b --- nova/utils.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 96f0a57f0..85f39dbe0 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -44,6 +44,7 @@ from eventlet import event from eventlet import greenthread from eventlet import semaphore from eventlet.green import subprocess +import iso8601 import netaddr from nova import exception @@ -53,7 +54,7 @@ from nova.openstack.common import cfg LOG = logging.getLogger(__name__) -ISO_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" +ISO_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S" PERFECT_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f" FLAGS = flags.FLAGS @@ -534,13 +535,27 @@ def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT): def isotime(at=None): - """Returns iso formatted utcnow.""" - return strtime(at, ISO_TIME_FORMAT) + """Stringify time in ISO 8601 format""" + if not at: + at = datetime.datetime.utcnow() + str = at.strftime(ISO_TIME_FORMAT) + tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC' + str += ('Z' if tz == 'UTC' else tz) + return str def parse_isotime(timestr): """Turn an iso formatted time back into a datetime.""" - return parse_strtime(timestr, ISO_TIME_FORMAT) + try: + return iso8601.parse_date(timestr) + except (iso8601.ParseError, TypeError) as e: + raise ValueError(e.message) + + +def normalize_time(timestamp): + """Normalize time in arbitrary timezone to UTC""" + offset = timestamp.utcoffset() + return timestamp.replace(tzinfo=None) - offset if offset else timestamp def parse_mailmap(mailmap='.mailmap'): -- cgit