1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #1443 -- Django's various bits now support dates before 1900. Thanks to SmileyChris, Chris Green, Fredrik Lundh and others for patches and design help

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7946 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2008-07-18 03:47:27 +00:00
parent f6fafc02c8
commit df2b19cc17
9 changed files with 162 additions and 19 deletions

View File

@@ -23,6 +23,7 @@ from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy, ugettext as _
from django.utils.encoding import smart_unicode, force_unicode, smart_str
from django.utils.maxlength import LegacyMaxlength
from django.utils import datetime_safe
class NOT_PROVIDED:
pass
@@ -557,7 +558,7 @@ class DateField(Field):
if lookup_type in ('range', 'in'):
value = [smart_unicode(v) for v in value]
elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte') and hasattr(value, 'strftime'):
value = value.strftime('%Y-%m-%d')
value = datetime_safe.new_date(value).strftime('%Y-%m-%d')
else:
value = smart_unicode(value)
return Field.get_db_prep_lookup(self, lookup_type, value)
@@ -589,7 +590,7 @@ class DateField(Field):
# Casts dates into string format for entry into database.
if value is not None:
try:
value = value.strftime('%Y-%m-%d')
value = datetime_safe.new_date(value).strftime('%Y-%m-%d')
except AttributeError:
# If value is already a string it won't have a strftime method,
# so we'll just let it pass through.
@@ -601,7 +602,11 @@ class DateField(Field):
def flatten_data(self, follow, obj=None):
val = self._get_val_from_obj(obj)
return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
if val is None:
data = ''
else:
data = datetime_safe.new_date(val).strftime("%Y-%m-%d")
return {self.attname: data}
def formfield(self, **kwargs):
defaults = {'form_class': forms.DateField}
@@ -668,8 +673,13 @@ class DateTimeField(DateField):
def flatten_data(self,follow, obj = None):
val = self._get_val_from_obj(obj)
date_field, time_field = self.get_manipulator_field_names('')
return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''),
time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
if val is None:
date_data = time_data = ''
else:
d = datetime_safe.new_datetime(val)
date_data = d.strftime('%Y-%m-%d')
time_data = d.strftime('%H:%M:%S')
return {date_field: date_data, time_field: time_data}
def formfield(self, **kwargs):
defaults = {'form_class': forms.DateTimeField}