Fixed #17134: Corrected Python 2.5 fallback code for parsing microseconds in time values. Thanks aaugustin and jcd.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17092 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2011-11-13 00:43:02 +00:00
parent b45099eefb
commit 0db571b417
2 changed files with 8 additions and 2 deletions

View File

@ -340,11 +340,13 @@ class BaseTemporalField(Field):
return self.strptime(value, format) return self.strptime(value, format)
except ValueError: except ValueError:
if format.endswith('.%f'): if format.endswith('.%f'):
if value.count('.') != 1: # Compatibility with datetime in pythons < 2.6.
# See: http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
if value.count('.') != format.count('.'):
continue continue
try: try:
datetime_str, usecs_str = value.rsplit('.', 1) datetime_str, usecs_str = value.rsplit('.', 1)
usecs = int(usecs_str) usecs = int(usecs_str[:6].ljust(6, '0'))
dt = datetime.datetime.strptime(datetime_str, format[:-3]) dt = datetime.datetime.strptime(datetime_str, format[:-3])
return dt.replace(microsecond=usecs) return dt.replace(microsecond=usecs)
except ValueError: except ValueError:

View File

@ -407,6 +407,7 @@ class FieldsTests(SimpleTestCase):
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))) self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)))
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))) self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)))
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45, 200), f.clean('2006-10-25 14:30:45.000200')) self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45, 200), f.clean('2006-10-25 14:30:45.000200'))
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45, 200), f.clean('2006-10-25 14:30:45.0002'))
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean('2006-10-25 14:30:45')) self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean('2006-10-25 14:30:45'))
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30:00')) self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30:00'))
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30')) self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30'))
@ -452,6 +453,9 @@ class FieldsTests(SimpleTestCase):
self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 10/25/06 ')) self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 10/25/06 '))
self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, ' ') self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, ' ')
def test_datetimefield_5(self):
f = DateTimeField(input_formats=[u'%Y.%m.%d %H:%M:%S.%f'])
self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45, 200), f.clean('2006.10.25 14:30:45.0002'))
# RegexField ################################################################## # RegexField ##################################################################
def test_regexfield_1(self): def test_regexfield_1(self):