diff options
| author | Yunhong, Jiang <yunhong.jiang@intel.com> | 2012-09-11 16:04:03 +0800 |
|---|---|---|
| committer | Yunhong, Jiang <yunhong.jiang@intel.com> | 2012-09-13 17:50:34 +0800 |
| commit | b81938dc2fec8f6e15b0b3dcb8eabc276e647af4 (patch) | |
| tree | 388a0c8a56bb74104680cc1e8479420194124c6f | |
| parent | 2265258dad5c6a1e535336867d9cea5c7a2355d3 (diff) | |
| download | oslo-b81938dc2fec8f6e15b0b3dcb8eabc276e647af4.tar.gz oslo-b81938dc2fec8f6e15b0b3dcb8eabc276e647af4.tar.xz oslo-b81938dc2fec8f6e15b0b3dcb8eabc276e647af4.zip | |
Normalize_time() always return naive object
Currently the timeutils.normalize_time() returns naive datetime
object when parameter timestamp is an naive object or a non-UTC
aware datetime object, and returns aware datetime object if
parameter timestamp is a UTC aware datetime object.
The basic problem here is that utcoffset() of None means the object is naive
but a utcoffset() of zero means that the object is aware but represents
UTC time.
This is fragile implementation and will trigger potential issue, because
aware/native datetime is not interoperatable. For example,
"timeutils.utcnow() > timeutils.normalize_time(m)" will success at most
time, while trigger TypeError if m is a UTC aware object.
We want to normalize all objects into naive objects.
Fixes bug 1048636
Change-Id: I4a09246fa8f0dd63ca54362b877aa825d9b79772
Signed-off-by: Yunhong, Jiang <yunhong.jiang@intel.com>
| -rw-r--r-- | openstack/common/timeutils.py | 6 | ||||
| -rw-r--r-- | tests/unit/test_timeutils.py | 20 |
2 files changed, 24 insertions, 2 deletions
diff --git a/openstack/common/timeutils.py b/openstack/common/timeutils.py index 9901a4c..93b34fc 100644 --- a/openstack/common/timeutils.py +++ b/openstack/common/timeutils.py @@ -62,9 +62,11 @@ def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT): def normalize_time(timestamp): - """Normalize time in arbitrary timezone to UTC""" + """Normalize time in arbitrary timezone to UTC naive object""" offset = timestamp.utcoffset() - return timestamp.replace(tzinfo=None) - offset if offset else timestamp + if offset is None: + return timestamp + return timestamp.replace(tzinfo=None) - offset def is_older_than(before, seconds): diff --git a/tests/unit/test_timeutils.py b/tests/unit/test_timeutils.py index 129609e..7e6ae72 100644 --- a/tests/unit/test_timeutils.py +++ b/tests/unit/test_timeutils.py @@ -214,3 +214,23 @@ class TestIso8601Time(unittest.TestCase): west = timeutils.parse_isotime(str) normed = timeutils.normalize_time(west) self._instaneous(normed, 2012, 2, 13, 23, 53, 07, 0) + + def test_normalize_aware_to_naive(self): + dt = datetime.datetime(2011, 2, 14, 20, 53, 07) + str = '2011-02-14T20:53:07+21:00' + aware = timeutils.parse_isotime(str) + naive = timeutils.normalize_time(aware) + self.assertTrue(naive < dt) + + def test_normalize_zulu_aware_to_naive(self): + dt = datetime.datetime(2011, 2, 14, 20, 53, 07) + str = '2011-02-14T19:53:07Z' + aware = timeutils.parse_isotime(str) + naive = timeutils.normalize_time(aware) + self.assertTrue(naive < dt) + + def test_normalize_naive(self): + dt = datetime.datetime(2011, 2, 14, 20, 53, 07) + dtn = datetime.datetime(2011, 2, 14, 19, 53, 07) + naive = timeutils.normalize_time(dtn) + self.assertTrue(naive < dt) |
