summaryrefslogtreecommitdiff
path: root/connectivity/source/commontools/dbconversion.cxx
blob: 5db79e6ce3a0e2fec828977f7ac14acd65d1368f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*************************************************************************
 *
 *  $RCSfile: dbconversion.cxx,v $
 *
 *  $Revision: 1.14 $
 *
 *  last change: $Author: pl $ $Date: 2001-05-11 17:25:49 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _DBHELPER_DBCONVERSION_HXX_
#include <connectivity/dbconversion.hxx>
#endif
#ifndef _CONNECTIVITY_DBTOOLS_HXX_
#include <connectivity/dbtools.hxx>
#endif
#ifndef _COMPHELPER_NUMBERS_HXX_
#include <comphelper/numbers.hxx>
#endif
#ifndef _COMPHELPER_DATETIME_HXX_
#include <comphelper/datetime.hxx>
#endif
#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif
#ifndef _COM_SUN_STAR_UTIL_NUMBERFORMAT_HPP_
#include <com/sun/star/util/NumberFormat.hpp>
#endif
#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTYPES_HPP_
#include <com/sun/star/util/XNumberFormatTypes.hpp>
#endif
#ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
#include <com/sun/star/sdbc/DataType.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_XCOLUMNUPDATE_HPP_
#include <com/sun/star/sdb/XColumnUpdate.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_XCOLUMN_HPP_
#include <com/sun/star/sdb/XColumn.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
#include <com/sun/star/beans/XPropertySet.hpp>
#endif
#ifndef _COM_SUN_STAR_SCRIPT_XTYPECONVERTER_HPP_
#include <com/sun/star/script/XTypeConverter.hpp>
#endif
#ifndef _INC_STDIO
#include <stdio.h>
#endif
#ifndef _COMPHELPER_EXTRACT_HXX_
#include <comphelper/extract.hxx>
#endif


#define MAX_DAYS    3636532

//.........................................................................
namespace dbtools
{
//.........................................................................


using namespace ::comphelper;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::script;

//------------------------------------------------------------------------------
Date DBTypeConversion::STANDARD_DB_DATE(1, 1, 1900);
::com::sun::star::util::Date DBTypeConversion::getStandardDate()
{
    return STANDARD_DB_DATE;
}
//------------------------------------------------------------------------------
void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
                                const Reference<XNumberFormatter>& xFormatter,
                                const Date& rNullDate,
                                const ::rtl::OUString& rString,
                                sal_Int32 nKey,
                                sal_Int16 nFieldType,
                                sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException)
{
    double fValue = 0;
    if (rString.getLength())
    {
            // Muss der String formatiert werden?
        sal_Int16 nTypeClass = nKeyType & ~NumberFormat::DEFINED;
        sal_Bool bTextFormat = nTypeClass == NumberFormat::TEXT;
        sal_Int32 nKeyToUse  = bTextFormat ? 0 : nKey;
        sal_Int16 nRealUsedTypeClass = nTypeClass;
            // bei einem Text-Format muessen wir dem Formatter etwas mehr Freiheiten einraeumen, sonst
            // wirft convertStringToNumber eine NotNumericException
        try
        {
            fValue = xFormatter->convertStringToNumber(nKeyToUse, rString);
            sal_Int32 nRealUsedKey = xFormatter->detectNumberFormat(0, rString);
            if (nRealUsedKey != nKeyToUse)
                nRealUsedTypeClass = getNumberFormatType(xFormatter, nRealUsedKey) & ~NumberFormat::DEFINED;

            // und noch eine Sonderbehandlung, diesmal fuer Prozent-Formate
            if ((NumberFormat::NUMBER == nRealUsedTypeClass) && (NumberFormat::PERCENT == nTypeClass))
            {   // die Formatierung soll eigentlich als Prozent erfolgen, aber der String stellt nur eine
                // einfache Nummer dar -> anpassen
                ::rtl::OUString sExpanded(rString);
                static ::rtl::OUString s_sPercentSymbol = ::rtl::OUString::createFromAscii("%");
                    // need a method to add a sal_Unicode to a string, 'til then we use a static string ...
                sExpanded += s_sPercentSymbol;
                fValue = xFormatter->convertStringToNumber(nKeyToUse, sExpanded);
            }

            switch (nRealUsedTypeClass)
            {
                case NumberFormat::DATE:
                case NumberFormat::DATETIME:
                    xVariant->updateDouble(toStandardDbDate(rNullDate, fValue));
                    break;
                case NumberFormat::TIME:
                case NumberFormat::CURRENCY:
                case NumberFormat::NUMBER:
                case NumberFormat::SCIENTIFIC:
                case NumberFormat::FRACTION:
                case NumberFormat::PERCENT:
                    xVariant->updateDouble(fValue);
                    break;
                default:
                    xVariant->updateString(rString);
            }
        }
        catch(...)
        {
            xVariant->updateString(rString);
        }
    }
    else
    {
        switch (nFieldType)
        {
            case ::com::sun::star::sdbc::DataType::CHAR:
            case ::com::sun::star::sdbc::DataType::VARCHAR:
            case ::com::sun::star::sdbc::DataType::LONGVARCHAR:
                xVariant->updateString(rString);
                break;
            default:
                xVariant->updateNull();
        }
    }
}

//------------------------------------------------------------------------------
void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
                                const Date& rNullDate,
                                const double& rValue,
                                sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException)
{
    switch (nKeyType & ~NumberFormat::DEFINED)
    {
        case NumberFormat::DATE:
        case NumberFormat::DATETIME:
        //  case NumberFormat::TIME:
            xVariant->updateDouble(toStandardDbDate(rNullDate, rValue));
            break;
        default:
            xVariant->updateDouble(rValue);
    }
}

//------------------------------------------------------------------------------
double DBTypeConversion::getValue(const Reference<XColumn>& xVariant,
                                  const Date& rNullDate,
                                  sal_Int16 nKeyType)
{
    try
    {
        switch (nKeyType & ~NumberFormat::DEFINED)
        {
            case NumberFormat::DATE:
            case NumberFormat::DATETIME:
            //  case NumberFormat::TIME:
                return toNullDate(rNullDate, xVariant->getDouble());
            default:
                return xVariant->getDouble();
        }
    }
    catch(...)
    {
        return 0.0;
    }
}
//------------------------------------------------------------------------------
::rtl::OUString DBTypeConversion::getValue(const Reference< ::com::sun::star::beans::XPropertySet>& _xColumn,
                                           const Reference<XNumberFormatter>& _xFormatter,
                                           const ::com::sun::star::lang::Locale& _rLocale,
                                           const Date& _rNullDate)
{
    sal_Int32 nKey;
    sal_Int16 nKeyType;

    OSL_ENSURE(_xColumn.is() && _xFormatter.is(), "DBTypeConversion::getValue: invalid arg !");
    if (!_xColumn.is() || !_xFormatter.is())
        return ::rtl::OUString();

    try
    {
        _xColumn->getPropertyValue(::rtl::OUString::createFromAscii("FormatKey")) >>= nKey;
    }
    catch (...)
    {
    }

    if (!nKey)
    {
        Reference<XNumberFormats> xFormats( _xFormatter->getNumberFormatsSupplier()->getNumberFormats() );
        Reference<XNumberFormatTypes> xTypeList(_xFormatter->getNumberFormatsSupplier()->getNumberFormats(), UNO_QUERY);

        nKey = dbtools::getDefaultNumberFormat(_xColumn,
                                           Reference< XNumberFormatTypes > (xFormats, UNO_QUERY),
                                           _rLocale);

        nKeyType = getNumberFormatType(_xFormatter, nKey) & ~NumberFormat::DEFINED;
    }

    return DBTypeConversion::getValue(Reference< XColumn > (_xColumn, UNO_QUERY), _xFormatter, _rNullDate, nKey, nKeyType);
}

//------------------------------------------------------------------------------
::rtl::OUString DBTypeConversion::getValue(const Reference<XColumn>& xVariant,
                                   const Reference<XNumberFormatter>& xFormatter,
                                   const Date& rNullDate,
                                   sal_Int32 nKey,
                                   sal_Int16 nKeyType)
{
    ::rtl::OUString aString;
    if (xVariant.is())
    {
        try
        {
            switch (nKeyType & ~NumberFormat::DEFINED)
            {
                case NumberFormat::DATE:
                case NumberFormat::DATETIME:
                {
                    double fValue = xVariant->getDouble();
                    if (!xVariant->wasNull())
                        aString = xFormatter->convertNumberToString(nKey, toNullDate(rNullDate, fValue));
                }   break;
                case NumberFormat::TIME:
                case NumberFormat::NUMBER:
                case NumberFormat::SCIENTIFIC:
                case NumberFormat::FRACTION:
                case NumberFormat::PERCENT:
                {
                    double fValue = xVariant->getDouble();
                    if (!xVariant->wasNull())
                        aString = xFormatter->convertNumberToString(nKey, fValue);
                }   break;
                case NumberFormat::CURRENCY:
                {
                    double fValue = xVariant->getDouble();
                    if (!xVariant->wasNull())
                        aString = xFormatter->getInputString(nKey, fValue);
                }   break;
                case NumberFormat::TEXT:
                    aString = xFormatter->formatString(nKey, xVariant->getString());
                    break;
                default:
                    aString = xVariant->getString();
            }
        }
        catch(...)
        {
            aString = xVariant->getString();
        }
    }
    return aString;
}

//------------------------------------------------------------------------------
Date DBTypeConversion::toDate(sal_Int32 _nVal)
{
    Date aReturn;
    aReturn.Day = (sal_uInt16)(_nVal % 100);
    aReturn.Month = (sal_uInt16)((_nVal  / 100) % 100);
    aReturn.Year = (sal_uInt16)(_nVal  / 10000);
    return aReturn;
}

//------------------------------------------------------------------------------
Time DBTypeConversion::toTime(sal_Int32 _nVal)
{
    Time aReturn;
    aReturn.Hours = (sal_uInt16)(((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 1000000);
    aReturn.Minutes = (sal_uInt16)((((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 10000) % 100);
    aReturn.Seconds = (sal_uInt16)((((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 100) % 100);
    aReturn.HundredthSeconds = (sal_uInt16)(((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) % 100);
    return aReturn;
}

const double fMilliSecondsPerDay = 86400000.0;
//------------------------------------------------------------------------------
sal_Int32 DBTypeConversion::toINT32(const Date& rVal)
{
    return ((sal_Int32)(rVal.Day%100)) +
          (((sal_Int32)(rVal.Month%100))*100) +
          (((sal_Int32) rVal.Year%10000)*10000);
}

//------------------------------------------------------------------------------
sal_Int32 DBTypeConversion::toINT32(const Time& rVal)
{
    // Zeit normalisieren
    sal_Int32 nSeconds          = rVal.Seconds + rVal.HundredthSeconds / 100;
    sal_Int32 nHundredthSeconds = rVal.HundredthSeconds % 100;
    sal_Int32 nMinutes          = rVal.Minutes + nSeconds / 60;
    nSeconds                    = nSeconds % 60;
    sal_Int32 nHours            = rVal.Hours + nMinutes / 60;
    nMinutes                    = nMinutes % 60;

    // Zeit zusammenbauen
    return (sal_Int32)(nHundredthSeconds + (nSeconds*100) + (nMinutes*10000) + (nHours*1000000));
}

//------------------------------------------------------------------------------
sal_Int64 DBTypeConversion::toINT64(const DateTime& rVal)
{
    // Zeit normalisieren
    sal_Int32 nSeconds          = rVal.Seconds + rVal.HundredthSeconds / 100;
    sal_Int32 nHundredthSeconds = rVal.HundredthSeconds % 100;
    sal_Int32 nMinutes          = rVal.Minutes + nSeconds / 60;
    nSeconds                    = nSeconds % 60;
    sal_Int32 nHours            = rVal.Hours + nMinutes / 60;
    nMinutes                    = nMinutes % 60;

    // Zeit zusammenbauen
    sal_Int32 nTime = (sal_Int32)(nHundredthSeconds + (nSeconds*100) + (nMinutes*10000) + (nHours*1000000));
    sal_Int32 nDate = ((sal_Int32)(rVal.Day%100)) + (((sal_Int32)(rVal.Month%100))*100) + (((sal_Int32) rVal.Year%10000)*10000);
    sal_Int64 nRet;

    nRet = (sal_Int64) nTime;
    nRet <<= 32;
    nRet += nDate;

    return nRet;
}

//------------------------------------------------------------------------------
sal_Int32 DBTypeConversion::getMsFromTime(const Time& rVal)
{
    sal_Int16   nSign     = (toINT32(rVal) >= 0) ? +1 : -1;
    sal_Int32   nHour     = rVal.Hours;
    sal_Int32   nMin      = rVal.Minutes;
    sal_Int32   nSec      = rVal.Seconds;
    sal_Int32   n100Sec   = rVal.HundredthSeconds;

    return (((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10))*nSign);
}

//------------------------------------------------------------------------------
static sal_Int32 aDaysInMonth[12] = {   31, 28, 31, 30, 31, 30,
                                        31, 31, 30, 31, 30, 31 };

//------------------------------------------------------------------------------
static sal_Bool implIsLeapYear(sal_Int32 _nYear)
{
    return  (   (   ((_nYear % 4) == 0)
                &&  ((_nYear % 100) != 0)
                )
            )
            ||  ((_nYear % 400) == 0)
            ;
}

//------------------------------------------------------------------------------
static sal_Int32 implDaysInMonth(sal_Int32 _nMonth, sal_Int32 _nYear)
{
    OSL_ENSURE(_nMonth > 0 && _nMonth < 13,"Month as invalid value!");
    if (_nMonth != 2)
        return aDaysInMonth[_nMonth-1];
    else
    {
        if (implIsLeapYear(_nYear))
            return aDaysInMonth[_nMonth-1] + 1;
        else
            return aDaysInMonth[_nMonth-1];
    }
}

//------------------------------------------------------------------------------
static sal_Int32 implRelativeToAbsoluteNull(const Date& _rDate)
{
    sal_Int32 nDays = 0;

    // ripped this code from the implementation of tools::Date
    sal_Int32 nNormalizedYear = _rDate.Year - 1;
    nDays = nNormalizedYear * 365;
    // leap years
    nDays += (nNormalizedYear / 4) - (nNormalizedYear / 100) + (nNormalizedYear / 400);

    for (sal_Int32 i = 1; i < _rDate.Month; ++i)
        nDays += implDaysInMonth(i, _rDate.Year);

    nDays += _rDate.Day;
    return nDays;
}
//------------------------------------------------------------------------------
static void implBuildFromRelative( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear)
{
    sal_Int32   nTempDays;
    sal_Int32   i = 0;
    sal_Bool    bCalc;

    do
    {
        nTempDays = nDays;
        rYear = ((nTempDays / 365) - i);
        nTempDays -= (rYear-1) * 365;
        nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400);
        bCalc = sal_False;
        if ( nTempDays < 1 )
        {
            i++;
            bCalc = sal_True;
        }
        else
        {
            if ( nTempDays > 365 )
            {
                if ( (nTempDays != 366) || !implIsLeapYear( rYear ) )
                {
                    i--;
                    bCalc = sal_True;
                }
            }
        }
    }
    while ( bCalc );

    rMonth = 1;
    while ( nTempDays > implDaysInMonth( rMonth, rYear ) )
    {
        nTempDays -= implDaysInMonth( rMonth, rYear );
        rMonth++;
    }
    rDay = nTempDays;
}
//------------------------------------------------------------------------------
sal_Int32 DBTypeConversion::toDays(const Date& _rVal, const Date& _rNullDate)
{
    return implRelativeToAbsoluteNull(_rVal) - implRelativeToAbsoluteNull(_rNullDate);
}

//------------------------------------------------------------------------------
double DBTypeConversion::toDouble(const Date& rVal, const Date& _rNullDate)
{
    return (double)toDays(rVal, _rNullDate);
}

//------------------------------------------------------------------------------
double DBTypeConversion::toDouble(const Time& rVal)
{
    return (double)getMsFromTime(rVal) / fMilliSecondsPerDay;
}

//------------------------------------------------------------------------------
double DBTypeConversion::toDouble(const DateTime& _rVal, const Date& _rNullDate)
{
    sal_Int64   nTime     = toDays(Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate);
    sal_Int16   nSign     = (nTime >= 0) ? +1 : -1;
    sal_Int32   nHour     = _rVal.Hours;
    sal_Int32   nMin      = _rVal.Minutes;
    sal_Int32   nSec      = _rVal.Seconds;
    sal_Int32   n100Sec   = _rVal.HundredthSeconds;

    double nVal = (((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10))*nSign);

    return ((double)nTime) + (nVal * (1/fMilliSecondsPerDay));
}
// -------------------------------------------------------------------------
static void addDays(sal_Int32 nDays, Date& _rDate)
{
    sal_Int32   nTempDays = implRelativeToAbsoluteNull( _rDate );

    nTempDays += nDays;
    if ( nTempDays > MAX_DAYS )
    {
        _rDate.Day      = 31;
        _rDate.Month    = 12;
        _rDate.Year     = 9999;
    }
    else if ( nTempDays <= 0 )
    {
        _rDate.Day      = 1;
        _rDate.Month    = 1;
        _rDate.Year     = 00;
    }
    else
        implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
}
// -----------------------------------------------------------------------
static void subDays( sal_Int32 nDays, Date& _rDate )
{
    sal_Int32   nTempDays = implRelativeToAbsoluteNull( _rDate );

    nTempDays -= nDays;
    if ( nTempDays > MAX_DAYS )
    {
        _rDate.Day      = 31;
        _rDate.Month    = 12;
        _rDate.Year     = 9999;
    }
    else if ( nTempDays <= 0 )
    {
        _rDate.Day      = 1;
        _rDate.Month    = 1;
        _rDate.Year     = 00;
    }
    else
        implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
}
// -------------------------------------------------------------------------
Date DBTypeConversion::toDate(double dVal, const Date& _rNullDate)
{
    Date aRet = _rNullDate;

    if (dVal >= 0)
        addDays((sal_Int32)dVal,aRet);
    else
        subDays((sal_uInt32)(-dVal),aRet);
        //  x -= (sal_uInt32)(-nDays);

    return aRet;
}
// -------------------------------------------------------------------------
Time DBTypeConversion::toTime(double dVal)
{
    sal_Int32 nDays     = (sal_Int32)dVal;
    sal_Int32 nMS = sal_Int32((dVal - (double)nDays) * fMilliSecondsPerDay + 0.5);

    sal_uInt16 nSign;
    if ( nMS < 0 )
    {
        nMS *= -1;
        nSign = -1;
    }
    else
        nSign = 1;

    Time xRet;
    // Zeit normalisieren
    xRet.HundredthSeconds   = nMS/10;
    xRet.Seconds            = xRet.HundredthSeconds / 100;
    xRet.HundredthSeconds   = xRet.HundredthSeconds % 100;
    xRet.Minutes            = xRet.Seconds / 60;
    xRet.Seconds            = xRet.Seconds % 60;
    xRet.Hours              = xRet.Minutes / 60;
    xRet.Minutes            = xRet.Minutes % 60;

    // Zeit zusammenbauen
    sal_Int32 nTime = (sal_Int32)(xRet.HundredthSeconds + (xRet.Seconds*100) + (xRet.Minutes*10000) + (xRet.Hours*1000000)) * nSign;

    if(nTime < 0)
    {
        xRet.HundredthSeconds   = 99;
        xRet.Minutes            = 59;
        xRet.Seconds            = 59;
        xRet.Hours              = 23;
    }
    return xRet;

}
//------------------------------------------------------------------------------
DateTime DBTypeConversion::toDateTime(double dVal, const Date& _rNullDate)
{
    Date aDate = toDate(dVal, _rNullDate);
    Time aTime = toTime(dVal);

    DateTime xRet;

    xRet.Day                = aDate.Day;
    xRet.Month              = aDate.Month;
    xRet.Year               = aDate.Year;

    xRet.HundredthSeconds   = aTime.HundredthSeconds;
    xRet.Minutes            = aTime.Minutes;
    xRet.Seconds            = aTime.Seconds;
    xRet.Hours              = aTime.Hours;


    return xRet;
}
//------------------------------------------------------------------------------
Date DBTypeConversion::getNULLDate(const Reference< XNumberFormatsSupplier > &xSupplier)
{
    OSL_ENSURE(xSupplier.is(), "getNULLDate : the formatter doesn't implement a supplier !");
    if (xSupplier.is())
    {
        try
        {
            // get the null date
            Date aDate;
            xSupplier->getNumberFormatSettings()->getPropertyValue(::rtl::OUString::createFromAscii("NullDate")) >>= aDate;
            return aDate;
        }
        catch ( ... )
        {
        }
    }

    return STANDARD_DB_DATE;
}
//------------------------------------------------------------------
::rtl::OUString DBTypeConversion::toDateString(const Date& rDate)
{
    sal_Char s[11];
    sprintf(s,"%04d-%02d-%02d",
                (int)rDate.Year,
                (int)rDate.Month,
                (int)rDate.Day);
    s[10] = 0;
    return ::rtl::OUString::createFromAscii(s);
}
//------------------------------------------------------------------
::rtl::OUString DBTypeConversion::toTimeString(const Time& rTime)
{
    sal_Char s[9];
    sprintf(s,"%02d:%02d:%02d",
            (int)rTime.Hours,
            (int)rTime.Minutes,
            (int)rTime.Seconds);
    s[8] = 0;
    return ::rtl::OUString::createFromAscii(s);
}

//------------------------------------------------------------------
::rtl::OUString DBTypeConversion::toDateTimeString(const DateTime& _rDateTime)
{
    Date aDate(_rDateTime.Day,_rDateTime.Month,_rDateTime.Year);
    ::rtl::OUString aTemp(toDateString(aDate));
    aTemp += ::rtl::OUString::createFromAscii(" ");
    Time aTime(0,_rDateTime.Seconds,_rDateTime.Minutes,_rDateTime.Hours);
    aTemp += toTimeString(aTime);
    return  aTemp;
}
// -----------------------------------------------------------------------------
::rtl::OUString DBTypeConversion::toSQLString(sal_Int32 eType, const Any& _rVal, sal_Bool bQuote,
                                              const Reference< XTypeConverter >&  _rxTypeConverter)
{
    ::rtl::OUString aRet;
    if (_rVal.hasValue())
    {
        try
        {
            switch (eType)
            {
                case DataType::INTEGER:
                case DataType::BIT:
                case DataType::TINYINT:
                case DataType::SMALLINT:
                    if (_rVal.getValueType().getTypeClass() == ::com::sun::star::uno::TypeClass_BOOLEAN)
                    {
                        if (::cppu::any2bool(_rVal))
                            aRet = ::rtl::OUString::createFromAscii("1");
                        else
                            aRet = ::rtl::OUString::createFromAscii("0");
                    }
                    else
                        _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aRet;
                    break;
                case DataType::CHAR:
                case DataType::VARCHAR:
                    if (bQuote)
                        aRet += ::rtl::OUString::createFromAscii("'");
                    {
                        ::rtl::OUString aTemp;
                        _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aTemp;
                        aRet += aTemp;
                    }
                    if (bQuote)
                        aRet += ::rtl::OUString::createFromAscii("'");
                    break;
                case DataType::REAL:
                case DataType::DOUBLE:
                case DataType::DECIMAL:
                case DataType::NUMERIC:
                    _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aRet;
                    break;
                case DataType::TIMESTAMP:
                {
                    DateTime aDateTime;

                    // check if this is really a timestamp or only a date
                    if ((_rVal >>= aDateTime) &&
                        (aDateTime.Hours || aDateTime.Minutes || aDateTime.Seconds || aDateTime.HundredthSeconds))
                    {
                        if (bQuote) aRet += ::rtl::OUString::createFromAscii("{TS '");
                        aRet += DBTypeConversion::toDateTimeString(aDateTime);
                        if (bQuote) aRet += ::rtl::OUString::createFromAscii("'}");
                        break;
                    }
                    // else continue
                }
                case DataType::DATE:
                {
                    Date aDate;
                    sal_Bool bRet = _rVal >>= aDate;
                    OSL_ENSURE(bRet,"DBTypeConversion::toSQLString: _rVal is not date!");
                    if (bQuote) aRet += ::rtl::OUString::createFromAscii("{D '");
                    aRet += DBTypeConversion::toDateString(aDate);;
                    if (bQuote) aRet += ::rtl::OUString::createFromAscii("'}");
                }   break;
                case DataType::TIME:
                {
                    Time aTime;
                    sal_Bool bRet = _rVal >>= aTime;
                    OSL_ENSURE(bRet,"DBTypeConversion::toSQLString: _rVal is not time!");
                    if (bQuote) aRet += ::rtl::OUString::createFromAscii("{T '");
                    aRet += DBTypeConversion::toTimeString(aTime);
                    if (bQuote) aRet += ::rtl::OUString::createFromAscii("'}");
                } break;
                default:
                    _rxTypeConverter->convertToSimpleType(_rVal, TypeClass_STRING) >>= aRet;
            }
        }
        catch ( ... )
        {
            OSL_ENSURE(0,"TypeConversion Error");
        }
    }
    else
        aRet = ::rtl::OUString::createFromAscii(" NULL ");
    return aRet;
}
// -----------------------------------------------------------------------------


//.........................................................................
}   // namespace dbtools
//.........................................................................


/*************************************************************************
 * history:
 *  $Log: not supported by cvs2svn $
 *  Revision 1.13  2001/03/27 12:19:34  jl
 *  calls to sal_setInt64 hhave been removed
 *
 *  Revision 1.12  2001/03/21 13:37:07  jl
 *  OSL_ENSHURE replaced by OSL_ENSURE
 *
 *  Revision 1.11  2001/03/15 08:45:56  fs
 *  cppuhelper/extract -> comphelper/extract
 *
 *  Revision 1.10  2001/01/03 09:02:13  oj
 *  check month >0 and < 13
 *
 *  Revision 1.9  2000/12/06 12:14:59  oj
 *  #80219# toDays corrected
 *
 *  Revision 1.8  2000/11/30 15:29:40  oj
 *  #80934# standarddate is no longer public
 *
 *  Revision 1.7  2000/11/09 08:46:09  oj
 *  some new methods for db's
 *
 *  Revision 1.6  2000/11/08 09:28:45  oj
 *  forget assignment of return value
 *
 *  Revision 1.5  2000/10/27 07:04:22  fs
 *  corrected the starutil namespace
 *
 *  Revision 1.4  2000/10/27 07:01:07  fs
 *  new: toDate(sal_Int32) / toTime(sal_Int32)
 *
 *  Revision 1.3  2000/10/24 15:00:32  oj
 *  make strings unique for lib's
 *
 *  Revision 1.2  2000/10/19 11:46:15  oj
 *  remove tools from dbtools
 *
 *  Revision 1.1  2000/10/05 08:50:32  fs
 *  moved the files from unotools to here
 *
 *
 *  Revision 1.0 29.09.00 08:17:18  fs
 ************************************************************************/