Opened 6 years ago
Closed 5 years ago
#29724 closed Bug (fixed)
Admin date_hierarchy filter by month displays an extra day at timezone boundary.
Reported by: | Lavrenov Ivan | Owned by: | Hasan Ramezani |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description (last modified by )
When I authorized by user with not-UTC timezone, like America/Los_Angeles , and open filter by date in month, I see one extra day, that follows to the first day of the previous month
Attachments (1)
Change History (19)
by , 6 years ago
Attachment: | Снимок экрана 2018-08-29 в 16.59.28.png added |
---|
follow-up: 3 comment:1 by , 6 years ago
Summary: | Bug with time filter in django admin → Admin date_hierarchy filter by month displays an extra day |
---|
Could you be more specific about how to reproduce this? I thought you meant TIME_ZONE = 'Los-Angeles/America'
but I get "Incorrect timezone setting". Which database are you using?
comment:2 by , 6 years ago
The timezone is America/Los_Angeles, sorry for inaccuracy.
I'm using Mysql database. I think it is something сonnected with converstion to local time. I have a record, which in UTC in the 1st September, but in local time it is still in the 31 August.
P.S. Values stored in UTC format
comment:3 by , 6 years ago
Replying to Tim Graham:
Could you be more specific about how to reproduce this? I thought you meant
TIME_ZONE = 'Los-Angeles/America'
but I get "Incorrect timezone setting". Which database are you using?
The timezone is America/Los_Angeles, sorry for inaccuracy.
I'm using Mysql database. I think it is something сonnected with converstion to local time. I have a record, which in UTC in the 1st September, but in local time it is still in the 31 August.
P.S. Values stored in UTC format
comment:4 by , 6 years ago
Description: | modified (diff) |
---|
comment:5 by , 6 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Version: | 2.0 → master |
Yes. I can reproduce this.
- Have a DateTimeField in
date_hierarchy
(and optionally `list_display) - Create an object in the DB for midnight 1st Sep (UTC).
- Set project TIME_ZONE = 'America/Los_Angeles'
- Test object is show in list at Aug 31 5PM.
- Date hierarchy choice is created for 1st September
- Link for that is incorrect:
?<field>__day=1&<field>__month=8&<field>__year=2018
, i.e. Aug 1st.
Presumably error is in `date_hierarchy()` template tag function.
comment:6 by , 6 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Patch needs improvement: | set |
Status: | new → assigned |
comment:7 by , 5 years ago
Summary: | Admin date_hierarchy filter by month displays an extra day → Admin date_hierarchy filter by month displays an extra day at timezone boundary. |
---|
comment:9 by , 5 years ago
Two distinct date_hierarchy bugs : one relate to TZ effects and one related to DST effects :
#29724 (TZ matter)
It may be solved modifying django/contrib/admin/templatetags/admin_list.py using queryset.filter instead of queryset.dates and creating a tz-aware days list :
from django.utils.timezone import make_naive from django.conf import settings
def link (filters):
month_filter=field_name+'__month' year_filter=field_name+'__year' dates_or_datetimes = cl.model.objects.filter(**{year_filter:year_lookup, month_filter:month_lookup}).values_list(field_name, flat=True) days = [] if dates_or_datetimes.count()>0 and isinstance(dates_or_datetimes.first(), datetime.datetime) and settings.USE_TZ: for day in dates_or_datetimes: if make_naive(day).date() not in days: days.append(make_naive(day).date()) else: for day in dates_or_datetimes: if day not in days: days.append(day)
###30749 (DST matter)
In Europe/Paris timezone, on the October DST change, the from_date + timedelta(days=32)).replace(day=1) used in django/contrib/admin/views/main.py to get next month's first day does not select objects of October 31th. They are not shown in the change list when October is selected through date_hierarchy. (steps to reproduce : sqlite, DateField, USE_TZ, TIMEZONE='Europe/Paris', Object date 2019-10-31 or 2018-10-31...).
It seems to be resolved by adding make_aware and tzinfo=None to to_date :
django/contrib/admin/views/main.py:
elif month: to_date = (from_date + timedelta(days=32)).replace(day=1) if settings.USE_TZ: to_date = make_aware(to_date.replace(tzinfo=None))
comment:10 by , 5 years ago
Needs tests: | set |
---|
follow-up: 12 comment:11 by , 5 years ago
OK, thanks for the follow-up yab. TBH I'm still inclined to view these as the same issue: incorrect collection of items at time-period boundaries (demonstrated with DST and TZ examples). However... one-ticket/two-tickets is not so important.
More is that, we need to make sure we have test cases for both these types of example, and then we can make sure any fix addresses both of those.
Super. 👍
comment:12 by , 5 years ago
OK, my proposal passes existing tests. Not sure how to create specific test cases for that purpose. My proposal is above and there : https://github.com/yab228/django/blob/master/django/contrib/admin/templatetags/admin_list.py
follow-up: 14 comment:13 by , 5 years ago
yab: please make a pull request with your suggested changes. (See the contributing guide.)
For tests, please add a test case with the example(s) here. Presumably they should fail with the exisiting code, showing the bug, and then pass with your patch applied.
comment:16 by , 5 years ago
Needs tests: | unset |
---|---|
Owner: | changed from | to
Patch needs improvement: | unset |
representation of bug