Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / basic / source / sbx / sbxdate.cxx
blob057e16f09d03ce87815781ba42dfe135f5ccc7f5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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>
36 double ImpGetDate( const SbxValues* p )
38 double nRes;
39 SbxValue* pVal;
41 switch( +p->eType )
43 case SbxNULL:
44 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
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 )
96 nRes = 0;
98 else
100 LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType();
101 std::shared_ptr<SvNumberFormatter> pFormatter;
102 if (GetSbData()->pInst)
104 pFormatter = GetSbData()->pInst->GetNumberFormatter();
106 else
108 sal_uInt32 nDummy;
109 pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy );
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;
117 // Default templates of the formatter have only two-digit
118 // date. Therefore register an own format.
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
125 SvtSysLocale aSysLocale;
126 DateOrder eDate = aSysLocale.GetLocaleData().getDateOrder();
127 OUString aDateStr;
128 switch( eDate )
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;
136 OUString aStr = aDateStr + " HH:MM:SS";
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 )
143 SvNumFormatType nType_ = pFormatter->GetType( nIndex );
144 if(!(nType_ & ( SvNumFormatType::DATETIME | SvNumFormatType::DATE |
145 SvNumFormatType::TIME | SvNumFormatType::DEFINED )))
147 bSuccess = false;
151 if ( !bSuccess )
153 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
156 #else
157 nRes = 0;
158 #endif
159 break;
160 case SbxOBJECT:
161 pVal = dynamic_cast<SbxValue*>( p->pObj );
162 if( pVal )
164 nRes = pVal->GetDate();
166 else
168 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT ); nRes = 0;
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:
208 SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); nRes = 0;
209 break;
211 return nRes;
214 void ImpPutDate( SbxValues* p, double n )
216 SbxValues aTmp;
217 SbxDecimal* pDec;
218 SbxValue* pVal;
220 start:
221 switch( +p->eType )
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 ) )
263 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
265 break;
266 direct:
267 aTmp.eType = SbxDataType( p->eType | SbxBYREF );
268 p = &aTmp; goto start;
270 case SbxBYREF | SbxSTRING:
271 case SbxSTRING:
272 case SbxLPSTR:
274 #if HAVE_FEATURE_SCRIPTING
275 if( !p->pOUString )
277 p->pOUString = new OUString;
279 const Color* pColor;
281 LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType();
282 std::shared_ptr<SvNumberFormatter> pFormatter;
283 if (GetSbData()->pInst)
285 pFormatter = GetSbData()->pInst->GetNumberFormatter();
287 else
289 sal_uInt32 nDummy;
290 pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy );
293 sal_uInt32 nIndex;
294 sal_Int32 nCheckPos = 0;
295 SvNumFormatType nType;
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 )
303 // Time only if != 00:00:00
304 if( rtl::math::approxEqual(floor( n ), n) )
306 switch( eDate )
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;
314 else
316 switch( eDate )
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;
325 else
327 aStr = "HH:MM:SS";
329 pFormatter->PutandConvertEntry( aStr,
330 nCheckPos,
331 nType,
332 nIndex,
333 LANGUAGE_ENGLISH_US,
334 eLangType, true);
335 pFormatter->GetOutputString( n, nIndex, *p->pOUString, &pColor );
336 #endif
337 break;
339 case SbxOBJECT:
340 pVal = dynamic_cast<SbxValue*>( p->pObj );
341 if( pVal )
343 pVal->PutDate( n );
345 else
347 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
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 )
373 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXSNG;
375 else if( n < SbxMINSNG )
377 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINSNG;
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 )
394 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCURR;
396 else if( n < SbxMINCURR )
398 SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCURR;
400 *p->pnInt64 = ImpDoubleToCurrency( n );
401 break;
402 default:
403 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
404 break;
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */