Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#17134 closed Bug (fixed)

Wrong parsing of microseconds in !BaseTemporalField.to_python

Reported by: Aymeric Augustin Owned by: Cliff Dyer
Component: Forms Version: dev
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 Cliff Dyer 13 years ago.
Tests that show the bug on python < 2.6
17134_datetime_bug_tests.2.diff (1.5 KB ) - added by Cliff Dyer 13 years ago.
Test to exercise the bug in python < 2.6
17134_datetime_bug_tests.3.diff (1.5 KB ) - added by Cliff Dyer 13 years ago.
Tests that show the bug on python < 2.6
17134_datetime_bug_fix.diff (1.1 KB ) - added by Cliff Dyer 13 years ago.
Fix for the bug.

Download all attachments as: .zip

Change History (13)

comment:1 by Aymeric Augustin, 13 years ago

Easy pickings: set
Triage Stage: UnreviewedAccepted

comment:2 by Calvin Spealman, 13 years ago

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 by Cliff Dyer, 13 years ago

Owner: changed from nobody to Cliff Dyer
Status: newassigned

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.

Version 0, edited 13 years ago by Cliff Dyer (next)

comment:4 by Cliff Dyer, 13 years ago

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 by Cliff Dyer, 13 years ago

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.

by Cliff Dyer, 13 years ago

Tests that show the bug on python < 2.6

by Cliff Dyer, 13 years ago

Test to exercise the bug in python < 2.6

by Cliff Dyer, 13 years ago

Tests that show the bug on python < 2.6

by Cliff Dyer, 13 years ago

Attachment: 17134_datetime_bug_fix.diff added

Fix for the bug.

comment:6 by Cliff Dyer, 13 years ago

Has patch: set
Resolution: fixed
Status: assignedclosed

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 by Karen Tracey, 13 years ago

Resolution: fixed
Status: closedreopened

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

comment:8 by Karen Tracey, 13 years ago

Resolution: fixed
Status: reopenedclosed

In [17092]:

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

comment:9 by Cliff Dyer, 13 years ago

Thanks kmtracey, and sorry for the process faux pas.

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