﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29754	Trunc() should allow passing is_dst resolution to avoid NonExistentTimeError/AmbiguousTimeError	Alexander Holmbäck	Alexander Holmbäck	"When `Trunc()` truncates to a nonexisting or ambiguous datetime, the exception raised by pytz remains unhandled. The expected behavior would, IMO, be to not check the validity of truncated dates.

This test for example:
{{{#!python
import datetime
import pytz

from django.db.models.functions import Trunc
from django.test import TestCase
from django.utils import timezone

from .models import Log


class TestTruncateToInvalidTime(TestCase):

    def test_truncate_to_dst_ends_stockholm(self):
        tzinfo = pytz.timezone('Europe/Stockholm')
        timestamp = datetime.datetime(2018, 10, 28, 2, tzinfo=tzinfo)
        Log.objects.create(timestamp=timestamp)
        logs = Log.objects.annotate(day=Trunc('timestamp', 'hour')).all()

        timezone.activate(tzinfo)
        self.assertEqual(logs[0].day.day, 28)
}}}

Results in the following error:
{{{
======================================================================
ERROR: test_truncate_to_dst_ends_stockholm (trunc.tests.TestTruncateInvalidTime)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/alex/tickets/trunc/tests.py"", line 47, in test_truncate_to_dst_ends_stockholm
    self.assertEqual(logs[0].day.day, 28)
  File ""/home/alex/django/django/db/models/query.py"", line 303, in __getitem__
    qs._fetch_all()
  File ""/home/alex/django/django/db/models/query.py"", line 1190, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/home/alex/django/django/db/models/query.py"", line 64, in __iter__
    for row in compiler.results_iter(results):
  File ""/home/alex/django/django/db/models/sql/compiler.py"", line 1013, in apply_converters
    value = converter(value, expression, connection)
  File ""/home/alex/django/django/db/models/functions/datetime.py"", line 225, in convert_value
    value = timezone.make_aware(value, self.tzinfo)
  File ""/home/alex/django/django/utils/timezone.py"", line 270, in make_aware
    return timezone.localize(value, is_dst=is_dst)
  File ""/home/alex/.virtualenvs/djangodev/lib/python3.6/site-packages/pytz/tzinfo.py"", line 363, in localize
    raise AmbiguousTimeError(dt)
pytz.exceptions.AmbiguousTimeError: 2018-10-28 02:00:00
}}}"	New feature	closed	Database layer (models, ORM)	dev	Normal	fixed	pytz, Trunc, is_dst		Ready for checkin	1	0	0	0	0	0
