Code

Opened 4 years ago

Closed 4 years ago

Last modified 4 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: UI/UX:

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 4 years ago.

Download all attachments as: .zip

Change History (5)

Changed 4 years ago by philipn

comment:1 Changed 4 years ago by philipn

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 4 years ago by ramiro

  • Triage Stage changed from Unreviewed to 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 Changed 4 years ago by ramiro

  • Resolution set to fixed
  • Status changed from new to closed

(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 Changed 4 years ago by ramiro

(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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.