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
This commit is contained in:
parent
3b5d6b7375
commit
de017a2dff
@ -143,8 +143,8 @@ def utcnow_ts(microsecond=False):
|
|||||||
return timestamp
|
return timestamp
|
||||||
|
|
||||||
|
|
||||||
def utcnow():
|
def utcnow(with_timezone=False):
|
||||||
"""Overridable version of utils.utcnow.
|
"""Overridable version of utils.utcnow that can return a TZ-aware datetime.
|
||||||
|
|
||||||
See :py:class:`oslo_utils.fixture.TimeFixture`.
|
See :py:class:`oslo_utils.fixture.TimeFixture`.
|
||||||
|
|
||||||
@ -154,6 +154,8 @@ def utcnow():
|
|||||||
return utcnow.override_time.pop(0)
|
return utcnow.override_time.pop(0)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return utcnow.override_time
|
return utcnow.override_time
|
||||||
|
if with_timezone:
|
||||||
|
return datetime.datetime.now(tz=iso8601.iso8601.UTC)
|
||||||
return datetime.datetime.utcnow()
|
return datetime.datetime.utcnow()
|
||||||
|
|
||||||
|
|
||||||
|
@ -185,6 +185,9 @@ class TimeUtilsTest(test_base.BaseTestCase):
|
|||||||
|
|
||||||
self.assertTrue(timeutils.utcnow())
|
self.assertTrue(timeutils.utcnow())
|
||||||
|
|
||||||
|
self.assertEqual(timeutils.utcnow(True).tzinfo,
|
||||||
|
iso8601.iso8601.UTC)
|
||||||
|
|
||||||
def test_advance_time_delta(self):
|
def test_advance_time_delta(self):
|
||||||
timeutils.set_time_override(self.one_minute_before)
|
timeutils.set_time_override(self.one_minute_before)
|
||||||
timeutils.advance_time_delta(datetime.timedelta(seconds=60))
|
timeutils.advance_time_delta(datetime.timedelta(seconds=60))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user