jsonutils: replace strtime() usage with isoformat()
Following discussion in change I8b5119e64369ccac3423dccc04421f99912df733 in oslo.utils, we plan to deprecated and remove strtime() which is to be considered dangerous. This changes the json serialization code to embed the timezone in the encoded datetime string if it's available. Change-Id: I262391987d25b483d2bec4ac3abe94b6e9d032ad
This commit is contained in:
parent
42b355f10d
commit
5b0827a3df
@ -57,7 +57,6 @@ else:
|
||||
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import importutils
|
||||
from oslo_utils import timeutils
|
||||
import six
|
||||
import six.moves.xmlrpc_client as xmlrpclib
|
||||
|
||||
@ -105,7 +104,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||
|
||||
if isinstance(value, datetime.datetime):
|
||||
if convert_datetime:
|
||||
return timeutils.strtime(value)
|
||||
return value.isoformat()
|
||||
else:
|
||||
return value
|
||||
|
||||
@ -146,7 +145,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||
value = datetime.datetime(*tuple(value.timetuple())[:6])
|
||||
|
||||
if convert_datetime and isinstance(value, datetime.datetime):
|
||||
return timeutils.strtime(value)
|
||||
return value.isoformat()
|
||||
elif hasattr(value, 'iteritems'):
|
||||
return recursive(dict(value.iteritems()), level=level + 1)
|
||||
elif hasattr(value, '__iter__'):
|
||||
|
@ -17,6 +17,7 @@ import collections
|
||||
import datetime
|
||||
import json
|
||||
|
||||
import iso8601
|
||||
import mock
|
||||
import netaddr
|
||||
from oslo_i18n import fixture
|
||||
@ -135,6 +136,12 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
||||
self.assertEqual(jsonutils.to_primitive(x),
|
||||
'1920-02-03T04:05:06.000007')
|
||||
|
||||
def test_datetime_timezone(self):
|
||||
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7,
|
||||
tzinfo=iso8601.iso8601.UTC)
|
||||
self.assertEqual(jsonutils.to_primitive(x),
|
||||
'1920-02-03T04:05:06.000007+00:00')
|
||||
|
||||
def test_datetime_preserve(self):
|
||||
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
|
||||
self.assertEqual(jsonutils.to_primitive(x, convert_datetime=False), x)
|
||||
@ -143,7 +150,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
||||
x = xmlrpclib.DateTime()
|
||||
x.decode("19710203T04:05:06")
|
||||
self.assertEqual(jsonutils.to_primitive(x),
|
||||
'1971-02-03T04:05:06.000000')
|
||||
'1971-02-03T04:05:06')
|
||||
|
||||
def test_iter(self):
|
||||
class IterClass(object):
|
||||
|
@ -161,7 +161,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
||||
x = xmlrpclib.DateTime()
|
||||
x.decode("19710203T04:05:06")
|
||||
self.assertEqual(jsonutils.to_primitive(x),
|
||||
'1971-02-03T04:05:06.000000')
|
||||
'1971-02-03T04:05:06')
|
||||
|
||||
def test_iter(self):
|
||||
class IterClass(object):
|
||||
|
Loading…
x
Reference in New Issue
Block a user