mirror of https://github.com/django/django.git
Refs #32966 -- Added _to_naive() and _get_naive_now() for use in DateTimeCheckMixin classes.
This commit is contained in:
parent
542e749475
commit
eebebfe0a9
|
@ -1103,6 +1103,16 @@ class CommaSeparatedIntegerField(CharField):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _to_naive(value):
|
||||||
|
if timezone.is_aware(value):
|
||||||
|
value = timezone.make_naive(value, timezone.utc)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _get_naive_now():
|
||||||
|
return _to_naive(timezone.now())
|
||||||
|
|
||||||
|
|
||||||
class DateTimeCheckMixin:
|
class DateTimeCheckMixin:
|
||||||
|
|
||||||
def check(self, **kwargs):
|
def check(self, **kwargs):
|
||||||
|
@ -1161,13 +1171,10 @@ class DateField(DateTimeCheckMixin, Field):
|
||||||
if not self.has_default():
|
if not self.has_default():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
now = timezone.now()
|
now = _get_naive_now()
|
||||||
if not timezone.is_naive(now):
|
|
||||||
now = timezone.make_naive(now, timezone.utc)
|
|
||||||
value = self.default
|
value = self.default
|
||||||
if isinstance(value, datetime.datetime):
|
if isinstance(value, datetime.datetime):
|
||||||
if not timezone.is_naive(value):
|
value = _to_naive(value)
|
||||||
value = timezone.make_naive(value, timezone.utc)
|
|
||||||
value = value.date()
|
value = value.date()
|
||||||
elif isinstance(value, datetime.date):
|
elif isinstance(value, datetime.date):
|
||||||
# Nothing to do, as dates don't have tz information
|
# Nothing to do, as dates don't have tz information
|
||||||
|
@ -1301,16 +1308,13 @@ class DateTimeField(DateField):
|
||||||
if not self.has_default():
|
if not self.has_default():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
now = timezone.now()
|
now = _get_naive_now()
|
||||||
if not timezone.is_naive(now):
|
|
||||||
now = timezone.make_naive(now, timezone.utc)
|
|
||||||
value = self.default
|
value = self.default
|
||||||
if isinstance(value, datetime.datetime):
|
if isinstance(value, datetime.datetime):
|
||||||
second_offset = datetime.timedelta(seconds=10)
|
second_offset = datetime.timedelta(seconds=10)
|
||||||
lower = now - second_offset
|
lower = now - second_offset
|
||||||
upper = now + second_offset
|
upper = now + second_offset
|
||||||
if timezone.is_aware(value):
|
value = _to_naive(value)
|
||||||
value = timezone.make_naive(value, timezone.utc)
|
|
||||||
elif isinstance(value, datetime.date):
|
elif isinstance(value, datetime.date):
|
||||||
second_offset = datetime.timedelta(seconds=10)
|
second_offset = datetime.timedelta(seconds=10)
|
||||||
lower = now - second_offset
|
lower = now - second_offset
|
||||||
|
@ -2197,23 +2201,19 @@ class TimeField(DateTimeCheckMixin, Field):
|
||||||
if not self.has_default():
|
if not self.has_default():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
now = timezone.now()
|
now = _get_naive_now()
|
||||||
if not timezone.is_naive(now):
|
|
||||||
now = timezone.make_naive(now, timezone.utc)
|
|
||||||
value = self.default
|
value = self.default
|
||||||
if isinstance(value, datetime.datetime):
|
if isinstance(value, datetime.datetime):
|
||||||
second_offset = datetime.timedelta(seconds=10)
|
second_offset = datetime.timedelta(seconds=10)
|
||||||
lower = now - second_offset
|
lower = now - second_offset
|
||||||
upper = now + second_offset
|
upper = now + second_offset
|
||||||
if timezone.is_aware(value):
|
value = _to_naive(value)
|
||||||
value = timezone.make_naive(value, timezone.utc)
|
|
||||||
elif isinstance(value, datetime.time):
|
elif isinstance(value, datetime.time):
|
||||||
second_offset = datetime.timedelta(seconds=10)
|
second_offset = datetime.timedelta(seconds=10)
|
||||||
lower = now - second_offset
|
lower = now - second_offset
|
||||||
upper = now + second_offset
|
upper = now + second_offset
|
||||||
value = datetime.datetime.combine(now.date(), value)
|
value = datetime.datetime.combine(now.date(), value)
|
||||||
if timezone.is_aware(value):
|
value = _to_naive(value)
|
||||||
value = timezone.make_naive(value, timezone.utc)
|
|
||||||
else:
|
else:
|
||||||
# No explicit time / datetime value -- no checks necessary
|
# No explicit time / datetime value -- no checks necessary
|
||||||
return []
|
return []
|
||||||
|
|
Loading…
Reference in New Issue