Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly JSON-encode datetimes aware of non-UTC timezones #2374

Merged
merged 8 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Major release, unreleased
- Removed error handler caching because it caused unexpected results for some
exception inheritance hierarchies. Register handlers explicitly for each
exception if you don't want to traverse the MRO. (`#2362`_)
- Fix incorrect JSON encoding of aware, non-UTC datetimes. (`#2374`_)
- Template auto reloading will honor the ``run`` command's ``debug`` flag even
if ``app.jinja_env`` was already accessed. (`#2373`_)

Expand All @@ -104,6 +105,7 @@ Major release, unreleased
.. _#2354: https://github.com/pallets/flask/pull/2354
.. _#2358: https://github.com/pallets/flask/pull/2358
.. _#2362: https://github.com/pallets/flask/pull/2362
.. _#2374: https://github.com/pallets/flask/pull/2374
.. _#2373: https://github.com/pallets/flask/pull/2373

Version 0.12.2
Expand Down
4 changes: 3 additions & 1 deletion flask/json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import io
import uuid
from datetime import date
from datetime import date, datetime
from flask.globals import current_app, request
from flask._compat import text_type, PY2

Expand Down Expand Up @@ -62,6 +62,8 @@ def default(self, o):
return list(iterable)
return JSONEncoder.default(self, o)
"""
if isinstance(o, datetime):
return http_date(o.utctimetuple())
if isinstance(o, date):
return http_date(o.timetuple())
if isinstance(o, uuid.UUID):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ def test_jsonify_date_types(self, app, client):
assert rv.mimetype == 'application/json'
assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple())

@pytest.mark.parametrize('tz', (('UTC', 0), ('PST', -8), ('KST', 9)))
def test_jsonify_aware_datetimes(self, tz):
"""Test if aware datetime.datetime objects are converted into GMT."""
tzinfo = datetime.timezone(datetime.timedelta(hours=tz[1]), name=tz[0])
dt = datetime.datetime(2017, 1, 1, 12, 34, 56, tzinfo=tzinfo)
gmt = datetime.timezone(datetime.timedelta(), name='GMT')
expected = dt.astimezone(gmt).strftime('"%a, %d %b %Y %H:%M:%S %Z"')
assert flask.json.JSONEncoder().encode(dt) == expected

def test_jsonify_uuid_types(self, app, client):
"""Test jsonify with uuid.UUID types"""

Expand Down