diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index 7c901d78d89..ad4c13652e3 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -3,6 +3,7 @@ import uuid from django.conf import settings from django.db.backends.base.operations import BaseDatabaseOperations from django.utils import timezone +from django.utils.duration import duration_microseconds from django.utils.encoding import force_text @@ -105,7 +106,7 @@ class DatabaseOperations(BaseDatabaseOperations): return "TIME(%s)" % (field_name) def date_interval_sql(self, timedelta): - return "INTERVAL '%06f' SECOND_MICROSECOND" % timedelta.total_seconds() + return 'INTERVAL %s MICROSECOND' % duration_microseconds(timedelta) def format_for_duration_arithmetic(self, sql): return 'INTERVAL %s MICROSECOND' % sql diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index d15352dcc6e..d3caa6d12a2 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -17,6 +17,7 @@ from django.utils import timezone from django.utils.dateparse import ( parse_date, parse_datetime, parse_duration, parse_time, ) +from django.utils.duration import duration_microseconds from .client import DatabaseClient # isort:skip from .creation import DatabaseCreation # isort:skip @@ -471,7 +472,7 @@ def _sqlite_time_diff(lhs, rhs): def _sqlite_timestamp_diff(lhs, rhs): left = backend_utils.typecast_timestamp(lhs) right = backend_utils.typecast_timestamp(rhs) - return (left - right).total_seconds() * 1000000 + return duration_microseconds(left - right) def _sqlite_regexp(re_pattern, re_string): diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index a67cf9b6478..188b5bb907e 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -24,7 +24,7 @@ from django.utils.datastructures import DictWrapper from django.utils.dateparse import ( parse_date, parse_datetime, parse_duration, parse_time, ) -from django.utils.duration import duration_string +from django.utils.duration import duration_microseconds, duration_string from django.utils.encoding import force_bytes, smart_text from django.utils.functional import Promise, cached_property from django.utils.ipv6 import clean_ipv6_address @@ -1617,8 +1617,7 @@ class DurationField(Field): return value if value is None: return None - # Discard any fractional microseconds due to floating point arithmetic. - return round(value.total_seconds() * 1000000) + return duration_microseconds(value) def get_db_converters(self, connection): converters = [] diff --git a/django/utils/duration.py b/django/utils/duration.py index 53322b51d23..466603d46cf 100644 --- a/django/utils/duration.py +++ b/django/utils/duration.py @@ -38,3 +38,7 @@ def duration_iso_string(duration): days, hours, minutes, seconds, microseconds = _get_duration_components(duration) ms = '.{:06d}'.format(microseconds) if microseconds else "" return '{}P{}DT{:02d}H{:02d}M{:02d}{}S'.format(sign, days, hours, minutes, seconds, ms) + + +def duration_microseconds(delta): + return (24 * 60 * 60 * delta.days + delta.seconds) * 1000000 + delta.microseconds diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 5d54e46c02d..2a6714a65ff 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -1250,6 +1250,16 @@ class FTimeDeltaTests(TestCase): ] self.assertEqual(over_estimate, ['e4']) + @skipUnlessDBFeature('supports_temporal_subtraction') + def test_datetime_subtraction_microseconds(self): + delta = datetime.timedelta(microseconds=8999999999999999) + Experiment.objects.update(end=F('start') + delta) + qs = Experiment.objects.annotate( + delta=ExpressionWrapper(F('end') - F('start'), output_field=models.DurationField()) + ) + for e in qs: + self.assertEqual(e.delta, delta) + def test_duration_with_datetime(self): # Exclude e1 which has very high precision so we can test this on all # backends regardless of whether or not it supports @@ -1259,6 +1269,15 @@ class FTimeDeltaTests(TestCase): ).order_by('name') self.assertQuerysetEqual(over_estimate, ['e3', 'e4', 'e5'], lambda e: e.name) + def test_duration_with_datetime_microseconds(self): + delta = datetime.timedelta(microseconds=8999999999999999) + qs = Experiment.objects.annotate(dt=ExpressionWrapper( + F('start') + delta, + output_field=models.DateTimeField(), + )) + for e in qs: + self.assertEqual(e.dt, e.start + delta) + def test_date_minus_duration(self): more_than_4_days = Experiment.objects.filter( assigned__lt=F('completed') - Value(datetime.timedelta(days=4), output_field=models.DurationField()) diff --git a/tests/model_fields/test_durationfield.py b/tests/model_fields/test_durationfield.py index d39126a0ab7..b73994f86ae 100644 --- a/tests/model_fields/test_durationfield.py +++ b/tests/model_fields/test_durationfield.py @@ -12,7 +12,7 @@ from .models import DurationModel, NullDurationModel class TestSaveLoad(TestCase): def test_simple_roundtrip(self): - duration = datetime.timedelta(days=123, seconds=123, microseconds=123) + duration = datetime.timedelta(microseconds=8999999999999999) DurationModel.objects.create(field=duration) loaded = DurationModel.objects.get() self.assertEqual(loaded.field, duration) diff --git a/tests/utils_tests/test_duration.py b/tests/utils_tests/test_duration.py index d0564f396fc..84a3a0893fd 100644 --- a/tests/utils_tests/test_duration.py +++ b/tests/utils_tests/test_duration.py @@ -2,7 +2,9 @@ import datetime import unittest from django.utils.dateparse import parse_duration -from django.utils.duration import duration_iso_string, duration_string +from django.utils.duration import ( + duration_iso_string, duration_microseconds, duration_string, +) class TestDurationString(unittest.TestCase): @@ -79,3 +81,17 @@ class TestParseISODurationRoundtrip(unittest.TestCase): def test_negative(self): duration = datetime.timedelta(days=-1, hours=1, minutes=3, seconds=5) self.assertEqual(parse_duration(duration_iso_string(duration)).total_seconds(), duration.total_seconds()) + + +class TestDurationMicroseconds(unittest.TestCase): + def test(self): + deltas = [ + datetime.timedelta.max, + datetime.timedelta.min, + datetime.timedelta.resolution, + -datetime.timedelta.resolution, + datetime.timedelta(microseconds=8999999999999999), + ] + for delta in deltas: + with self.subTest(delta=delta): + self.assertEqual(datetime.timedelta(microseconds=duration_microseconds(delta)), delta)