Fixed #17742 -- Handled aware datetimes in DateField

Converted aware datetimes to the default time zone before using them
in the context of a DateField.
This commit is contained in:
Aymeric Augustin 2012-05-01 10:40:25 +02:00
parent 4fdd378beb
commit 46b082e05c
3 changed files with 26 additions and 2 deletions

View File

@ -667,6 +667,11 @@ class DateField(Field):
if value is None: if value is None:
return value return value
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
if settings.USE_TZ and timezone.is_aware(value):
# Convert aware datetimes to the current time zone
# before casting them to dates (#17742).
default_timezone = timezone.get_default_timezone()
value = timezone.make_naive(value, default_timezone)
return value.date() return value.date()
if isinstance(value, datetime.date): if isinstance(value, datetime.date):
return value return value

View File

@ -16,3 +16,6 @@ class SessionEvent(models.Model):
class Timestamp(models.Model): class Timestamp(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True)
class AllDayEvent(models.Model):
day = models.DateField()

View File

@ -23,7 +23,7 @@ from django.utils.tzinfo import FixedOffset
from django.utils.unittest import skipIf, skipUnless from django.utils.unittest import skipIf, skipUnless
from .forms import EventForm, EventSplitForm, EventModelForm from .forms import EventForm, EventSplitForm, EventModelForm
from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp, AllDayEvent
# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time) # These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
@ -244,6 +244,14 @@ class LegacyDatabaseTests(TestCase):
[event], [event],
transform=lambda d: d) transform=lambda d: d)
def test_filter_date_field_with_aware_datetime(self):
# Regression test for #17742
day = datetime.date(2011, 9, 1)
event = AllDayEvent.objects.create(day=day)
# This is 2011-09-02T01:30:00+03:00 in EAT
dt = datetime.datetime(2011, 9, 1, 22, 30, 0, tzinfo=UTC)
self.assertTrue(AllDayEvent.objects.filter(day__gte=dt).exists())
@override_settings(TIME_ZONE='Africa/Nairobi', USE_TZ=True) @override_settings(TIME_ZONE='Africa/Nairobi', USE_TZ=True)
class NewDatabaseTests(TestCase): class NewDatabaseTests(TestCase):
@ -456,8 +464,16 @@ class NewDatabaseTests(TestCase):
[event], [event],
transform=lambda d: d) transform=lambda d: d)
def test_filter_date_field_with_aware_datetime(self):
# Regression test for #17742
day = datetime.date(2011, 9, 1)
event = AllDayEvent.objects.create(day=day)
# This is 2011-09-02T01:30:00+03:00 in EAT
dt = datetime.datetime(2011, 9, 1, 22, 30, 0, tzinfo=UTC)
self.assertFalse(AllDayEvent.objects.filter(day__gte=dt).exists())
def test_null_datetime(self): def test_null_datetime(self):
# Regression for #17294 # Regression test for #17294
e = MaybeEvent.objects.create() e = MaybeEvent.objects.create()
self.assertEqual(e.dt, None) self.assertEqual(e.dt, None)