Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17742 -- Handled aware datetimes in DateField

Converted aware datetimes to the default time zone before using them
in the context of a DateField.
  • Loading branch information...
commit 46b082e05ccedffeb623028caf70adbaba7bce6f 1 parent 4fdd378
@aaugustin aaugustin authored
View
5 django/db/models/fields/__init__.py
@@ -667,6 +667,11 @@ def to_python(self, value):
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
View
3  tests/modeltests/timezones/models.py
@@ -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()
View
20 tests/modeltests/timezones/tests.py
@@ -23,7 +23,7 @@
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 @@ def test_raw_sql(self):
[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 @@ def test_raw_sql(self):
[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)
Please sign in to comment.
Something went wrong with that request. Please try again.