summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3/base.py
diff options
context:
space:
mode:
authorTobias Bengfort <tobias.bengfort@posteo.de>2021-04-20 09:05:52 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-04-20 10:07:42 +0200
commit9e1ccd7283e8544d86cba35c820a7d741f5d2712 (patch)
tree6f1f1a0153245281bd310fea6cf430ad16cc0e77 /django/db/backends/sqlite3/base.py
parent4bb27c87194729c0a3d8b41f1af7012afc5f2a15 (diff)
Refs #25287 -- Added _sqlite_prepare_dtdelta_param() hook.
Diffstat (limited to 'django/db/backends/sqlite3/base.py')
-rw-r--r--django/db/backends/sqlite3/base.py28
1 files 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