summaryrefslogtreecommitdiff
path: root/connectivity/source/commontools/dbconversion.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'connectivity/source/commontools/dbconversion.cxx')
-rw-r--r--connectivity/source/commontools/dbconversion.cxx128
1 files changed, 28 insertions, 100 deletions
diff --git a/connectivity/source/commontools/dbconversion.cxx b/connectivity/source/commontools/dbconversion.cxx
index fe26e3341d9f..704e168ad736 100644
--- a/connectivity/source/commontools/dbconversion.cxx
+++ b/connectivity/source/commontools/dbconversion.cxx
@@ -18,16 +18,16 @@
*/
#include <connectivity/dbconversion.hxx>
-#include <osl/diagnose.h>
#include <com/sun/star/sdbc/SQLException.hpp>
#include <com/sun/star/util/Date.hpp>
#include <com/sun/star/util/Time.hpp>
#include <com/sun/star/util/DateTime.hpp>
#include <rtl/character.hxx>
-#include <rtl/ustrbuf.hxx>
#include <rtl/math.hxx>
#include <sal/log.hxx>
#include <unotools/datetime.hxx>
+#include <comphelper/date.hxx>
+#include <o3tl/string_view.hxx>
#include <sstream>
#include <iomanip>
@@ -81,7 +81,7 @@ namespace dbtools
ostr << setw(4) << rDate.Year << "-"
<< setw(2) << rDate.Month << "-"
<< setw(2) << rDate.Day;
- return OUString::createFromAscii(ostr.str().c_str());
+ return OUString::createFromAscii(ostr.str());
}
OUString DBTypeConversion::toTimeStringS(const css::util::Time& rTime)
@@ -92,7 +92,7 @@ namespace dbtools
ostr << setw(2) << rTime.Hours << ":"
<< setw(2) << rTime.Minutes << ":"
<< setw(2) << rTime.Seconds;
- return OUString::createFromAscii(ostr.str().c_str());
+ return OUString::createFromAscii(ostr.str());
}
OUString DBTypeConversion::toTimeString(const css::util::Time& rTime)
@@ -104,7 +104,7 @@ namespace dbtools
<< setw(2) << rTime.Minutes << ":"
<< setw(2) << rTime.Seconds << "."
<< setw(9) << rTime.NanoSeconds;
- return OUString::createFromAscii(ostr.str().c_str());
+ return OUString::createFromAscii(ostr.str());
}
OUString DBTypeConversion::toDateTimeString(const css::util::DateTime& _rDateTime)
@@ -149,95 +149,25 @@ namespace dbtools
nHour * (minInHour * secInMin * nanoSecInSec);
}
-
- const sal_Int32 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30,
- 31, 31, 30, 31, 30, 31 };
-
-
- static bool implIsLeapYear(sal_Int32 _nYear)
- {
- return ( ((_nYear % 4) == 0)
- && ((_nYear % 100) != 0)
- )
-
- || ((_nYear % 400) == 0)
- ;
- }
-
- static sal_Int32 implDaysInMonth(sal_Int32 _nMonth, sal_Int32 _nYear)
- {
- SAL_WARN_IF(_nMonth < 1 || _nMonth > 12, "connectivity.commontools", "Month has invalid value: " << _nMonth);
- if (_nMonth < 1)
- _nMonth = 1;
- else if (_nMonth > 12)
- _nMonth = 12;
- if (_nMonth != 2)
- return aDaysInMonth[_nMonth-1];
- else
- {
- if (implIsLeapYear(_nYear))
- return aDaysInMonth[_nMonth-1] + 1;
- else
- return aDaysInMonth[_nMonth-1];
- }
- }
-
static sal_Int32 implRelativeToAbsoluteNull(const css::util::Date& _rDate)
{
- sal_Int32 nDays = 0;
-
- // ripped this code from the implementation of tools::Date
- sal_Int32 nNormalizedYear = _rDate.Year - 1;
- nDays = nNormalizedYear * 365;
- // leap years
- nDays += (nNormalizedYear / 4) - (nNormalizedYear / 100) + (nNormalizedYear / 400);
-
- for (sal_Int32 i = 1; i < _rDate.Month; ++i)
- nDays += implDaysInMonth(i, _rDate.Year);
-
- nDays += _rDate.Day;
- return nDays;
- }
-
- static void implBuildFromRelative( const sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_Int16& rYear)
- {
- sal_Int32 nTempDays;
- sal_Int32 i = 0;
- bool bCalc;
-
- do
- {
- nTempDays = nDays;
- rYear = static_cast<sal_uInt16>((nTempDays / 365) - i);
- nTempDays -= (rYear-1) * 365;
- nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400);
- bCalc = false;
- if ( nTempDays < 1 )
- {
- i++;
- bCalc = true;
- }
- else
- {
- if ( nTempDays > 365 )
- {
- if ( (nTempDays != 366) || !implIsLeapYear( rYear ) )
- {
- i--;
- bCalc = true;
- }
- }
- }
- }
- while ( bCalc );
-
- rMonth = 1;
- while ( nTempDays > implDaysInMonth( rMonth, rYear ) )
+ if (_rDate.Day == 0 && _rDate.Month == 0 && _rDate.Year == 0)
{
- nTempDays -= implDaysInMonth( rMonth, rYear );
- rMonth++;
+ // 0000-00-00 is *NOT* a valid date and passing it to the date
+ // conversion even when normalizing rightly asserts. Whatever we
+ // return here, it will be wrong. The old before commit
+ // 52ff16771ac160d27fd7beb78a4cfba22ad84f06 wrong implementation
+ // calculated -365 for that, effectively that would be a date of
+ // -0001-01-01 now but it was likely assumed that would be
+ // 0000-00-01 or even 0000-00-00 instead. Try if we get away with 0
+ // for -0001-12-31, the same that
+ // comphelper::date::convertDateToDaysNormalizing()
+ // would return if comphelper::date::normalize() wouldn't ignore
+ // such "empty" date.
+
+ return 0;
}
- rDay = static_cast<sal_uInt16>(nTempDays);
+ return comphelper::date::convertDateToDaysNormalizing( _rDate.Day, _rDate.Month, _rDate.Year);
}
sal_Int32 DBTypeConversion::toDays(const css::util::Date& _rVal, const css::util::Date& _rNullDate)
@@ -283,7 +213,6 @@ namespace dbtools
_rDate.Year = 9999;
}
// TODO: can we replace that check by minDays? Would allow dates BCE
- // implBuildFromRelative probably needs to be updated for the "no year 0" question
else if ( nTempDays <= 0 )
{
_rDate.Day = 1;
@@ -291,7 +220,7 @@ namespace dbtools
_rDate.Year = 1;
}
else
- implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
+ comphelper::date::convertDaysToDate( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
}
static void subDays(const sal_Int32 nDays, css::util::Date& _rDate )
@@ -306,7 +235,6 @@ namespace dbtools
_rDate.Year = 9999;
}
// TODO: can we replace that check by minDays? Would allow dates BCE
- // implBuildFromRelative probably needs to be updated for the "no year 0" question
else if ( nTempDays <= 0 )
{
_rDate.Day = 1;
@@ -314,7 +242,7 @@ namespace dbtools
_rDate.Year = 1;
}
else
- implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
+ comphelper::date::convertDaysToDate( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
}
css::util::Date DBTypeConversion::toDate(const double dVal, const css::util::Date& _rNullDate)
@@ -405,7 +333,7 @@ namespace dbtools
return aRet;
}
- css::util::Date DBTypeConversion::toDate(const OUString& _sSQLString)
+ css::util::Date DBTypeConversion::toDate(std::u16string_view _sSQLString)
{
// get the token out of a string
static const sal_Unicode sDateSep = '-';
@@ -414,12 +342,12 @@ namespace dbtools
sal_uInt16 nYear = 0,
nMonth = 0,
nDay = 0;
- nYear = static_cast<sal_uInt16>(_sSQLString.getToken(0,sDateSep,nIndex).toInt32());
+ nYear = static_cast<sal_uInt16>(o3tl::toInt32(o3tl::getToken(_sSQLString, 0,sDateSep,nIndex)));
if(nIndex != -1)
{
- nMonth = static_cast<sal_uInt16>(_sSQLString.getToken(0,sDateSep,nIndex).toInt32());
+ nMonth = static_cast<sal_uInt16>(o3tl::toInt32(o3tl::getToken(_sSQLString, 0,sDateSep,nIndex)));
if(nIndex != -1)
- nDay = static_cast<sal_uInt16>(_sSQLString.getToken(0,sDateSep,nIndex).toInt32());
+ nDay = static_cast<sal_uInt16>(o3tl::toInt32(o3tl::getToken(_sSQLString, 0,sDateSep,nIndex)));
}
return css::util::Date(nDay,nMonth,nYear);
@@ -442,7 +370,7 @@ namespace dbtools
const sal_Unicode *const begin = p;
while (rtl::isAsciiWhiteSpace(*p)) { ++p; }
nSeparation += p - begin;
- aTime = toTime( _sSQLString.copy( nSeparation ) );
+ aTime = toTime( _sSQLString.subView( nSeparation ) );
}
return css::util::DateTime(aTime.NanoSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours,
@@ -450,7 +378,7 @@ namespace dbtools
}
- css::util::Time DBTypeConversion::toTime(const OUString& _sSQLString)
+ css::util::Time DBTypeConversion::toTime(std::u16string_view _sSQLString)
{
css::util::Time aTime;
::utl::ISO8601parseTime(_sSQLString, aTime);