Correctly load and dump items with datetime.date(s)
Change-Id: I3fbe21df4b5ac12d13dd8633b7c362aeabbfc94f
This commit is contained in:
parent
9f2a2edb56
commit
2857ee3b68
@ -63,6 +63,22 @@ def _deserialize_datetime(blob):
|
||||
return dt
|
||||
|
||||
|
||||
def _serialize_date(d):
|
||||
dct = {
|
||||
'year': d.year,
|
||||
'month': d.month,
|
||||
'day': d.day,
|
||||
}
|
||||
return dumps(dct)
|
||||
|
||||
|
||||
def _deserialize_date(blob):
|
||||
dct = loads(blob)
|
||||
return datetime.date(year=dct['year'],
|
||||
month=dct['month'],
|
||||
day=dct['day'])
|
||||
|
||||
|
||||
def _serializer(obj):
|
||||
# Applications can assign 0 to 127 to store
|
||||
# application-specific type information...
|
||||
@ -96,6 +112,8 @@ def _serializer(obj):
|
||||
if isinstance(obj, xmlrpclib.DateTime):
|
||||
dt = datetime.datetime(*tuple(obj.timetuple())[:6])
|
||||
return msgpack.ExtType(6, _serialize_datetime(dt))
|
||||
if isinstance(obj, datetime.date):
|
||||
return msgpack.ExtType(7, _serialize_date(obj))
|
||||
raise TypeError("Unknown type: %r" % (obj,))
|
||||
|
||||
|
||||
@ -122,6 +140,8 @@ def _unserializer(code, data):
|
||||
if code == 6:
|
||||
dt = _deserialize_datetime(data)
|
||||
return xmlrpclib.DateTime(dt.timetuple())
|
||||
if code == 7:
|
||||
return _deserialize_date(data)
|
||||
return msgpack.ExtType(code, data)
|
||||
|
||||
|
||||
|
@ -75,6 +75,7 @@ class MsgPackUtilsTestMixin(test_base.BaseTestCase):
|
||||
'zzz': uuid.uuid4(),
|
||||
'yyy': 'yyy',
|
||||
'ddd': b'bbb',
|
||||
'today': datetime.date.today(),
|
||||
}
|
||||
self.assertEqual(_dumps_loads(src), src)
|
||||
|
||||
@ -120,6 +121,10 @@ class MsgPackUtilsTestMixin(test_base.BaseTestCase):
|
||||
thing = {'ip_addr': netaddr.IPAddress('1.2.3.4')}
|
||||
self.assertEqual(_dumps_loads(thing), thing)
|
||||
|
||||
def test_today(self):
|
||||
today = datetime.date.today()
|
||||
self.assertEqual(today, _dumps_loads(today))
|
||||
|
||||
def test_datetime_tz_clone(self):
|
||||
eastern = timezone('US/Eastern')
|
||||
now = datetime.datetime.now()
|
||||
|
Loading…
x
Reference in New Issue
Block a user