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

Generated by: LCOV version 1.10