diff options
author | Julien Danjou <julien@danjou.info> | 2015-05-12 16:23:15 +0200 |
---|---|---|
committer | Julien Danjou <julien@danjou.info> | 2015-05-13 17:05:01 +0200 |
commit | d0993d7cc08ff3838ba484aa3588ea70ca600ec4 (patch) | |
tree | 02b70d95fe1755c254456ec29eef78dfe28972b1 /oslo_utils/timeutils.py | |
parent | bb7eebb4c29e8eb38f9272acae06c546dd2d1cd6 (diff) | |
download | oslo-utils-d0993d7cc08ff3838ba484aa3588ea70ca600ec4.tar.gz |
timeutils: make marshall timezone aware
Currently, timestamp marshalling completely squeezes out timezone
information, leading to potential data loss if a datetime object has a
timezone info. This patch fixes that.
Change-Id: I6fbde8578f9701daa58c8b0d40669d6f99fc9812
Diffstat (limited to 'oslo_utils/timeutils.py')
-rw-r--r-- | oslo_utils/timeutils.py | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/oslo_utils/timeutils.py b/oslo_utils/timeutils.py index 5245360..e1a70dd 100644 --- a/oslo_utils/timeutils.py +++ b/oslo_utils/timeutils.py @@ -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): |