Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17134: Corrected Python 2.5 fallback code for parsing microsec…

…onds in time values. Thanks aaugustin and jcd.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17092 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0db571b417ce9f9f10e30fae8513fbeaebd55fa7 1 parent b45099e
Karen Tracey authored
6  django/forms/fields.py
@@ -340,11 +340,13 @@ def to_python(self, value):
340 340
                     return self.strptime(value, format)
341 341
                 except ValueError:
342 342
                     if format.endswith('.%f'):
343  
-                        if value.count('.') != 1:
  343
+                        # Compatibility with datetime in pythons < 2.6.
  344
+                        # See: http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
  345
+                        if value.count('.') != format.count('.'):
344 346
                             continue
345 347
                         try:
346 348
                             datetime_str, usecs_str = value.rsplit('.', 1)
347  
-                            usecs = int(usecs_str)
  349
+                            usecs = int(usecs_str[:6].ljust(6, '0'))
348 350
                             dt = datetime.datetime.strptime(datetime_str, format[:-3])
349 351
                             return dt.replace(microsecond=usecs)
350 352
                         except ValueError:
4  tests/regressiontests/forms/tests/fields.py
@@ -407,6 +407,7 @@ def test_datetimefield_1(self):
407 407
         self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)))
408 408
         self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)))
409 409
         self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45, 200), f.clean('2006-10-25 14:30:45.000200'))
  410
+        self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45, 200), f.clean('2006-10-25 14:30:45.0002'))
410 411
         self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean('2006-10-25 14:30:45'))
411 412
         self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30:00'))
412 413
         self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30'))
@@ -452,6 +453,9 @@ def test_datetimefield_4(self):
452 453
         self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 10/25/06 '))
453 454
         self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '   ')
454 455
 
  456
+    def test_datetimefield_5(self):
  457
+        f = DateTimeField(input_formats=[u'%Y.%m.%d %H:%M:%S.%f'])
  458
+        self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45, 200), f.clean('2006.10.25 14:30:45.0002'))
455 459
     # RegexField ##################################################################
456 460
 
457 461
     def test_regexfield_1(self):

0 notes on commit 0db571b

Please sign in to comment.
Something went wrong with that request. Please try again.