#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)
Change History (13)
comment:1 by , 13 years ago
Easy pickings: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 13 years ago
comment:3 by , 13 years ago
Owner: | changed from | to
---|---|
Status: | new → 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.
comment:4 by , 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 , 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 , 13 years ago
Attachment: | 17134_datetime_bug_tests.diff added |
---|
Tests that show the bug on python < 2.6
by , 13 years ago
Attachment: | 17134_datetime_bug_tests.2.diff added |
---|
Test to exercise the bug in python < 2.6
by , 13 years ago
Attachment: | 17134_datetime_bug_tests.3.diff added |
---|
Tests that show the bug on python < 2.6
comment:6 by , 13 years ago
Has patch: | set |
---|---|
Resolution: | → fixed |
Status: | assigned → 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 by , 13 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
It's not fixed until it's checked into the repo...
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?