DateConverter and dates before 1900
"Andi Albrecht" <albrecht.andi-gM/[email protected]> Wed, 13 Aug 2008 13:06:31 +0200
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Hi everyone,
I've some troubles with the current FormEncode distributed along with
TurboGears (v 1.0.5). In my application I have to validate dates
before 1900 but DateConverter doesn't allow dates before 1900. Are
there any plans to extend the DateConverter?
FWIW, we have mx.DateTime available on our systems, so here's the
patch I'm using now to make things work:
-------------------------------------------------
Index: formencode/validators.py
===================================================================
--- formencode/validators.py (revision 3547)
+++ formencode/validators.py (working copy)
@@ -1863,6 +1863,8 @@
datetime.date(2009, 12, 3)
>>> d.to_python('12/3/2009')
datetime.date(2009, 12, 3)
+ >>> d.to_python('12/3/1809')
+ datetime.date(1809, 12, 3)
>>> d.to_python('2/30/04')
Traceback (most recent call last):
...
@@ -1874,7 +1876,7 @@
>>> d.to_python('1/1/200')
Traceback (most recent call last):
...
- Invalid: Please enter a four-digit year after 1899
+ Invalid: Please enter a four-digit year after 999
If you change ``month_style`` you can get European-style dates::
@@ -1927,7 +1929,8 @@
'invalidDate': _('That is not a valid day (%(exception)s)'),
'unknownMonthName': _("Unknown month name: %(month)s"),
'invalidYear': _('Please enter a number for the year'),
- 'fourDigitYear': _('Please enter a four-digit year after 1899'),
+ 'fourDigitYear': _('Please enter a four-digit year after 999'),
+ 'fourDigitYear1900': _('Please enter a four-digit year after 1899'),
'wrongFormat': _('Please enter the date in the form %(format)s'),
}
@@ -1993,9 +1996,12 @@
year = year + 2000
if year >= 50 and year < 100:
year = year + 1900
- if (year > 20 and year < 50) or (year>99 and year<1900):
+ if (year > 20 and year < 50) or len(str(year)) == 3:
raise Invalid(self.message('fourDigitYear', state),
year, state)
+ if year < 1900 and self.datetime_module != 'mxdatetime':
+ raise Invalid(self.message('fourDigitYear1900', state),
+ year, state)
return year
def convert_month(self, value, state):
Index: tests/test_validators.py
===================================================================
--- tests/test_validators.py (revision 3547)
+++ tests/test_validators.py (working copy)
@@ -97,7 +97,21 @@
try:
date.to_python('20/12/150')
except Invalid, e:
+ assert 'Please enter a four-digit year after 999' in str(e)
+
+def test_date_before_1900():
+ date = DateConverter(month_style='dd/mm/yyy', datetime_module='datetime')
+ d = datetime.date(1808, 12, 20)
+ try:
+ date.to_python('20/12/1808')
+ except Invalid, e:
assert 'Please enter a four-digit year after 1899' in str(e)
+ try:
+ import mx.DateTime
+ mx_present = True
+ except ImportError:
+ mx_present = False
+ date = DateConverter(month_style='dd/mm/yyy', datetime_module='mxdatetime')
def test_foreach_if_missing():
-------------------------------------------------
Regards,
Andi
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/