[egenix-users] DateTime error rounding seconds

Edwin Grubbs egrubbs at rackspace.com
Tue Apr 2 15:26:16 CEST 2002


On Tue, 2 Apr 2002, M.-A. Lemburg wrote:

> Edwin Grubbs wrote:
> > 
> > I am having a problem with DateTime incorrectly rounding the floating
> > point second value down 1/100 of second.
> > 
> > Python 2.2 (#1, Mar 13 2002, 12:34:11)
> > [GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> from mx.DateTime import *
> > >>> DateTime(2002, 1, 2, 3, 4, 5.00)
> > <DateTime object for '2002-01-02 03:04:05.00' at 8126658>
> > >>> DateTime(2002, 1, 2, 3, 4, 5.01)
> > <DateTime object for '2002-01-02 03:04:05.00' at 8152098>
> > >>> DateTime(2002, 1, 2, 3, 4, 5.02)
> > <DateTime object for '2002-01-02 03:04:05.01' at 8126658>
> > >>> DateTime(2002, 1, 2, 3, 4, 5.03)
> > <DateTime object for '2002-01-02 03:04:05.03' at 8152098>
> > >>> DateTime(2002, 1, 2, 3, 4, 5.04)
> > <DateTime object for '2002-01-02 03:04:05.04' at 8126658>
> > >>> DateTime(2002, 1, 2, 3, 4, 5.05)
> > <DateTime object for '2002-01-02 03:04:05.04' at 8152098>
> > >>> DateTime(2002, 1, 2, 3, 4, 5.06)
> > <DateTime object for '2002-01-02 03:04:05.05' at 8126658>
> 
> That's because 5.05 is really 5.0499999999999998. mxDateTime's
> repr() function truncates the seconds value to avoid accidental
> cases of a 60 reading when you really only have 59.6 seconds.
> 
> Use now().second to access the true seconds value with
> full accuracy.
> 
> HTH, 
> 

The real problem with it, is that str() returns the truncated date just
like repr(), and we are using str() to insert datetime fields. If you
create a DateTime object for 03:04:05.02 and insert it into a Postgres 7
database using its str() value, it is stored as 03:04:05.01. Then when you
retrieve 03:04:05.01 from the database, the DateTime object shows it as
03:04:05.00. Then you cannot match the row that you just retrieved with 
its datetime column. How is the DateTime object supposed to be inserted 
and retrieved from the database?

I understand that a float cannot represent most decimal values exactly,
but truncating just increases the inaccuracy even though it might seem
more coherent to have "2002-03-28 10:59:59.9999" truncated to "2002-03-28
10:59:59.99" instead of rounded to "2002-03-28 11:00:00.00".

-Edwin











More information about the egenix-users mailing list