LCOV - code coverage report
Current view: top level - connectivity/source/commontools - dbconversion.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 3 204 1.5 %
Date: 2014-04-11 Functions: 1 24 4.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <connectivity/dbconversion.hxx>
      21             : #include <connectivity/dbcharset.hxx>
      22             : #include <osl/diagnose.h>
      23             : #include <stdio.h>
      24             : #include <com/sun/star/sdbc/SQLException.hpp>
      25             : #include <com/sun/star/util/Date.hpp>
      26             : #include <com/sun/star/util/Time.hpp>
      27             : #include <com/sun/star/util/DateTime.hpp>
      28             : #include <rtl/ustrbuf.hxx>
      29             : #include <rtl/math.hxx>
      30             : #include <unotools/datetime.hxx>
      31             : #include <sstream>
      32             : #include <iomanip>
      33             : 
      34             : #define MAX_DAYS    3636532
      35             : 
      36             : namespace
      37             : {
      38             :     const sal_Int64 nanoSecInSec = 1000000000;
      39             :     const sal_Int16 secInMin  = 60;
      40             :     const sal_Int16 minInHour = 60;
      41             : 
      42             :     const sal_Int64 secMask  = 1000000000;
      43             :     const sal_Int64 minMask  = 100000000000LL;
      44             :     const sal_Int64 hourMask = 10000000000000LL;
      45             : 
      46             :     const double fNanoSecondsPerDay = nanoSecInSec * secInMin * minInHour * 24.0;
      47             : }
      48             : 
      49             : 
      50             : namespace dbtools
      51             : {
      52             : 
      53             : 
      54             :     using namespace ::com::sun::star::uno;
      55             :     using namespace ::com::sun::star::util;
      56             :     namespace utl = ::com::sun::star::util;
      57             :     using namespace ::com::sun::star::sdb;
      58             :     using namespace ::com::sun::star::sdbc;
      59             :     using namespace ::com::sun::star::lang;
      60             :     using namespace ::com::sun::star::beans;
      61             : 
      62             : 
      63             : 
      64          66 :     ::com::sun::star::util::Date DBTypeConversion::getStandardDate()
      65             :     {
      66          66 :         static ::com::sun::star::util::Date STANDARD_DB_DATE(1,1,1900);
      67          66 :         return STANDARD_DB_DATE;
      68             :     }
      69             : 
      70           0 :     OUString DBTypeConversion::toDateString(const utl::Date& rDate)
      71             :     {
      72             :         sal_Char s[11];
      73             :         snprintf(s,
      74             :                 sizeof(s),
      75             :                 "%04d-%02d-%02d",
      76             :                 (int)rDate.Year,
      77             :                 (int)rDate.Month,
      78           0 :                 (int)rDate.Day);
      79           0 :         s[10] = 0;
      80           0 :         return OUString::createFromAscii(s);
      81             :     }
      82             : 
      83           0 :     OUString DBTypeConversion::toTimeStringS(const utl::Time& rTime)
      84             :     {
      85           0 :         std::ostringstream ostr;
      86             :         using std::setw;
      87           0 :         ostr.fill('0');
      88           0 :         ostr << setw(2) << rTime.Hours   << ":"
      89           0 :              << setw(2) << rTime.Minutes << ":"
      90           0 :              << setw(2) << rTime.Seconds;
      91           0 :         return OUString::createFromAscii(ostr.str().c_str());
      92             :     }
      93             : 
      94           0 :     OUString DBTypeConversion::toTimeString(const utl::Time& rTime)
      95             :     {
      96           0 :         std::ostringstream ostr;
      97             :         using std::setw;
      98           0 :         ostr.fill('0');
      99           0 :         ostr << setw(2) << rTime.Hours   << ":"
     100           0 :              << setw(2) << rTime.Minutes << ":"
     101           0 :              << setw(2) << rTime.Seconds << "."
     102           0 :              << setw(9) << rTime.NanoSeconds;
     103           0 :         return OUString::createFromAscii(ostr.str().c_str());
     104             :     }
     105             : 
     106           0 :     OUString DBTypeConversion::toDateTimeString(const utl::DateTime& _rDateTime)
     107             :     {
     108           0 :         utl::Date aDate(_rDateTime.Day,_rDateTime.Month,_rDateTime.Year);
     109           0 :         OUStringBuffer aTemp(toDateString(aDate));
     110           0 :         aTemp.appendAscii(" ");
     111             :         utl::Time const aTime(_rDateTime.NanoSeconds, _rDateTime.Seconds,
     112           0 :                 _rDateTime.Minutes, _rDateTime.Hours, _rDateTime.IsUTC);
     113           0 :         aTemp.append( toTimeString(aTime) );
     114           0 :         return  aTemp.makeStringAndClear();
     115             :     }
     116             : 
     117           0 :     utl::Date DBTypeConversion::toDate(sal_Int32 _nVal)
     118             :     {
     119           0 :         utl::Date aReturn;
     120           0 :         aReturn.Day = (sal_uInt16)(_nVal % 100);
     121           0 :         aReturn.Month = (sal_uInt16)((_nVal  / 100) % 100);
     122           0 :         aReturn.Year = (sal_uInt16)(_nVal  / 10000);
     123           0 :         return aReturn;
     124             :     }
     125             : 
     126             : 
     127           0 :     utl::Time DBTypeConversion::toTime(sal_Int64 _nVal)
     128             :     {
     129           0 :         utl::Time aReturn;
     130           0 :         sal_uInt64 unVal = static_cast<sal_uInt64>(_nVal >= 0 ? _nVal : -_nVal);
     131           0 :         aReturn.Hours = unVal / hourMask;
     132           0 :         aReturn.Minutes = (unVal / minMask) % 100;
     133           0 :         aReturn.Seconds = (unVal / secMask) % 100;
     134           0 :         aReturn.NanoSeconds = unVal % secMask;
     135           0 :         return aReturn;
     136             :     }
     137             : 
     138           0 :     sal_Int64 DBTypeConversion::getNsFromTime(const utl::Time& rVal)
     139             :     {
     140           0 :         sal_Int32   nHour     = rVal.Hours;
     141           0 :         sal_Int32   nMin      = rVal.Minutes;
     142           0 :         sal_Int32   nSec      = rVal.Seconds;
     143           0 :         sal_Int32   nNanoSec  = rVal.NanoSeconds;
     144             : 
     145           0 :         return nNanoSec +
     146           0 :                nSec  * nanoSecInSec +
     147           0 :                nMin  * (secInMin * nanoSecInSec) +
     148           0 :                nHour * (minInHour * secInMin * nanoSecInSec);
     149             :     }
     150             : 
     151             : 
     152             :     static const sal_Int32 aDaysInMonth[12] = {   31, 28, 31, 30, 31, 30,
     153             :                                             31, 31, 30, 31, 30, 31 };
     154             : 
     155             : 
     156           0 :     static sal_Bool implIsLeapYear(sal_Int32 _nYear)
     157             :     {
     158           0 :         return  (   (   ((_nYear % 4) == 0)
     159           0 :                     &&  ((_nYear % 100) != 0)
     160             :                     )
     161             :                 )
     162           0 :                 ||  ((_nYear % 400) == 0)
     163             :                 ;
     164             :     }
     165             : 
     166             : 
     167           0 :     static sal_Int32 implDaysInMonth(sal_Int32 _nMonth, sal_Int32 _nYear)
     168             :     {
     169             :         OSL_ENSURE(_nMonth > 0 && _nMonth < 13,"Month as invalid value!");
     170           0 :         if (_nMonth != 2)
     171           0 :             return aDaysInMonth[_nMonth-1];
     172             :         else
     173             :         {
     174           0 :             if (implIsLeapYear(_nYear))
     175           0 :                 return aDaysInMonth[_nMonth-1] + 1;
     176             :             else
     177           0 :                 return aDaysInMonth[_nMonth-1];
     178             :         }
     179             :     }
     180             : 
     181             : 
     182           0 :     static sal_Int32 implRelativeToAbsoluteNull(const utl::Date& _rDate)
     183             :     {
     184           0 :         sal_Int32 nDays = 0;
     185             : 
     186             :         // ripped this code from the implementation of tools::Date
     187           0 :         sal_Int32 nNormalizedYear = _rDate.Year - 1;
     188           0 :         nDays = nNormalizedYear * 365;
     189             :         // leap years
     190           0 :         nDays += (nNormalizedYear / 4) - (nNormalizedYear / 100) + (nNormalizedYear / 400);
     191             : 
     192           0 :         for (sal_Int32 i = 1; i < _rDate.Month; ++i)
     193           0 :             nDays += implDaysInMonth(i, _rDate.Year);
     194             : 
     195           0 :         nDays += _rDate.Day;
     196           0 :         return nDays;
     197             :     }
     198             : 
     199           0 :     static void implBuildFromRelative( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_Int16& rYear)
     200             :     {
     201             :         sal_Int32   nTempDays;
     202           0 :         sal_Int32   i = 0;
     203             :         sal_Bool    bCalc;
     204             : 
     205           0 :         do
     206             :         {
     207           0 :             nTempDays = nDays;
     208           0 :             rYear = (sal_uInt16)((nTempDays / 365) - i);
     209           0 :             nTempDays -= (rYear-1) * 365;
     210           0 :             nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400);
     211           0 :             bCalc = sal_False;
     212           0 :             if ( nTempDays < 1 )
     213             :             {
     214           0 :                 i++;
     215           0 :                 bCalc = sal_True;
     216             :             }
     217             :             else
     218             :             {
     219           0 :                 if ( nTempDays > 365 )
     220             :                 {
     221           0 :                     if ( (nTempDays != 366) || !implIsLeapYear( rYear ) )
     222             :                     {
     223           0 :                         i--;
     224           0 :                         bCalc = sal_True;
     225             :                     }
     226             :                 }
     227             :             }
     228             :         }
     229             :         while ( bCalc );
     230             : 
     231           0 :         rMonth = 1;
     232           0 :         while ( nTempDays > implDaysInMonth( rMonth, rYear ) )
     233             :         {
     234           0 :             nTempDays -= implDaysInMonth( rMonth, rYear );
     235           0 :             rMonth++;
     236             :         }
     237           0 :         rDay = (sal_uInt16)nTempDays;
     238           0 :     }
     239             : 
     240           0 :     sal_Int32 DBTypeConversion::toDays(const utl::Date& _rVal, const utl::Date& _rNullDate)
     241             :     {
     242           0 :         return implRelativeToAbsoluteNull(_rVal) - implRelativeToAbsoluteNull(_rNullDate);
     243             :     }
     244             : 
     245             : 
     246           0 :     double DBTypeConversion::toDouble(const utl::Date& rVal, const utl::Date& _rNullDate)
     247             :     {
     248           0 :         return (double)toDays(rVal, _rNullDate);
     249             :     }
     250             : 
     251             : 
     252           0 :     double DBTypeConversion::toDouble(const utl::Time& rVal)
     253             :     {
     254           0 :         return (double)getNsFromTime(rVal) / fNanoSecondsPerDay;
     255             :     }
     256             : 
     257             : 
     258           0 :     double DBTypeConversion::toDouble(const utl::DateTime& _rVal, const utl::Date& _rNullDate)
     259             :     {
     260           0 :         sal_Int64   nTime     = toDays(utl::Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate);
     261           0 :         utl::Time aTimePart;
     262             : 
     263           0 :         aTimePart.Hours             = _rVal.Hours;
     264           0 :         aTimePart.Minutes           = _rVal.Minutes;
     265           0 :         aTimePart.Seconds           = _rVal.Seconds;
     266           0 :         aTimePart.NanoSeconds       = _rVal.NanoSeconds;
     267             : 
     268           0 :         return ((double)nTime) + toDouble(aTimePart);
     269             :     }
     270             : 
     271           0 :     static void addDays(sal_Int32 nDays, utl::Date& _rDate)
     272             :     {
     273           0 :         sal_Int32   nTempDays = implRelativeToAbsoluteNull( _rDate );
     274             : 
     275           0 :         nTempDays += nDays;
     276           0 :         if ( nTempDays > MAX_DAYS )
     277             :         {
     278           0 :             _rDate.Day      = 31;
     279           0 :             _rDate.Month    = 12;
     280           0 :             _rDate.Year     = 9999;
     281             :         }
     282           0 :         else if ( nTempDays <= 0 )
     283             :         {
     284           0 :             _rDate.Day      = 1;
     285           0 :             _rDate.Month    = 1;
     286           0 :             _rDate.Year     = 00;
     287             :         }
     288             :         else
     289           0 :             implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
     290           0 :     }
     291             : 
     292           0 :     static void subDays( sal_Int32 nDays, utl::Date& _rDate )
     293             :     {
     294           0 :         sal_Int32   nTempDays = implRelativeToAbsoluteNull( _rDate );
     295             : 
     296           0 :         nTempDays -= nDays;
     297           0 :         if ( nTempDays > MAX_DAYS )
     298             :         {
     299           0 :             _rDate.Day      = 31;
     300           0 :             _rDate.Month    = 12;
     301           0 :             _rDate.Year     = 9999;
     302             :         }
     303           0 :         else if ( nTempDays <= 0 )
     304             :         {
     305           0 :             _rDate.Day      = 1;
     306           0 :             _rDate.Month    = 1;
     307           0 :             _rDate.Year     = 00;
     308             :         }
     309             :         else
     310           0 :             implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
     311           0 :     }
     312             : 
     313           0 :     utl::Date DBTypeConversion::toDate(double dVal, const utl::Date& _rNullDate)
     314             :     {
     315           0 :         utl::Date aRet = _rNullDate;
     316             : 
     317           0 :         if (dVal >= 0)
     318           0 :             addDays((sal_Int32)dVal,aRet);
     319             :         else
     320           0 :             subDays((sal_uInt32)(-dVal),aRet);
     321             :             //  x -= (sal_uInt32)(-nDays);
     322             : 
     323           0 :         return aRet;
     324             :     }
     325             : 
     326           0 :     utl::Time DBTypeConversion::toTime(double dVal, short nDigits)
     327             :     {
     328           0 :         sal_Int32 nDays     = (sal_Int32)dVal;
     329             :         sal_Int64 nNS;
     330             :         {
     331           0 :             double fSeconds((dVal - (double)nDays) * (fNanoSecondsPerDay / nanoSecInSec));
     332           0 :             fSeconds = ::rtl::math::round( fSeconds, nDigits );
     333           0 :             nNS = fSeconds * nanoSecInSec;
     334             :         }
     335             : 
     336             :         sal_Int16 nSign;
     337           0 :         if ( nNS < 0 )
     338             :         {
     339           0 :             nNS *= -1;
     340           0 :             nSign = -1;
     341             :         }
     342             :         else
     343           0 :             nSign = 1;
     344             : 
     345           0 :         utl::Time xRet;
     346             :         // normalize time
     347             :         // we have to sal_Int32 here because otherwise we get an overflow
     348           0 :         sal_Int64 nNanoSeconds      = nNS;
     349           0 :         sal_Int32 nSeconds          = nNanoSeconds / nanoSecInSec;
     350           0 :         sal_Int32 nMinutes          = nSeconds / secInMin;
     351             : 
     352           0 :         xRet.NanoSeconds            = nNanoSeconds % nanoSecInSec;
     353           0 :         xRet.Seconds                = nSeconds % secInMin;
     354           0 :         xRet.Hours                  = nMinutes / minInHour;
     355           0 :         xRet.Minutes                = nMinutes % minInHour;
     356             : 
     357             :         // assemble time
     358           0 :         sal_Int64 nTime = nSign *
     359           0 :                           (xRet.NanoSeconds +
     360           0 :                            xRet.Seconds * secMask +
     361           0 :                            xRet.Minutes * minMask +
     362           0 :                            xRet.Hours   * hourMask);
     363             : 
     364           0 :         if(nTime < 0)
     365             :         {
     366           0 :             xRet.NanoSeconds  = nanoSecInSec-1;
     367           0 :             xRet.Seconds      = secInMin-1;
     368           0 :             xRet.Minutes      = minInHour-1;
     369           0 :             xRet.Hours        = 23;
     370             :         }
     371           0 :         return xRet;
     372             :     }
     373             : 
     374           0 :     utl::DateTime DBTypeConversion::toDateTime(double dVal, const utl::Date& _rNullDate)
     375             :     {
     376           0 :         utl::Date aDate = toDate(dVal, _rNullDate);
     377             :         // there is not enough precision in a double to have both a date
     378             :         // and a time up to nanoseconds -> limit to microseconds to have
     379             :         // correct rounding, that is e.g. 13:00:00.000000000 instead of
     380             :         // 12:59:59.999999790
     381           0 :         utl::Time aTime = toTime(dVal, 6);
     382             : 
     383           0 :         utl::DateTime xRet;
     384             : 
     385           0 :         xRet.Day          = aDate.Day;
     386           0 :         xRet.Month        = aDate.Month;
     387           0 :         xRet.Year         = aDate.Year;
     388             : 
     389           0 :         xRet.NanoSeconds  = aTime.NanoSeconds;
     390           0 :         xRet.Minutes      = aTime.Minutes;
     391           0 :         xRet.Seconds      = aTime.Seconds;
     392           0 :         xRet.Hours        = aTime.Hours;
     393             : 
     394             : 
     395           0 :         return xRet;
     396             :     }
     397             : 
     398           0 :     utl::Date DBTypeConversion::toDate(const OUString& _sSQLString)
     399             :     {
     400             :         // get the token out of a string
     401             :         static sal_Unicode sDateSep = '-';
     402             : 
     403           0 :         sal_Int32 nIndex    = 0;
     404           0 :         sal_uInt16  nYear   = 0,
     405           0 :                     nMonth  = 0,
     406           0 :                     nDay    = 0;
     407           0 :         nYear   = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
     408           0 :         if(nIndex != -1)
     409             :         {
     410           0 :             nMonth = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
     411           0 :             if(nIndex != -1)
     412           0 :                 nDay = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
     413             :         }
     414             : 
     415           0 :         return utl::Date(nDay,nMonth,nYear);
     416             :     }
     417             : 
     418             : 
     419           0 :     utl::DateTime DBTypeConversion::toDateTime(const OUString& _sSQLString)
     420             :     {
     421             :         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Timestamp.html#valueOf(java.lang.String)
     422             :         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html#valueOf(java.lang.String)
     423             :         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Time.html#valueOf(java.lang.String)
     424             : 
     425             :         // the date part
     426           0 :         utl::Date aDate = toDate(_sSQLString);
     427           0 :         utl::Time aTime;
     428           0 :         sal_Int32 nSeparation = _sSQLString.indexOf( ' ' );
     429           0 :         if ( -1 != nSeparation )
     430             :         {
     431           0 :             const sal_Unicode *p = _sSQLString.getStr() + nSeparation;
     432           0 :             const sal_Unicode *const begin = p;
     433           0 :             while (isspace(*p)) { ++p; }
     434           0 :             nSeparation += p - begin;
     435           0 :             aTime = toTime( _sSQLString.copy( nSeparation ) );
     436             :         }
     437             : 
     438             :         return utl::DateTime(aTime.NanoSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours,
     439           0 :                         aDate.Day, aDate.Month, aDate.Year, false);
     440             :     }
     441             : 
     442             : 
     443           0 :     utl::Time DBTypeConversion::toTime(const OUString& _sSQLString)
     444             :     {
     445           0 :         utl::Time aTime;
     446           0 :         ::utl::ISO8601parseTime(_sSQLString, aTime);
     447           0 :         return aTime;
     448             :     }
     449             : 
     450             : 
     451             : }   // namespace dbtools
     452             : 
     453             : 
     454             : 
     455             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10