From 9e1ccd7283e8544d86cba35c820a7d741f5d2712 Mon Sep 17 00:00:00 2001
From: Tobias Bengfort <tobias.bengfort@posteo.de>
Date: Tue, 20 Apr 2021 09:05:52 +0200
Subject: [PATCH] Refs #25287 -- Added _sqlite_prepare_dtdelta_param() hook.

---
 django/db/backends/sqlite3/base.py | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index a0b32d25d5..35466189e6 100644
--- a/django/db/backends/sqlite3/base.py
+++ b/django/db/backends/sqlite3/base.py
@@ -548,6 +548,15 @@ def _sqlite_time_extract(lookup_type, dt):
     return getattr(dt, lookup_type)
 
 
+def _sqlite_prepare_dtdelta_param(conn, param):
+    if conn in ['+', '-']:
+        if isinstance(param, int):
+            return datetime.timedelta(0, 0, param)
+        else:
+            return backend_utils.typecast_timestamp(param)
+    return param
+
+
 @none_guard
 def _sqlite_format_dtdelta(conn, lhs, rhs):
     """
@@ -555,18 +564,19 @@ def _sqlite_format_dtdelta(conn, lhs, rhs):
     - An integer number of microseconds
     - A string representing a datetime
     """
+    conn = conn.strip()
     try:
-        real_lhs = datetime.timedelta(0, 0, lhs) if isinstance(lhs, int) else backend_utils.typecast_timestamp(lhs)
-        real_rhs = datetime.timedelta(0, 0, rhs) if isinstance(rhs, int) else backend_utils.typecast_timestamp(rhs)
-        if conn.strip() == '+':
-            out = real_lhs + real_rhs
-        else:
-            out = real_lhs - real_rhs
+        real_lhs = _sqlite_prepare_dtdelta_param(conn, lhs)
+        real_rhs = _sqlite_prepare_dtdelta_param(conn, rhs)
     except (ValueError, TypeError):
         return None
-    # typecast_timestamp returns a date or a datetime without timezone.
-    # It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
-    return str(out)
+    if conn == '+':
+        # typecast_timestamp returns a date or a datetime without timezone.
+        # It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
+        out = str(real_lhs + real_rhs)
+    else:
+        out = str(real_lhs - real_rhs)
+    return out
 
 
 @none_guard