summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2011-11-18 13:01:06 +0000
committerAymeric Augustin <aymeric.augustin@m4x.org>2011-11-18 13:01:06 +0000
commit9b1cb755a28f020e27d4268c214b25315d4de42e (patch)
tree2ff0827176f0eb49defa4ce7ce10164f2fc26e86 /django/forms
parent01f70349c9ef23d6751437dcd57d2efc193b2661 (diff)
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
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/fields.py18
-rw-r--r--django/forms/util.py31
-rw-r--r--django/forms/widgets.py3
3 files changed, 46 insertions, 6 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 7f8bac5052..0ba7467191 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -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
diff --git a/django/forms/util.py b/django/forms/util.py
index 85baffa005..886f08e581 100644
--- a/django/forms/util.py
+++ b/django/forms/util.py
@@ -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
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index 8c1e20b179..f756957960 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -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]