##// END OF EJS Templates
use `isoformat()` in jsonutil...
MinRK -
Show More
@@ -33,7 +33,7 b" next_attr_name = '__next__' if py3compat.PY3 else 'next'"
33
33
34 # timestamp formats
34 # timestamp formats
35 ISO8601="%Y-%m-%dT%H:%M:%S.%f"
35 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})?$")
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Classes and functions
39 # Classes and functions
@@ -72,7 +72,9 b' def extract_dates(obj):'
72 obj = [ extract_dates(o) for o in obj ]
72 obj = [ extract_dates(o) for o in obj ]
73 elif isinstance(obj, basestring):
73 elif isinstance(obj, basestring):
74 if ISO8601_PAT.match(obj):
74 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)
76 return obj
78 return obj
77
79
78 def squash_dates(obj):
80 def squash_dates(obj):
@@ -84,13 +86,13 b' def squash_dates(obj):'
84 elif isinstance(obj, (list, tuple)):
86 elif isinstance(obj, (list, tuple)):
85 obj = [ squash_dates(o) for o in obj ]
87 obj = [ squash_dates(o) for o in obj ]
86 elif isinstance(obj, datetime):
88 elif isinstance(obj, datetime):
87 obj = obj.strftime(ISO8601)
89 obj = obj.isoformat()
88 return obj
90 return obj
89
91
90 def date_default(obj):
92 def date_default(obj):
91 """default function for packing datetime objects in JSON."""
93 """default function for packing datetime objects in JSON."""
92 if isinstance(obj, datetime):
94 if isinstance(obj, datetime):
93 return obj.strftime(ISO8601)
95 return obj.isoformat()
94 else:
96 else:
95 raise TypeError("%r is not JSON serializable"%obj)
97 raise TypeError("%r is not JSON serializable"%obj)
96
98
General Comments 0
You need to be logged in to leave comments. Login now