#14453 closed (fixed)
typecast_timestamp in db.backends.util gives incorrect behavior
Reported by: | philipn | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In [9]: typecast_timestamp('2010-10-12 15:29:22.063202') Out[9]: datetime.datetime(2010, 10, 12, 15, 29, 22, 63201) In [8]: typecast_timestamp('2010-10-12 15:29:22.063201') Out[8]: datetime.datetime(2010, 10, 12, 15, 29, 22, 63200) In [10]: typecast_timestamp('2010-10-12 15:29:22.063205') Out[10]: datetime.datetime(2010, 10, 12, 15, 29, 22, 63205)
This causes sqlite to give occasional incorrect results when dealing with datetimes.
Suggested patch attached. Issue is with the cast to float.
Attachments (1)
Change History (5)
by , 14 years ago
Attachment: | typecast_timestamp_fix.diff added |
---|
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Great work. Some performance comparison, just in case:
$ cat timings.sh #!/bin/sh echo "float():" python -m timeit -n 1000000 -c "int(float('.'+'063202') * 1000000)" echo "ljust():" python -m timeit -n 1000000 -c "int('063202'.ljust(6, '0')[:6])" echo "+ '000000':" python -m timeit -n 1000000 -c "int('063202'+'000000'[:6])"
$ ./timings.sh float(): 1000000 loops, best of 3: 0.85 usec per loop ljust(): 1000000 loops, best of 3: 0.98 usec per loop + '000000': 1000000 loops, best of 3: 0.94 usec per loop
Will commit a fix implementing philipn's approach.
comment:3 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:4 by , 14 years ago
Note:
See TracTickets
for help on using tickets.
It's worth noting that this means that DateTimeFields are effectively broken with sqlite.