21#include <com/sun/star/sdbc/SQLException.hpp>
22#include <com/sun/star/util/Date.hpp>
23#include <com/sun/star/util/Time.hpp>
24#include <com/sun/star/util/DateTime.hpp>
25#include <rtl/character.hxx>
26#include <rtl/math.hxx>
36 const sal_Int64 nanoSecInSec = 1000000000;
37 const sal_Int16 secInMin = 60;
38 const sal_Int16 minInHour = 60;
40 const sal_Int64 secMask = 1000000000;
41 const sal_Int64 minMask = 100000000000LL;
42 const sal_Int64 hourMask = 10000000000000LL;
44 const double fNanoSecondsPerDay = nanoSecInSec * secInMin * minInHour * 24.0;
47 const sal_Int32 maxDays = 11967896;
72 static css::util::Date STANDARD_DB_DATE(1,1,1900);
73 return STANDARD_DB_DATE;
78 std::ostringstream ostr;
81 ostr << setw(4) << rDate.Year <<
"-"
82 << setw(2) << rDate.Month <<
"-"
83 << setw(2) << rDate.Day;
84 return OUString::createFromAscii(ostr.str());
89 std::ostringstream ostr;
92 ostr << setw(2) << rTime.Hours <<
":"
93 << setw(2) << rTime.Minutes <<
":"
94 << setw(2) << rTime.Seconds;
95 return OUString::createFromAscii(ostr.str());
100 std::ostringstream ostr;
103 ostr << setw(2) << rTime.Hours <<
":"
104 << setw(2) << rTime.Minutes <<
":"
105 << setw(2) << rTime.Seconds <<
"."
106 << setw(9) << rTime.NanoSeconds;
107 return OUString::createFromAscii(ostr.str());
112 css::util::Date aDate(_rDateTime.Day,_rDateTime.Month,_rDateTime.Year);
113 css::util::Time
const aTime(_rDateTime.NanoSeconds, _rDateTime.Seconds,
114 _rDateTime.Minutes, _rDateTime.Hours, _rDateTime.IsUTC);
120 css::util::Date aReturn;
121 aReturn.Day =
static_cast<sal_uInt16
>(_nVal % 100);
122 aReturn.Month =
static_cast<sal_uInt16
>((_nVal / 100) % 100);
123 aReturn.Year =
static_cast<sal_uInt16
>(_nVal / 10000);
130 css::util::Time aReturn;
131 sal_uInt64 unVal =
static_cast<sal_uInt64
>(_nVal >= 0 ? _nVal : -_nVal);
132 aReturn.Hours = unVal / hourMask;
133 aReturn.Minutes = (unVal / minMask) % 100;
134 aReturn.Seconds = (unVal / secMask) % 100;
135 aReturn.NanoSeconds = unVal % secMask;
141 sal_Int32 nHour = rVal.Hours;
142 sal_Int32 nMin = rVal.Minutes;
143 sal_Int32 nSec = rVal.Seconds;
144 sal_Int32 nNanoSec = rVal.NanoSeconds;
147 nSec * nanoSecInSec +
148 nMin * (secInMin * nanoSecInSec) +
149 nHour * (minInHour * secInMin * nanoSecInSec);
154 if (_rDate.Day == 0 && _rDate.Month == 0 && _rDate.Year == 0)
181 return static_cast<double>(
toDays(rVal, _rNullDate));
187 return static_cast<double>(
getNsFromTime(rVal)) / fNanoSecondsPerDay;
193 sal_Int64 nTime =
toDays(css::util::Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate);
194 css::util::Time aTimePart;
196 aTimePart.Hours = _rVal.Hours;
197 aTimePart.Minutes = _rVal.Minutes;
198 aTimePart.Seconds = _rVal.Seconds;
199 aTimePart.NanoSeconds = _rVal.NanoSeconds;
201 return static_cast<double>(nTime) +
toDouble(aTimePart);
204 static void addDays(
const sal_Int32 nDays, css::util::Date& _rDate)
209 if ( nTempDays > maxDays )
216 else if ( nTempDays <= 0 )
226 static void subDays(
const sal_Int32 nDays, css::util::Date& _rDate )
231 if ( nTempDays > maxDays )
238 else if ( nTempDays <= 0 )
250 css::util::Date aRet = _rNullDate;
253 addDays(
static_cast<sal_Int32
>(dVal),aRet);
255 subDays(
static_cast<sal_uInt32
>(-dVal),aRet);
263 const double nDays = std::trunc(dVal);
264 double fSeconds((dVal - nDays) * (fNanoSecondsPerDay / nanoSecInSec));
265 fSeconds = ::rtl::math::round(fSeconds, nDigits);
266 sal_Int64 nNS = fSeconds * nanoSecInSec;
277 css::util::Time aRet;
280 sal_Int64 nNanoSeconds = nNS;
281 sal_Int32 nSeconds = nNanoSeconds / nanoSecInSec;
282 sal_Int32 nMinutes = nSeconds / secInMin;
284 aRet.NanoSeconds = nNanoSeconds % nanoSecInSec;
285 aRet.Seconds = nSeconds % secInMin;
286 aRet.Hours = nMinutes / minInHour;
287 aRet.Minutes = nMinutes % minInHour;
290 sal_Int64 nTime = nSign *
292 aRet.Seconds * secMask +
293 aRet.Minutes * minMask +
294 aRet.Hours * hourMask);
298 aRet.NanoSeconds = nanoSecInSec-1;
299 aRet.Seconds = secInMin-1;
300 aRet.Minutes = minInHour-1;
308 css::util::DateTime aRet;
310 if (!std::isfinite(dVal))
312 SAL_WARN(
"connectivity.commontools",
"DateTime has invalid value: " << dVal);
316 css::util::Date aDate =
toDate(dVal, _rNullDate);
321 css::util::Time aTime =
toTime(dVal, 6);
323 aRet.Day = aDate.Day;
324 aRet.Month = aDate.Month;
325 aRet.Year = aDate.Year;
327 aRet.NanoSeconds = aTime.NanoSeconds;
328 aRet.Minutes = aTime.Minutes;
329 aRet.Seconds = aTime.Seconds;
330 aRet.Hours = aTime.Hours;
342 sal_uInt16 nYear = 0,
353 return css::util::Date(nDay,nMonth,nYear);
364 css::util::Date aDate =
toDate(_sSQLString);
365 css::util::Time aTime;
366 sal_Int32 nSeparation = _sSQLString.indexOf(
' ' );
367 if ( -1 != nSeparation )
369 const sal_Unicode *
p = _sSQLString.getStr() + nSeparation;
371 while (rtl::isAsciiWhiteSpace(*
p)) { ++
p; }
373 aTime =
toTime( _sSQLString.subView( nSeparation ) );
376 return css::util::DateTime(aTime.NanoSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours,
377 aDate.Day, aDate.Month, aDate.Year,
false);
383 css::util::Time aTime;
384 ::utl::ISO8601parseTime(_sSQLString, aTime);
static double toDouble(std::string_view rString)
#define SAL_WARN(area, stream)
void convertDaysToDate(sal_Int32 nDays, sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
sal_Int32 convertDateToDaysNormalizing(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
enumrange< T >::Iterator begin(enumrange< T >)