Ticket #18217: 46b082e0-backport.patch

File 46b082e0-backport.patch, 3.5 KB (added by Aymeric Augustin, 13 years ago)
  • django/db/models/fields/__init__.py

    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):  
    666666        if value is None:
    667667            return value
    668668        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)
    669674            return value.date()
    670675        if isinstance(value, datetime.date):
    671676            return value
  • tests/modeltests/timezones/models.py

    diff --git a/tests/modeltests/timezones/models.py b/tests/modeltests/timezones/models.py
    index f0cc792..c49e42f 100644
    a b class SessionEvent(models.Model):  
    1616class Timestamp(models.Model):
    1717    created = models.DateTimeField(auto_now_add=True)
    1818    updated = models.DateTimeField(auto_now=True)
     19
     20class AllDayEvent(models.Model):
     21    day = models.DateField()
  • tests/modeltests/timezones/tests.py

    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  
    2525from django.utils.unittest import skipIf, skipUnless
    2626
    2727from .forms import EventForm, EventSplitForm, EventModelForm
    28 from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp
     28from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp, AllDayEvent
    2929
    3030
    3131# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
    class LegacyDatabaseTests(BaseDateTimeTests):  
    270270                [event],
    271271                transform=lambda d: d)
    272272
     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
    273281LegacyDatabaseTests = override_settings(USE_TZ=False)(LegacyDatabaseTests)
    274282
    275283
    class NewDatabaseTests(BaseDateTimeTests):  
    489497                [event],
    490498                transform=lambda d: d)
    491499
     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
    492508    def test_null_datetime(self):
    493         # Regression for #17294
     509        # Regression test for #17294
    494510        e = MaybeEvent.objects.create()
    495511        self.assertEqual(e.dt, None)
    496512
Back to Top