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/ustrbuf.hxx>
28 #include <rtl/math.hxx>
29 #include <unotools/datetime.hxx>
33 #define MAX_DAYS 3636532
37 const sal_Int64 nanoSecInSec
= 1000000000;
38 const sal_Int16 secInMin
= 60;
39 const sal_Int16 minInHour
= 60;
41 const sal_Int64 secMask
= 1000000000;
42 const sal_Int64 minMask
= 100000000000LL;
43 const sal_Int64 hourMask
= 10000000000000LL;
45 const double fNanoSecondsPerDay
= nanoSecInSec
* secInMin
* minInHour
* 24.0;
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::util
;
55 using namespace ::com::sun::star::sdb
;
56 using namespace ::com::sun::star::sdbc
;
57 using namespace ::com::sun::star::lang
;
58 using namespace ::com::sun::star::beans
;
62 css::util::Date
DBTypeConversion::getStandardDate()
64 static css::util::Date
STANDARD_DB_DATE(1,1,1900);
65 return STANDARD_DB_DATE
;
68 OUString
DBTypeConversion::toDateString(const css::util::Date
& rDate
)
78 return OUString::createFromAscii(s
);
81 OUString
DBTypeConversion::toTimeStringS(const css::util::Time
& rTime
)
83 std::ostringstream ostr
;
86 ostr
<< setw(2) << rTime
.Hours
<< ":"
87 << setw(2) << rTime
.Minutes
<< ":"
88 << setw(2) << rTime
.Seconds
;
89 return OUString::createFromAscii(ostr
.str().c_str());
92 OUString
DBTypeConversion::toTimeString(const css::util::Time
& rTime
)
94 std::ostringstream ostr
;
97 ostr
<< setw(2) << rTime
.Hours
<< ":"
98 << setw(2) << rTime
.Minutes
<< ":"
99 << setw(2) << rTime
.Seconds
<< "."
100 << setw(9) << rTime
.NanoSeconds
;
101 return OUString::createFromAscii(ostr
.str().c_str());
104 OUString
DBTypeConversion::toDateTimeString(const css::util::DateTime
& _rDateTime
)
106 css::util::Date
aDate(_rDateTime
.Day
,_rDateTime
.Month
,_rDateTime
.Year
);
107 OUStringBuffer
aTemp(toDateString(aDate
));
108 aTemp
.appendAscii(" ");
109 css::util::Time
const aTime(_rDateTime
.NanoSeconds
, _rDateTime
.Seconds
,
110 _rDateTime
.Minutes
, _rDateTime
.Hours
, _rDateTime
.IsUTC
);
111 aTemp
.append( toTimeString(aTime
) );
112 return aTemp
.makeStringAndClear();
115 css::util::Date
DBTypeConversion::toDate(sal_Int32 _nVal
)
117 css::util::Date aReturn
;
118 aReturn
.Day
= (sal_uInt16
)(_nVal
% 100);
119 aReturn
.Month
= (sal_uInt16
)((_nVal
/ 100) % 100);
120 aReturn
.Year
= (sal_uInt16
)(_nVal
/ 10000);
125 css::util::Time
DBTypeConversion::toTime(sal_Int64 _nVal
)
127 css::util::Time aReturn
;
128 sal_uInt64 unVal
= static_cast<sal_uInt64
>(_nVal
>= 0 ? _nVal
: -_nVal
);
129 aReturn
.Hours
= unVal
/ hourMask
;
130 aReturn
.Minutes
= (unVal
/ minMask
) % 100;
131 aReturn
.Seconds
= (unVal
/ secMask
) % 100;
132 aReturn
.NanoSeconds
= unVal
% secMask
;
136 sal_Int64
DBTypeConversion::getNsFromTime(const css::util::Time
& rVal
)
138 sal_Int32 nHour
= rVal
.Hours
;
139 sal_Int32 nMin
= rVal
.Minutes
;
140 sal_Int32 nSec
= rVal
.Seconds
;
141 sal_Int32 nNanoSec
= rVal
.NanoSeconds
;
144 nSec
* nanoSecInSec
+
145 nMin
* (secInMin
* nanoSecInSec
) +
146 nHour
* (minInHour
* secInMin
* nanoSecInSec
);
150 static const sal_Int32 aDaysInMonth
[12] = { 31, 28, 31, 30, 31, 30,
151 31, 31, 30, 31, 30, 31 };
154 static bool implIsLeapYear(sal_Int32 _nYear
)
156 return ( ( ((_nYear
% 4) == 0)
157 && ((_nYear
% 100) != 0)
160 || ((_nYear
% 400) == 0)
165 static sal_Int32
implDaysInMonth(sal_Int32 _nMonth
, sal_Int32 _nYear
)
167 OSL_ENSURE(_nMonth
> 0 && _nMonth
< 13,"Month as invalid value!");
169 return aDaysInMonth
[_nMonth
-1];
172 if (implIsLeapYear(_nYear
))
173 return aDaysInMonth
[_nMonth
-1] + 1;
175 return aDaysInMonth
[_nMonth
-1];
180 static sal_Int32
implRelativeToAbsoluteNull(const css::util::Date
& _rDate
)
184 // ripped this code from the implementation of tools::Date
185 sal_Int32 nNormalizedYear
= _rDate
.Year
- 1;
186 nDays
= nNormalizedYear
* 365;
188 nDays
+= (nNormalizedYear
/ 4) - (nNormalizedYear
/ 100) + (nNormalizedYear
/ 400);
190 for (sal_Int32 i
= 1; i
< _rDate
.Month
; ++i
)
191 nDays
+= implDaysInMonth(i
, _rDate
.Year
);
197 static void implBuildFromRelative( sal_Int32 nDays
, sal_uInt16
& rDay
, sal_uInt16
& rMonth
, sal_Int16
& rYear
)
206 rYear
= (sal_uInt16
)((nTempDays
/ 365) - i
);
207 nTempDays
-= (rYear
-1) * 365;
208 nTempDays
-= ((rYear
-1) / 4) - ((rYear
-1) / 100) + ((rYear
-1) / 400);
217 if ( nTempDays
> 365 )
219 if ( (nTempDays
!= 366) || !implIsLeapYear( rYear
) )
230 while ( nTempDays
> implDaysInMonth( rMonth
, rYear
) )
232 nTempDays
-= implDaysInMonth( rMonth
, rYear
);
235 rDay
= (sal_uInt16
)nTempDays
;
238 sal_Int32
DBTypeConversion::toDays(const css::util::Date
& _rVal
, const css::util::Date
& _rNullDate
)
240 return implRelativeToAbsoluteNull(_rVal
) - implRelativeToAbsoluteNull(_rNullDate
);
244 double DBTypeConversion::toDouble(const css::util::Date
& rVal
, const css::util::Date
& _rNullDate
)
246 return (double)toDays(rVal
, _rNullDate
);
250 double DBTypeConversion::toDouble(const css::util::Time
& rVal
)
252 return (double)getNsFromTime(rVal
) / fNanoSecondsPerDay
;
256 double DBTypeConversion::toDouble(const css::util::DateTime
& _rVal
, const css::util::Date
& _rNullDate
)
258 sal_Int64 nTime
= toDays(css::util::Date(_rVal
.Day
, _rVal
.Month
, _rVal
.Year
), _rNullDate
);
259 css::util::Time aTimePart
;
261 aTimePart
.Hours
= _rVal
.Hours
;
262 aTimePart
.Minutes
= _rVal
.Minutes
;
263 aTimePart
.Seconds
= _rVal
.Seconds
;
264 aTimePart
.NanoSeconds
= _rVal
.NanoSeconds
;
266 return ((double)nTime
) + toDouble(aTimePart
);
269 static void addDays(sal_Int32 nDays
, css::util::Date
& _rDate
)
271 sal_Int32 nTempDays
= implRelativeToAbsoluteNull( _rDate
);
274 if ( nTempDays
> MAX_DAYS
)
280 else if ( nTempDays
<= 0 )
287 implBuildFromRelative( nTempDays
, _rDate
.Day
, _rDate
.Month
, _rDate
.Year
);
290 static void subDays( sal_Int32 nDays
, css::util::Date
& _rDate
)
292 sal_Int32 nTempDays
= implRelativeToAbsoluteNull( _rDate
);
295 if ( nTempDays
> MAX_DAYS
)
301 else if ( nTempDays
<= 0 )
308 implBuildFromRelative( nTempDays
, _rDate
.Day
, _rDate
.Month
, _rDate
.Year
);
311 css::util::Date
DBTypeConversion::toDate(double dVal
, const css::util::Date
& _rNullDate
)
313 css::util::Date aRet
= _rNullDate
;
316 addDays((sal_Int32
)dVal
,aRet
);
318 subDays((sal_uInt32
)(-dVal
),aRet
);
319 // x -= (sal_uInt32)(-nDays);
324 css::util::Time
DBTypeConversion::toTime(double dVal
, short nDigits
)
326 sal_Int32 nDays
= (sal_Int32
)dVal
;
329 double fSeconds((dVal
- (double)nDays
) * (fNanoSecondsPerDay
/ nanoSecInSec
));
330 fSeconds
= ::rtl::math::round( fSeconds
, nDigits
);
331 nNS
= fSeconds
* nanoSecInSec
;
343 css::util::Time xRet
;
345 // we have to sal_Int32 here because otherwise we get an overflow
346 sal_Int64 nNanoSeconds
= nNS
;
347 sal_Int32 nSeconds
= nNanoSeconds
/ nanoSecInSec
;
348 sal_Int32 nMinutes
= nSeconds
/ secInMin
;
350 xRet
.NanoSeconds
= nNanoSeconds
% nanoSecInSec
;
351 xRet
.Seconds
= nSeconds
% secInMin
;
352 xRet
.Hours
= nMinutes
/ minInHour
;
353 xRet
.Minutes
= nMinutes
% minInHour
;
356 sal_Int64 nTime
= nSign
*
358 xRet
.Seconds
* secMask
+
359 xRet
.Minutes
* minMask
+
360 xRet
.Hours
* hourMask
);
364 xRet
.NanoSeconds
= nanoSecInSec
-1;
365 xRet
.Seconds
= secInMin
-1;
366 xRet
.Minutes
= minInHour
-1;
372 css::util::DateTime
DBTypeConversion::toDateTime(double dVal
, const css::util::Date
& _rNullDate
)
374 css::util::Date aDate
= toDate(dVal
, _rNullDate
);
375 // there is not enough precision in a double to have both a date
376 // and a time up to nanoseconds -> limit to microseconds to have
377 // correct rounding, that is e.g. 13:00:00.000000000 instead of
378 // 12:59:59.999999790
379 css::util::Time aTime
= toTime(dVal
, 6);
381 css::util::DateTime xRet
;
383 xRet
.Day
= aDate
.Day
;
384 xRet
.Month
= aDate
.Month
;
385 xRet
.Year
= aDate
.Year
;
387 xRet
.NanoSeconds
= aTime
.NanoSeconds
;
388 xRet
.Minutes
= aTime
.Minutes
;
389 xRet
.Seconds
= aTime
.Seconds
;
390 xRet
.Hours
= aTime
.Hours
;
396 css::util::Date
DBTypeConversion::toDate(const OUString
& _sSQLString
)
398 // get the token out of a string
399 static sal_Unicode sDateSep
= '-';
401 sal_Int32 nIndex
= 0;
402 sal_uInt16 nYear
= 0,
405 nYear
= (sal_uInt16
)_sSQLString
.getToken(0,sDateSep
,nIndex
).toInt32();
408 nMonth
= (sal_uInt16
)_sSQLString
.getToken(0,sDateSep
,nIndex
).toInt32();
410 nDay
= (sal_uInt16
)_sSQLString
.getToken(0,sDateSep
,nIndex
).toInt32();
413 return css::util::Date(nDay
,nMonth
,nYear
);
417 css::util::DateTime
DBTypeConversion::toDateTime(const OUString
& _sSQLString
)
419 //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Timestamp.html#valueOf(java.lang.String)
420 //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html#valueOf(java.lang.String)
421 //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Time.html#valueOf(java.lang.String)
424 css::util::Date aDate
= toDate(_sSQLString
);
425 css::util::Time aTime
;
426 sal_Int32 nSeparation
= _sSQLString
.indexOf( ' ' );
427 if ( -1 != nSeparation
)
429 const sal_Unicode
*p
= _sSQLString
.getStr() + nSeparation
;
430 const sal_Unicode
*const begin
= p
;
431 while (isspace(*p
)) { ++p
; }
432 nSeparation
+= p
- begin
;
433 aTime
= toTime( _sSQLString
.copy( nSeparation
) );
436 return css::util::DateTime(aTime
.NanoSeconds
, aTime
.Seconds
, aTime
.Minutes
, aTime
.Hours
,
437 aDate
.Day
, aDate
.Month
, aDate
.Year
, false);
441 css::util::Time
DBTypeConversion::toTime(const OUString
& _sSQLString
)
443 css::util::Time aTime
;
444 ::utl::ISO8601parseTime(_sSQLString
, aTime
);
449 } // namespace dbtools
453 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */