1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #26656 -- Added duration (timedelta) support to DjangoJSONEncoder.

This commit is contained in:
Will Hardy
2016-05-26 14:48:36 +02:00
committed by Tim Graham
parent a7b5dfd170
commit 8ef78b8165
7 changed files with 93 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
import decimal
import json
import re
@@ -302,3 +303,15 @@ class DjangoJSONEncoderTests(SimpleTestCase):
json.dumps({'lang': ugettext_lazy("French")}, cls=DjangoJSONEncoder),
'{"lang": "Fran\\u00e7ais"}'
)
def test_timedelta(self):
duration = datetime.timedelta(days=1, hours=2, seconds=3)
self.assertEqual(
json.dumps({'duration': duration}, cls=DjangoJSONEncoder),
'{"duration": "P1DT02H00M03S"}'
)
duration = datetime.timedelta(0)
self.assertEqual(
json.dumps({'duration': duration}, cls=DjangoJSONEncoder),
'{"duration": "P0DT00H00M00S"}'
)