1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <connectivity/dbconversion.hxx>
21 #include <connectivity/dbcharset.hxx>
22 #include <osl/diagnose.h>
23 #include <com/sun/star/sdbc/SQLException.hpp>
24 #include <com/sun/star/util/Date.hpp>
25 #include <com/sun/star/util/Time.hpp>
26 #include <com/sun/star/util/DateTime.hpp>
27 #include <rtl/character.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/math.hxx>
30 #include <unotools/datetime.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;
46 // 32767-12-31 in "(days since 0001-01-01) + 1" format
47 const sal_Int32 maxDays
= 11967896;
48 // -32768-01-01 in "(days since 0001-01-01) + 1" format
49 // Yes, I know it is currently unused. Will have to be used
50 // when we implement negative years. Writing down the correct
51 // value for future reference.
52 // *** Please don't remove just because it is unused ***
53 // Lionel Élie Mamane 2017-08-02
54 // const sal_Int32 minDays = -11968270;
62 using namespace ::com::sun::star::uno
;
63 using namespace ::com::sun::star::util
;
64 using namespace ::com::sun::star::sdb
;
65 using namespace ::com::sun::star::sdbc
;
66 using namespace ::com::sun::star::lang
;
67 using namespace ::com::sun::star::beans
;
70 css::util::Date
const & DBTypeConversion::getStandardDate()
72 static css::util::Date
STANDARD_DB_DATE(1,1,1900);
73 return STANDARD_DB_DATE
;
76 OUString
DBTypeConversion::toDateString(const css::util::Date
& rDate
)
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().c_str());
87 OUString
DBTypeConversion::toTimeStringS(const css::util::Time
& rTime
)
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().c_str());
98 OUString
DBTypeConversion::toTimeString(const css::util::Time
& rTime
)
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().c_str());
110 OUString
DBTypeConversion::toDateTimeString(const css::util::DateTime
& _rDateTime
)
112 css::util::Date
aDate(_rDateTime
.Day
,_rDateTime
.Month
,_rDateTime
.Year
);
113 OUStringBuffer
aTemp(toDateString(aDate
));
115 css::util::Time
const aTime(_rDateTime
.NanoSeconds
, _rDateTime
.Seconds
,
116 _rDateTime
.Minutes
, _rDateTime
.Hours
, _rDateTime
.IsUTC
);
117 aTemp
.append( toTimeString(aTime
) );
118 return aTemp
.makeStringAndClear();
121 css::util::Date
DBTypeConversion::toDate(const sal_Int32 _nVal
)
123 css::util::Date aReturn
;
124 aReturn
.Day
= static_cast<sal_uInt16
>(_nVal
% 100);
125 aReturn
.Month
= static_cast<sal_uInt16
>((_nVal
/ 100) % 100);
126 aReturn
.Year
= static_cast<sal_uInt16
>(_nVal
/ 10000);
131 css::util::Time
DBTypeConversion::toTime(const sal_Int64 _nVal
)
133 css::util::Time aReturn
;
134 sal_uInt64 unVal
= static_cast<sal_uInt64
>(_nVal
>= 0 ? _nVal
: -_nVal
);
135 aReturn
.Hours
= unVal
/ hourMask
;
136 aReturn
.Minutes
= (unVal
/ minMask
) % 100;
137 aReturn
.Seconds
= (unVal
/ secMask
) % 100;
138 aReturn
.NanoSeconds
= unVal
% secMask
;
142 sal_Int64
DBTypeConversion::getNsFromTime(const css::util::Time
& rVal
)
144 sal_Int32 nHour
= rVal
.Hours
;
145 sal_Int32 nMin
= rVal
.Minutes
;
146 sal_Int32 nSec
= rVal
.Seconds
;
147 sal_Int32 nNanoSec
= rVal
.NanoSeconds
;
150 nSec
* nanoSecInSec
+
151 nMin
* (secInMin
* nanoSecInSec
) +
152 nHour
* (minInHour
* secInMin
* nanoSecInSec
);
156 static const sal_Int32 aDaysInMonth
[12] = { 31, 28, 31, 30, 31, 30,
157 31, 31, 30, 31, 30, 31 };
160 static bool implIsLeapYear(sal_Int32 _nYear
)
162 return ( ((_nYear
% 4) == 0)
163 && ((_nYear
% 100) != 0)
166 || ((_nYear
% 400) == 0)
171 static sal_Int32
implDaysInMonth(sal_Int32 _nMonth
, sal_Int32 _nYear
)
173 OSL_ENSURE(_nMonth
> 0 && _nMonth
< 13,"Month as invalid value!");
175 return aDaysInMonth
[_nMonth
-1];
178 if (implIsLeapYear(_nYear
))
179 return aDaysInMonth
[_nMonth
-1] + 1;
181 return aDaysInMonth
[_nMonth
-1];
186 static sal_Int32
implRelativeToAbsoluteNull(const css::util::Date
& _rDate
)
190 // ripped this code from the implementation of tools::Date
191 sal_Int32 nNormalizedYear
= _rDate
.Year
- 1;
192 nDays
= nNormalizedYear
* 365;
194 nDays
+= (nNormalizedYear
/ 4) - (nNormalizedYear
/ 100) + (nNormalizedYear
/ 400);
196 for (sal_Int32 i
= 1; i
< _rDate
.Month
; ++i
)
197 nDays
+= implDaysInMonth(i
, _rDate
.Year
);
203 static void implBuildFromRelative( const sal_Int32 nDays
, sal_uInt16
& rDay
, sal_uInt16
& rMonth
, sal_Int16
& rYear
)
212 rYear
= static_cast<sal_uInt16
>((nTempDays
/ 365) - i
);
213 nTempDays
-= (rYear
-1) * 365;
214 nTempDays
-= ((rYear
-1) / 4) - ((rYear
-1) / 100) + ((rYear
-1) / 400);
223 if ( nTempDays
> 365 )
225 if ( (nTempDays
!= 366) || !implIsLeapYear( rYear
) )
236 while ( nTempDays
> implDaysInMonth( rMonth
, rYear
) )
238 nTempDays
-= implDaysInMonth( rMonth
, rYear
);
241 rDay
= static_cast<sal_uInt16
>(nTempDays
);
244 sal_Int32
DBTypeConversion::toDays(const css::util::Date
& _rVal
, const css::util::Date
& _rNullDate
)
246 return implRelativeToAbsoluteNull(_rVal
) - implRelativeToAbsoluteNull(_rNullDate
);
250 double DBTypeConversion::toDouble(const css::util::Date
& rVal
, const css::util::Date
& _rNullDate
)
252 return static_cast<double>(toDays(rVal
, _rNullDate
));
256 double DBTypeConversion::toDouble(const css::util::Time
& rVal
)
258 return static_cast<double>(getNsFromTime(rVal
)) / fNanoSecondsPerDay
;
262 double DBTypeConversion::toDouble(const css::util::DateTime
& _rVal
, const css::util::Date
& _rNullDate
)
264 sal_Int64 nTime
= toDays(css::util::Date(_rVal
.Day
, _rVal
.Month
, _rVal
.Year
), _rNullDate
);
265 css::util::Time aTimePart
;
267 aTimePart
.Hours
= _rVal
.Hours
;
268 aTimePart
.Minutes
= _rVal
.Minutes
;
269 aTimePart
.Seconds
= _rVal
.Seconds
;
270 aTimePart
.NanoSeconds
= _rVal
.NanoSeconds
;
272 return static_cast<double>(nTime
) + toDouble(aTimePart
);
275 static void addDays(const sal_Int32 nDays
, css::util::Date
& _rDate
)
277 sal_Int32 nTempDays
= implRelativeToAbsoluteNull( _rDate
);
280 if ( nTempDays
> maxDays
)
286 // TODO: can we replace that check by minDays? Would allow dates BCE
287 // implBuildFromRelative probably needs to be updated for the "no year 0" question
288 else if ( nTempDays
<= 0 )
295 implBuildFromRelative( nTempDays
, _rDate
.Day
, _rDate
.Month
, _rDate
.Year
);
298 static void subDays(const sal_Int32 nDays
, css::util::Date
& _rDate
)
300 sal_Int32 nTempDays
= implRelativeToAbsoluteNull( _rDate
);
303 if ( nTempDays
> maxDays
)
309 // TODO: can we replace that check by minDays? Would allow dates BCE
310 // implBuildFromRelative probably needs to be updated for the "no year 0" question
311 else if ( nTempDays
<= 0 )
318 implBuildFromRelative( nTempDays
, _rDate
.Day
, _rDate
.Month
, _rDate
.Year
);
321 css::util::Date
DBTypeConversion::toDate(const double dVal
, const css::util::Date
& _rNullDate
)
323 css::util::Date aRet
= _rNullDate
;
326 addDays(static_cast<sal_Int32
>(dVal
),aRet
);
328 subDays(static_cast<sal_uInt32
>(-dVal
),aRet
);
329 // x -= (sal_uInt32)(-nDays);
334 css::util::Time
DBTypeConversion::toTime(const double dVal
, short nDigits
)
336 const sal_Int32 nDays
= static_cast<sal_Int32
>(dVal
);
339 double fSeconds((dVal
- static_cast<double>(nDays
)) * (fNanoSecondsPerDay
/ nanoSecInSec
));
340 fSeconds
= ::rtl::math::round( fSeconds
, nDigits
);
341 nNS
= fSeconds
* nanoSecInSec
;
353 css::util::Time aRet
;
355 // we have to sal_Int32 here because otherwise we get an overflow
356 sal_Int64 nNanoSeconds
= nNS
;
357 sal_Int32 nSeconds
= nNanoSeconds
/ nanoSecInSec
;
358 sal_Int32 nMinutes
= nSeconds
/ secInMin
;
360 aRet
.NanoSeconds
= nNanoSeconds
% nanoSecInSec
;
361 aRet
.Seconds
= nSeconds
% secInMin
;
362 aRet
.Hours
= nMinutes
/ minInHour
;
363 aRet
.Minutes
= nMinutes
% minInHour
;
366 sal_Int64 nTime
= nSign
*
368 aRet
.Seconds
* secMask
+
369 aRet
.Minutes
* minMask
+
370 aRet
.Hours
* hourMask
);
374 aRet
.NanoSeconds
= nanoSecInSec
-1;
375 aRet
.Seconds
= secInMin
-1;
376 aRet
.Minutes
= minInHour
-1;
382 css::util::DateTime
DBTypeConversion::toDateTime(const double dVal
, const css::util::Date
& _rNullDate
)
384 css::util::Date aDate
= toDate(dVal
, _rNullDate
);
385 // there is not enough precision in a double to have both a date
386 // and a time up to nanoseconds -> limit to microseconds to have
387 // correct rounding, that is e.g. 13:00:00.000000000 instead of
388 // 12:59:59.999999790
389 css::util::Time aTime
= toTime(dVal
, 6);
391 css::util::DateTime aRet
;
393 aRet
.Day
= aDate
.Day
;
394 aRet
.Month
= aDate
.Month
;
395 aRet
.Year
= aDate
.Year
;
397 aRet
.NanoSeconds
= aTime
.NanoSeconds
;
398 aRet
.Minutes
= aTime
.Minutes
;
399 aRet
.Seconds
= aTime
.Seconds
;
400 aRet
.Hours
= aTime
.Hours
;
406 css::util::Date
DBTypeConversion::toDate(const OUString
& _sSQLString
)
408 // get the token out of a string
409 static const sal_Unicode sDateSep
= '-';
411 sal_Int32 nIndex
= 0;
412 sal_uInt16 nYear
= 0,
415 nYear
= static_cast<sal_uInt16
>(_sSQLString
.getToken(0,sDateSep
,nIndex
).toInt32());
418 nMonth
= static_cast<sal_uInt16
>(_sSQLString
.getToken(0,sDateSep
,nIndex
).toInt32());
420 nDay
= static_cast<sal_uInt16
>(_sSQLString
.getToken(0,sDateSep
,nIndex
).toInt32());
423 return css::util::Date(nDay
,nMonth
,nYear
);
427 css::util::DateTime
DBTypeConversion::toDateTime(const OUString
& _sSQLString
)
429 //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Timestamp.html#valueOf(java.lang.String)
430 //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html#valueOf(java.lang.String)
431 //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Time.html#valueOf(java.lang.String)
434 css::util::Date aDate
= toDate(_sSQLString
);
435 css::util::Time aTime
;
436 sal_Int32 nSeparation
= _sSQLString
.indexOf( ' ' );
437 if ( -1 != nSeparation
)
439 const sal_Unicode
*p
= _sSQLString
.getStr() + nSeparation
;
440 const sal_Unicode
*const begin
= p
;
441 while (rtl::isAsciiWhiteSpace(*p
)) { ++p
; }
442 nSeparation
+= p
- begin
;
443 aTime
= toTime( _sSQLString
.copy( nSeparation
) );
446 return css::util::DateTime(aTime
.NanoSeconds
, aTime
.Seconds
, aTime
.Minutes
, aTime
.Hours
,
447 aDate
.Day
, aDate
.Month
, aDate
.Year
, false);
451 css::util::Time
DBTypeConversion::toTime(const OUString
& _sSQLString
)
453 css::util::Time aTime
;
454 ::utl::ISO8601parseTime(_sSQLString
, aTime
);
459 } // namespace dbtools
462 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */