xref: /AOO41X/main/connectivity/source/commontools/dbconversion.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include <connectivity/dbconversion.hxx>
31*cdf0e10cSrcweir #include <connectivity/dbcharset.hxx>
32*cdf0e10cSrcweir #include <osl/diagnose.h>
33*cdf0e10cSrcweir #ifndef _INC_STDIO
34*cdf0e10cSrcweir #include <stdio.h>
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLException.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
40*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #define MAX_DAYS    3636532
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //.........................................................................
45*cdf0e10cSrcweir namespace dbtools
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir //.........................................................................
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     using namespace ::comphelper;
51*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
52*cdf0e10cSrcweir     using namespace ::com::sun::star::util;
53*cdf0e10cSrcweir     using namespace ::com::sun::star::sdb;
54*cdf0e10cSrcweir     using namespace ::com::sun::star::sdbc;
55*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
56*cdf0e10cSrcweir     using namespace ::com::sun::star::beans;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     //------------------------------------------------------------------------------
60*cdf0e10cSrcweir     ::com::sun::star::util::Date DBTypeConversion::getStandardDate()
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         static ::com::sun::star::util::Date STANDARD_DB_DATE(1,1,1900);
63*cdf0e10cSrcweir         return STANDARD_DB_DATE;
64*cdf0e10cSrcweir     }
65*cdf0e10cSrcweir     //------------------------------------------------------------------------------
66*cdf0e10cSrcweir     ::rtl::OUString DBTypeConversion::toDateString(const Date& rDate)
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         sal_Char s[11];
69*cdf0e10cSrcweir         snprintf(s,
70*cdf0e10cSrcweir                 sizeof(s),
71*cdf0e10cSrcweir                 "%04d-%02d-%02d",
72*cdf0e10cSrcweir                 (int)rDate.Year,
73*cdf0e10cSrcweir                 (int)rDate.Month,
74*cdf0e10cSrcweir                 (int)rDate.Day);
75*cdf0e10cSrcweir         s[10] = 0;
76*cdf0e10cSrcweir         return ::rtl::OUString::createFromAscii(s);
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir     //------------------------------------------------------------------
79*cdf0e10cSrcweir     ::rtl::OUString DBTypeConversion::toTimeString(const Time& rTime)
80*cdf0e10cSrcweir     {
81*cdf0e10cSrcweir         sal_Char s[9];
82*cdf0e10cSrcweir         snprintf(s,
83*cdf0e10cSrcweir                 sizeof(s),
84*cdf0e10cSrcweir                 "%02d:%02d:%02d",
85*cdf0e10cSrcweir                 (int)rTime.Hours,
86*cdf0e10cSrcweir                 (int)rTime.Minutes,
87*cdf0e10cSrcweir                 (int)rTime.Seconds);
88*cdf0e10cSrcweir         s[8] = 0;
89*cdf0e10cSrcweir         return ::rtl::OUString::createFromAscii(s);
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     //------------------------------------------------------------------
93*cdf0e10cSrcweir     ::rtl::OUString DBTypeConversion::toDateTimeString(const DateTime& _rDateTime)
94*cdf0e10cSrcweir     {
95*cdf0e10cSrcweir         Date aDate(_rDateTime.Day,_rDateTime.Month,_rDateTime.Year);
96*cdf0e10cSrcweir         ::rtl::OUStringBuffer aTemp(toDateString(aDate));
97*cdf0e10cSrcweir         aTemp.appendAscii(" ");
98*cdf0e10cSrcweir         Time aTime(0,_rDateTime.Seconds,_rDateTime.Minutes,_rDateTime.Hours);
99*cdf0e10cSrcweir         aTemp.append( toTimeString(aTime) );
100*cdf0e10cSrcweir         aTemp.appendAscii(".");
101*cdf0e10cSrcweir         aTemp.append( static_cast<sal_Int32>(_rDateTime.HundredthSeconds));
102*cdf0e10cSrcweir         return  aTemp.makeStringAndClear();
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir     //------------------------------------------------------------------------------
105*cdf0e10cSrcweir     Date DBTypeConversion::toDate(sal_Int32 _nVal)
106*cdf0e10cSrcweir     {
107*cdf0e10cSrcweir         Date aReturn;
108*cdf0e10cSrcweir         aReturn.Day = (sal_uInt16)(_nVal % 100);
109*cdf0e10cSrcweir         aReturn.Month = (sal_uInt16)((_nVal  / 100) % 100);
110*cdf0e10cSrcweir         aReturn.Year = (sal_uInt16)(_nVal  / 10000);
111*cdf0e10cSrcweir         return aReturn;
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     //------------------------------------------------------------------------------
115*cdf0e10cSrcweir     Time DBTypeConversion::toTime(sal_Int32 _nVal)
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         Time aReturn;
118*cdf0e10cSrcweir         aReturn.Hours = (sal_uInt16)(((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 1000000);
119*cdf0e10cSrcweir         aReturn.Minutes = (sal_uInt16)((((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 10000) % 100);
120*cdf0e10cSrcweir         aReturn.Seconds = (sal_uInt16)((((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 100) % 100);
121*cdf0e10cSrcweir         aReturn.HundredthSeconds = (sal_uInt16)(((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) % 100);
122*cdf0e10cSrcweir         return aReturn;
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     const double fMilliSecondsPerDay = 86400000.0;
126*cdf0e10cSrcweir     //------------------------------------------------------------------------------
127*cdf0e10cSrcweir     sal_Int32 DBTypeConversion::toINT32(const Date& rVal)
128*cdf0e10cSrcweir     {
129*cdf0e10cSrcweir         return ((sal_Int32)(rVal.Day%100)) +
130*cdf0e10cSrcweir             (((sal_Int32)(rVal.Month%100))*100) +
131*cdf0e10cSrcweir             (((sal_Int32) rVal.Year%10000)*10000);
132*cdf0e10cSrcweir     }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     //------------------------------------------------------------------------------
135*cdf0e10cSrcweir     sal_Int32 DBTypeConversion::toINT32(const Time& rVal)
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         // Zeit normalisieren
138*cdf0e10cSrcweir         sal_Int32 nSeconds          = rVal.Seconds + rVal.HundredthSeconds / 100;
139*cdf0e10cSrcweir         sal_Int32 nHundredthSeconds = rVal.HundredthSeconds % 100;
140*cdf0e10cSrcweir         sal_Int32 nMinutes          = rVal.Minutes + nSeconds / 60;
141*cdf0e10cSrcweir         nSeconds                    = nSeconds % 60;
142*cdf0e10cSrcweir         sal_Int32 nHours            = rVal.Hours + nMinutes / 60;
143*cdf0e10cSrcweir         nMinutes                    = nMinutes % 60;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         // Zeit zusammenbauen
146*cdf0e10cSrcweir         return (sal_Int32)(nHundredthSeconds + (nSeconds*100) + (nMinutes*10000) + (nHours*1000000));
147*cdf0e10cSrcweir     }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     //------------------------------------------------------------------------------
150*cdf0e10cSrcweir     sal_Int64 DBTypeConversion::toINT64(const DateTime& rVal)
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         // Zeit normalisieren
153*cdf0e10cSrcweir         sal_Int32 nSeconds          = rVal.Seconds + rVal.HundredthSeconds / 100;
154*cdf0e10cSrcweir         sal_Int32 nHundredthSeconds = rVal.HundredthSeconds % 100;
155*cdf0e10cSrcweir         sal_Int32 nMinutes          = rVal.Minutes + nSeconds / 60;
156*cdf0e10cSrcweir         nSeconds                    = nSeconds % 60;
157*cdf0e10cSrcweir         sal_Int32 nHours            = rVal.Hours + nMinutes / 60;
158*cdf0e10cSrcweir         nMinutes                    = nMinutes % 60;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         // Zeit zusammenbauen
161*cdf0e10cSrcweir         sal_Int32 nTime = (sal_Int32)(nHundredthSeconds + (nSeconds*100) + (nMinutes*10000) + (nHours*1000000));
162*cdf0e10cSrcweir         sal_Int32 nDate = ((sal_Int32)(rVal.Day%100)) + (((sal_Int32)(rVal.Month%100))*100) + (((sal_Int32) rVal.Year%10000)*10000);
163*cdf0e10cSrcweir         sal_Int64 nRet;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         nRet = (sal_Int64) nTime;
166*cdf0e10cSrcweir         nRet <<= 32;
167*cdf0e10cSrcweir         nRet += nDate;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         return nRet;
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     //------------------------------------------------------------------------------
173*cdf0e10cSrcweir     sal_Int32 DBTypeConversion::getMsFromTime(const Time& rVal)
174*cdf0e10cSrcweir     {
175*cdf0e10cSrcweir         sal_Int32   nHour     = rVal.Hours;
176*cdf0e10cSrcweir         sal_Int32   nMin      = rVal.Minutes;
177*cdf0e10cSrcweir         sal_Int32   nSec      = rVal.Seconds;
178*cdf0e10cSrcweir         sal_Int32   n100Sec   = rVal.HundredthSeconds;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         return ((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10));
181*cdf0e10cSrcweir     }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     //------------------------------------------------------------------------------
184*cdf0e10cSrcweir     static sal_Int32 aDaysInMonth[12] = {   31, 28, 31, 30, 31, 30,
185*cdf0e10cSrcweir                                             31, 31, 30, 31, 30, 31 };
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     //------------------------------------------------------------------------------
188*cdf0e10cSrcweir     static sal_Bool implIsLeapYear(sal_Int32 _nYear)
189*cdf0e10cSrcweir     {
190*cdf0e10cSrcweir         return  (   (   ((_nYear % 4) == 0)
191*cdf0e10cSrcweir                     &&  ((_nYear % 100) != 0)
192*cdf0e10cSrcweir                     )
193*cdf0e10cSrcweir                 )
194*cdf0e10cSrcweir                 ||  ((_nYear % 400) == 0)
195*cdf0e10cSrcweir                 ;
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     //------------------------------------------------------------------------------
199*cdf0e10cSrcweir     static sal_Int32 implDaysInMonth(sal_Int32 _nMonth, sal_Int32 _nYear)
200*cdf0e10cSrcweir     {
201*cdf0e10cSrcweir         OSL_ENSURE(_nMonth > 0 && _nMonth < 13,"Month as invalid value!");
202*cdf0e10cSrcweir         if (_nMonth != 2)
203*cdf0e10cSrcweir             return aDaysInMonth[_nMonth-1];
204*cdf0e10cSrcweir         else
205*cdf0e10cSrcweir         {
206*cdf0e10cSrcweir             if (implIsLeapYear(_nYear))
207*cdf0e10cSrcweir                 return aDaysInMonth[_nMonth-1] + 1;
208*cdf0e10cSrcweir             else
209*cdf0e10cSrcweir                 return aDaysInMonth[_nMonth-1];
210*cdf0e10cSrcweir         }
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     //------------------------------------------------------------------------------
214*cdf0e10cSrcweir     static sal_Int32 implRelativeToAbsoluteNull(const Date& _rDate)
215*cdf0e10cSrcweir     {
216*cdf0e10cSrcweir         sal_Int32 nDays = 0;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir         // ripped this code from the implementation of tools::Date
219*cdf0e10cSrcweir         sal_Int32 nNormalizedYear = _rDate.Year - 1;
220*cdf0e10cSrcweir         nDays = nNormalizedYear * 365;
221*cdf0e10cSrcweir         // leap years
222*cdf0e10cSrcweir         nDays += (nNormalizedYear / 4) - (nNormalizedYear / 100) + (nNormalizedYear / 400);
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir         for (sal_Int32 i = 1; i < _rDate.Month; ++i)
225*cdf0e10cSrcweir             nDays += implDaysInMonth(i, _rDate.Year);
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         nDays += _rDate.Day;
228*cdf0e10cSrcweir         return nDays;
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir     //------------------------------------------------------------------------------
231*cdf0e10cSrcweir     static void implBuildFromRelative( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear)
232*cdf0e10cSrcweir     {
233*cdf0e10cSrcweir         sal_Int32   nTempDays;
234*cdf0e10cSrcweir         sal_Int32   i = 0;
235*cdf0e10cSrcweir         sal_Bool    bCalc;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         do
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir             nTempDays = nDays;
240*cdf0e10cSrcweir             rYear = (sal_uInt16)((nTempDays / 365) - i);
241*cdf0e10cSrcweir             nTempDays -= (rYear-1) * 365;
242*cdf0e10cSrcweir             nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400);
243*cdf0e10cSrcweir             bCalc = sal_False;
244*cdf0e10cSrcweir             if ( nTempDays < 1 )
245*cdf0e10cSrcweir             {
246*cdf0e10cSrcweir                 i++;
247*cdf0e10cSrcweir                 bCalc = sal_True;
248*cdf0e10cSrcweir             }
249*cdf0e10cSrcweir             else
250*cdf0e10cSrcweir             {
251*cdf0e10cSrcweir                 if ( nTempDays > 365 )
252*cdf0e10cSrcweir                 {
253*cdf0e10cSrcweir                     if ( (nTempDays != 366) || !implIsLeapYear( rYear ) )
254*cdf0e10cSrcweir                     {
255*cdf0e10cSrcweir                         i--;
256*cdf0e10cSrcweir                         bCalc = sal_True;
257*cdf0e10cSrcweir                     }
258*cdf0e10cSrcweir                 }
259*cdf0e10cSrcweir             }
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir         while ( bCalc );
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir         rMonth = 1;
264*cdf0e10cSrcweir         while ( nTempDays > implDaysInMonth( rMonth, rYear ) )
265*cdf0e10cSrcweir         {
266*cdf0e10cSrcweir             nTempDays -= implDaysInMonth( rMonth, rYear );
267*cdf0e10cSrcweir             rMonth++;
268*cdf0e10cSrcweir         }
269*cdf0e10cSrcweir         rDay = (sal_uInt16)nTempDays;
270*cdf0e10cSrcweir     }
271*cdf0e10cSrcweir     //------------------------------------------------------------------------------
272*cdf0e10cSrcweir     sal_Int32 DBTypeConversion::toDays(const Date& _rVal, const Date& _rNullDate)
273*cdf0e10cSrcweir     {
274*cdf0e10cSrcweir         return implRelativeToAbsoluteNull(_rVal) - implRelativeToAbsoluteNull(_rNullDate);
275*cdf0e10cSrcweir     }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     //------------------------------------------------------------------------------
278*cdf0e10cSrcweir     double DBTypeConversion::toDouble(const Date& rVal, const Date& _rNullDate)
279*cdf0e10cSrcweir     {
280*cdf0e10cSrcweir         return (double)toDays(rVal, _rNullDate);
281*cdf0e10cSrcweir     }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir     //------------------------------------------------------------------------------
284*cdf0e10cSrcweir     double DBTypeConversion::toDouble(const Time& rVal)
285*cdf0e10cSrcweir     {
286*cdf0e10cSrcweir         return (double)getMsFromTime(rVal) / fMilliSecondsPerDay;
287*cdf0e10cSrcweir     }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     //------------------------------------------------------------------------------
290*cdf0e10cSrcweir     double DBTypeConversion::toDouble(const DateTime& _rVal, const Date& _rNullDate)
291*cdf0e10cSrcweir     {
292*cdf0e10cSrcweir         sal_Int64   nTime     = toDays(Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate);
293*cdf0e10cSrcweir         Time aTimePart;
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir         aTimePart.Hours             = _rVal.Hours;
296*cdf0e10cSrcweir         aTimePart.Minutes           = _rVal.Minutes;
297*cdf0e10cSrcweir         aTimePart.Seconds           = _rVal.Seconds;
298*cdf0e10cSrcweir         aTimePart.HundredthSeconds  = _rVal.HundredthSeconds;
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir         return ((double)nTime) + toDouble(aTimePart);
301*cdf0e10cSrcweir     }
302*cdf0e10cSrcweir     // -------------------------------------------------------------------------
303*cdf0e10cSrcweir     static void addDays(sal_Int32 nDays, Date& _rDate)
304*cdf0e10cSrcweir     {
305*cdf0e10cSrcweir         sal_Int32   nTempDays = implRelativeToAbsoluteNull( _rDate );
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir         nTempDays += nDays;
308*cdf0e10cSrcweir         if ( nTempDays > MAX_DAYS )
309*cdf0e10cSrcweir         {
310*cdf0e10cSrcweir             _rDate.Day      = 31;
311*cdf0e10cSrcweir             _rDate.Month    = 12;
312*cdf0e10cSrcweir             _rDate.Year     = 9999;
313*cdf0e10cSrcweir         }
314*cdf0e10cSrcweir         else if ( nTempDays <= 0 )
315*cdf0e10cSrcweir         {
316*cdf0e10cSrcweir             _rDate.Day      = 1;
317*cdf0e10cSrcweir             _rDate.Month    = 1;
318*cdf0e10cSrcweir             _rDate.Year     = 00;
319*cdf0e10cSrcweir         }
320*cdf0e10cSrcweir         else
321*cdf0e10cSrcweir             implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
322*cdf0e10cSrcweir     }
323*cdf0e10cSrcweir     // -----------------------------------------------------------------------
324*cdf0e10cSrcweir     static void subDays( sal_Int32 nDays, Date& _rDate )
325*cdf0e10cSrcweir     {
326*cdf0e10cSrcweir         sal_Int32   nTempDays = implRelativeToAbsoluteNull( _rDate );
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir         nTempDays -= nDays;
329*cdf0e10cSrcweir         if ( nTempDays > MAX_DAYS )
330*cdf0e10cSrcweir         {
331*cdf0e10cSrcweir             _rDate.Day      = 31;
332*cdf0e10cSrcweir             _rDate.Month    = 12;
333*cdf0e10cSrcweir             _rDate.Year     = 9999;
334*cdf0e10cSrcweir         }
335*cdf0e10cSrcweir         else if ( nTempDays <= 0 )
336*cdf0e10cSrcweir         {
337*cdf0e10cSrcweir             _rDate.Day      = 1;
338*cdf0e10cSrcweir             _rDate.Month    = 1;
339*cdf0e10cSrcweir             _rDate.Year     = 00;
340*cdf0e10cSrcweir         }
341*cdf0e10cSrcweir         else
342*cdf0e10cSrcweir             implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
343*cdf0e10cSrcweir     }
344*cdf0e10cSrcweir     // -------------------------------------------------------------------------
345*cdf0e10cSrcweir     Date DBTypeConversion::toDate(double dVal, const Date& _rNullDate)
346*cdf0e10cSrcweir     {
347*cdf0e10cSrcweir         Date aRet = _rNullDate;
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir         if (dVal >= 0)
350*cdf0e10cSrcweir             addDays((sal_Int32)dVal,aRet);
351*cdf0e10cSrcweir         else
352*cdf0e10cSrcweir             subDays((sal_uInt32)(-dVal),aRet);
353*cdf0e10cSrcweir             //  x -= (sal_uInt32)(-nDays);
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir         return aRet;
356*cdf0e10cSrcweir     }
357*cdf0e10cSrcweir     // -------------------------------------------------------------------------
358*cdf0e10cSrcweir     Time DBTypeConversion::toTime(double dVal)
359*cdf0e10cSrcweir     {
360*cdf0e10cSrcweir         sal_Int32 nDays     = (sal_Int32)dVal;
361*cdf0e10cSrcweir         sal_Int32 nMS = sal_Int32((dVal - (double)nDays) * fMilliSecondsPerDay + 0.5);
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir         sal_Int16 nSign;
364*cdf0e10cSrcweir         if ( nMS < 0 )
365*cdf0e10cSrcweir         {
366*cdf0e10cSrcweir             nMS *= -1;
367*cdf0e10cSrcweir             nSign = -1;
368*cdf0e10cSrcweir         }
369*cdf0e10cSrcweir         else
370*cdf0e10cSrcweir             nSign = 1;
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir         Time xRet;
373*cdf0e10cSrcweir         // Zeit normalisieren
374*cdf0e10cSrcweir         // we have to sal_Int32 here because otherwise we get an overflow
375*cdf0e10cSrcweir         sal_Int32 nHundredthSeconds = nMS/10;
376*cdf0e10cSrcweir         sal_Int32 nSeconds          = nHundredthSeconds / 100;
377*cdf0e10cSrcweir         sal_Int32 nMinutes          = nSeconds / 60;
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir         xRet.HundredthSeconds       = (sal_uInt16)(nHundredthSeconds % 100);
380*cdf0e10cSrcweir         xRet.Seconds                = (sal_uInt16)(nSeconds % 60);
381*cdf0e10cSrcweir         xRet.Hours                  = (sal_uInt16)(nMinutes / 60);
382*cdf0e10cSrcweir         xRet.Minutes                = (sal_uInt16)(nMinutes % 60);
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir         // Zeit zusammenbauen
385*cdf0e10cSrcweir         sal_Int32 nTime = (sal_Int32)(xRet.HundredthSeconds + (xRet.Seconds*100) + (xRet.Minutes*10000) + (xRet.Hours*1000000)) * nSign;
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir         if(nTime < 0)
388*cdf0e10cSrcweir         {
389*cdf0e10cSrcweir             xRet.HundredthSeconds   = 99;
390*cdf0e10cSrcweir             xRet.Minutes            = 59;
391*cdf0e10cSrcweir             xRet.Seconds            = 59;
392*cdf0e10cSrcweir             xRet.Hours              = 23;
393*cdf0e10cSrcweir         }
394*cdf0e10cSrcweir         return xRet;
395*cdf0e10cSrcweir     }
396*cdf0e10cSrcweir     //------------------------------------------------------------------------------
397*cdf0e10cSrcweir     DateTime DBTypeConversion::toDateTime(double dVal, const Date& _rNullDate)
398*cdf0e10cSrcweir     {
399*cdf0e10cSrcweir         Date aDate = toDate(dVal, _rNullDate);
400*cdf0e10cSrcweir         Time aTime = toTime(dVal);
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir         DateTime xRet;
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir         xRet.Day                = aDate.Day;
405*cdf0e10cSrcweir         xRet.Month              = aDate.Month;
406*cdf0e10cSrcweir         xRet.Year               = aDate.Year;
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir         xRet.HundredthSeconds   = aTime.HundredthSeconds;
409*cdf0e10cSrcweir         xRet.Minutes            = aTime.Minutes;
410*cdf0e10cSrcweir         xRet.Seconds            = aTime.Seconds;
411*cdf0e10cSrcweir         xRet.Hours              = aTime.Hours;
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir         return xRet;
415*cdf0e10cSrcweir     }
416*cdf0e10cSrcweir     //------------------------------------------------------------------------------
417*cdf0e10cSrcweir     Date DBTypeConversion::toDate(const ::rtl::OUString& _sSQLString)
418*cdf0e10cSrcweir     {
419*cdf0e10cSrcweir         // get the token out of a string
420*cdf0e10cSrcweir         static sal_Unicode sDateSep = '-';
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir         sal_Int32 nIndex    = 0;
423*cdf0e10cSrcweir         sal_uInt16  nYear   = 0,
424*cdf0e10cSrcweir                     nMonth  = 0,
425*cdf0e10cSrcweir                     nDay    = 0;
426*cdf0e10cSrcweir         nYear   = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
427*cdf0e10cSrcweir         if(nIndex != -1)
428*cdf0e10cSrcweir         {
429*cdf0e10cSrcweir             nMonth = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
430*cdf0e10cSrcweir             if(nIndex != -1)
431*cdf0e10cSrcweir                 nDay = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
432*cdf0e10cSrcweir         }
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir         return Date(nDay,nMonth,nYear);
435*cdf0e10cSrcweir     }
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir     //-----------------------------------------------------------------------------
438*cdf0e10cSrcweir     DateTime DBTypeConversion::toDateTime(const ::rtl::OUString& _sSQLString)
439*cdf0e10cSrcweir     {
440*cdf0e10cSrcweir         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Timestamp.html#valueOf(java.lang.String)
441*cdf0e10cSrcweir         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html#valueOf(java.lang.String)
442*cdf0e10cSrcweir         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Time.html#valueOf(java.lang.String)
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir         // the date part
445*cdf0e10cSrcweir         Date aDate = toDate(_sSQLString);
446*cdf0e10cSrcweir         Time aTime;
447*cdf0e10cSrcweir         sal_Int32 nSeparation = _sSQLString.indexOf( ' ' );
448*cdf0e10cSrcweir         if ( -1 != nSeparation )
449*cdf0e10cSrcweir             aTime = toTime( _sSQLString.copy( nSeparation ) );
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir         return DateTime(aTime.HundredthSeconds,aTime.Seconds,aTime.Minutes,aTime.Hours,aDate.Day,aDate.Month,aDate.Year);
452*cdf0e10cSrcweir     }
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir     //-----------------------------------------------------------------------------
455*cdf0e10cSrcweir     Time DBTypeConversion::toTime(const ::rtl::OUString& _sSQLString)
456*cdf0e10cSrcweir     {
457*cdf0e10cSrcweir         static sal_Unicode sTimeSep = ':';
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir         sal_Int32 nIndex    = 0;
460*cdf0e10cSrcweir         sal_uInt16  nHour   = 0,
461*cdf0e10cSrcweir                     nMinute = 0,
462*cdf0e10cSrcweir                     nSecond = 0,
463*cdf0e10cSrcweir                     nHundredthSeconds   = 0;
464*cdf0e10cSrcweir         nHour   = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32();
465*cdf0e10cSrcweir         if(nIndex != -1)
466*cdf0e10cSrcweir         {
467*cdf0e10cSrcweir             nMinute = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32();
468*cdf0e10cSrcweir             if(nIndex != -1)
469*cdf0e10cSrcweir             {
470*cdf0e10cSrcweir                 nSecond = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32();
471*cdf0e10cSrcweir                 nIndex = 0;
472*cdf0e10cSrcweir                 ::rtl::OUString sNano(_sSQLString.getToken(1,'.',nIndex));
473*cdf0e10cSrcweir                 if ( sNano.getLength() )
474*cdf0e10cSrcweir                 {
475*cdf0e10cSrcweir                     // our time struct only supports hundredth seconds
476*cdf0e10cSrcweir                     sNano = sNano.copy(0,::std::min<sal_Int32>(sNano.getLength(),2));
477*cdf0e10cSrcweir                     const static ::rtl::OUString s_Zeros(RTL_CONSTASCII_USTRINGPARAM("00"));
478*cdf0e10cSrcweir                     sNano += s_Zeros.copy(0,s_Zeros.getLength() - sNano.getLength());
479*cdf0e10cSrcweir                     nHundredthSeconds = static_cast<sal_uInt16>(sNano.toInt32());
480*cdf0e10cSrcweir                 }
481*cdf0e10cSrcweir             }
482*cdf0e10cSrcweir         }
483*cdf0e10cSrcweir         return Time(nHundredthSeconds,nSecond,nMinute,nHour);
484*cdf0e10cSrcweir     }
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir //.........................................................................
487*cdf0e10cSrcweir }   // namespace dbtools
488*cdf0e10cSrcweir //.........................................................................
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir 
491