File tree Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,14 @@ def unicode_to_repr(value):
33
33
return value
34
34
35
35
36
+ def total_seconds (timedelta ):
37
+ # TimeDelta.total_seconds() is only available in Python 2.7
38
+ if hasattr (timedelta , 'total_seconds' ):
39
+ return timedelta .total_seconds ()
40
+ else :
41
+ return (timedelta .days * 86400.0 ) + float (timedelta .seconds ) + (timedelta .microseconds / 1000000.0 )
42
+
43
+
36
44
# OrderedDict only available in Python 2.7.
37
45
# This will always be the case in Django 1.7 and above, as these versions
38
46
# no longer support Python 2.6.
Original file line number Diff line number Diff line change 6
6
from django .utils import six , timezone
7
7
from django .utils .encoding import force_text
8
8
from django .utils .functional import Promise
9
- from rest_framework .compat import OrderedDict
9
+ from rest_framework .compat import OrderedDict , total_seconds
10
10
from rest_framework .utils .serializer_helpers import ReturnDict , ReturnList
11
11
import datetime
12
12
import decimal
@@ -41,7 +41,7 @@ def default(self, obj):
41
41
representation = representation [:12 ]
42
42
return representation
43
43
elif isinstance (obj , datetime .timedelta ):
44
- return six .text_type (obj . total_seconds ())
44
+ return six .text_type (total_seconds (obj ))
45
45
elif isinstance (obj , decimal .Decimal ):
46
46
# Serializers will coerce decimals to strings by default.
47
47
return float (obj )
You can’t perform that action at this time.
0 commit comments