diff options
| author | Jenkins <jenkins@review.openstack.org> | 2012-04-11 06:21:20 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2012-04-11 06:21:20 +0000 |
| commit | c2bcf4bd9420e413bb2be7c4b17e8c7686eeea1e (patch) | |
| tree | 61369d9fa10d5965e05e8a4b4475901f137d5932 /openstack | |
| parent | 3b6b2c5e8e6d67446127c9f99d7a5104843cb472 (diff) | |
| parent | 1dde301866bcc5c40fd41aaaf82cf8542c69cb2c (diff) | |
| download | oslo-c2bcf4bd9420e413bb2be7c4b17e8c7686eeea1e.tar.gz oslo-c2bcf4bd9420e413bb2be7c4b17e8c7686eeea1e.tar.xz oslo-c2bcf4bd9420e413bb2be7c4b17e8c7686eeea1e.zip | |
Merge "Create openstack.common.timeutils."
Diffstat (limited to 'openstack')
| -rw-r--r-- | openstack/common/timeutils.py | 73 | ||||
| -rw-r--r-- | openstack/common/utils.py | 49 |
2 files changed, 73 insertions, 49 deletions
diff --git a/openstack/common/timeutils.py b/openstack/common/timeutils.py new file mode 100644 index 0000000..c536cb2 --- /dev/null +++ b/openstack/common/timeutils.py @@ -0,0 +1,73 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Time related utilities and helper functions. +""" + +import datetime + +import iso8601 + + +TIME_FORMAT = "%Y-%m-%dT%H:%M:%S" + + +def isotime(at=None): + """Stringify time in ISO 8601 format""" + if not at: + at = datetime.datetime.utcnow() + str = at.strftime(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): + """Parse time from ISO 8601 format""" + try: + return iso8601.parse_date(timestr) + except iso8601.ParseError as e: + raise ValueError(e.message) + except 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 utcnow(): + """Overridable version of utils.utcnow.""" + if utcnow.override_time: + return utcnow.override_time + return datetime.datetime.utcnow() + + +utcnow.override_time = None + + +def set_time_override(override_time=datetime.datetime.utcnow()): + """Override utils.utcnow to return a constant time.""" + utcnow.override_time = override_time + + +def clear_time_override(): + """Remove the overridden time.""" + utcnow.override_time = None diff --git a/openstack/common/utils.py b/openstack/common/utils.py index 0191036..d3d01fa 100644 --- a/openstack/common/utils.py +++ b/openstack/common/utils.py @@ -19,7 +19,6 @@ System-level utilities and helper functions. """ -import datetime import logging import os import random @@ -28,12 +27,10 @@ import sys from eventlet import greenthread from eventlet.green import subprocess -import iso8601 from openstack.common import exception -TIME_FORMAT = "%Y-%m-%dT%H:%M:%S" LOG = logging.getLogger(__name__) @@ -163,52 +160,6 @@ def import_object(import_str): return import_class(import_str) -def isotime(at=None): - """Stringify time in ISO 8601 format""" - if not at: - at = datetime.datetime.utcnow() - str = at.strftime(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): - """Parse time from ISO 8601 format""" - try: - return iso8601.parse_date(timestr) - except iso8601.ParseError as e: - raise ValueError(e.message) - except 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 utcnow(): - """Overridable version of utils.utcnow.""" - if utcnow.override_time: - return utcnow.override_time - return datetime.datetime.utcnow() - - -utcnow.override_time = None - - -def set_time_override(override_time=datetime.datetime.utcnow()): - """Override utils.utcnow to return a constant time.""" - utcnow.override_time = override_time - - -def clear_time_override(): - """Remove the overridden time.""" - utcnow.override_time = None - - def auth_str_equal(provided, known): """Constant-time string comparison. |
