Truncation/rounding error in strftime(x, 'unixepoch')

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Truncation/rounding error in strftime(x, 'unixepoch')

Stefan Brüns

some time ago there was an error reported when running the testsuite on ix86,
in the date.test/date-2.2c-*.

The error happens as a string like 1237962480.003 gets parsed and rounded to
1237962480.002999... and is later truncated to 1237962480.002.

The rounding error happend in date.c:parseModifier(...):
p->iJD = (sqlite3_int64)r;

i.e. the double r is truncated by casting it to int.

Doing the following change fixes the error on ix86, and lets the testsuites
pass on i586, x86_64, aarch64, ppc64le, ...

- p->iJD = (sqlite3_int64)r;
+ p->iJD = (sqlite3_int64)(r + 0.5);

Also compare with date.c:setRawDateNumber(...), where rounding is already

p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);

Kind regards,


Stefan Brüns  /  Bergstraße 21  /  52062 Aachen
home: +49 241 53809034     mobile: +49 151 50412019
sqlite-users mailing list
[hidden email]

signature.asc (201 bytes) Download Attachment