fix date time serialization

This commit is contained in:
j 2013-07-02 14:44:18 +02:00
parent 78986c671e
commit 1d429b6d33
2 changed files with 10 additions and 4 deletions

View file

@ -11,9 +11,13 @@ from ox.utils import json
def to_json(python_object): def to_json(python_object):
if isinstance(python_object, datetime.datetime): if isinstance(python_object, datetime.datetime):
value = datetime_safe.datetime.fromordinal(python_object.toordinal()) if python_object.year < 1900:
tt = python_ojbect.timetuple()
value = '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6])
else:
value = python_ojbect.strftime('%Y-%m-%dT%H:%M:%SZ')
return {'__class__': 'datetime.datetime', return {'__class__': 'datetime.datetime',
'__value__': value.strftime('%Y-%m-%dT%H:%M:%SZ')} '__value__': value}
if isinstance(python_object, datetime_safe.datetime): if isinstance(python_object, datetime_safe.datetime):
return {'__class__': 'datetime.datetime', return {'__class__': 'datetime.datetime',
'__value__': python_object.strftime('%Y-%m-%dT%H:%M:%SZ')} '__value__': python_object.strftime('%Y-%m-%dT%H:%M:%SZ')}

View file

@ -20,8 +20,10 @@ def json_response(data=None, status=200, text='ok'):
def _to_json(python_object): def _to_json(python_object):
if isinstance(python_object, datetime.datetime): if isinstance(python_object, datetime.datetime):
value = datetime_safe.datetime.fromordinal(python_object.toordinal()) if python_object.year < 1900:
return value.strftime('%Y-%m-%dT%H:%M:%SZ') tt = python_ojbect.timetuple()
return '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6])
return python_ojbect.strftime('%Y-%m-%dT%H:%M:%SZ')
if isinstance(python_object, datetime_safe.datetime): if isinstance(python_object, datetime_safe.datetime):
return python_object.strftime('%Y-%m-%dT%H:%M:%SZ') return python_object.strftime('%Y-%m-%dT%H:%M:%SZ')
raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object))) raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object)))