2000-10-05 07:50:51 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
|
|
|
* $RCSfile: dbconversion.cxx,v $
|
|
|
|
*
|
2000-10-19 10:47:14 +00:00
|
|
|
* $Revision: 1.2 $
|
2000-10-05 07:50:51 +00:00
|
|
|
*
|
2000-10-19 10:47:14 +00:00
|
|
|
* last change: $Author: oj $ $Date: 2000-10-19 11:46:15 $
|
2000-10-05 07:50:51 +00:00
|
|
|
*
|
|
|
|
* The Contents of this file are made available subject to the terms of
|
|
|
|
* either of the following licenses
|
|
|
|
*
|
|
|
|
* - GNU Lesser General Public License Version 2.1
|
|
|
|
* - Sun Industry Standards Source License Version 1.1
|
|
|
|
*
|
|
|
|
* Sun Microsystems Inc., October, 2000
|
|
|
|
*
|
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2000 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Sun Industry Standards Source License Version 1.1
|
|
|
|
* =================================================
|
|
|
|
* The contents of this file are subject to the Sun Industry Standards
|
|
|
|
* Source License Version 1.1 (the "License"); You may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of the
|
|
|
|
* License at http://www.openoffice.org/license.html.
|
|
|
|
*
|
|
|
|
* Software provided under this License is provided on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,
|
|
|
|
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
|
|
|
|
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
|
|
|
|
* See the License for the specific provisions governing your rights and
|
|
|
|
* obligations concerning the Software.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is: Sun Microsystems, Inc..
|
|
|
|
*
|
|
|
|
* Copyright: 2000 by Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s): _______________________________________
|
|
|
|
*
|
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#ifndef _DBHELPER_DBCONVERSION_HXX_
|
|
|
|
#include <connectivity/dbconversion.hxx>
|
|
|
|
#endif
|
|
|
|
#ifndef _CONNECTIVITY_DBTOOLS_HXX_
|
|
|
|
#include <connectivity/dbtools.hxx>
|
|
|
|
#endif
|
|
|
|
#ifndef _COMPHELPER_NUMBERS_HXX_
|
|
|
|
#include <comphelper/numbers.hxx>
|
|
|
|
#endif
|
2000-10-19 10:47:14 +00:00
|
|
|
#ifndef _COMPHELPER_DATETIME_HXX_
|
2000-10-05 07:50:51 +00:00
|
|
|
#include <comphelper/datetime.hxx>
|
|
|
|
#endif
|
|
|
|
#ifndef _OSL_DIAGNOSE_H_
|
|
|
|
#include <osl/diagnose.h>
|
|
|
|
#endif
|
|
|
|
#ifndef _COM_SUN_STAR_UTIL_NUMBERFORMAT_HPP_
|
|
|
|
#include <com/sun/star/util/NumberFormat.hpp>
|
|
|
|
#endif
|
|
|
|
#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTYPES_HPP_
|
|
|
|
#include <com/sun/star/util/XNumberFormatTypes.hpp>
|
|
|
|
#endif
|
|
|
|
#ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
|
|
|
|
#include <com/sun/star/sdbc/DataType.hpp>
|
|
|
|
#endif
|
2000-10-19 10:47:14 +00:00
|
|
|
#ifndef _COM_SUN_STAR_SDB_XCOLUMNUPDATE_HPP_
|
|
|
|
#include <com/sun/star/sdb/XColumnUpdate.hpp>
|
|
|
|
#endif
|
|
|
|
#ifndef _COM_SUN_STAR_SDB_XCOLUMN_HPP_
|
|
|
|
#include <com/sun/star/sdb/XColumn.hpp>
|
|
|
|
#endif
|
|
|
|
#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
|
|
|
|
#include <com/sun/star/beans/XPropertySet.hpp>
|
|
|
|
#endif
|
|
|
|
|
2000-10-05 07:50:51 +00:00
|
|
|
|
|
|
|
#define MAX_DAYS 3636532
|
|
|
|
|
|
|
|
//.........................................................................
|
|
|
|
namespace dbtools
|
|
|
|
{
|
|
|
|
//.........................................................................
|
|
|
|
|
|
|
|
|
|
|
|
using namespace ::comphelper;
|
2000-10-19 10:47:14 +00:00
|
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
using namespace ::com::sun::star::util;
|
|
|
|
using namespace ::com::sun::star::sdb;
|
|
|
|
using namespace ::com::sun::star::lang;
|
|
|
|
using namespace ::com::sun::star::beans;
|
2000-10-05 07:50:51 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
Date DBTypeConversion::STANDARD_DB_DATE(1, 1, 1900);
|
2000-10-05 07:50:51 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
|
|
|
|
const Reference<XNumberFormatter>& xFormatter,
|
|
|
|
const Date& rNullDate,
|
2000-10-05 07:50:51 +00:00
|
|
|
const ::rtl::OUString& rString,
|
|
|
|
sal_Int32 nKey,
|
|
|
|
sal_Int16 nFieldType,
|
|
|
|
sal_Int16 nKeyType) throw(starlang::IllegalArgumentException)
|
|
|
|
{
|
|
|
|
double fValue = 0;
|
|
|
|
if (rString.len())
|
|
|
|
{
|
|
|
|
// Muss der String formatiert werden?
|
2000-10-19 10:47:14 +00:00
|
|
|
sal_Int16 nTypeClass = nKeyType & ~NumberFormat::DEFINED;
|
|
|
|
sal_Bool bTextFormat = nTypeClass == NumberFormat::TEXT;
|
2000-10-05 07:50:51 +00:00
|
|
|
sal_Int32 nKeyToUse = bTextFormat ? 0 : nKey;
|
|
|
|
sal_Int16 nRealUsedTypeClass = nTypeClass;
|
|
|
|
// bei einem Text-Format muessen wir dem Formatter etwas mehr Freiheiten einraeumen, sonst
|
|
|
|
// wirft convertStringToNumber eine NotNumericException
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fValue = xFormatter->convertStringToNumber(nKeyToUse, rString);
|
|
|
|
sal_Int32 nRealUsedKey = xFormatter->detectNumberFormat(0, rString);
|
|
|
|
if (nRealUsedKey != nKeyToUse)
|
2000-10-19 10:47:14 +00:00
|
|
|
nRealUsedTypeClass = getNumberFormatType(xFormatter, nRealUsedKey) & ~NumberFormat::DEFINED;
|
2000-10-05 07:50:51 +00:00
|
|
|
|
|
|
|
// und noch eine Sonderbehandlung, diesmal fuer Prozent-Formate
|
2000-10-19 10:47:14 +00:00
|
|
|
if ((NumberFormat::NUMBER == nRealUsedTypeClass) && (NumberFormat::PERCENT == nTypeClass))
|
2000-10-05 07:50:51 +00:00
|
|
|
{ // die Formatierung soll eigentlich als Prozent erfolgen, aber der String stellt nur eine
|
|
|
|
// einfache Nummer dar -> anpassen
|
|
|
|
::rtl::OUString sExpanded(rString);
|
|
|
|
static ::rtl::OUString s_sPercentSymbol = ::rtl::OUString::createFromAscii("%");
|
|
|
|
// need a method to add a sal_Unicode to a string, 'til then we use a static string ...
|
|
|
|
sExpanded += s_sPercentSymbol;
|
|
|
|
fValue = xFormatter->convertStringToNumber(nKeyToUse, sExpanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (nRealUsedTypeClass)
|
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::DATE:
|
|
|
|
case NumberFormat::DATETIME:
|
2000-10-05 07:50:51 +00:00
|
|
|
xVariant->updateDouble(toStandardDbDate(rNullDate, fValue));
|
|
|
|
break;
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::TIME:
|
|
|
|
case NumberFormat::CURRENCY:
|
|
|
|
case NumberFormat::NUMBER:
|
|
|
|
case NumberFormat::SCIENTIFIC:
|
|
|
|
case NumberFormat::FRACTION:
|
|
|
|
case NumberFormat::PERCENT:
|
2000-10-05 07:50:51 +00:00
|
|
|
xVariant->updateDouble(fValue);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xVariant->updateString(rString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
xVariant->updateString(rString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (nFieldType)
|
|
|
|
{
|
|
|
|
case starsdbc::DataType::CHAR:
|
|
|
|
case starsdbc::DataType::VARCHAR:
|
|
|
|
case starsdbc::DataType::LONGVARCHAR:
|
|
|
|
xVariant->updateString(rString);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xVariant->updateNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
void DBTypeConversion::setValue(const Reference<XColumnUpdate>& xVariant,
|
|
|
|
const Date& rNullDate,
|
2000-10-05 07:50:51 +00:00
|
|
|
const double& rValue,
|
|
|
|
sal_Int16 nKeyType) throw(starlang::IllegalArgumentException)
|
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
switch (nKeyType & ~NumberFormat::DEFINED)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::DATE:
|
|
|
|
case NumberFormat::DATETIME:
|
|
|
|
// case NumberFormat::TIME:
|
2000-10-05 07:50:51 +00:00
|
|
|
xVariant->updateDouble(toStandardDbDate(rNullDate, rValue));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
xVariant->updateDouble(rValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
double DBTypeConversion::getValue(const Reference<XColumn>& xVariant,
|
|
|
|
const Date& rNullDate,
|
2000-10-05 07:50:51 +00:00
|
|
|
sal_Int16 nKeyType)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
switch (nKeyType & ~NumberFormat::DEFINED)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::DATE:
|
|
|
|
case NumberFormat::DATETIME:
|
|
|
|
// case NumberFormat::TIME:
|
2000-10-05 07:50:51 +00:00
|
|
|
return toNullDate(rNullDate, xVariant->getDouble());
|
|
|
|
default:
|
|
|
|
return xVariant->getDouble();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
::rtl::OUString DBTypeConversion::getValue(const Reference<starbeans::XPropertySet>& _xColumn,
|
|
|
|
const Reference<XNumberFormatter>& _xFormatter,
|
2000-10-05 07:50:51 +00:00
|
|
|
const starlang::Locale& _rLocale,
|
2000-10-19 10:47:14 +00:00
|
|
|
const Date& _rNullDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
sal_Int32 nKey;
|
|
|
|
sal_Int16 nKeyType;
|
|
|
|
|
|
|
|
OSL_ENSHURE(_xColumn.is() && _xFormatter.is(), "DBTypeConversion::getValue: invalid arg !");
|
|
|
|
if (!_xColumn.is() || !_xFormatter.is())
|
|
|
|
return ::rtl::OUString();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_xColumn->getPropertyValue(::rtl::OUString::createFromAscii("FormatKey")) >>= nKey;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nKey)
|
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
Reference<XNumberFormats> xFormats( _xFormatter->getNumberFormatsSupplier()->getNumberFormats() );
|
|
|
|
Reference<XNumberFormatTypes> xTypeList(_xFormatter->getNumberFormatsSupplier()->getNumberFormats(), UNO_QUERY);
|
2000-10-05 07:50:51 +00:00
|
|
|
|
|
|
|
nKey = dbtools::getDefaultNumberFormat(_xColumn,
|
2000-10-19 10:47:14 +00:00
|
|
|
Reference< XNumberFormatTypes > (xFormats, UNO_QUERY),
|
2000-10-05 07:50:51 +00:00
|
|
|
_rLocale);
|
|
|
|
|
2000-10-19 10:47:14 +00:00
|
|
|
nKeyType = getNumberFormatType(_xFormatter, nKey) & ~NumberFormat::DEFINED;
|
2000-10-05 07:50:51 +00:00
|
|
|
}
|
|
|
|
|
2000-10-19 10:47:14 +00:00
|
|
|
return DBTypeConversion::getValue(Reference< XColumn > (_xColumn, UNO_QUERY), _xFormatter, _rNullDate, nKey, nKeyType);
|
2000-10-05 07:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
::rtl::OUString DBTypeConversion::getValue(const Reference<XColumn>& xVariant,
|
|
|
|
const Reference<XNumberFormatter>& xFormatter,
|
|
|
|
const Date& rNullDate,
|
2000-10-05 07:50:51 +00:00
|
|
|
sal_Int32 nKey,
|
|
|
|
sal_Int16 nKeyType)
|
|
|
|
{
|
|
|
|
::rtl::OUString aString;
|
|
|
|
if (xVariant.is())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
switch (nKeyType & ~NumberFormat::DEFINED)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::DATE:
|
|
|
|
case NumberFormat::DATETIME:
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
double fValue = xVariant->getDouble();
|
|
|
|
if (!xVariant->wasNull())
|
|
|
|
xFormatter->convertNumberToString(nKey, toNullDate(rNullDate, fValue));
|
|
|
|
} break;
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::TIME:
|
|
|
|
case NumberFormat::NUMBER:
|
|
|
|
case NumberFormat::SCIENTIFIC:
|
|
|
|
case NumberFormat::FRACTION:
|
|
|
|
case NumberFormat::PERCENT:
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
double fValue = xVariant->getDouble();
|
|
|
|
if (!xVariant->wasNull())
|
|
|
|
aString = xFormatter->convertNumberToString(nKey, fValue);
|
|
|
|
} break;
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::CURRENCY:
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
double fValue = xVariant->getDouble();
|
|
|
|
if (!xVariant->wasNull())
|
|
|
|
aString = xFormatter->getInputString(nKey, fValue);
|
|
|
|
} break;
|
2000-10-19 10:47:14 +00:00
|
|
|
case NumberFormat::TEXT:
|
2000-10-05 07:50:51 +00:00
|
|
|
aString = xFormatter->formatString(nKey, xVariant->getString());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
aString = xVariant->getString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
aString = xVariant->getString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const double fMilliSecondsPerDay = 86400000.0;
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
sal_Int32 DBTypeConversion::toINT32(const Date& rVal)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
return ((sal_Int32)(rVal.Day%100)) +
|
|
|
|
(((sal_Int32)(rVal.Month%100))*100) +
|
|
|
|
(((sal_Int32) rVal.Year%10000)*10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
sal_Int32 DBTypeConversion::toINT32(const Time& rVal)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
// Zeit normalisieren
|
|
|
|
sal_Int32 nSeconds = rVal.Seconds + rVal.HundredthSeconds / 100;
|
|
|
|
sal_Int32 nHundredthSeconds = rVal.HundredthSeconds % 100;
|
|
|
|
sal_Int32 nMinutes = rVal.Minutes + nSeconds / 60;
|
|
|
|
nSeconds = nSeconds % 60;
|
|
|
|
sal_Int32 nHours = rVal.Hours + nMinutes / 60;
|
|
|
|
nMinutes = nMinutes % 60;
|
|
|
|
|
|
|
|
// Zeit zusammenbauen
|
|
|
|
return (sal_Int32)(nHundredthSeconds + (nSeconds*100) + (nMinutes*10000) + (nHours*1000000));
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
sal_Int64 DBTypeConversion::toINT64(const DateTime& rVal)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
// Zeit normalisieren
|
|
|
|
sal_Int32 nSeconds = rVal.Seconds + rVal.HundredthSeconds / 100;
|
|
|
|
sal_Int32 nHundredthSeconds = rVal.HundredthSeconds % 100;
|
|
|
|
sal_Int32 nMinutes = rVal.Minutes + nSeconds / 60;
|
|
|
|
nSeconds = nSeconds % 60;
|
|
|
|
sal_Int32 nHours = rVal.Hours + nMinutes / 60;
|
|
|
|
nMinutes = nMinutes % 60;
|
|
|
|
|
|
|
|
// Zeit zusammenbauen
|
|
|
|
sal_Int32 nTime = (sal_Int32)(nHundredthSeconds + (nSeconds*100) + (nMinutes*10000) + (nHours*1000000));
|
|
|
|
sal_Int32 nDate = ((sal_Int32)(rVal.Day%100)) + (((sal_Int32)(rVal.Month%100))*100) + (((sal_Int32) rVal.Year%10000)*10000);
|
|
|
|
sal_Int64 nRet;
|
|
|
|
sal_setInt64(&nRet,nDate,nTime);
|
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
sal_Int32 DBTypeConversion::getMsFromTime(const Time& rVal)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
sal_Int16 nSign = (toINT32(rVal) >= 0) ? +1 : -1;
|
|
|
|
sal_Int32 nHour = rVal.Hours;
|
|
|
|
sal_Int32 nMin = rVal.Minutes;
|
|
|
|
sal_Int32 nSec = rVal.Seconds;
|
|
|
|
sal_Int32 n100Sec = rVal.HundredthSeconds;
|
|
|
|
|
|
|
|
return (((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10))*nSign);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static sal_Int32 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30,
|
|
|
|
31, 31, 30, 31, 30, 31 };
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static sal_Bool implIsLeapYear(sal_Int32 _nYear)
|
|
|
|
{
|
|
|
|
return ( ( ((_nYear % 4) == 0)
|
|
|
|
&& ((_nYear % 100) != 0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|| ((_nYear % 400) == 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static sal_Int32 implDaysInMonth(sal_Int32 _nMonth, sal_Int32 _nYear)
|
|
|
|
{
|
|
|
|
if (_nMonth != 2)
|
|
|
|
return aDaysInMonth[_nMonth-1];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (implIsLeapYear(_nYear))
|
|
|
|
return aDaysInMonth[_nMonth-1] + 1;
|
|
|
|
else
|
|
|
|
return aDaysInMonth[_nMonth-1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
static sal_Int32 implRelativeToAbsoluteNull(const Date& _rDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
sal_Int32 nDays = 0;
|
|
|
|
|
|
|
|
// ripped this code from the implementation of tools::Date
|
|
|
|
sal_Int32 nNormalizedYear = _rDate.Year - 1;
|
|
|
|
nDays = nNormalizedYear * 365;
|
|
|
|
// leap years
|
|
|
|
nDays += (nNormalizedYear / 4) - (nNormalizedYear / 100) + (nNormalizedYear / 400);
|
|
|
|
|
|
|
|
for (sal_Int32 i = 1; i < _rDate.Month; ++i)
|
|
|
|
nDays += implDaysInMonth(i, _rDate.Month);
|
|
|
|
|
|
|
|
nDays += _rDate.Day;
|
|
|
|
return nDays;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void implBuildFromRelative( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear)
|
|
|
|
{
|
|
|
|
sal_Int32 nTempDays;
|
|
|
|
sal_Int32 i = 0;
|
|
|
|
sal_Bool bCalc;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nTempDays = nDays;
|
|
|
|
rYear = ((nTempDays / 365) - i);
|
|
|
|
nTempDays -= (rYear-1) * 365;
|
|
|
|
nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400);
|
|
|
|
bCalc = sal_False;
|
|
|
|
if ( nTempDays < 1 )
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
bCalc = sal_True;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( nTempDays > 365 )
|
|
|
|
{
|
|
|
|
if ( (nTempDays != 366) || !implIsLeapYear( rYear ) )
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
bCalc = sal_True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ( bCalc );
|
|
|
|
|
|
|
|
rMonth = 1;
|
|
|
|
while ( nTempDays > implDaysInMonth( rMonth, rYear ) )
|
|
|
|
{
|
|
|
|
nTempDays -= implDaysInMonth( rMonth, rYear );
|
|
|
|
rMonth++;
|
|
|
|
}
|
|
|
|
rDay = nTempDays;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
sal_Int32 DBTypeConversion::toDays(const Date& _rVal, const Date& _rNullDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
return implRelativeToAbsoluteNull(_rVal) - implRelativeToAbsoluteNull(_rNullDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
double DBTypeConversion::toDouble(const Date& rVal, const Date& _rNullDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
return (double)toDays(rVal, _rNullDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
double DBTypeConversion::toDouble(const Time& rVal)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
return (double)getMsFromTime(rVal) / fMilliSecondsPerDay;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
double DBTypeConversion::toDouble(const DateTime& _rVal, const Date& _rNullDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
sal_Int64 nTime = toDays(Date(_rVal.Day, _rVal.Month, _rVal.Year), _rNullDate);
|
2000-10-05 07:50:51 +00:00
|
|
|
sal_Int16 nSign = (nTime >= 0) ? +1 : -1;
|
|
|
|
sal_Int32 nHour = _rVal.Hours;
|
|
|
|
sal_Int32 nMin = _rVal.Minutes;
|
|
|
|
sal_Int32 nSec = _rVal.Seconds;
|
|
|
|
sal_Int32 n100Sec = _rVal.HundredthSeconds;
|
|
|
|
|
|
|
|
double nVal = (((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10))*nSign);
|
|
|
|
|
|
|
|
return ((double)nTime) + (nVal * (1/fMilliSecondsPerDay));
|
|
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
static void addDays(sal_Int32 nDays, Date& _rDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
sal_Int32 nTempDays = implRelativeToAbsoluteNull( _rDate );
|
|
|
|
|
|
|
|
nTempDays += nDays;
|
|
|
|
if ( nTempDays > MAX_DAYS )
|
|
|
|
{
|
|
|
|
_rDate.Day = 31;
|
|
|
|
_rDate.Month = 12;
|
|
|
|
_rDate.Year = 9999;
|
|
|
|
}
|
|
|
|
else if ( nTempDays <= 0 )
|
|
|
|
{
|
|
|
|
_rDate.Day = 1;
|
|
|
|
_rDate.Month = 1;
|
|
|
|
_rDate.Year = 00;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
|
|
|
|
}
|
|
|
|
// -----------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
static void subDays( sal_Int32 nDays, Date& _rDate )
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
sal_Int32 nTempDays = implRelativeToAbsoluteNull( _rDate );
|
|
|
|
|
|
|
|
nTempDays -= nDays;
|
|
|
|
if ( nTempDays > MAX_DAYS )
|
|
|
|
{
|
|
|
|
_rDate.Day = 31;
|
|
|
|
_rDate.Month = 12;
|
|
|
|
_rDate.Year = 9999;
|
|
|
|
}
|
|
|
|
else if ( nTempDays <= 0 )
|
|
|
|
{
|
|
|
|
_rDate.Day = 1;
|
|
|
|
_rDate.Month = 1;
|
|
|
|
_rDate.Year = 00;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
implBuildFromRelative( nTempDays, _rDate.Day, _rDate.Month, _rDate.Year );
|
|
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
Date DBTypeConversion::toDate(double dVal, const Date& _rNullDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
Date aRet = _rNullDate;
|
2000-10-05 07:50:51 +00:00
|
|
|
|
|
|
|
if (dVal >= 0)
|
|
|
|
addDays((sal_Int32)dVal,aRet);
|
|
|
|
else
|
|
|
|
subDays((sal_uInt32)(-dVal),aRet);
|
|
|
|
// x -= (sal_uInt32)(-nDays);
|
|
|
|
|
|
|
|
return aRet;
|
|
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
Time DBTypeConversion::toTime(double dVal)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
|
|
|
sal_Int32 nDays = (sal_Int32)dVal;
|
|
|
|
sal_Int32 nMS = sal_Int32((dVal - (double)nDays) * fMilliSecondsPerDay + 0.5);
|
|
|
|
|
|
|
|
sal_uInt16 nSign;
|
|
|
|
if ( nMS < 0 )
|
|
|
|
{
|
|
|
|
nMS *= -1;
|
|
|
|
nSign = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nSign = 1;
|
|
|
|
|
2000-10-19 10:47:14 +00:00
|
|
|
Time xRet;
|
2000-10-05 07:50:51 +00:00
|
|
|
// Zeit normalisieren
|
|
|
|
xRet.HundredthSeconds = nMS/10;
|
|
|
|
xRet.Seconds = xRet.HundredthSeconds / 100;
|
|
|
|
xRet.HundredthSeconds = xRet.HundredthSeconds % 100;
|
|
|
|
xRet.Minutes = xRet.Seconds / 60;
|
|
|
|
xRet.Seconds = xRet.Seconds % 60;
|
|
|
|
xRet.Hours = xRet.Minutes / 60;
|
|
|
|
xRet.Minutes = xRet.Minutes % 60;
|
|
|
|
|
|
|
|
// Zeit zusammenbauen
|
|
|
|
sal_Int32 nTime = (sal_Int32)(xRet.HundredthSeconds + (xRet.Seconds*100) + (xRet.Minutes*10000) + (xRet.Hours*1000000)) * nSign;
|
|
|
|
|
|
|
|
if(nTime < 0)
|
|
|
|
{
|
|
|
|
xRet.HundredthSeconds = 99;
|
|
|
|
xRet.Minutes = 59;
|
|
|
|
xRet.Seconds = 59;
|
|
|
|
xRet.Hours = 23;
|
|
|
|
}
|
|
|
|
return xRet;
|
|
|
|
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
2000-10-19 10:47:14 +00:00
|
|
|
DateTime DBTypeConversion::toDateTime(double dVal, const Date& _rNullDate)
|
2000-10-05 07:50:51 +00:00
|
|
|
{
|
2000-10-19 10:47:14 +00:00
|
|
|
Date aDate = toDate(dVal, _rNullDate);
|
|
|
|
Time aTime = toTime(dVal);
|
2000-10-05 07:50:51 +00:00
|
|
|
|
2000-10-19 10:47:14 +00:00
|
|
|
DateTime xRet;
|
2000-10-05 07:50:51 +00:00
|
|
|
|
|
|
|
xRet.Day = aDate.Day;
|
|
|
|
xRet.Month = aDate.Month;
|
|
|
|
xRet.Year = aDate.Year;
|
|
|
|
|
|
|
|
xRet.HundredthSeconds = aTime.HundredthSeconds;
|
|
|
|
xRet.Minutes = aTime.Minutes;
|
|
|
|
xRet.Seconds = aTime.Seconds;
|
|
|
|
xRet.Hours = aTime.Hours;
|
|
|
|
|
|
|
|
|
|
|
|
return xRet;
|
|
|
|
}
|
2000-10-19 10:47:14 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Date DBTypeConversion::getNULLDate(const Reference< XNumberFormatsSupplier > &xSupplier)
|
|
|
|
{
|
|
|
|
OSL_ENSHURE(xSupplier.is(), "getNULLDate : the formatter doesn't implement a supplier !");
|
|
|
|
if (xSupplier.is())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// get the null date
|
|
|
|
Date aDate;
|
|
|
|
xSupplier->getNumberFormatSettings()->getPropertyValue(::rtl::OUString::createFromAscii("NullDate")) >>= aDate;
|
|
|
|
return aDate;
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STANDARD_DB_DATE;
|
|
|
|
}
|
|
|
|
|
2000-10-05 07:50:51 +00:00
|
|
|
//.........................................................................
|
|
|
|
} // namespace dbtools
|
|
|
|
//.........................................................................
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* history:
|
|
|
|
* $Log: not supported by cvs2svn $
|
2000-10-19 10:47:14 +00:00
|
|
|
* Revision 1.1 2000/10/05 08:50:32 fs
|
|
|
|
* moved the files from unotools to here
|
|
|
|
*
|
2000-10-05 07:50:51 +00:00
|
|
|
*
|
|
|
|
* Revision 1.0 29.09.00 08:17:18 fs
|
|
|
|
************************************************************************/
|
|
|
|
|