From a5ce8c77a0b2dca2ab5a3564feed1cd3b8104eaa Mon Sep 17 00:00:00 2001 From: Lionel Elie Mamane Date: Sat, 21 Dec 2013 10:37:11 +0100 Subject: fdo#72916 correct rounding of time part of DateTime Change-Id: I135478755f9e5a844119129b470fef8de2cd0409 --- connectivity/source/commontools/dbconversion.cxx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'connectivity') diff --git a/connectivity/source/commontools/dbconversion.cxx b/connectivity/source/commontools/dbconversion.cxx index b15dc92ca898..d6e48315806b 100644 --- a/connectivity/source/commontools/dbconversion.cxx +++ b/connectivity/source/commontools/dbconversion.cxx @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -322,10 +323,15 @@ namespace dbtools return aRet; } // ------------------------------------------------------------------------- - utl::Time DBTypeConversion::toTime(double dVal) + utl::Time DBTypeConversion::toTime(double dVal, short nDigits) { sal_Int32 nDays = (sal_Int32)dVal; - sal_Int64 nNS = static_cast((dVal - (double)nDays) * fNanoSecondsPerDay + 0.5); + sal_Int64 nNS; + { + double fSeconds((dVal - (double)nDays) * (fNanoSecondsPerDay / nanoSecInSec)); + fSeconds = ::rtl::math::round( fSeconds, nDigits ); + nNS = fSeconds * nanoSecInSec; + } sal_Int16 nSign; if ( nNS < 0 ) @@ -368,7 +374,11 @@ namespace dbtools utl::DateTime DBTypeConversion::toDateTime(double dVal, const utl::Date& _rNullDate) { utl::Date aDate = toDate(dVal, _rNullDate); - utl::Time aTime = toTime(dVal); + // there is not enough precision in a double to have both a date + // and a time up to nanoseconds -> limit to microseconds to have + // correct rounding, that is e.g. 13:00:00.000000000 instead of + // 12:59:59.999999790 + utl::Time aTime = toTime(dVal, 6); utl::DateTime xRet; -- cgit