diff options
author | Lionel Elie Mamane <lionel@mamane.lu> | 2013-06-27 18:59:12 +0200 |
---|---|---|
committer | Lionel Elie Mamane <lionel@mamane.lu> | 2013-06-27 19:52:44 +0200 |
commit | efe0e8b8116e1ba60acb00aa73123ffb060e3c90 (patch) | |
tree | 2ca8c6f969941a30ebfc9c7104b52779c8c398ea /connectivity/source | |
parent | 667ef4356af609a6d54edf88ffbdf054509272f3 (diff) |
fix nanosecond computation
when less/more than nine digits
Change-Id: I222ae7c51e37468a01abc9caab91657ea2593d13
Diffstat (limited to 'connectivity/source')
-rw-r--r-- | connectivity/source/commontools/dbconversion.cxx | 101 |
1 files changed, 43 insertions, 58 deletions
diff --git a/connectivity/source/commontools/dbconversion.cxx b/connectivity/source/commontools/dbconversion.cxx index 81248d5481a3..bf36c93c693f 100644 --- a/connectivity/source/commontools/dbconversion.cxx +++ b/connectivity/source/commontools/dbconversion.cxx @@ -28,6 +28,7 @@ #include <com/sun/star/util/Time.hpp> #include <com/sun/star/util/DateTime.hpp> #include <rtl/ustrbuf.hxx> +#include <unotools/datetime.hxx> #include <sstream> #include <iomanip> @@ -55,6 +56,7 @@ namespace dbtools using namespace ::comphelper; using namespace ::com::sun::star::uno; using namespace ::com::sun::star::util; + namespace utl = ::com::sun::star::util; using namespace ::com::sun::star::sdb; using namespace ::com::sun::star::sdbc; using namespace ::com::sun::star::lang; @@ -68,7 +70,7 @@ namespace dbtools return STANDARD_DB_DATE; } //------------------------------------------------------------------------------ - OUString DBTypeConversion::toDateString(const Date& rDate) + OUString DBTypeConversion::toDateString(const utl::Date& rDate) { sal_Char s[11]; snprintf(s, @@ -81,7 +83,7 @@ namespace dbtools return OUString::createFromAscii(s); } //------------------------------------------------------------------ - OUString DBTypeConversion::toTimeStringS(const Time& rTime) + OUString DBTypeConversion::toTimeStringS(const utl::Time& rTime) { std::ostringstream ostr; using std::setw; @@ -92,7 +94,7 @@ namespace dbtools return OUString::createFromAscii(ostr.str().c_str()); } //------------------------------------------------------------------ - OUString DBTypeConversion::toTimeString(const Time& rTime) + OUString DBTypeConversion::toTimeString(const utl::Time& rTime) { std::ostringstream ostr; using std::setw; @@ -104,19 +106,19 @@ namespace dbtools return OUString::createFromAscii(ostr.str().c_str()); } //------------------------------------------------------------------ - OUString DBTypeConversion::toDateTimeString(const DateTime& _rDateTime) + OUString DBTypeConversion::toDateTimeString(const utl::DateTime& _rDateTime) { - Date aDate(_rDateTime.Day,_rDateTime.Month,_rDateTime.Year); + utl::Date aDate(_rDateTime.Day,_rDateTime.Month,_rDateTime.Year); OUStringBuffer aTemp(toDateString(aDate)); aTemp.appendAscii(" "); - Time aTime(_rDateTime.NanoSeconds,_rDateTime.Seconds,_rDateTime.Minutes,_rDateTime.Hours); + utl::Time aTime(_rDateTime.NanoSeconds,_rDateTime.Seconds,_rDateTime.Minutes,_rDateTime.Hours); aTemp.append( toTimeString(aTime) ); return aTemp.makeStringAndClear(); } //------------------------------------------------------------------------------ - Date DBTypeConversion::toDate(sal_Int32 _nVal) + utl::Date DBTypeConversion::toDate(sal_Int32 _nVal) { - Date aReturn; + utl::Date aReturn; aReturn.Day = (sal_uInt16)(_nVal % 100); aReturn.Month = (sal_uInt16)((_nVal / 100) % 100); aReturn.Year = (sal_uInt16)(_nVal / 10000); @@ -124,9 +126,9 @@ namespace dbtools } //------------------------------------------------------------------------------ - Time DBTypeConversion::toTime(sal_Int64 _nVal) + utl::Time DBTypeConversion::toTime(sal_Int64 _nVal) { - Time aReturn; + utl::Time aReturn; sal_uInt64 unVal = static_cast<sal_uInt64>(_nVal >= 0 ? _nVal : -_nVal); aReturn.Hours = unVal / hourMask; aReturn.Minutes = (unVal / minMask) % 100; @@ -136,7 +138,7 @@ namespace dbtools } //------------------------------------------------------------------------------ - sal_Int32 DBTypeConversion::toINT32(const Date& rVal) + sal_Int32 DBTypeConversion::toINT32(const utl::Date& rVal) { return ((sal_Int32)(rVal.Day%100)) + (((sal_Int32)(rVal.Month%100))*100) + @@ -144,7 +146,7 @@ namespace dbtools } //------------------------------------------------------------------------------ - sal_Int64 DBTypeConversion::toINT64(const Time& rVal) + sal_Int64 DBTypeConversion::toINT64(const utl::Time& rVal) { // normalize time sal_Int32 nSeconds = rVal.Seconds + rVal.NanoSeconds / nanoSecInSec; @@ -162,7 +164,7 @@ namespace dbtools } //------------------------------------------------------------------------------ - sal_Int32 DBTypeConversion::getMsFromTime(const Time& rVal) + sal_Int32 DBTypeConversion::getMsFromTime(const utl::Time& rVal) { sal_Int32 nHour = rVal.Hours; sal_Int32 nMin = rVal.Minutes; @@ -173,7 +175,7 @@ namespace dbtools } //------------------------------------------------------------------------------ - sal_Int64 DBTypeConversion::getNsFromTime(const Time& rVal) + sal_Int64 DBTypeConversion::getNsFromTime(const utl::Time& rVal) { sal_Int32 nHour = rVal.Hours; sal_Int32 nMin = rVal.Minutes; @@ -217,7 +219,7 @@ namespace dbtools } //------------------------------------------------------------------------------ - static sal_Int32 implRelativeToAbsoluteNull(const Date& _rDate) + static sal_Int32 implRelativeToAbsoluteNull(const utl::Date& _rDate) { sal_Int32 nDays = 0; @@ -275,28 +277,28 @@ namespace dbtools rDay = (sal_uInt16)nTempDays; } //------------------------------------------------------------------------------ - sal_Int32 DBTypeConversion::toDays(const Date& _rVal, const Date& _rNullDate) + sal_Int32 DBTypeConversion::toDays(const utl::Date& _rVal, const utl::Date& _rNullDate) { return implRelativeToAbsoluteNull(_rVal) - implRelativeToAbsoluteNull(_rNullDate); } //------------------------------------------------------------------------------ - double DBTypeConversion::toDouble(const Date& rVal, const Date& _rNullDate) + double DBTypeConversion::toDouble(const utl::Date& rVal, const utl::Date& _rNullDate) { return (double)toDays(rVal, _rNullDate); } //------------------------------------------------------------------------------ - double DBTypeConversion::toDouble(const Time& rVal) + double DBTypeConversion::toDouble(const utl::Time& rVal) { return (double)getNsFromTime(rVal) / fNanoSecondsPerDay; } //------------------------------------------------------------------------------ - double DBTypeConversion::toDouble(const DateTime& _rVal, const Date& _rNullDate) + double DBTypeConversion::toDouble(const utl::DateTime& _rVal, const utl::Date& _rNullDate) { - sal_Int64 nTime = toDays(Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate); - Time aTimePart; + sal_Int64 nTime = toDays(utl::Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate); + utl::Time aTimePart; aTimePart.Hours = _rVal.Hours; aTimePart.Minutes = _rVal.Minutes; @@ -306,7 +308,7 @@ namespace dbtools return ((double)nTime) + toDouble(aTimePart); } // ------------------------------------------------------------------------- - static void addDays(sal_Int32 nDays, Date& _rDate) + static void addDays(sal_Int32 nDays, utl::Date& _rDate) { sal_Int32 nTempDays = implRelativeToAbsoluteNull( _rDate ); @@ -327,7 +329,7 @@ namespace dbtools implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year ); } // ----------------------------------------------------------------------- - static void subDays( sal_Int32 nDays, Date& _rDate ) + static void subDays( sal_Int32 nDays, utl::Date& _rDate ) { sal_Int32 nTempDays = implRelativeToAbsoluteNull( _rDate ); @@ -348,9 +350,9 @@ namespace dbtools implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year ); } // ------------------------------------------------------------------------- - Date DBTypeConversion::toDate(double dVal, const Date& _rNullDate) + utl::Date DBTypeConversion::toDate(double dVal, const utl::Date& _rNullDate) { - Date aRet = _rNullDate; + utl::Date aRet = _rNullDate; if (dVal >= 0) addDays((sal_Int32)dVal,aRet); @@ -361,7 +363,7 @@ namespace dbtools return aRet; } // ------------------------------------------------------------------------- - Time DBTypeConversion::toTime(double dVal) + utl::Time DBTypeConversion::toTime(double dVal) { sal_Int32 nDays = (sal_Int32)dVal; sal_Int64 nNS = static_cast<sal_Int64>((dVal - (double)nDays) * fNanoSecondsPerDay + 0.5); @@ -375,7 +377,7 @@ namespace dbtools else nSign = 1; - Time xRet; + utl::Time xRet; // normalize time // we have to sal_Int32 here because otherwise we get an overflow sal_Int64 nNanoSeconds = nNS; @@ -404,12 +406,12 @@ namespace dbtools return xRet; } //------------------------------------------------------------------------------ - DateTime DBTypeConversion::toDateTime(double dVal, const Date& _rNullDate) + utl::DateTime DBTypeConversion::toDateTime(double dVal, const utl::Date& _rNullDate) { - Date aDate = toDate(dVal, _rNullDate); - Time aTime = toTime(dVal); + utl::Date aDate = toDate(dVal, _rNullDate); + utl::Time aTime = toTime(dVal); - DateTime xRet; + utl::DateTime xRet; xRet.Day = aDate.Day; xRet.Month = aDate.Month; @@ -424,7 +426,7 @@ namespace dbtools return xRet; } //------------------------------------------------------------------------------ - Date DBTypeConversion::toDate(const OUString& _sSQLString) + utl::Date DBTypeConversion::toDate(const OUString& _sSQLString) { // get the token out of a string static sal_Unicode sDateSep = '-'; @@ -441,50 +443,33 @@ namespace dbtools nDay = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32(); } - return Date(nDay,nMonth,nYear); + return utl::Date(nDay,nMonth,nYear); } //----------------------------------------------------------------------------- - DateTime DBTypeConversion::toDateTime(const OUString& _sSQLString) + utl::DateTime DBTypeConversion::toDateTime(const OUString& _sSQLString) { //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Timestamp.html#valueOf(java.lang.String) //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html#valueOf(java.lang.String) //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Time.html#valueOf(java.lang.String) // the date part - Date aDate = toDate(_sSQLString); - Time aTime; + utl::Date aDate = toDate(_sSQLString); + utl::Time aTime; sal_Int32 nSeparation = _sSQLString.indexOf( ' ' ); if ( -1 != nSeparation ) aTime = toTime( _sSQLString.copy( nSeparation ) ); - return DateTime(aTime.NanoSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours, + return utl::DateTime(aTime.NanoSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours, aDate.Day, aDate.Month, aDate.Year); } //----------------------------------------------------------------------------- - Time DBTypeConversion::toTime(const OUString& _sSQLString) + utl::Time DBTypeConversion::toTime(const OUString& _sSQLString) { - static sal_Unicode sTimeSep = ':'; - - sal_Int32 nIndex = 0; - sal_uInt16 nHour = 0, - nMinute = 0, - nSecond = 0; - sal_uInt32 nNanoSeconds = 0; - nHour = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32(); - if(nIndex != -1) - { - nMinute = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32(); - if(nIndex != -1) - { - nSecond = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32(); - nIndex = 0; - OUString sNano(_sSQLString.getToken(1,'.',nIndex)); - nNanoSeconds = sNano.toInt32(); - } - } - return Time(nNanoSeconds, nSecond, nMinute, nHour); + utl::Time aTime; + ::utl::ISO8601parseTime(_sSQLString, aTime); + return aTime; } //......................................................................... |