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 <i18nlangtag/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
= new SvNumberFormatter( comphelper::getProcessComponentContext(), eLangType
);
104 sal_Int32 nCheckPos
= 0;
107 // Default templates of the formatter have only two-digit
108 // date. Therefore register an own format.
110 // HACK, because the number formatter in PutandConvertEntry replace the wildcard
111 // for month, day, year not according to the configuration.
112 // Problem: Print Year(Date) under Engl. OS
113 // quod vide basic/source/runtime/runtime.cxx
115 SvtSysLocale aSysLocale
;
116 DateFormat eDate
= aSysLocale
.GetLocaleData().getDateFormat();
121 case MDY
: aDateStr
= "MM/DD/YYYY"; break;
122 case DMY
: aDateStr
= "DD/MM/YYYY"; break;
123 case YMD
: aDateStr
= "YYYY/MM/DD"; break;
126 OUString
aStr( aDateStr
);
129 pFormatter
->PutandConvertEntry( aStr
, nCheckPos
, nType
,
130 nIndex
, LANGUAGE_ENGLISH_US
, eLangType
);
131 sal_Bool bSuccess
= pFormatter
->IsNumberFormat( *p
->pOUString
, nIndex
, nRes
);
134 short nType_
= pFormatter
->GetType( nIndex
);
135 if(!(nType_
& ( NUMBERFORMAT_DATETIME
| NUMBERFORMAT_DATE
|
136 NUMBERFORMAT_TIME
| NUMBERFORMAT_DEFINED
)))
138 bSuccess
= sal_False
;
144 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
151 pVal
= PTR_CAST(SbxValue
,p
->pObj
);
154 nRes
= pVal
->GetDate();
158 SbxBase::SetError( SbxERR_NO_OBJECT
); nRes
= 0;
161 case SbxBYREF
| SbxCHAR
:
164 case SbxBYREF
| SbxBYTE
:
167 case SbxBYREF
| SbxINTEGER
:
168 case SbxBYREF
| SbxBOOL
:
171 case SbxBYREF
| SbxLONG
:
174 case SbxBYREF
| SbxULONG
:
177 case SbxBYREF
| SbxERROR
:
178 case SbxBYREF
| SbxUSHORT
:
181 case SbxBYREF
| SbxSINGLE
:
184 case SbxBYREF
| SbxDATE
:
185 case SbxBYREF
| SbxDOUBLE
:
188 case SbxBYREF
| SbxCURRENCY
:
189 nRes
= ImpCurrencyToDouble( *p
->pnInt64
);
191 case SbxBYREF
| SbxSALINT64
:
192 nRes
= static_cast< double >(*p
->pnInt64
);
194 case SbxBYREF
| SbxSALUINT64
:
195 nRes
= ImpSalUInt64ToDouble( *p
->puInt64
);
198 SbxBase::SetError( SbxERR_CONVERSION
); nRes
= 0;
204 void ImpPutDate( SbxValues
* p
, double n
)
217 // from here will be tested
219 aTmp
.pChar
= &p
->nChar
;
222 aTmp
.pByte
= &p
->nByte
;
226 aTmp
.pInteger
= &p
->nInteger
;
229 aTmp
.pLong
= &p
->nLong
;
232 aTmp
.pULong
= &p
->nULong
;
236 aTmp
.pUShort
= &p
->nUShort
;
239 aTmp
.pSingle
= &p
->nSingle
;
243 aTmp
.pnInt64
= &p
->nInt64
;
246 aTmp
.puInt64
= &p
->uInt64
;
249 case SbxBYREF
| SbxDECIMAL
:
250 pDec
= ImpCreateDecimal( p
);
251 if( !pDec
->setDouble( n
) )
253 SbxBase::SetError( SbxERR_OVERFLOW
);
257 aTmp
.eType
= SbxDataType( p
->eType
| SbxBYREF
);
258 p
= &aTmp
; goto start
;
260 case SbxBYREF
| SbxSTRING
:
266 p
->pOUString
= new OUString
;
270 LanguageType eLangType
= GetpApp()->GetSettings().GetLanguageTag().getLanguageType();
271 SvNumberFormatter
* pFormatter
= new SvNumberFormatter( comphelper::getProcessComponentContext(), eLangType
);
274 sal_Int32 nCheckPos
= 0;
277 SvtSysLocale aSysLocale
;
278 DateFormat eDate
= aSysLocale
.GetLocaleData().getDateFormat();
280 // if the whole-number part is 0, we want no year!
281 if( n
<= -1.0 || n
>= 1.0 )
283 // Time only if != 00:00:00
284 if( floor( n
) == n
)
288 case MDY
: aStr
= "MM.TT.JJJJ"; break;
289 case DMY
: aStr
= "TT.MM.JJJJ"; break;
290 case YMD
: aStr
= "JJJJ.MM.TT"; break;
291 default: aStr
= "MM.TT.JJJJ";
298 case MDY
: aStr
= "MM.TT.JJJJ HH:MM:SS"; break;
299 case DMY
: aStr
= "TT.MM.JJJJ HH:MM:SS"; break;
300 case YMD
: aStr
= "JJJJ.MM.TT HH:MM:SS"; break;
301 default: aStr
= "MM.TT.JJJJ HH:MM:SS";
309 pFormatter
->PutandConvertEntry( aStr
,
316 pFormatter
->GetOutputString( n
, nIndex
, aTmpString
, &pColor
);
317 *p
->pOUString
= aTmpString
;
322 pVal
= PTR_CAST(SbxValue
,p
->pObj
);
329 SbxBase::SetError( SbxERR_NO_OBJECT
);
332 case SbxBYREF
| SbxCHAR
:
335 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCHAR
;
337 else if( n
< SbxMINCHAR
)
339 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCHAR
;
341 *p
->pChar
= (sal_Unicode
) n
;
343 case SbxBYREF
| SbxBYTE
:
346 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXBYTE
;
350 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
352 *p
->pByte
= (sal_uInt8
) n
;
354 case SbxBYREF
| SbxINTEGER
:
355 case SbxBYREF
| SbxBOOL
:
358 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXINT
;
360 else if( n
< SbxMININT
)
362 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMININT
;
364 *p
->pInteger
= (sal_Int16
) n
;
366 case SbxBYREF
| SbxERROR
:
367 case SbxBYREF
| SbxUSHORT
:
370 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXUINT
;
374 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
376 *p
->pUShort
= (sal_uInt16
) n
;
378 case SbxBYREF
| SbxLONG
:
381 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXLNG
;
383 else if( n
< SbxMINLNG
)
385 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINLNG
;
387 *p
->pLong
= (sal_Int32
) n
;
389 case SbxBYREF
| SbxULONG
:
392 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXULNG
;
396 SbxBase::SetError( SbxERR_OVERFLOW
); n
= 0;
398 *p
->pULong
= (sal_uInt32
) n
;
400 case SbxBYREF
| SbxSINGLE
:
403 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXSNG
;
405 else if( n
< SbxMINSNG
)
407 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINSNG
;
409 *p
->pSingle
= (float) n
;
411 case SbxBYREF
| SbxSALINT64
:
412 *p
->pnInt64
= ImpDoubleToSalInt64( n
);
414 case SbxBYREF
| SbxSALUINT64
:
415 *p
->puInt64
= ImpDoubleToSalUInt64( n
);
417 case SbxBYREF
| SbxDATE
:
418 case SbxBYREF
| SbxDOUBLE
:
419 *p
->pDouble
= (double) n
;
421 case SbxBYREF
| SbxCURRENCY
:
424 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMAXCURR
;
426 else if( n
< SbxMINCURR
)
428 SbxBase::SetError( SbxERR_OVERFLOW
); n
= SbxMINCURR
;
430 *p
->pnInt64
= ImpDoubleToCurrency( n
);
433 SbxBase::SetError( SbxERR_CONVERSION
);
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */