diff --git a/IPython/utils/tests/test_jsonutil.py b/IPython/utils/tests/test_jsonutil.py index f70e941..61f1df7 100644 --- a/IPython/utils/tests/test_jsonutil.py +++ b/IPython/utils/tests/test_jsonutil.py @@ -11,6 +11,7 @@ # Imports #----------------------------------------------------------------------------- # stdlib +import datetime import json from base64 import decodestring @@ -19,6 +20,7 @@ import nose.tools as nt # our own from IPython.testing import decorators as dec +from IPython.utils import jsonutil, tz from ..jsonutil import json_clean, encode_images from ..py3compat import unicode_to_str, str_to_bytes @@ -94,6 +96,33 @@ def test_lambda(): assert '' in jc json.dumps(jc) +def test_extract_dates(): + timestamps = [ + '2013-07-03T16:34:52.249482', + '2013-07-03T16:34:52.249482Z', + '2013-07-03T16:34:52.249482Z-0800', + '2013-07-03T16:34:52.249482Z+0800', + '2013-07-03T16:34:52.249482Z+08:00', + '2013-07-03T16:34:52.249482Z-08:00', + '2013-07-03T16:34:52.249482-0800', + '2013-07-03T16:34:52.249482+0800', + '2013-07-03T16:34:52.249482+08:00', + '2013-07-03T16:34:52.249482-08:00', + ] + extracted = jsonutil.extract_dates(timestamps) + ref = extracted[0] + for dt in extracted: + nt.assert_true(isinstance(dt, datetime.datetime)) + nt.assert_equal(dt, ref) + +def test_date_default(): + data = dict(today=datetime.datetime.now(), utcnow=tz.utcnow()) + jsondata = json.dumps(data, default=jsonutil.date_default) + nt.assert_in("+00", jsondata) + nt.assert_equal(jsondata.count("+00"), 1) + extracted = jsonutil.extract_dates(json.loads(jsondata)) + for dt in extracted.values(): + nt.assert_true(isinstance(dt, datetime.datetime)) def test_exception(): bad_dicts = [{1:'number', '1':'string'},