Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3d578e

Browse files
committed
use isoformat() in jsonutil
allows preservation of timezone info. Timezone info is still ignored in the extract-dates part, for now.
1 parent 8be0653 commit b3d578e

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

IPython/utils/jsonutil.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# timestamp formats
3535
ISO8601="%Y-%m-%dT%H:%M:%S.%f"
36-
ISO8601_PAT=re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+$")
36+
ISO8601_PAT=re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+(Z?[\+\-]\d{2}:?\d{2})?$")
3737

3838
#-----------------------------------------------------------------------------
3939
# Classes and functions
@@ -72,7 +72,9 @@ def extract_dates(obj):
7272
obj = [ extract_dates(o) for o in obj ]
7373
elif isinstance(obj, basestring):
7474
if ISO8601_PAT.match(obj):
75-
obj = datetime.strptime(obj, ISO8601)
75+
# FIXME: add actual timezone support
76+
notz = obj.split('Z',1)[0]
77+
obj = datetime.strptime(notz, ISO8601)
7678
return obj
7779

7880
def squash_dates(obj):
@@ -84,13 +86,13 @@ def squash_dates(obj):
8486
elif isinstance(obj, (list, tuple)):
8587
obj = [ squash_dates(o) for o in obj ]
8688
elif isinstance(obj, datetime):
87-
obj = obj.strftime(ISO8601)
89+
obj = obj.isoformat()
8890
return obj
8991

9092
def date_default(obj):
9193
"""default function for packing datetime objects in JSON."""
9294
if isinstance(obj, datetime):
93-
return obj.strftime(ISO8601)
95+
return obj.isoformat()
9496
else:
9597
raise TypeError("%r is not JSON serializable"%obj)
9698

0 commit comments

Comments
 (0)