1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/svapp.hxx>
21 #include <svl/zforlist.hxx>
22 #include <tools/errcode.hxx>
23 #include <tools/color.hxx>
24 #include <i18npool/lang.h>
25 #include <basic/sbx.hxx>
26 #include "sbxconv.hxx"
28 #include <comphelper/processfactory.hxx>
31 double ImpGetDate( const SbxValues
* p
)
39 SbxBase::SetError( SbxERR_CONVERSION
);
58 nRes
= (double) p
->nLong
;
61 nRes
= (double) p
->nULong
;
71 nRes
= ImpCurrencyToDouble( p
->nInt64
);
74 nRes
= static_cast< double >(p
->nInt64
);
77 nRes
= ImpSalUInt64ToDouble( p
->uInt64
);
80 case SbxBYREF
| SbxDECIMAL
:
83 p
->pDecimal
->getDouble( nRes
);
90 case SbxBYREF
| SbxSTRING
:
99 LanguageType eLangType
= GetpApp()->GetSettings().GetLanguageTag().getLanguageType();
101 SvNumberFormatter
* pFormatter
;
102 com::sun::star::uno::Reference
< com::sun::star::lang::XMultiServiceFactory
>
103 xFactory
= comphelper::getProcessServiceFactory();
104 pFormatter
= new SvNumberFormatter( xFactory
, eLangType
);
107 sal_Int32 nCheckPos
= 0;
110 // Default templates of the formatter have only two-digit
111 // date. Therefore register an own format.
113 // HACK, because the number formatter in PutandConvertEntry replace the wildcard
114 // for month, day, year not according to the configuration.
115 // Problem: Print Year(Date) under Engl. OS
116 // quod vide basic\source\runtime\runtime.cxx
118 SvtSysLocale aSysLocale
;
119 DateFormat eDate
= aSysLocale
.GetLocaleData().getDateFormat();
123 case MDY
: aDateStr
= "MM.TT.JJJJ"; break;
124 case DMY
: aDateStr
= "TT.MM.JJJJ"; break;
125 case YMD
: aDateStr
= "JJJJ.MM.TT"; break;
126 default: aDateStr
= "MM.TT.JJJJ";
129 OUString
aStr( aDateStr
);
132 pFormatter
->PutandConvertEntry( aStr
, nCheckPos
, nType
,
133 nIndex
, LANGUAGE_GERMAN
, eLangType
);
134 sal_Bool bSuccess
= pFormatter
->IsNumberFormat( *p
->pOUString
, nIndex
, nRes
);
137 short nType_
= pFormatter
->GetType( nIndex
);
138 if(!(nType_
& ( NUMBERFORMAT_DATETIME
| NUMBERFORMAT_DATE
|
139 NUMBERFORMAT_TIME
| NUMBERFORMAT_DEFINED
)))
141 bSuccess
= sal_False
;
147 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
154 pVal
= PTR_CAST(SbxValue
,p
->pObj
);
157 nRes
= pVal
->GetDate();
161 SbxBase::SetError( SbxERR_NO_OBJECT
); nRes
= 0;
164 case SbxBYREF
| SbxCHAR
:
167 case SbxBYREF
| SbxBYTE
:
170 case SbxBYREF
| SbxINTEGER
:
171 case SbxBYREF
| SbxBOOL
:
174 case SbxBYREF
| SbxLONG
:
177 case SbxBYREF
| SbxULONG
:
180 case SbxBYREF
| SbxERROR
:
181 case SbxBYREF
| SbxUSHORT
:
184 case SbxBYREF
| SbxSINGLE
:
187 case SbxBYREF
| SbxDATE
:
188 case SbxBYREF
| SbxDOUBLE
:
191 case SbxBYREF
| SbxCURRENCY
:
192 nRes
= ImpCurrencyToDouble( *p
->pnInt64
);
194 case SbxBYREF
| SbxSALINT64
:
195 nRes
= static_cast< double >(*p
->pnInt64
);
197 case SbxBYREF
| SbxSALUINT64
:
198 nRes
= ImpSalUInt64ToDouble( *p
->puInt64
);
201 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
207 void ImpPutDate( SbxValues
* p
, double n
)
220 // from here will be tested
222 aTmp
.pChar
= &p
->nChar
;
225 aTmp
.pByte
= &p
->nByte
;
229 aTmp
.pInteger
= &p
->nInteger
;
232 aTmp
.pLong
= &p
->nLong
;
235 aTmp
.pULong
= &p
->nULong
;
239 aTmp
.pUShort
= &p
->nUShort
;
242 aTmp
.pSingle
= &p
->nSingle
;
246 aTmp
.pnInt64
= &p
->nInt64
;
249 aTmp
.puInt64
= &p
->uInt64
;
252 case SbxBYREF
| SbxDECIMAL
:
253 pDec
= ImpCreateDecimal( p
);
254 if( !pDec
->setDouble( n
) )
256 SbxBase::SetError( SbxERR_OVERFLOW
);
260 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
261 p
= &aTmp
; goto start
;
263 case SbxBYREF
| SbxSTRING
:
269 p
->pOUString
= new OUString
;
273 LanguageType eLangType
= GetpApp()->GetSettings().GetLanguageTag().getLanguageType();
274 SvNumberFormatter
* pFormatter
;
275 com::sun::star::uno::Reference
< com::sun::star::lang::XMultiServiceFactory
>
276 xFactory
= comphelper::getProcessServiceFactory();
277 pFormatter
= new SvNumberFormatter( xFactory
, eLangType
);
280 sal_Int32 nCheckPos
= 0;
283 SvtSysLocale aSysLocale
;
284 DateFormat eDate
= aSysLocale
.GetLocaleData().getDateFormat();
286 // if the whole-number part is 0, we want no year!
287 if( n
<= -1.0 || n
>= 1.0 )
289 // Time only if != 00:00:00
290 if( floor( n
) == n
)
294 case MDY
: aStr
= "MM.TT.JJJJ"; break;
295 case DMY
: aStr
= "TT.MM.JJJJ"; break;
296 case YMD
: aStr
= "JJJJ.MM.TT"; break;
297 default: aStr
= "MM.TT.JJJJ";
304 case MDY
: aStr
= "MM.TT.JJJJ HH:MM:SS"; break;
305 case DMY
: aStr
= "TT.MM.JJJJ HH:MM:SS"; break;
306 case YMD
: aStr
= "JJJJ.MM.TT HH:MM:SS"; break;
307 default: aStr
= "MM.TT.JJJJ HH:MM:SS";
315 pFormatter
->PutandConvertEntry( aStr
,
322 pFormatter
->GetOutputString( n
, nIndex
, aTmpString
, &pColor
);
323 *p
->pOUString
= aTmpString
;
328 pVal
= PTR_CAST(SbxValue
,p
->pObj
);
335 SbxBase::SetError( SbxERR_NO_OBJECT
);
338 case SbxBYREF
| SbxCHAR
:
341 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCHAR
;
343 else if( n
< SbxMINCHAR
)
345 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCHAR
;
347 *p
->pChar
= (sal_Unicode
) n
;
349 case SbxBYREF
| SbxBYTE
:
352 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXBYTE
;
356 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
358 *p
->pByte
= (sal_uInt8
) n
;
360 case SbxBYREF
| SbxINTEGER
:
361 case SbxBYREF
| SbxBOOL
:
364 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXINT
;
366 else if( n
< SbxMININT
)
368 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMININT
;
370 *p
->pInteger
= (sal_Int16
) n
;
372 case SbxBYREF
| SbxERROR
:
373 case SbxBYREF
| SbxUSHORT
:
376 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXUINT
;
380 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
382 *p
->pUShort
= (sal_uInt16
) n
;
384 case SbxBYREF
| SbxLONG
:
387 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXLNG
;
389 else if( n
< SbxMINLNG
)
391 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINLNG
;
393 *p
->pLong
= (sal_Int32
) n
;
395 case SbxBYREF
| SbxULONG
:
398 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXULNG
;
402 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
404 *p
->pULong
= (sal_uInt32
) n
;
406 case SbxBYREF
| SbxSINGLE
:
409 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG
;
411 else if( n
< SbxMINSNG
)
413 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG
;
415 *p
->pSingle
= (float) n
;
417 case SbxBYREF
| SbxSALINT64
:
418 *p
->pnInt64
= ImpDoubleToSalInt64( n
);
420 case SbxBYREF
| SbxSALUINT64
:
421 *p
->puInt64
= ImpDoubleToSalUInt64( n
);
423 case SbxBYREF
| SbxDATE
:
424 case SbxBYREF
| SbxDOUBLE
:
425 *p
->pDouble
= (double) n
;
427 case SbxBYREF
| SbxCURRENCY
:
430 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCURR
;
432 else if( n
< SbxMINCURR
)
434 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCURR
;
436 *p
->pnInt64
= ImpDoubleToCurrency( n
);
439 SbxBase::SetError( SbxERR_CONVERSION
);
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */