From 787b6e3e50aca5f9c0a461d4654f12c21b1e3471 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 3 Jun 2015 15:50:11 +0200 Subject: [PATCH] timeutils: fix newer/older comparison with TZ aware datetime Blindly replacing the timezone information from a timestamp does not make it correctly UTC. It just strips information away, and therefore does not make any valid comparison. This fixes the comparison function by normalizing the timestamps to UTC before doing any comparison. Change-Id: I3a1b1eae497200ca951ccb003dbcc75fb75380fa --- oslo_utils/tests/test_timeutils.py | 10 ++++++++++ oslo_utils/timeutils.py | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/oslo_utils/tests/test_timeutils.py b/oslo_utils/tests/test_timeutils.py index 5777c4d5..ea0b5c1c 100644 --- a/oslo_utils/tests/test_timeutils.py +++ b/oslo_utils/tests/test_timeutils.py @@ -112,6 +112,11 @@ def test_is_older_than_aware(self): self._test_is_older_than(lambda x: x.replace( tzinfo=iso8601.iso8601.UTC)) + def test_is_older_than_aware_no_utc(self): + self._test_is_older_than(lambda x: x.replace( + tzinfo=iso8601.iso8601.FixedOffset(1, 0, 'foo')).replace( + hour=7)) + def _test_is_newer_than(self, fn): strptime = datetime.datetime.strptime with mock.patch('datetime.datetime') as datetime_mock: @@ -138,6 +143,11 @@ def test_is_newer_than_aware(self): self._test_is_newer_than(lambda x: x.replace( tzinfo=iso8601.iso8601.UTC)) + def test_is_newer_than_aware_no_utc(self): + self._test_is_newer_than(lambda x: x.replace( + tzinfo=iso8601.iso8601.FixedOffset(1, 0, 'foo')).replace( + hour=7)) + def test_set_time_override_using_default(self): now = timeutils.utcnow_ts() diff --git a/oslo_utils/timeutils.py b/oslo_utils/timeutils.py index 2b9e996d..17495e49 100644 --- a/oslo_utils/timeutils.py +++ b/oslo_utils/timeutils.py @@ -119,9 +119,9 @@ def normalize_time(timestamp): def is_older_than(before, seconds): """Return True if before is older than seconds.""" if isinstance(before, six.string_types): - before = parse_isotime(before).replace(tzinfo=None) - else: - before = before.replace(tzinfo=None) + before = parse_isotime(before) + + before = normalize_time(before) return utcnow() - before > datetime.timedelta(seconds=seconds) @@ -129,9 +129,9 @@ def is_older_than(before, seconds): def is_newer_than(after, seconds): """Return True if after is newer than seconds.""" if isinstance(after, six.string_types): - after = parse_isotime(after).replace(tzinfo=None) - else: - after = after.replace(tzinfo=None) + after = parse_isotime(after) + + after = normalize_time(after) return after - utcnow() > datetime.timedelta(seconds=seconds)