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

Generated by: LCOV version 1.10