Drop compat wrapper for TimeDelta.total_seconds() (#5577)

TimeDelta.total_seconds() was introduced in Python 2.7 and 3.2. It is
available on all supported Python versions.

https://docs.python.org/2/library/datetime.html#datetime.timedelta.total_seconds
https://docs.python.org/3/library/datetime.html#datetime.timedelta.total_seconds
This commit is contained in:
Jon Dufresne 2017-11-09 00:03:48 -08:00 committed by Carlton Gibson
parent 9234ac576e
commit f8e8381c00
3 changed files with 2 additions and 19 deletions

View File

@ -78,14 +78,6 @@ def unicode_http_header(value):
return value return value
def total_seconds(timedelta):
# TimeDelta.total_seconds() is only available in Python 2.7
if hasattr(timedelta, 'total_seconds'):
return timedelta.total_seconds()
else:
return (timedelta.days * 86400.0) + float(timedelta.seconds) + (timedelta.microseconds / 1000000.0)
def distinct(queryset, base): def distinct(queryset, base):
if settings.DATABASES[queryset.db]["ENGINE"] == "django.db.backends.oracle": if settings.DATABASES[queryset.db]["ENGINE"] == "django.db.backends.oracle":
# distinct analogue for Oracle users # distinct analogue for Oracle users

View File

@ -13,7 +13,7 @@ from django.utils import six, timezone
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.functional import Promise from django.utils.functional import Promise
from rest_framework.compat import coreapi, total_seconds from rest_framework.compat import coreapi
class JSONEncoder(json.JSONEncoder): class JSONEncoder(json.JSONEncoder):
@ -39,7 +39,7 @@ class JSONEncoder(json.JSONEncoder):
representation = obj.isoformat() representation = obj.isoformat()
return representation return representation
elif isinstance(obj, datetime.timedelta): elif isinstance(obj, datetime.timedelta):
return six.text_type(total_seconds(obj)) return six.text_type(obj.total_seconds())
elif isinstance(obj, decimal.Decimal): elif isinstance(obj, decimal.Decimal):
# Serializers will coerce decimals to strings by default. # Serializers will coerce decimals to strings by default.
return float(obj) return float(obj)

View File

@ -13,15 +13,6 @@ class CompatTests(TestCase):
compat.django.VERSION = self.original_django_version compat.django.VERSION = self.original_django_version
compat.transaction = self.original_transaction compat.transaction = self.original_transaction
def test_total_seconds(self):
class MockTimedelta(object):
days = 1
seconds = 1
microseconds = 100
timedelta = MockTimedelta()
expected = (timedelta.days * 86400.0) + float(timedelta.seconds) + (timedelta.microseconds / 1000000.0)
assert compat.total_seconds(timedelta) == expected
def test_set_rollback_for_transaction_in_managed_mode(self): def test_set_rollback_for_transaction_in_managed_mode(self):
class MockTransaction(object): class MockTransaction(object):
called_rollback = False called_rollback = False