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