commit ddb8a7d560b12b4e951b4613eb9921c2b3fd760f
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue May 1 10:40:25 2012 +0200
[1.4.x] Fixed #17742 -- Handled aware datetimes in DateField
Converted aware datetimes to the default time zone before using them
in the context of a DateField.
Backport of 46b082e05ccedffeb623028caf70adbaba7bce6f from master.
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 22546c2..ae68006 100644
a
|
b
|
class DateField(Field):
|
666 | 666 | if value is None: |
667 | 667 | return value |
668 | 668 | if isinstance(value, datetime.datetime): |
| 669 | if settings.USE_TZ and timezone.is_aware(value): |
| 670 | # Convert aware datetimes to the current time zone |
| 671 | # before casting them to dates (#17742). |
| 672 | default_timezone = timezone.get_default_timezone() |
| 673 | value = timezone.make_naive(value, default_timezone) |
669 | 674 | return value.date() |
670 | 675 | if isinstance(value, datetime.date): |
671 | 676 | return value |
diff --git a/tests/modeltests/timezones/models.py b/tests/modeltests/timezones/models.py
index f0cc792..c49e42f 100644
a
|
b
|
class SessionEvent(models.Model):
|
16 | 16 | class Timestamp(models.Model): |
17 | 17 | created = models.DateTimeField(auto_now_add=True) |
18 | 18 | updated = models.DateTimeField(auto_now=True) |
| 19 | |
| 20 | class AllDayEvent(models.Model): |
| 21 | day = models.DateField() |
diff --git a/tests/modeltests/timezones/tests.py b/tests/modeltests/timezones/tests.py
index 171c218..44d2fea 100644
a
|
b
|
from django.utils.tzinfo import FixedOffset
|
25 | 25 | from django.utils.unittest import skipIf, skipUnless |
26 | 26 | |
27 | 27 | from .forms import EventForm, EventSplitForm, EventModelForm |
28 | | from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp |
| 28 | from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp, AllDayEvent |
29 | 29 | |
30 | 30 | |
31 | 31 | # These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time) |
… |
… |
class LegacyDatabaseTests(BaseDateTimeTests):
|
270 | 270 | [event], |
271 | 271 | transform=lambda d: d) |
272 | 272 | |
| 273 | def test_filter_date_field_with_aware_datetime(self): |
| 274 | # Regression test for #17742 |
| 275 | day = datetime.date(2011, 9, 1) |
| 276 | event = AllDayEvent.objects.create(day=day) |
| 277 | # This is 2011-09-02T01:30:00+03:00 in EAT |
| 278 | dt = datetime.datetime(2011, 9, 1, 22, 30, 0, tzinfo=UTC) |
| 279 | self.assertTrue(AllDayEvent.objects.filter(day__gte=dt).exists()) |
| 280 | |
273 | 281 | LegacyDatabaseTests = override_settings(USE_TZ=False)(LegacyDatabaseTests) |
274 | 282 | |
275 | 283 | |
… |
… |
class NewDatabaseTests(BaseDateTimeTests):
|
489 | 497 | [event], |
490 | 498 | transform=lambda d: d) |
491 | 499 | |
| 500 | def test_filter_date_field_with_aware_datetime(self): |
| 501 | # Regression test for #17742 |
| 502 | day = datetime.date(2011, 9, 1) |
| 503 | event = AllDayEvent.objects.create(day=day) |
| 504 | # This is 2011-09-02T01:30:00+03:00 in EAT |
| 505 | dt = datetime.datetime(2011, 9, 1, 22, 30, 0, tzinfo=UTC) |
| 506 | self.assertFalse(AllDayEvent.objects.filter(day__gte=dt).exists()) |
| 507 | |
492 | 508 | def test_null_datetime(self): |
493 | | # Regression for #17294 |
| 509 | # Regression test for #17294 |
494 | 510 | e = MaybeEvent.objects.create() |
495 | 511 | self.assertEqual(e.dt, None) |
496 | 512 | |