From b81938dc2fec8f6e15b0b3dcb8eabc276e647af4 Mon Sep 17 00:00:00 2001 From: "Yunhong, Jiang" Date: Tue, 11 Sep 2012 16:04:03 +0800 Subject: 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 --- openstack/common/timeutils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'openstack') 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): -- cgit