#31640: Trunc() function take tzinfo param into account only when
DateTimeField()
are used as output_field
-------------------------------------+-------------------------------------
Reporter: Serhii Romanov | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: trunc timezone tz | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* stage: Unreviewed => Accepted
Comment:
It looks like the logic should be based off `self.lhs.output_field`
instead.
Assuming you are using PostgreSQL does the following patch addresses your
issue?
{{{#!diff
diff --git a/django/db/backends/postgresql/operations.py
b/django/db/backends/postgresql/operations.py
index c67062a4a7..3877a38b43 100644
--- a/django/db/backends/postgresql/operations.py
+++ b/django/db/backends/postgresql/operations.py
@@ -38,10 +38,6 @@ class DatabaseOperations(BaseDatabaseOperations):
else:
return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
- def date_trunc_sql(self, lookup_type, field_name):
- # https://www.postgresql.org/docs/current/functions-datetime.html
#FUNCTIONS-DATETIME-TRUNC
- return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
-
def _prepare_tzname_delta(self, tzname):
if '+' in tzname:
return tzname.replace('+', '-')
@@ -50,7 +46,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 +67,16 @@ 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 date_trunc_sql(self, lookup_type, field_name, tzname=None):
+ if tzname:
+ 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)
+
+ def time_trunc_sql(self, lookup_type, field_name, tzname=None):
+ if tzname:
+ field_name = self._convert_field_to_tz(field_name, tzname)
+ field_name = self._convert_field_to_tz(field_name, tzname)
return "DATE_TRUNC('%s', %s)::time" % (lookup_type, field_name)
def json_cast_text_sql(self, field_name):
diff --git a/django/db/models/functions/datetime.py
b/django/db/models/functions/datetime.py
index b6594b043b..52714aa9bf 100644
--- a/django/db/models/functions/datetime.py
+++ b/django/db/models/functions/datetime.py
@@ -191,13 +191,15 @@ class TruncBase(TimezoneMixin, Transform):
def as_sql(self, compiler, connection):
inner_sql, inner_params = compiler.compile(self.lhs)
- if isinstance(self.output_field, DateTimeField):
+ tzname = None
+ if isinstance(self.lhs.output_field, DateTimeField):
tzname = self.get_tzname()
+ if isinstance(self.output_field, DateTimeField):
sql = connection.ops.datetime_trunc_sql(self.kind, inner_sql,
tzname)
elif isinstance(self.output_field, DateField):
- sql = connection.ops.date_trunc_sql(self.kind, inner_sql)
+ sql = connection.ops.date_trunc_sql(self.kind, inner_sql,
tzname)
elif isinstance(self.output_field, TimeField):
- sql = connection.ops.time_trunc_sql(self.kind, inner_sql)
+ sql = connection.ops.time_trunc_sql(self.kind, inner_sql,
tzname)
else:
raise ValueError('Trunc only valid on DateField, TimeField,
or DateTimeField.')
return sql, inner_params
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31640#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/071.f1ddb49434c577bcec909415fdf75115%40djangoproject.com.