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:
parent
4fdd378beb
commit
46b082e05c
|
@ -667,6 +667,11 @@ class DateField(Field):
|
|||
if value is None:
|
||||
return value
|
||||
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()
|
||||
if isinstance(value, datetime.date):
|
||||
return value
|
||||
|
|
|
@ -16,3 +16,6 @@ class SessionEvent(models.Model):
|
|||
class Timestamp(models.Model):
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
|
||||
class AllDayEvent(models.Model):
|
||||
day = models.DateField()
|
||||
|
|
|
@ -23,7 +23,7 @@ from django.utils.tzinfo import FixedOffset
|
|||
from django.utils.unittest import skipIf, skipUnless
|
||||
|
||||
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)
|
||||
|
@ -244,6 +244,14 @@ class LegacyDatabaseTests(TestCase):
|
|||
[event],
|
||||
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)
|
||||
class NewDatabaseTests(TestCase):
|
||||
|
@ -456,8 +464,16 @@ class NewDatabaseTests(TestCase):
|
|||
[event],
|
||||
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):
|
||||
# Regression for #17294
|
||||
# Regression test for #17294
|
||||
e = MaybeEvent.objects.create()
|
||||
self.assertEqual(e.dt, None)
|
||||
|
||||
|
|
Loading…
Reference in New Issue