diff options
author | David-Wobrock <david.wobrock@gmail.com> | 2020-10-04 19:28:21 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-10-14 20:06:26 +0200 |
commit | ee005328c8eec5c013c6bf9d6fbb2ae9d540df14 (patch) | |
tree | 5d9641ee00639c05b815cbc26061744e039552d5 /django/db/backends/postgresql/operations.py | |
parent | 8d018231ac64c044c9740b79c0acf6d0abd7c513 (diff) | |
download | django-ee005328c8eec5c013c6bf9d6fbb2ae9d540df14.tar.gz |
Fixed #31640 -- Made Trunc() truncate datetimes to Date/TimeField in a specific timezone.
Diffstat (limited to 'django/db/backends/postgresql/operations.py')
-rw-r--r-- | django/db/backends/postgresql/operations.py | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index 1ce5755bf5..8d19872bea 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -38,7 +38,8 @@ class DatabaseOperations(BaseDatabaseOperations): else: return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name) - def date_trunc_sql(self, lookup_type, field_name): + def date_trunc_sql(self, lookup_type, field_name, tzname=None): + field_name = self._convert_field_to_tz(field_name, tzname) # https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) @@ -50,7 +51,7 @@ class DatabaseOperations(BaseDatabaseOperations): return tzname def _convert_field_to_tz(self, field_name, tzname): - if settings.USE_TZ: + if tzname and settings.USE_TZ: field_name = "%s AT TIME ZONE '%s'" % (field_name, self._prepare_tzname_delta(tzname)) return field_name @@ -71,7 +72,8 @@ class DatabaseOperations(BaseDatabaseOperations): # https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) - def time_trunc_sql(self, lookup_type, field_name): + def time_trunc_sql(self, lookup_type, field_name, tzname=None): + field_name = self._convert_field_to_tz(field_name, tzname) return "DATE_TRUNC('%s', %s)::time" % (lookup_type, field_name) def deferrable_sql(self): |