mirror of
https://github.com/django/django.git
synced 2025-10-26 07:06:08 +00:00
Added support for time zones. Thanks Luke Plant for the review. Fixed #2626.
For more information on this project, see this thread: http://groups.google.com/group/django-developers/browse_thread/thread/cf0423bbb85b1bbf git-svn-id: http://code.djangoproject.com/svn/django/trunk@17106 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -17,7 +17,7 @@ except ImportError:
|
||||
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.forms.util import ErrorList
|
||||
from django.forms.util import ErrorList, from_current_timezone, to_current_timezone
|
||||
from django.forms.widgets import (TextInput, PasswordInput, HiddenInput,
|
||||
MultipleHiddenInput, ClearableFileInput, CheckboxInput, Select,
|
||||
NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput,
|
||||
@@ -409,6 +409,11 @@ class DateTimeField(BaseTemporalField):
|
||||
'invalid': _(u'Enter a valid date/time.'),
|
||||
}
|
||||
|
||||
def prepare_value(self, value):
|
||||
if isinstance(value, datetime.datetime):
|
||||
value = to_current_timezone(value)
|
||||
return value
|
||||
|
||||
def to_python(self, value):
|
||||
"""
|
||||
Validates that the input can be converted to a datetime. Returns a
|
||||
@@ -417,9 +422,10 @@ class DateTimeField(BaseTemporalField):
|
||||
if value in validators.EMPTY_VALUES:
|
||||
return None
|
||||
if isinstance(value, datetime.datetime):
|
||||
return value
|
||||
return from_current_timezone(value)
|
||||
if isinstance(value, datetime.date):
|
||||
return datetime.datetime(value.year, value.month, value.day)
|
||||
result = datetime.datetime(value.year, value.month, value.day)
|
||||
return from_current_timezone(result)
|
||||
if isinstance(value, list):
|
||||
# Input comes from a SplitDateTimeWidget, for example. So, it's two
|
||||
# components: date and time.
|
||||
@@ -428,7 +434,8 @@ class DateTimeField(BaseTemporalField):
|
||||
if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES:
|
||||
return None
|
||||
value = '%s %s' % tuple(value)
|
||||
return super(DateTimeField, self).to_python(value)
|
||||
result = super(DateTimeField, self).to_python(value)
|
||||
return from_current_timezone(result)
|
||||
|
||||
def strptime(self, value, format):
|
||||
return datetime.datetime.strptime(value, format)
|
||||
@@ -979,7 +986,8 @@ class SplitDateTimeField(MultiValueField):
|
||||
raise ValidationError(self.error_messages['invalid_date'])
|
||||
if data_list[1] in validators.EMPTY_VALUES:
|
||||
raise ValidationError(self.error_messages['invalid_time'])
|
||||
return datetime.datetime.combine(*data_list)
|
||||
result = datetime.datetime.combine(*data_list)
|
||||
return from_current_timezone(result)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from django.conf import settings
|
||||
from django.utils.html import conditional_escape
|
||||
from django.utils.encoding import StrAndUnicode, force_unicode
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
# Import ValidationError so that it can be imported from this
|
||||
# module to maintain backwards compatibility.
|
||||
@@ -52,3 +55,31 @@ class ErrorList(list, StrAndUnicode):
|
||||
def __repr__(self):
|
||||
return repr([force_unicode(e) for e in self])
|
||||
|
||||
# Utilities for time zone support in DateTimeField et al.
|
||||
|
||||
def from_current_timezone(value):
|
||||
"""
|
||||
When time zone support is enabled, convert naive datetimes
|
||||
entered in the current time zone to aware datetimes.
|
||||
"""
|
||||
if settings.USE_TZ and value is not None and timezone.is_naive(value):
|
||||
current_timezone = timezone.get_current_timezone()
|
||||
try:
|
||||
return timezone.make_aware(value, current_timezone)
|
||||
except Exception, e:
|
||||
raise ValidationError(_('%(datetime)s couldn\'t be interpreted '
|
||||
'in time zone %(current_timezone)s; it '
|
||||
'may be ambiguous or it may not exist.')
|
||||
% {'datetime': value,
|
||||
'current_timezone': current_timezone})
|
||||
return value
|
||||
|
||||
def to_current_timezone(value):
|
||||
"""
|
||||
When time zone support is enabled, convert aware datetimes
|
||||
to naive dateimes in the current time zone for display.
|
||||
"""
|
||||
if settings.USE_TZ and value is not None and timezone.is_aware(value):
|
||||
current_timezone = timezone.get_current_timezone()
|
||||
return timezone.make_naive(value, current_timezone)
|
||||
return value
|
||||
|
||||
@@ -10,7 +10,7 @@ from itertools import chain
|
||||
from urlparse import urljoin
|
||||
|
||||
from django.conf import settings
|
||||
from django.forms.util import flatatt
|
||||
from django.forms.util import flatatt, to_current_timezone
|
||||
from django.utils.datastructures import MultiValueDict, MergeDict
|
||||
from django.utils.html import escape, conditional_escape
|
||||
from django.utils.translation import ugettext, ugettext_lazy
|
||||
@@ -847,6 +847,7 @@ class SplitDateTimeWidget(MultiWidget):
|
||||
|
||||
def decompress(self, value):
|
||||
if value:
|
||||
value = to_current_timezone(value)
|
||||
return [value.date(), value.time().replace(microsecond=0)]
|
||||
return [None, None]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user