LCOV - code coverage report
Current view: top level - libreoffice/connectivity/source/commontools - dbconversion.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 205 0.0 %
Date: 2012-12-27 Functions: 0 25 0.0 %
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             : #ifndef _INC_STDIO
      24             : #include <stdio.h>
      25             : #endif
      26             : #include <com/sun/star/sdbc/SQLException.hpp>
      27             : #include <com/sun/star/util/Date.hpp>
      28             : #include <com/sun/star/util/Time.hpp>
      29             : #include <com/sun/star/util/DateTime.hpp>
      30             : #include <rtl/ustrbuf.hxx>
      31             : 
      32             : #define MAX_DAYS    3636532
      33             : 
      34             : //.........................................................................
      35             : namespace dbtools
      36             : {
      37             : //.........................................................................
      38             : 
      39             : 
      40             :     using namespace ::comphelper;
      41             :     using namespace ::com::sun::star::uno;
      42             :     using namespace ::com::sun::star::util;
      43             :     using namespace ::com::sun::star::sdb;
      44             :     using namespace ::com::sun::star::sdbc;
      45             :     using namespace ::com::sun::star::lang;
      46             :     using namespace ::com::sun::star::beans;
      47             : 
      48             : 
      49             :     //------------------------------------------------------------------------------
      50           0 :     ::com::sun::star::util::Date DBTypeConversion::getStandardDate()
      51             :     {
      52           0 :         static ::com::sun::star::util::Date STANDARD_DB_DATE(1,1,1900);
      53           0 :         return STANDARD_DB_DATE;
      54             :     }
      55             :     //------------------------------------------------------------------------------
      56           0 :     ::rtl::OUString DBTypeConversion::toDateString(const Date& rDate)
      57             :     {
      58             :         sal_Char s[11];
      59             :         snprintf(s,
      60             :                 sizeof(s),
      61             :                 "%04d-%02d-%02d",
      62             :                 (int)rDate.Year,
      63             :                 (int)rDate.Month,
      64           0 :                 (int)rDate.Day);
      65           0 :         s[10] = 0;
      66           0 :         return ::rtl::OUString::createFromAscii(s);
      67             :     }
      68             :     //------------------------------------------------------------------
      69           0 :     ::rtl::OUString DBTypeConversion::toTimeString(const Time& rTime)
      70             :     {
      71             :         sal_Char s[9];
      72             :         snprintf(s,
      73             :                 sizeof(s),
      74             :                 "%02d:%02d:%02d",
      75             :                 (int)rTime.Hours,
      76             :                 (int)rTime.Minutes,
      77           0 :                 (int)rTime.Seconds);
      78           0 :         s[8] = 0;
      79           0 :         return ::rtl::OUString::createFromAscii(s);
      80             :     }
      81             : 
      82             :     //------------------------------------------------------------------
      83           0 :     ::rtl::OUString DBTypeConversion::toDateTimeString(const DateTime& _rDateTime)
      84             :     {
      85           0 :         Date aDate(_rDateTime.Day,_rDateTime.Month,_rDateTime.Year);
      86           0 :         ::rtl::OUStringBuffer aTemp(toDateString(aDate));
      87           0 :         aTemp.appendAscii(" ");
      88           0 :         Time aTime(0,_rDateTime.Seconds,_rDateTime.Minutes,_rDateTime.Hours);
      89           0 :         aTemp.append( toTimeString(aTime) );
      90           0 :         aTemp.appendAscii(".");
      91           0 :         aTemp.append( static_cast<sal_Int32>(_rDateTime.HundredthSeconds));
      92           0 :         return  aTemp.makeStringAndClear();
      93             :     }
      94             :     //------------------------------------------------------------------------------
      95           0 :     Date DBTypeConversion::toDate(sal_Int32 _nVal)
      96             :     {
      97           0 :         Date aReturn;
      98           0 :         aReturn.Day = (sal_uInt16)(_nVal % 100);
      99           0 :         aReturn.Month = (sal_uInt16)((_nVal  / 100) % 100);
     100           0 :         aReturn.Year = (sal_uInt16)(_nVal  / 10000);
     101           0 :         return aReturn;
     102             :     }
     103             : 
     104             :     //------------------------------------------------------------------------------
     105           0 :     Time DBTypeConversion::toTime(sal_Int32 _nVal)
     106             :     {
     107           0 :         Time aReturn;
     108           0 :         aReturn.Hours = (sal_uInt16)(((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 1000000);
     109           0 :         aReturn.Minutes = (sal_uInt16)((((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 10000) % 100);
     110           0 :         aReturn.Seconds = (sal_uInt16)((((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) / 100) % 100);
     111           0 :         aReturn.HundredthSeconds = (sal_uInt16)(((sal_uInt32)(_nVal >= 0 ? _nVal : _nVal*-1)) % 100);
     112           0 :         return aReturn;
     113             :     }
     114             : 
     115             :     const double fMilliSecondsPerDay = 86400000.0;
     116             :     //------------------------------------------------------------------------------
     117           0 :     sal_Int32 DBTypeConversion::toINT32(const Date& rVal)
     118             :     {
     119             :         return ((sal_Int32)(rVal.Day%100)) +
     120             :             (((sal_Int32)(rVal.Month%100))*100) +
     121           0 :             (((sal_Int32) rVal.Year%10000)*10000);
     122             :     }
     123             : 
     124             :     //------------------------------------------------------------------------------
     125           0 :     sal_Int32 DBTypeConversion::toINT32(const Time& rVal)
     126             :     {
     127             :         // normalize time
     128           0 :         sal_Int32 nSeconds          = rVal.Seconds + rVal.HundredthSeconds / 100;
     129           0 :         sal_Int32 nHundredthSeconds = rVal.HundredthSeconds % 100;
     130           0 :         sal_Int32 nMinutes          = rVal.Minutes + nSeconds / 60;
     131           0 :         nSeconds                    = nSeconds % 60;
     132           0 :         sal_Int32 nHours            = rVal.Hours + nMinutes / 60;
     133           0 :         nMinutes                    = nMinutes % 60;
     134             : 
     135             :         // assemble time
     136           0 :         return (sal_Int32)(nHundredthSeconds + (nSeconds*100) + (nMinutes*10000) + (nHours*1000000));
     137             :     }
     138             : 
     139             :     //------------------------------------------------------------------------------
     140           0 :     sal_Int32 DBTypeConversion::getMsFromTime(const Time& rVal)
     141             :     {
     142           0 :         sal_Int32   nHour     = rVal.Hours;
     143           0 :         sal_Int32   nMin      = rVal.Minutes;
     144           0 :         sal_Int32   nSec      = rVal.Seconds;
     145           0 :         sal_Int32   n100Sec   = rVal.HundredthSeconds;
     146             : 
     147           0 :         return ((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10));
     148             :     }
     149             : 
     150             :     //------------------------------------------------------------------------------
     151             :     static sal_Int32 aDaysInMonth[12] = {   31, 28, 31, 30, 31, 30,
     152             :                                             31, 31, 30, 31, 30, 31 };
     153             : 
     154             :     //------------------------------------------------------------------------------
     155           0 :     static sal_Bool implIsLeapYear(sal_Int32 _nYear)
     156             :     {
     157             :         return  (   (   ((_nYear % 4) == 0)
     158             :                     &&  ((_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 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_uInt16& rYear)
     199             :     {
     200             :         sal_Int32   nTempDays;
     201           0 :         sal_Int32   i = 0;
     202             :         sal_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 = sal_False;
     211           0 :             if ( nTempDays < 1 )
     212             :             {
     213           0 :                 i++;
     214           0 :                 bCalc = sal_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 = sal_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 Date& _rVal, const Date& _rNullDate)
     240             :     {
     241           0 :         return implRelativeToAbsoluteNull(_rVal) - implRelativeToAbsoluteNull(_rNullDate);
     242             :     }
     243             : 
     244             :     //------------------------------------------------------------------------------
     245           0 :     double DBTypeConversion::toDouble(const Date& rVal, const Date& _rNullDate)
     246             :     {
     247           0 :         return (double)toDays(rVal, _rNullDate);
     248             :     }
     249             : 
     250             :     //------------------------------------------------------------------------------
     251           0 :     double DBTypeConversion::toDouble(const Time& rVal)
     252             :     {
     253           0 :         return (double)getMsFromTime(rVal) / fMilliSecondsPerDay;
     254             :     }
     255             : 
     256             :     //------------------------------------------------------------------------------
     257           0 :     double DBTypeConversion::toDouble(const DateTime& _rVal, const Date& _rNullDate)
     258             :     {
     259           0 :         sal_Int64   nTime     = toDays(Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate);
     260           0 :         Time aTimePart;
     261             : 
     262           0 :         aTimePart.Hours             = _rVal.Hours;
     263           0 :         aTimePart.Minutes           = _rVal.Minutes;
     264           0 :         aTimePart.Seconds           = _rVal.Seconds;
     265           0 :         aTimePart.HundredthSeconds  = _rVal.HundredthSeconds;
     266             : 
     267           0 :         return ((double)nTime) + toDouble(aTimePart);
     268             :     }
     269             :     // -------------------------------------------------------------------------
     270           0 :     static void addDays(sal_Int32 nDays, 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, 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 :     Date DBTypeConversion::toDate(double dVal, const Date& _rNullDate)
     313             :     {
     314           0 :         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 :     Time DBTypeConversion::toTime(double dVal)
     326             :     {
     327           0 :         sal_Int32 nDays     = (sal_Int32)dVal;
     328           0 :         sal_Int32 nMS = sal_Int32((dVal - (double)nDays) * fMilliSecondsPerDay + 0.5);
     329             : 
     330             :         sal_Int16 nSign;
     331           0 :         if ( nMS < 0 )
     332             :         {
     333           0 :             nMS *= -1;
     334           0 :             nSign = -1;
     335             :         }
     336             :         else
     337           0 :             nSign = 1;
     338             : 
     339           0 :         Time xRet;
     340             :         // normalize time
     341             :         // we have to sal_Int32 here because otherwise we get an overflow
     342           0 :         sal_Int32 nHundredthSeconds = nMS/10;
     343           0 :         sal_Int32 nSeconds          = nHundredthSeconds / 100;
     344           0 :         sal_Int32 nMinutes          = nSeconds / 60;
     345             : 
     346           0 :         xRet.HundredthSeconds       = (sal_uInt16)(nHundredthSeconds % 100);
     347           0 :         xRet.Seconds                = (sal_uInt16)(nSeconds % 60);
     348           0 :         xRet.Hours                  = (sal_uInt16)(nMinutes / 60);
     349           0 :         xRet.Minutes                = (sal_uInt16)(nMinutes % 60);
     350             : 
     351             :         // assemble time
     352           0 :         sal_Int32 nTime = (sal_Int32)(xRet.HundredthSeconds + (xRet.Seconds*100) + (xRet.Minutes*10000) + (xRet.Hours*1000000)) * nSign;
     353             : 
     354           0 :         if(nTime < 0)
     355             :         {
     356           0 :             xRet.HundredthSeconds   = 99;
     357           0 :             xRet.Minutes            = 59;
     358           0 :             xRet.Seconds            = 59;
     359           0 :             xRet.Hours              = 23;
     360             :         }
     361           0 :         return xRet;
     362             :     }
     363             :     //------------------------------------------------------------------------------
     364           0 :     DateTime DBTypeConversion::toDateTime(double dVal, const Date& _rNullDate)
     365             :     {
     366           0 :         Date aDate = toDate(dVal, _rNullDate);
     367           0 :         Time aTime = toTime(dVal);
     368             : 
     369           0 :         DateTime xRet;
     370             : 
     371           0 :         xRet.Day                = aDate.Day;
     372           0 :         xRet.Month              = aDate.Month;
     373           0 :         xRet.Year               = aDate.Year;
     374             : 
     375           0 :         xRet.HundredthSeconds   = aTime.HundredthSeconds;
     376           0 :         xRet.Minutes            = aTime.Minutes;
     377           0 :         xRet.Seconds            = aTime.Seconds;
     378           0 :         xRet.Hours              = aTime.Hours;
     379             : 
     380             : 
     381           0 :         return xRet;
     382             :     }
     383             :     //------------------------------------------------------------------------------
     384           0 :     Date DBTypeConversion::toDate(const ::rtl::OUString& _sSQLString)
     385             :     {
     386             :         // get the token out of a string
     387             :         static sal_Unicode sDateSep = '-';
     388             : 
     389           0 :         sal_Int32 nIndex    = 0;
     390           0 :         sal_uInt16  nYear   = 0,
     391           0 :                     nMonth  = 0,
     392           0 :                     nDay    = 0;
     393           0 :         nYear   = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
     394           0 :         if(nIndex != -1)
     395             :         {
     396           0 :             nMonth = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
     397           0 :             if(nIndex != -1)
     398           0 :                 nDay = (sal_uInt16)_sSQLString.getToken(0,sDateSep,nIndex).toInt32();
     399             :         }
     400             : 
     401           0 :         return Date(nDay,nMonth,nYear);
     402             :     }
     403             : 
     404             :     //-----------------------------------------------------------------------------
     405           0 :     DateTime DBTypeConversion::toDateTime(const ::rtl::OUString& _sSQLString)
     406             :     {
     407             :         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Timestamp.html#valueOf(java.lang.String)
     408             :         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html#valueOf(java.lang.String)
     409             :         //@see http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Time.html#valueOf(java.lang.String)
     410             : 
     411             :         // the date part
     412           0 :         Date aDate = toDate(_sSQLString);
     413           0 :         Time aTime;
     414           0 :         sal_Int32 nSeparation = _sSQLString.indexOf( ' ' );
     415           0 :         if ( -1 != nSeparation )
     416           0 :             aTime = toTime( _sSQLString.copy( nSeparation ) );
     417             : 
     418           0 :         return DateTime(aTime.HundredthSeconds,aTime.Seconds,aTime.Minutes,aTime.Hours,aDate.Day,aDate.Month,aDate.Year);
     419             :     }
     420             : 
     421             :     //-----------------------------------------------------------------------------
     422           0 :     Time DBTypeConversion::toTime(const ::rtl::OUString& _sSQLString)
     423             :     {
     424             :         static sal_Unicode sTimeSep = ':';
     425             : 
     426           0 :         sal_Int32 nIndex    = 0;
     427           0 :         sal_uInt16  nHour   = 0,
     428           0 :                     nMinute = 0,
     429           0 :                     nSecond = 0,
     430           0 :                     nHundredthSeconds   = 0;
     431           0 :         nHour   = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32();
     432           0 :         if(nIndex != -1)
     433             :         {
     434           0 :             nMinute = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32();
     435           0 :             if(nIndex != -1)
     436             :             {
     437           0 :                 nSecond = (sal_uInt16)_sSQLString.getToken(0,sTimeSep,nIndex).toInt32();
     438           0 :                 nIndex = 0;
     439           0 :                 ::rtl::OUString sNano(_sSQLString.getToken(1,'.',nIndex));
     440           0 :                 if ( !sNano.isEmpty() )
     441             :                 {
     442             :                     // our time struct only supports hundredth seconds
     443           0 :                     sNano = sNano.copy(0,::std::min<sal_Int32>(sNano.getLength(),2));
     444           0 :                     const static ::rtl::OUString s_Zeros(RTL_CONSTASCII_USTRINGPARAM("00"));
     445           0 :                     sNano += s_Zeros.copy(0,s_Zeros.getLength() - sNano.getLength());
     446           0 :                     nHundredthSeconds = static_cast<sal_uInt16>(sNano.toInt32());
     447           0 :                 }
     448             :             }
     449             :         }
     450           0 :         return Time(nHundredthSeconds,nSecond,nMinute,nHour);
     451             :     }
     452             : 
     453             : //.........................................................................
     454             : }   // namespace dbtools
     455             : //.........................................................................
     456             : 
     457             : 
     458             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10