diff options
| author | Simon Charette <charette.s@gmail.com> | 2016-06-18 23:38:24 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2016-07-08 12:35:34 -0400 |
| commit | 082c52dbedd76c312cebf3b23e04c449a94c20b6 (patch) | |
| tree | 0ae566d52a61ac42f5a918931684fd3779f045af /django/db/backends/sqlite3/base.py | |
| parent | 90468079ec6f72a1b8b6a908d81d3201a3204b24 (diff) | |
Refs #25774, #26348 -- Allowed Trunc functions to operate with time fields.
Thanks Josh for the amazing testing setup and Tim for the review.
Diffstat (limited to 'django/db/backends/sqlite3/base.py')
| -rw-r--r-- | django/db/backends/sqlite3/base.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 4f52cc3637..70d511f108 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -213,6 +213,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): conn.create_function("django_datetime_extract", 3, _sqlite_datetime_extract) conn.create_function("django_datetime_trunc", 3, _sqlite_datetime_trunc) conn.create_function("django_time_extract", 2, _sqlite_time_extract) + conn.create_function("django_time_trunc", 2, _sqlite_time_trunc) conn.create_function("django_time_diff", 2, _sqlite_time_diff) conn.create_function("django_timestamp_diff", 2, _sqlite_timestamp_diff) conn.create_function("regexp", 2, _sqlite_regexp) @@ -370,6 +371,19 @@ def _sqlite_date_trunc(lookup_type, dt): return "%i-%02i-%02i" % (dt.year, dt.month, dt.day) +def _sqlite_time_trunc(lookup_type, dt): + try: + dt = backend_utils.typecast_time(dt) + except (ValueError, TypeError): + return None + if lookup_type == 'hour': + return "%02i:00:00" % dt.hour + elif lookup_type == 'minute': + return "%02i:%02i:00" % (dt.hour, dt.minute) + elif lookup_type == 'second': + return "%02i:%02i:%02i" % (dt.hour, dt.minute, dt.second) + + def _sqlite_datetime_parse(dt, tzname): if dt is None: return None |
