From de017a2dfff41890051a9e9756e1777b21ef1c32 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 13 May 2015 11:41:25 +0200 Subject: [PATCH] timeutils: utcnow() can return a value with a timezone utcnow() used to return a non-timezone-aware timezone, which is terrible as there is then no way to know if it's a local time object or a UTC time zone object. This patch changes utcnow() so it can carry that information in the datetime object. Change-Id: Ib098ef16063d97e37ff2b9a0fc934f67fd9ed5f3 --- oslo_utils/timeutils.py | 6 ++++-- tests/test_timeutils.py | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/oslo_utils/timeutils.py b/oslo_utils/timeutils.py index 94b728da..9e15c120 100644 --- a/oslo_utils/timeutils.py +++ b/oslo_utils/timeutils.py @@ -143,8 +143,8 @@ def utcnow_ts(microsecond=False): return timestamp -def utcnow(): - """Overridable version of utils.utcnow. +def utcnow(with_timezone=False): + """Overridable version of utils.utcnow that can return a TZ-aware datetime. See :py:class:`oslo_utils.fixture.TimeFixture`. @@ -154,6 +154,8 @@ def utcnow(): return utcnow.override_time.pop(0) except AttributeError: return utcnow.override_time + if with_timezone: + return datetime.datetime.now(tz=iso8601.iso8601.UTC) return datetime.datetime.utcnow() diff --git a/tests/test_timeutils.py b/tests/test_timeutils.py index fdfab2cc..bc13a31c 100644 --- a/tests/test_timeutils.py +++ b/tests/test_timeutils.py @@ -185,6 +185,9 @@ class TimeUtilsTest(test_base.BaseTestCase): self.assertTrue(timeutils.utcnow()) + self.assertEqual(timeutils.utcnow(True).tzinfo, + iso8601.iso8601.UTC) + def test_advance_time_delta(self): timeutils.set_time_override(self.one_minute_before) timeutils.advance_time_delta(datetime.timedelta(seconds=60))