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/mysql/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/mysql/operations.py')
-rw-r--r-- | django/db/backends/mysql/operations.py | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index 871c533870..5d2a981226 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -55,7 +55,8 @@ class DatabaseOperations(BaseDatabaseOperations): # EXTRACT returns 1-53 based on ISO-8601 for the week number. return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), 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) fields = { 'year': '%%Y-01-01', 'month': '%%Y-%%m-01', @@ -82,7 +83,7 @@ class DatabaseOperations(BaseDatabaseOperations): return tzname def _convert_field_to_tz(self, field_name, tzname): - if settings.USE_TZ and self.connection.timezone_name != tzname: + if tzname and settings.USE_TZ and self.connection.timezone_name != tzname: field_name = "CONVERT_TZ(%s, '%s', '%s')" % ( field_name, self.connection.timezone_name, @@ -128,7 +129,8 @@ class DatabaseOperations(BaseDatabaseOperations): sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) return sql - 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) fields = { 'hour': '%%H:00:00', 'minute': '%%H:%%i:00', |