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 /openstack | |
| 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>
Diffstat (limited to 'openstack')
| -rw-r--r-- | openstack/common/timeutils.py | 6 |
1 files changed, 4 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): |
