Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#17134 closed Bug (fixed)

Wrong parsing of microseconds in !BaseTemporalField.to_python

Reported by: aaugustin Owned by: jcd
Component: Forms Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Lines 345-348 of django/forms/filelds.py read:

                            datetime_str, usecs_str = value.rsplit('.', 1)
                            usecs = int(usecs_str)
                            dt = datetime.datetime.strptime(datetime_str, format[:-3])
                            return dt.replace(microsecond=usecs)

This only works if there are exactly 6 digits after the dot.

I suggest something along the lines of usecs = int(usecs_str[:6].ljust(6, '0')).

Attachments (4)

17134_datetime_bug_tests.diff (1.5 KB) - added by jcd 4 years ago.
Tests that show the bug on python < 2.6
17134_datetime_bug_tests.2.diff (1.5 KB) - added by jcd 4 years ago.
Test to exercise the bug in python < 2.6
17134_datetime_bug_tests.3.diff (1.5 KB) - added by jcd 4 years ago.
Tests that show the bug on python < 2.6
17134_datetime_bug_fix.diff (1.1 KB) - added by jcd 4 years ago.
Fix for the bug.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 4 years ago by aaugustin

  • Easy pickings set
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by calvinspealman

Can you explain or demonstrate how it fails currently? I'm not seeing the problem. maybe an example of a value that won't parse correctly and why?

comment:3 Changed 4 years ago by jcd

  • Owner changed from nobody to jcd
  • Status changed from new to assigned

I just figured it out. %f was added to datetime in python2.6 It should fail in the way this bug lists in python2.5 and lower. I'll check it out.

Last edited 4 years ago by jcd (previous) (diff)

comment:4 Changed 4 years ago by jcd

Another bug in there: It checks to see that there is one and only one '.' in the input before applying the workaround. However, a format like '%Y.%m.%d %H:%M:%S.%f' would require three dots, and would break this. It should instead test that the number of dots in the input match the number of dots in the format, and then apply the fix. I'll make sure that case gets tested as well.

comment:5 Changed 4 years ago by jcd

I'm having trouble getting a 2.5 environment set up right now, but the following regression test patch should exercise the bug, and the other patch should fix it.

Changed 4 years ago by jcd

Tests that show the bug on python < 2.6

Changed 4 years ago by jcd

Test to exercise the bug in python < 2.6

Changed 4 years ago by jcd

Tests that show the bug on python < 2.6

Changed 4 years ago by jcd

Fix for the bug.

comment:6 Changed 4 years ago by jcd

  • Has patch set
  • Resolution set to fixed
  • Status changed from assigned to closed

Use 17134_datetime_bug_tests.3.diff and 17134_datetime_bug_fix.diff. Thanks to Jim Allman, I managed to verify that the tests and fix both do what they're supposed to do in a python2.5 environment.

comment:7 Changed 4 years ago by kmtracey

  • Resolution fixed deleted
  • Status changed from closed to reopened

It's not fixed until it's checked into the repo...

comment:8 Changed 4 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from reopened to closed

In [17092]:

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

comment:9 Changed 4 years ago by jcd

Thanks kmtracey, and sorry for the process faux pas.

Note: See TracTickets for help on using tickets.
Back to Top