diff options
| author | Eoghan Glynn <eglynn@redhat.com> | 2012-02-15 16:48:50 +0000 |
|---|---|---|
| committer | Eoghan Glynn <eglynn@redhat.com> | 2012-02-17 20:40:51 +0000 |
| commit | c30193fbf5c0f2e77b09a44803246732c10e211d (patch) | |
| tree | b2639a58da7afb0af8c85b1764fb5b7bf7c4115c /nova/utils.py | |
| parent | ee541f0a7e447adb000d7a4f5e9f98e2dce33362 (diff) | |
| download | nova-c30193fbf5c0f2e77b09a44803246732c10e211d.tar.gz nova-c30193fbf5c0f2e77b09a44803246732c10e211d.tar.xz nova-c30193fbf5c0f2e77b09a44803246732c10e211d.zip | |
Support non-UTC timestamps in changes-since filter
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
Diffstat (limited to 'nova/utils.py')
| -rw-r--r-- | nova/utils.py | 23 |
1 files changed, 19 insertions, 4 deletions
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'): |
