Re: [Django] #29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError

2018-09-14 Thread Django
#29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError
-+-
 Reporter:  Alexander Holmbäck   |Owner:  nobody
 Type:  Bug  |   Status:  new
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:  pytz, Trunc()| Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-

Comment (by Alexander Holmbäck):

 Thanks Tim for the valid input, but the situation remains even with dates
 constructed according to pytz documentation, which I hadn't carefully read
 ;-)

 Btw, here's possible solution:
 [https://github.com/django/django/pull/10390 PR].

 If this is a no-go, I think the test
 [https://github.com/django/django/blob/master/tests/admin_views/tests.py#L942
 admin_views.tests.AdminViewBasicTest.test_date_hierarchy_timezone_dst]
 should be changed so that it expects an exception. Otherwise the date
 hierarchy tag is bound to only deal with non-local times (which leads to
 #29724).

-- 
Ticket URL: 
Django 
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 post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.31c2033ec27ca41d4ce105727db115e7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError

2018-09-13 Thread Django
#29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError
-+-
 Reporter:  Alexander Holmbäck   |Owner:  nobody
 Type:  Bug  |   Status:  new
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:  pytz, Trunc()| Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-

Comment (by Simon Charette):

 The `AmbiguousTimeError` case could be worked around by adding a
 `Trunc(is_dst=None)` optional argument that would be passed down to
 `localize`. That would make the API a bit more usable when tzinfo is
 provided and you know what you're after.

 I'm afraid there isn't much that can be done for `NonExistentTimeError`
 though.

-- 
Ticket URL: 
Django 
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 post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.bf5f4962af740d27bd6d170272ae5e35%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError

2018-09-13 Thread Django
#29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError
-+-
 Reporter:  Alexander Holmbäck   |Owner:  nobody
 Type:  Bug  |   Status:  new
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:  pytz, Trunc()| Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-

Comment (by Tim Graham):

 I don't have much expertise but reading the documentation, it sounds like
 you may be [https://docs.djangoproject.com/en/dev/topics/i18n/timezones
 /#interpretation-of-naive-datetime-objects creating invalid data]. Django
 converts datetimes when `USE_TZ` is activate, I don't think it can hide
 that exception. Did you carefully review the pytz documentation which
 states, "Unfortunately using the tzinfo argument of the standard datetime
 constructors ‘’does not work’’ with pytz for many timezones."

-- 
Ticket URL: 
Django 
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 post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.23fec88e135e0b5c1eb44d2aa8c0898f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError (was: Trunc() doesn't handle NonExistingTimeError/AmbiguousTimeError)

2018-09-13 Thread Django
#29754: Trunc() doesn't handle NonExistentTimeError/AmbiguousTimeError
-+-
 Reporter:  Alexander Holmbäck   |Owner:  nobody
 Type:  Bug  |   Status:  new
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:  pytz, Trunc()| 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 Alexander Holmbäck:

Old description:

> When `Trunc()` truncates to a nonexisting or ambiguous datetime, the
> exception raised by pytz remains unhandled. The expected behavior would,
> IMO, be to use naive datetimes for truncated dates, or adjust to closest
> existing non-ambiguous datetime.
>
> 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 TestTruncateToNonExistingTime(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.TestTruncateToNonExistingTime)
> --
> 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 description:

 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)