LibreOffice Module basic (master) 1
sbxdate.cxx
Go to the documentation of this file.
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 <rtl/math.hxx>
21#include <vcl/svapp.hxx>
22#include <vcl/settings.hxx>
23#include <svl/numformat.hxx>
24#include <svl/zforlist.hxx>
25#include <tools/color.hxx>
26#include <i18nlangtag/lang.h>
27#include <basic/sberrors.hxx>
28#include "sbxconv.hxx"
29#include <runtime.hxx>
30#include <sbintern.hxx>
31#include <math.h>
32#include <memory>
33#include <config_features.h>
34
35
36double ImpGetDate( const SbxValues* p )
37{
38 double nRes;
39 SbxValue* pVal;
40
41 switch( +p->eType )
42 {
43 case SbxNULL:
45 [[fallthrough]];
46 case SbxEMPTY:
47 nRes = 0;
48 break;
49 case SbxCHAR:
50 nRes = p->nChar;
51 break;
52 case SbxBYTE:
53 nRes = p->nByte;
54 break;
55 case SbxINTEGER:
56 case SbxBOOL:
57 nRes = p->nInteger;
58 break;
59 case SbxERROR:
60 case SbxUSHORT:
61 nRes = p->nUShort;
62 break;
63 case SbxLONG:
64 nRes = static_cast<double>(p->nLong);
65 break;
66 case SbxULONG:
67 nRes = static_cast<double>(p->nULong);
68 break;
69 case SbxSINGLE:
70 nRes = p->nSingle;
71 break;
72 case SbxDATE:
73 case SbxDOUBLE:
74 nRes = p->nDouble;
75 break;
76 case SbxCURRENCY:
77 nRes = ImpCurrencyToDouble( p->nInt64 );
78 break;
79 case SbxSALINT64:
80 nRes = static_cast< double >(p->nInt64);
81 break;
82 case SbxSALUINT64:
83 nRes = ImpSalUInt64ToDouble( p->uInt64 );
84 break;
85 case SbxDECIMAL:
86 case SbxBYREF | SbxDECIMAL:
87 if (!p->pDecimal || !p->pDecimal->getDouble(nRes))
88 nRes = 0.0;
89 break;
90 case SbxBYREF | SbxSTRING:
91 case SbxSTRING:
92 case SbxLPSTR:
93#if HAVE_FEATURE_SCRIPTING
94 if( !p->pOUString )
95 {
96 nRes = 0;
97 }
98 else
99 {
101 std::shared_ptr<SvNumberFormatter> pFormatter;
102 if (GetSbData()->pInst)
103 {
104 pFormatter = GetSbData()->pInst->GetNumberFormatter();
105 }
106 else
107 {
108 sal_uInt32 nDummy;
109 pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy );
110 }
111
112 sal_uInt32 nIndex;
113 sal_Int32 nCheckPos = 0;
114 SvNumFormatType nType = SvNumFormatType::DEFINED | SvNumFormatType::DATE | SvNumFormatType::TIME | SvNumFormatType::CURRENCY
115 | SvNumFormatType::NUMBER | SvNumFormatType::SCIENTIFIC | SvNumFormatType::FRACTION;
116
117 // Default templates of the formatter have only two-digit
118 // date. Therefore register an own format.
119
120 // HACK, because the number formatter in PutandConvertEntry replace the wildcard
121 // for month, day, year not according to the configuration.
122 // Problem: Print Year(Date) under Engl. OS
123 // quod vide basic/source/runtime/runtime.cxx
124
125 SvtSysLocale aSysLocale;
126 DateOrder eDate = aSysLocale.GetLocaleData().getDateOrder();
127 OUString aDateStr;
128 switch( eDate )
129 {
130 default:
131 case DateOrder::MDY: aDateStr = "MM/DD/YYYY"; break;
132 case DateOrder::DMY: aDateStr = "DD/MM/YYYY"; break;
133 case DateOrder::YMD: aDateStr = "YYYY/MM/DD"; break;
134 }
135
136 OUString aStr = aDateStr + " HH:MM:SS";
137
138 pFormatter->PutandConvertEntry( aStr, nCheckPos, nType,
139 nIndex, LANGUAGE_ENGLISH_US, eLangType, true);
140 bool bSuccess = pFormatter->IsNumberFormat( *p->pOUString, nIndex, nRes );
141 if ( bSuccess )
142 {
143 SvNumFormatType nType_ = pFormatter->GetType( nIndex );
144 if(!(nType_ & ( SvNumFormatType::DATETIME | SvNumFormatType::DATE |
145 SvNumFormatType::TIME | SvNumFormatType::DEFINED )))
146 {
147 bSuccess = false;
148 }
149 }
150
151 if ( !bSuccess )
152 {
154 }
155 }
156#else
157 nRes = 0;
158#endif
159 break;
160 case SbxOBJECT:
161 pVal = dynamic_cast<SbxValue*>( p->pObj );
162 if( pVal )
163 {
164 nRes = pVal->GetDate();
165 }
166 else
167 {
169 }
170 break;
171 case SbxBYREF | SbxCHAR:
172 nRes = *p->pChar;
173 break;
174 case SbxBYREF | SbxBYTE:
175 nRes = *p->pByte;
176 break;
177 case SbxBYREF | SbxINTEGER:
178 case SbxBYREF | SbxBOOL:
179 nRes = *p->pInteger;
180 break;
181 case SbxBYREF | SbxLONG:
182 nRes = *p->pLong;
183 break;
184 case SbxBYREF | SbxULONG:
185 nRes = *p->pULong;
186 break;
187 case SbxBYREF | SbxERROR:
188 case SbxBYREF | SbxUSHORT:
189 nRes = *p->pUShort;
190 break;
191 case SbxBYREF | SbxSINGLE:
192 nRes = *p->pSingle;
193 break;
194 case SbxBYREF | SbxDATE:
195 case SbxBYREF | SbxDOUBLE:
196 nRes = *p->pDouble;
197 break;
198 case SbxBYREF | SbxCURRENCY:
199 nRes = ImpCurrencyToDouble( *p->pnInt64 );
200 break;
201 case SbxBYREF | SbxSALINT64:
202 nRes = static_cast< double >(*p->pnInt64);
203 break;
204 case SbxBYREF | SbxSALUINT64:
205 nRes = ImpSalUInt64ToDouble( *p->puInt64 );
206 break;
207 default:
209 break;
210 }
211 return nRes;
212}
213
214void ImpPutDate( SbxValues* p, double n )
215{
216 SbxValues aTmp;
217 SbxDecimal* pDec;
218 SbxValue* pVal;
219
220start:
221 switch( +p->eType )
222 {
223 case SbxDATE:
224 case SbxDOUBLE:
225 p->nDouble = n;
226 break;
227 // from here will be tested
228 case SbxCHAR:
229 aTmp.pChar = &p->nChar;
230 goto direct;
231 case SbxBYTE:
232 aTmp.pByte = &p->nByte;
233 goto direct;
234 case SbxINTEGER:
235 case SbxBOOL:
236 aTmp.pInteger = &p->nInteger;
237 goto direct;
238 case SbxLONG:
239 aTmp.pLong = &p->nLong;
240 goto direct;
241 case SbxULONG:
242 aTmp.pULong = &p->nULong;
243 goto direct;
244 case SbxERROR:
245 case SbxUSHORT:
246 aTmp.pUShort = &p->nUShort;
247 goto direct;
248 case SbxSINGLE:
249 aTmp.pSingle = &p->nSingle;
250 goto direct;
251 case SbxCURRENCY:
252 case SbxSALINT64:
253 aTmp.pnInt64 = &p->nInt64;
254 goto direct;
255 case SbxSALUINT64:
256 aTmp.puInt64 = &p->uInt64;
257 goto direct;
258 case SbxDECIMAL:
259 case SbxBYREF | SbxDECIMAL:
260 pDec = ImpCreateDecimal( p );
261 if( !pDec->setDouble( n ) )
262 {
264 }
265 break;
266 direct:
267 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
268 p = &aTmp; goto start;
269
270 case SbxBYREF | SbxSTRING:
271 case SbxSTRING:
272 case SbxLPSTR:
273 {
274#if HAVE_FEATURE_SCRIPTING
275 if( !p->pOUString )
276 {
277 p->pOUString = new OUString;
278 }
279 const Color* pColor;
280
282 std::shared_ptr<SvNumberFormatter> pFormatter;
283 if (GetSbData()->pInst)
284 {
285 pFormatter = GetSbData()->pInst->GetNumberFormatter();
286 }
287 else
288 {
289 sal_uInt32 nDummy;
290 pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy );
291 }
292
293 sal_uInt32 nIndex;
294 sal_Int32 nCheckPos = 0;
296
297 SvtSysLocale aSysLocale;
298 DateOrder eDate = aSysLocale.GetLocaleData().getDateOrder();
299 OUString aStr;
300 // if the whole-number part is 0, we want no year!
301 if( n <= -1.0 || n >= 1.0 )
302 {
303 // Time only if != 00:00:00
304 if( rtl::math::approxEqual(floor( n ), n) )
305 {
306 switch( eDate )
307 {
308 default:
309 case DateOrder::MDY: aStr = "MM/DD/YYYY"; break;
310 case DateOrder::DMY: aStr = "DD/MM/YYYY"; break;
311 case DateOrder::YMD: aStr = "YYYY/MM/DD"; break;
312 }
313 }
314 else
315 {
316 switch( eDate )
317 {
318 default:
319 case DateOrder::MDY: aStr = "MM/DD/YYYY HH:MM:SS"; break;
320 case DateOrder::DMY: aStr = "DD/MM/YYYY HH:MM:SS"; break;
321 case DateOrder::YMD: aStr = "YYYY/MM/DD HH:MM:SS"; break;
322 }
323 }
324 }
325 else
326 {
327 aStr = "HH:MM:SS";
328 }
329 pFormatter->PutandConvertEntry( aStr,
330 nCheckPos,
331 nType,
332 nIndex,
334 eLangType, true);
335 pFormatter->GetOutputString( n, nIndex, *p->pOUString, &pColor );
336#endif
337 break;
338 }
339 case SbxOBJECT:
340 pVal = dynamic_cast<SbxValue*>( p->pObj );
341 if( pVal )
342 {
343 pVal->PutDate( n );
344 }
345 else
346 {
348 }
349 break;
350 case SbxBYREF | SbxCHAR:
351 *p->pChar = ImpDoubleToChar(n);
352 break;
353 case SbxBYREF | SbxBYTE:
354 *p->pByte = ImpDoubleToByte(n);
355 break;
356 case SbxBYREF | SbxINTEGER:
357 case SbxBYREF | SbxBOOL:
358 *p->pInteger = ImpDoubleToInteger(n);
359 break;
360 case SbxBYREF | SbxERROR:
361 case SbxBYREF | SbxUSHORT:
362 *p->pUShort = ImpDoubleToUShort(n);
363 break;
364 case SbxBYREF | SbxLONG:
365 *p->pLong = ImpDoubleToLong(n);
366 break;
367 case SbxBYREF | SbxULONG:
368 *p->pULong = ImpDoubleToULong(n);
369 break;
370 case SbxBYREF | SbxSINGLE:
371 if( n > SbxMAXSNG )
372 {
374 }
375 else if( n < SbxMINSNG )
376 {
378 }
379 *p->pSingle = static_cast<float>(n);
380 break;
381 case SbxBYREF | SbxSALINT64:
382 *p->pnInt64 = ImpDoubleToSalInt64( n );
383 break;
384 case SbxBYREF | SbxSALUINT64:
385 *p->puInt64 = ImpDoubleToSalUInt64( n );
386 break;
387 case SbxBYREF | SbxDATE:
388 case SbxBYREF | SbxDOUBLE:
389 *p->pDouble = n;
390 break;
391 case SbxBYREF | SbxCURRENCY:
392 if( n > SbxMAXCURR )
393 {
395 }
396 else if( n < SbxMINCURR )
397 {
399 }
400 *p->pnInt64 = ImpDoubleToCurrency( n );
401 break;
402 default:
404 break;
405 }
406}
407
408/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const LanguageTag & GetLanguageTag() const
static const AllSettings & GetSettings()
LanguageType getLanguageType(bool bResolveSystem=true) const
DateOrder getDateOrder() const
static std::shared_ptr< SvNumberFormatter > PrepareNumberFormatter(sal_uInt32 &rnStdDateIdx, sal_uInt32 &rnStdTimeIdx, sal_uInt32 &rnStdDateTimeIdx, LanguageType const *peFormatterLangType=nullptr, DateOrder const *peFormatterDateOrder=nullptr)
Definition: runtime.cxx:404
std::shared_ptr< SvNumberFormatter > const & GetNumberFormatter()
Definition: runtime.cxx:380
static void SetError(ErrCode)
Definition: sbxbase.cxx:116
bool setDouble(double val)
Definition: sbxdec.cxx:325
void PutDate(double)
Definition: sbxvalue.cxx:597
double GetDate() const
Definition: sbxvar.hxx:151
const LocaleDataWrapper & GetLocaleData() const
sal_Int32 nIndex
void * p
sal_Int64 n
#define LANGUAGE_ENGLISH_US
DateOrder
aStr
QPRO_FUNC_TYPE nType
#define ERRCODE_BASIC_NO_OBJECT
Definition: sberrors.hxx:34
#define ERRCODE_BASIC_MATH_OVERFLOW
Definition: sberrors.hxx:27
#define ERRCODE_BASIC_CONVERSION
Definition: sberrors.hxx:30
SbiGlobals * GetSbData()
Definition: sbintern.cxx:26
double ImpCurrencyToDouble(const sal_Int64 r)
Definition: sbxconv.hxx:113
auto ImpDoubleToChar(double f)
Definition: sbxconv.hxx:50
auto ImpDoubleToInteger(double f)
Definition: sbxconv.hxx:53
auto ImpDoubleToSalUInt64(double d)
Definition: sbxconv.hxx:56
auto ImpDoubleToLong(double f)
Definition: sbxconv.hxx:55
auto ImpDoubleToUShort(double f)
Definition: sbxconv.hxx:52
auto ImpDoubleToULong(double f)
Definition: sbxconv.hxx:54
double ImpSalUInt64ToDouble(sal_uInt64 n)
Definition: sbxint.cxx:316
sal_Int64 ImpDoubleToCurrency(double d)
Definition: sbxconv.hxx:105
auto ImpDoubleToSalInt64(double d)
Definition: sbxconv.hxx:57
SbxDecimal * ImpCreateDecimal(SbxValues *p)
Definition: sbxdec.cxx:376
auto ImpDoubleToByte(double f)
Definition: sbxconv.hxx:51
void ImpPutDate(SbxValues *p, double n)
Definition: sbxdate.cxx:214
double ImpGetDate(const SbxValues *p)
Definition: sbxdate.cxx:36
constexpr auto SbxMINSNG
Definition: sbxdef.hxx:206
constexpr auto SbxMINCURR
Definition: sbxdef.hxx:203
SbxBOOL
Definition: sbxdef.hxx:215
constexpr auto SbxMAXCURR
Definition: sbxdef.hxx:202
SbxDataType
Definition: sbxdef.hxx:37
@ SbxOBJECT
Definition: sbxdef.hxx:47
@ SbxSALINT64
Definition: sbxdef.hxx:75
@ SbxLONG
Definition: sbxdef.hxx:41
@ SbxSALUINT64
Definition: sbxdef.hxx:76
@ SbxNULL
Definition: sbxdef.hxx:39
@ SbxBYTE
Definition: sbxdef.hxx:55
@ SbxEMPTY
Definition: sbxdef.hxx:38
@ SbxDECIMAL
Definition: sbxdef.hxx:77
@ SbxULONG
Definition: sbxdef.hxx:57
@ SbxUSHORT
Definition: sbxdef.hxx:56
@ SbxERROR
Definition: sbxdef.hxx:48
@ SbxDATE
Definition: sbxdef.hxx:45
@ SbxCURRENCY
Definition: sbxdef.hxx:44
@ SbxLPSTR
Definition: sbxdef.hxx:68
@ SbxSINGLE
Definition: sbxdef.hxx:42
@ SbxBYREF
Definition: sbxdef.hxx:81
@ SbxCHAR
Definition: sbxdef.hxx:54
@ SbxSTRING
Definition: sbxdef.hxx:46
@ SbxINTEGER
Definition: sbxdef.hxx:40
@ SbxDOUBLE
Definition: sbxdef.hxx:43
constexpr auto SbxMAXSNG
Definition: sbxdef.hxx:205
SbiInstance * pInst
Definition: sbintern.hxx:108
sal_uInt64 * puInt64
Definition: sbxvar.hxx:69
float * pSingle
Definition: sbxvar.hxx:72
sal_uInt8 * pByte
Definition: sbxvar.hxx:63
sal_uInt32 * pULong
Definition: sbxvar.hxx:67
sal_uInt16 * pUShort
Definition: sbxvar.hxx:64
sal_Int16 * pInteger
Definition: sbxvar.hxx:66
sal_Int32 * pLong
Definition: sbxvar.hxx:68
SbxDataType eType
Definition: sbxvar.hxx:77
sal_Int64 * pnInt64
Definition: sbxvar.hxx:70
sal_Unicode * pChar
Definition: sbxvar.hxx:65
SvNumFormatType