summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRussell Bryant <rbryant@redhat.com>2012-07-16 10:30:25 -0400
committerRussell Bryant <rbryant@redhat.com>2012-07-16 10:39:44 -0400
commitce3071437d1871f77c4d8573cbe5f4ea8c817650 (patch)
tree5a9f6f292d96d94c762f7e3873d951e43903a47f
parent2adaf2ffafdb67bd237578f094b78d15da17b772 (diff)
downloadoslo-ce3071437d1871f77c4d8573cbe5f4ea8c817650.tar.gz
oslo-ce3071437d1871f77c4d8573cbe5f4ea8c817650.tar.xz
oslo-ce3071437d1871f77c4d8573cbe5f4ea8c817650.zip
Use strtime() in to_primitive() for datetime objs.
This patch updates jsonutils.to_primitive() to use timeutils.strtime() to convert a datimetime object to a string instead of just using str(). This ensures that we can easily convert the string back to a datetime using timeutils.parse_strtime(). Required for the nova blueprint no-db-messaging. Change-Id: I725b333695930e12e2832378102514326fec639c
-rw-r--r--openstack/common/jsonutils.py4
-rw-r--r--tests/unit/test_jsonutils.py6
-rw-r--r--tests/unit/test_timeutils.py6
3 files changed, 12 insertions, 4 deletions
diff --git a/openstack/common/jsonutils.py b/openstack/common/jsonutils.py
index 11b7e1e..6130a7f 100644
--- a/openstack/common/jsonutils.py
+++ b/openstack/common/jsonutils.py
@@ -39,6 +39,8 @@ import itertools
import json
import xmlrpclib
+from openstack.common import timeutils
+
def to_primitive(value, convert_instances=False, level=0):
"""Convert a complex object into primitives.
@@ -101,7 +103,7 @@ def to_primitive(value, convert_instances=False, level=0):
level=level)
return o
elif isinstance(value, datetime.datetime):
- return str(value)
+ return timeutils.strtime(value)
elif hasattr(value, 'iteritems'):
return to_primitive(dict(value.iteritems()),
convert_instances=convert_instances,
diff --git a/tests/unit/test_jsonutils.py b/tests/unit/test_jsonutils.py
index 9b987cd..4a18b77 100644
--- a/tests/unit/test_jsonutils.py
+++ b/tests/unit/test_jsonutils.py
@@ -54,15 +54,15 @@ class ToPrimitiveTestCase(unittest.TestCase):
self.assertEquals(jsonutils.to_primitive({}), {})
def test_datetime(self):
- x = datetime.datetime(1, 2, 3, 4, 5, 6, 7)
+ x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
self.assertEquals(jsonutils.to_primitive(x),
- "0001-02-03 04:05:06.000007")
+ '1920-02-03T04:05:06.000007')
def test_DateTime(self):
x = xmlrpclib.DateTime()
x.decode("19710203T04:05:06")
self.assertEquals(jsonutils.to_primitive(x),
- "1971-02-03 04:05:06")
+ '1971-02-03T04:05:06.000000')
def test_iter(self):
class IterClass(object):
diff --git a/tests/unit/test_timeutils.py b/tests/unit/test_timeutils.py
index d0b773a..7dbd608 100644
--- a/tests/unit/test_timeutils.py
+++ b/tests/unit/test_timeutils.py
@@ -59,6 +59,12 @@ class TimeUtilsTest(unittest.TestCase):
expect = timeutils.parse_strtime(perfect_time_format)
self.assertEqual(self.skynet_self_aware_time_perfect, expect)
+ def test_strtime_and_back(self):
+ orig_t = datetime.datetime(1997, 8, 29, 6, 14, 0)
+ s = timeutils.strtime(orig_t)
+ t = timeutils.parse_strtime(s)
+ self.assertEqual(orig_t, t)
+
def test_is_older_than(self):
with mock.patch('datetime.datetime') as datetime_mock:
datetime_mock.utcnow.return_value = self.skynet_self_aware_time