Skip to content

Commit

Permalink
timeutils: fix newer/older comparison with TZ aware datetime
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jd committed Jun 3, 2015
1 parent 421b562 commit 787b6e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 10 additions & 0 deletions oslo_utils/tests/test_timeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

Expand Down
12 changes: 6 additions & 6 deletions oslo_utils/timeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ 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)


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)

Expand Down

0 comments on commit 787b6e3

Please sign in to comment.