From d41e2e6a5db3211ecd3c3f3f55934474406ffac2 Mon Sep 17 00:00:00 2001 From: "Eugene M. Kim" Date: Wed, 14 Jun 2017 11:12:11 -0700 Subject: [PATCH] Correctly encode aware, non-UTC datetime objects http_date() requires timetuple in UTC, but JSONEncoder.default() was passing a local timetuple instead. --- CHANGES | 2 ++ flask/json/__init__.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 59e8c4c1..f37dfc54 100644 --- a/CHANGES +++ b/CHANGES @@ -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`_) .. _#1489: https://github.com/pallets/flask/pull/1489 .. _#1621: https://github.com/pallets/flask/pull/1621 @@ -102,6 +103,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 Version 0.12.2 -------------- diff --git a/flask/json/__init__.py b/flask/json/__init__.py index 93e6fdc4..6559c1aa 100644 --- a/flask/json/__init__.py +++ b/flask/json/__init__.py @@ -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 @@ -62,6 +62,8 @@ class JSONEncoder(_json.JSONEncoder): 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):