From b3d578e1beeed0741f14a4ce45033e8aa08847da 2013-07-03 19:09:00 From: MinRK Date: 2013-07-03 19:09:00 Subject: [PATCH] use `isoformat()` in jsonutil allows preservation of timezone info. Timezone info is still ignored in the extract-dates part, for now. --- diff --git a/IPython/utils/jsonutil.py b/IPython/utils/jsonutil.py index 0d25c3d..4449128 100644 --- a/IPython/utils/jsonutil.py +++ b/IPython/utils/jsonutil.py @@ -33,7 +33,7 @@ next_attr_name = '__next__' if py3compat.PY3 else 'next' # timestamp formats ISO8601="%Y-%m-%dT%H:%M:%S.%f" -ISO8601_PAT=re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+$") +ISO8601_PAT=re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+(Z?[\+\-]\d{2}:?\d{2})?$") #----------------------------------------------------------------------------- # Classes and functions @@ -72,7 +72,9 @@ def extract_dates(obj): obj = [ extract_dates(o) for o in obj ] elif isinstance(obj, basestring): if ISO8601_PAT.match(obj): - obj = datetime.strptime(obj, ISO8601) + # FIXME: add actual timezone support + notz = obj.split('Z',1)[0] + obj = datetime.strptime(notz, ISO8601) return obj def squash_dates(obj): @@ -84,13 +86,13 @@ def squash_dates(obj): elif isinstance(obj, (list, tuple)): obj = [ squash_dates(o) for o in obj ] elif isinstance(obj, datetime): - obj = obj.strftime(ISO8601) + obj = obj.isoformat() return obj def date_default(obj): """default function for packing datetime objects in JSON.""" if isinstance(obj, datetime): - return obj.strftime(ISO8601) + return obj.isoformat() else: raise TypeError("%r is not JSON serializable"%obj)