Browse Source

Correctly encode aware, non-UTC datetime objects

http_date() requires timetuple in UTC, but JSONEncoder.default() was
passing a local timetuple instead.
pull/2374/head
Eugene M. Kim 8 years ago
parent
commit
d41e2e6a5d
  1. 2
      CHANGES
  2. 4
      flask/json/__init__.py

2
CHANGES

@ -79,6 +79,7 @@ Major release, unreleased
- Removed error handler caching because it caused unexpected results for some - Removed error handler caching because it caused unexpected results for some
exception inheritance hierarchies. Register handlers explicitly for each exception inheritance hierarchies. Register handlers explicitly for each
exception if you don't want to traverse the MRO. (`#2362`_) exception if you don't want to traverse the MRO. (`#2362`_)
- Fix incorrect JSON encoding of aware, non-UTC datetimes. (`#2374`_)
.. _#1489: https://github.com/pallets/flask/pull/1489 .. _#1489: https://github.com/pallets/flask/pull/1489
.. _#1621: https://github.com/pallets/flask/pull/1621 .. _#1621: https://github.com/pallets/flask/pull/1621
@ -102,6 +103,7 @@ Major release, unreleased
.. _#2354: https://github.com/pallets/flask/pull/2354 .. _#2354: https://github.com/pallets/flask/pull/2354
.. _#2358: https://github.com/pallets/flask/pull/2358 .. _#2358: https://github.com/pallets/flask/pull/2358
.. _#2362: https://github.com/pallets/flask/pull/2362 .. _#2362: https://github.com/pallets/flask/pull/2362
.. _#2374: https://github.com/pallets/flask/pull/2374
Version 0.12.2 Version 0.12.2
-------------- --------------

4
flask/json/__init__.py

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import io import io
import uuid import uuid
from datetime import date from datetime import date, datetime
from flask.globals import current_app, request from flask.globals import current_app, request
from flask._compat import text_type, PY2 from flask._compat import text_type, PY2
@ -62,6 +62,8 @@ class JSONEncoder(_json.JSONEncoder):
return list(iterable) return list(iterable)
return JSONEncoder.default(self, o) return JSONEncoder.default(self, o)
""" """
if isinstance(o, datetime):
return http_date(o.utctimetuple())
if isinstance(o, date): if isinstance(o, date):
return http_date(o.timetuple()) return http_date(o.timetuple())
if isinstance(o, uuid.UUID): if isinstance(o, uuid.UUID):

Loading…
Cancel
Save