Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#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)

typecast_timestamp_fix.diff (568 bytes ) - added by philipn 14 years ago.

Download all attachments as: .zip

Change History (5)

by philipn, 14 years ago

Attachment: typecast_timestamp_fix.diff added

comment:1 by philipn, 14 years ago

It's worth noting that this means that DateTimeFields are effectively broken with sqlite.

comment:2 by Ramiro Morales, 14 years ago

Triage Stage: UnreviewedAccepted

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 Ramiro Morales, 14 years ago

Resolution: fixed
Status: newclosed

(In [14664]) Fixed #14453 -- Changed handling of microseconds part in typecast_timestamp() DB backend helper function to be more correct. Thanks philipn for the report and fix.

comment:4 by Ramiro Morales, 14 years ago

(In [14665]) [1.2.X] Fixed #14453 -- Changed handling of microseconds part in typecast_timestamp() DB backend helper function to be more correct. Thanks philipn for the report and fix.

Backport of [14664] from trunk

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