#29724: Admin date_hierarchy filter by month displays an extra day at timezone
boundary.
-------------------------------------+-------------------------------------
     Reporter:  Lavrenov Ivan        |                    Owner:  Alexander
                                     |  Holmbäck
         Type:  Bug                  |                   Status:  assigned
    Component:  contrib.admin        |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  1
-------------------------------------+-------------------------------------

Comment (by yab):

 Two distinct date_hierarchy bugs : one relate to TZ effects and one
 related to DST effects :

 #29724 (TZ matter)
 When year and month lookup are chosen, if you have an event late in last
 day of the month that is first day of next month in UTC, queryset.dates
 method gives a date_hierarchy choice for next month 1st.
 It seems it can be solved modifiyng
 admin/contrib/templatetags/admin_list.py this way, using **queryset.filter
 and lists that may be very slow** :
 Needs importing:
 {{{
 from django.utils.timezone import make_naive
 from django.conf import settings
 }}}
 and after "elif year_lookup and month_lookup:":
 {{{
                 elif year_lookup and month_lookup:
                         ### This is a way to avoid using queryset.dates
 fucntion, which was giving naive datetimes with problems when the
 monthrange is different between utc and localtime.

                         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).distinct()

                         days = dates_or_datetimes
                         if isinstance(dates_or_datetimes[0],
 datetime.datetime) and settings.USE_TZ:
                                 day_list = []
                                 days = []
                                 for day in dates_or_datetimes:
                                         if make_naive(day).day not in
 day_list:
 day_list.append(make_naive(day).day)
 days.append(make_naive(day))
                         ###
 }}}

 ###30749 (DST matter)
 If the month selected by date_hierarchy is the month of the DST, the
 objects of the last day of the month does not appear in the changelist.
 Changing the way "from_date" is set after ''elif month'' line 165 solves
 my problem: Instead of just adding 32 days with timedelta, I use
 make_aware with tzinfo=None. That way the to_date is set to the correct
 date and time.
 {{{
                                 elif month:
                                         ### The tzinfo changes between
 first and last day of the month when DST applies.  That's why we need to
 make_aware to_date separately from from_date
                                         to_date = make_aware((from_date +
 timedelta(days=32)).replace(day=1,tzinfo=None))
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29724#comment:9>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.df0f4734bbfa453729462123a5e64f48%40djangoproject.com.

Reply via email to