[Django] #31948: TruncDate Unexpected Behavior

43 views
Skip to first unread message

Django

unread,
Aug 26, 2020, 7:59:28 AM8/26/20
to django-...@googlegroups.com
#31948: TruncDate Unexpected Behavior
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
joeyjoejoejr |
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) |
Severity: Normal | Keywords: TruncDate
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
== Description
TruncDate inherits from TruncBase, which includes the TimeZone mixin.
This should allow a developer to pass in a tzinfo object to be used when
converting TruncDate, but it actually uses the return value from
get_current_timezone_name() unconditionally and completely discards the
passed in timezone info object. The result is that attempting to
aggregate by date doesn't work for timezones other than the global
django.utils.timezone. For example I can't have the django app be in UTC
and pass the "America/New_York" timezone in.

Here's the offending line:
[https://github.com/django/django/blob/master/django/db/models/functions/datetime.py#L295]
Note, that a similar issue is happening in TruncTime.

Here's the method I would expect it to use:
[https://github.com/django/django/blob/master/django/db/models/functions/datetime.py#L17]

== Example

{{{
class TimeSlots(models.Model):
start_at = models.DateTimeField()

tz = pytz.timezone("America/New_York")
report = (
TimeSlots.objects.annotate(start_date=TruncDate("start_at", tzinfo=tz))
.values("start_date")
.annotate(timeslot_count=Count("id"))
.values("start_date", "timeslot_count")
)
}}}

I would expect this to work, but currently the results are wrong for any
timezone other than the one returned by django.utils.timezone.

=== Workaround
There was a workaround for me. I was able to use TruncDay and then
convert the DateTimes returned outside of the database, but I found no way
to convert from DateTime to Date in the database. Maybe a Cast would
work, but I would expect TrunkDate to work.

--
Ticket URL: <https://code.djangoproject.com/ticket/31948>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 26, 2020, 9:42:36 AM8/26/20
to django-...@googlegroups.com
#31948: TruncDate Unexpected Behavior
-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: TruncDate | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Serhii Romanov):

Please check https://code.djangoproject.com/ticket/31640
Is it related to your issue?

--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:1>

Django

unread,
Aug 26, 2020, 10:25:46 AM8/26/20
to django-...@googlegroups.com
#31948: TruncDate Unexpected Behavior
-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: TruncDate | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Joe Jackson):

Replying to [comment:1 Serhii Romanov]:


> Please check https://code.djangoproject.com/ticket/31640
> Is it related to your issue?

It is related, but not exactly my issue. That patch updates the TruncBase
as_sql method, and world work. But the TruncDate and TruncTime classes
override as_sql and don't call super. So the timezone passed in is
ignored.

I can attempt to submit a patch for this issue. I'll just need to take a
minute and read up on how to properly do that.

--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:2>

Django

unread,
Aug 26, 2020, 10:27:51 AM8/26/20
to django-...@googlegroups.com
#31948: TruncDate Unexpected Behavior
-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: TruncDate | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Joe Jackson:

Old description:

New description:

== Example

work, but I would expect TruncDate to work.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:3>

Django

unread,
Aug 27, 2020, 2:27:04 AM8/27/20
to django-...@googlegroups.com
#31948: Add support for tzinfo parameter to TruncDate() and TruncTime().

-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* cc: Aymeric Augustin, Josh Smeaton (added)
* version: 3.1 => master
* keywords: TruncDate =>
* type: Bug => New feature
* stage: Unreviewed => Accepted


Comment:

`tzinfo` is not a documented (or tested) parameter of
[https://docs.djangoproject.com/en/3.1/ref/models/database-
functions/#django.db.models.functions.TruncDate TruncDate()] or
[https://docs.djangoproject.com/en/3.1/ref/models/database-
functions/#django.db.models.functions.TruncTime TruncTime()] so it's not a
bug but request for a new feature.

It sounds reasonable to support `tzinfo` in `TruncDate()` and
`TruncTime()`, and I cannot find any argument against it in the original
[https://github.com/django/django/pull/6243 PR] (maybe Josh will remember
sth).

--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:4>

Django

unread,
Aug 27, 2020, 8:13:35 PM8/27/20
to django-...@googlegroups.com
#31948: Add support for tzinfo parameter to TruncDate() and TruncTime().
-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Josh Smeaton):

There was no explicit reason to ignore any passed in tz parameter - it was
just a reimplementation of the existing method:
https://github.com/django/django/commit/2a4af0ea43512370764303d35bc5309f8abce666
#diff-b6b218ec29b7fb6a7d89868a94bfc73eL492

Trunc documents the use of tzinfo:
https://github.com/django/django/commit/2a4af0ea43512370764303d35bc5309f8abce666
#diff-34b63f01d4190c08facabac9c11075ccR512 and it should reasonably apply
to all subclasses of Trunc as well.

I think accept as a bug is the correct decision here.

--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:5>

Django

unread,
Aug 29, 2020, 1:33:58 PM8/29/20
to django-...@googlegroups.com
#31948: Add support for tzinfo parameter to TruncDate() and TruncTime().
-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: Joe
| Jackson
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Joe Jackson):

* owner: nobody => Joe Jackson
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:6>

Django

unread,
Aug 29, 2020, 3:00:44 PM8/29/20
to django-...@googlegroups.com
#31948: Add support for tzinfo parameter to TruncDate() and TruncTime().
-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: Joe
| Jackson
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Joe Jackson):

* has_patch: 0 => 1


Old description:

> work, but I would expect TruncDate to work.

New description:

== Example

work, but I would expect TruncDate to work.

=== Patch
[https://github.com/django/django/pull/13363 PR]

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:7>

Django

unread,
Sep 2, 2020, 2:18:24 AM9/2/20
to django-...@googlegroups.com
#31948: Add support for tzinfo parameter to TruncDate() and TruncTime().
-------------------------------------+-------------------------------------
Reporter: Joe Jackson | Owner: Joe
| Jackson
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"9d5d865fd6e989abe60fdf02e7f97fd4d98178a4" 9d5d865f]:
{{{
#!CommitTicketReference repository=""
revision="9d5d865fd6e989abe60fdf02e7f97fd4d98178a4"
Fixed #31948 -- Added tzinfo parameter to TruncDate() and TruncTime().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31948#comment:8>

Reply all
Reply to author
Forward
0 new messages