Merge "timeutils: make marshall timezone aware"

This commit is contained in:
Jenkins 2015-05-14 11:27:02 +00:00 committed by Gerrit Code Review
commit 63d350b93e
2 changed files with 30 additions and 14 deletions

View File

@ -191,6 +191,16 @@ class TimeUtilsTest(test_base.BaseTestCase):
backagain = timeutils.unmarshall_time(binary)
self.assertEqual(now, backagain)
def test_marshall_time_with_tz(self):
now = timeutils.utcnow()
now = now.replace(tzinfo=iso8601.iso8601.UTC)
binary = timeutils.marshall_now(now)
self.assertEqual("UTC", binary['tzname'])
backagain = timeutils.unmarshall_time(binary)
self.assertEqual(now, backagain)
self.assertIsNotNone(backagain.tzinfo)
self.assertEqual(now.utcoffset(), backagain.utcoffset())
def test_unmarshall_time_leap_second(self):
leap_dict = dict(day=30, month=6, year=2015,
hour=23, minute=59,

View File

@ -22,6 +22,7 @@ import datetime
import time
import iso8601
from pytz import timezone
import six
from oslo_utils import reflection
@ -200,15 +201,15 @@ def clear_time_override():
def marshall_now(now=None):
"""Make an rpc-safe datetime with microseconds.
Note: tzinfo is stripped, but not required for relative times.
"""
"""Make an rpc-safe datetime with microseconds."""
if not now:
now = utcnow()
return dict(day=now.day, month=now.month, year=now.year, hour=now.hour,
minute=now.minute, second=now.second,
microsecond=now.microsecond)
d = dict(day=now.day, month=now.month, year=now.year, hour=now.hour,
minute=now.minute, second=now.second,
microsecond=now.microsecond)
if now.tzinfo:
d['tzname'] = now.tzinfo.tzname(None)
return d
def unmarshall_time(tyme):
@ -218,13 +219,18 @@ def unmarshall_time(tyme):
# so the best thing we can do for now is dropping them
# http://bugs.python.org/issue23574
second = min(tyme['second'], _MAX_DATETIME_SEC)
return datetime.datetime(day=tyme['day'],
month=tyme['month'],
year=tyme['year'],
hour=tyme['hour'],
minute=tyme['minute'],
second=second,
microsecond=tyme['microsecond'])
dt = datetime.datetime(day=tyme['day'],
month=tyme['month'],
year=tyme['year'],
hour=tyme['hour'],
minute=tyme['minute'],
second=second,
microsecond=tyme['microsecond'])
tzname = tyme.get('tzname')
if tzname:
tzinfo = timezone(tzname)
dt = tzinfo.localize(dt)
return dt
def delta_seconds(before, after):