Opened 5 years ago

Closed 5 years ago

Last modified 5 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: 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 Cliff Dyer 5 years ago.
Tests that show the bug on python < 2.6
17134_datetime_bug_tests.2.diff (1.5 KB) - added by Cliff Dyer 5 years ago.
Test to exercise the bug in python < 2.6
17134_datetime_bug_tests.3.diff (1.5 KB) - added by Cliff Dyer 5 years ago.
Tests that show the bug on python < 2.6
17134_datetime_bug_fix.diff (1.1 KB) - added by Cliff Dyer 5 years ago.
Fix for the bug.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 5 years ago by Aymeric Augustin

Easy pickings: set
Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by Calvin Spealman

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

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.

Last edited 5 years ago by Cliff Dyer (previous) (diff)

comment:4 Changed 5 years ago by Cliff Dyer

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

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

Tests that show the bug on python < 2.6

Changed 5 years ago by Cliff Dyer

Test to exercise the bug in python < 2.6

Changed 5 years ago by Cliff Dyer

Tests that show the bug on python < 2.6

Changed 5 years ago by Cliff Dyer

Attachment: 17134_datetime_bug_fix.diff added

Fix for the bug.

comment:6 Changed 5 years ago by Cliff Dyer

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 Changed 5 years ago by Karen Tracey

Resolution: fixed
Status: closedreopened

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

comment:8 Changed 5 years ago by Karen Tracey

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 Changed 5 years ago by Cliff Dyer

Thanks kmtracey, and sorry for the process faux pas.

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