From 1d429b6d33fdcfef8604f7f0340b1ec67e3b6023 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 2 Jul 2013 14:44:18 +0200 Subject: [PATCH] fix date time serialization --- ox/django/fields.py | 8 ++++++-- ox/django/shortcuts.py | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ox/django/fields.py b/ox/django/fields.py index 77c3e50..006fcf7 100644 --- a/ox/django/fields.py +++ b/ox/django/fields.py @@ -11,9 +11,13 @@ from ox.utils import json def to_json(python_object): 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', - '__value__': value.strftime('%Y-%m-%dT%H:%M:%SZ')} + '__value__': value} if isinstance(python_object, datetime_safe.datetime): return {'__class__': 'datetime.datetime', '__value__': python_object.strftime('%Y-%m-%dT%H:%M:%SZ')} diff --git a/ox/django/shortcuts.py b/ox/django/shortcuts.py index bf600c6..862568a 100644 --- a/ox/django/shortcuts.py +++ b/ox/django/shortcuts.py @@ -20,8 +20,10 @@ def json_response(data=None, status=200, text='ok'): def _to_json(python_object): if isinstance(python_object, datetime.datetime): - value = datetime_safe.datetime.fromordinal(python_object.toordinal()) - return value.strftime('%Y-%m-%dT%H:%M:%SZ') + if python_object.year < 1900: + 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): 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)))