diff --git a/caravel/utils.py b/caravel/utils.py index ef6b35609..5921f3f23 100644 --- a/caravel/utils.py +++ b/caravel/utils.py @@ -4,7 +4,7 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals -from datetime import datetime +from datetime import datetime, date import decimal import functools import json @@ -318,6 +318,8 @@ def json_int_dttm_ser(obj): return val if isinstance(obj, datetime): obj = (obj - EPOCH).total_seconds() * 1000 + elif isinstance(obj, date): + obj = (obj - EPOCH.date()).total_seconds() * 1000 else: raise TypeError( "Unserializable object {} of type {}".format(obj, type(obj)) diff --git a/tests/utils_tests.py b/tests/utils_tests.py new file mode 100644 index 000000000..7f230fcb8 --- /dev/null +++ b/tests/utils_tests.py @@ -0,0 +1,19 @@ +from datetime import datetime, date, timedelta +from caravel import utils +import unittest + + +class UtilsTestCase(unittest.TestCase): + def test_json_int_dttm_ser(self): + today = date.today() + now = datetime.now() + ms = utils.json_int_dttm_ser(today) + deser = (utils.EPOCH + timedelta(milliseconds=ms)).date() + assert today == deser, "Serialization error: %s is not %s" % (str(today), str(deser)) + ms = utils.json_int_dttm_ser(now) + deser = (utils.EPOCH + timedelta(milliseconds=ms)) + assert now == deser, "Serialization error: %s is not %s" % (str(now), str(deser)) + + with self.assertRaises(TypeError): + utils.json_int_dttm_ser("this is not a date") +