Debian Codegraph @

basic/source/sbx/sbxdate.cxx

raw
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
36 double ImpGetDate( const SbxValues* p )
37 {
38 switch( +p->eType )
39 {
40 case SbxEMPTY:
41 return 0;
42 case SbxCHAR:
43 return p->nChar;
44 case SbxBYTE:
45 return p->nByte;
46 case SbxINTEGER:
47 case SbxBOOL:
48 return p->nInteger;
49 case SbxERROR:
50 case SbxUSHORT:
51 return p->nUShort;
52 case SbxLONG:
53 return p->nLong;
54 case SbxULONG:
55 return p->nULong;
56 case SbxSINGLE:
57 return p->nSingle;
58 case SbxDATE:
59 case SbxDOUBLE:
60 return p->nDouble;
61 case SbxCURRENCY:
62 return CurTo<double>(p->nInt64);
63 case SbxSALINT64:
64 return static_cast<double>(p->nInt64);
65 case SbxSALUINT64:
66 return static_cast<double>(p->uInt64);
67 case SbxDECIMAL:
68 case SbxBYREF | SbxDECIMAL:
69 if (p->pDecimal)
70 if (double d; p->pDecimal->getDouble(d))
71 return d;
72 return 0;
73 case SbxBYREF | SbxSTRING:
74 case SbxSTRING:
75 case SbxLPSTR:
76 #if HAVE_FEATURE_SCRIPTING
77 if (p->pOUString)
78 {
79 LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType();
80 std::shared_ptr<SvNumberFormatter> pFormatter;
81 if (GetSbData()->pInst)
82 {
83 pFormatter = GetSbData()->pInst->GetNumberFormatter();
84 }
85 else
86 {
87 sal_uInt32 nDummy;
88 pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy );
89 }
90
91 sal_uInt32 nIndex;
92 sal_Int32 nCheckPos = 0;
93 SvNumFormatType nType = SvNumFormatType::DEFINED | SvNumFormatType::DATE | SvNumFormatType::TIME | SvNumFormatType::CURRENCY
94 | SvNumFormatType::NUMBER | SvNumFormatType::SCIENTIFIC | SvNumFormatType::FRACTION;
95
96 // Default templates of the formatter have only two-digit
97 // date. Therefore register an own format.
98
99 // HACK, because the number formatter in PutandConvertEntry replace the wildcard
100 // for month, day, year not according to the configuration.
101 // Problem: Print Year(Date) under Engl. OS
102 // quod vide basic/source/runtime/runtime.cxx
103
104 SvtSysLocale aSysLocale;
105 DateOrder eDate = aSysLocale.GetLocaleData().getDateOrder();
106 OUString aDateStr;
107 switch( eDate )
108 {
109 default:
110 case DateOrder::MDY: aDateStr = "MM/DD/YYYY"; break;
111 case DateOrder::DMY: aDateStr = "DD/MM/YYYY"; break;
112 case DateOrder::YMD: aDateStr = "YYYY/MM/DD"; break;
113 }
114
115 OUString aStr = aDateStr + " HH:MM:SS";
116
117 pFormatter->PutandConvertEntry( aStr, nCheckPos, nType,
118 nIndex, LANGUAGE_ENGLISH_US, eLangType, true);
119 if (double d; pFormatter->IsNumberFormat(*p->pOUString, nIndex, d))
120 {
121 SvNumFormatType nType_ = pFormatter->GetType( nIndex );
122 if(nType_ & ( SvNumFormatType::DATETIME | SvNumFormatType::DATE |
123 SvNumFormatType::TIME | SvNumFormatType::DEFINED ))
124 {
125 return d;
126 }
127 }
128
129 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
130 }
131 #endif
132 return 0;
133 case SbxOBJECT:
134 if (SbxValue* pVal = dynamic_cast<SbxValue*>(p->pObj))
135 return pVal->GetDate();
136 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
137 return 0;
138 case SbxBYREF | SbxCHAR:
139 return *p->pChar;
140 case SbxBYREF | SbxBYTE:
141 return *p->pByte;
142 case SbxBYREF | SbxINTEGER:
143 case SbxBYREF | SbxBOOL:
144 return *p->pInteger;
145 case SbxBYREF | SbxLONG:
146 return *p->pLong;
147 case SbxBYREF | SbxULONG:
148 return *p->pULong;
149 case SbxBYREF | SbxERROR:
150 case SbxBYREF | SbxUSHORT:
151 return *p->pUShort;
152 case SbxBYREF | SbxSINGLE:
153 return *p->pSingle;
154 case SbxBYREF | SbxDATE:
155 case SbxBYREF | SbxDOUBLE:
156 return *p->pDouble;
157 case SbxBYREF | SbxCURRENCY:
158 return CurTo<double>(*p->pnInt64);
159 case SbxBYREF | SbxSALINT64:
160 return static_cast<double>(*p->pnInt64);
161 case SbxBYREF | SbxSALUINT64:
162 return static_cast<double>(*p->puInt64);
163 default:
164 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
165 return 0;
166 }
167 }
168
169 void ImpPutDate( SbxValues* p, double n )
170 {
171 switch( +p->eType )
172 {
173 case SbxDATE:
174 case SbxDOUBLE:
175 p->nDouble = n;
176 break;
177 case SbxBYREF | SbxDATE:
178 case SbxBYREF | SbxDOUBLE:
179 *p->pDouble = n;
180 break;
181 case SbxCHAR:
182 assignWithOverflowTo(p->nChar, n);
183 break;
184 case SbxBYREF | SbxCHAR:
185 assignWithOverflowTo(*p->pChar, n);
186 break;
187 case SbxBYTE:
188 assignWithOverflowTo(p->nByte, n);
189 break;
190 case SbxBYREF | SbxBYTE:
191 assignWithOverflowTo(*p->pByte, n);
192 break;
193 case SbxINTEGER:
194 case SbxBOOL:
195 assignWithOverflowTo(p->nInteger, n);
196 break;
197 case SbxBYREF | SbxINTEGER:
198 case SbxBYREF | SbxBOOL:
199 assignWithOverflowTo(*p->pInteger, n);
200 break;
201 case SbxERROR:
202 case SbxUSHORT:
203 assignWithOverflowTo(p->nUShort, n);
204 break;
205 case SbxBYREF | SbxERROR:
206 case SbxBYREF | SbxUSHORT:
207 assignWithOverflowTo(*p->pUShort, n);
208 break;
209 case SbxLONG:
210 assignWithOverflowTo(p->nLong, n);
211 break;
212 case SbxBYREF | SbxLONG:
213 assignWithOverflowTo(*p->pLong, n);
214 break;
215 case SbxULONG:
216 assignWithOverflowTo(p->nULong, n);
217 break;
218 case SbxBYREF | SbxULONG:
219 assignWithOverflowTo(*p->pULong, n);
220 break;
221 case SbxCURRENCY:
222 assignWithOverflowTo(p->nInt64, CurFrom(n));
223 break;
224 case SbxBYREF | SbxCURRENCY:
225 assignWithOverflowTo(*p->pnInt64, CurFrom(n));
226 break;
227 case SbxSALINT64:
228 assignWithOverflowTo(p->nInt64, n);
229 break;
230 case SbxBYREF | SbxSALINT64:
231 assignWithOverflowTo(*p->pnInt64, n);
232 break;
233 case SbxSALUINT64:
234 assignWithOverflowTo(p->uInt64, n);
235 break;
236 case SbxBYREF | SbxSALUINT64:
237 assignWithOverflowTo(*p->puInt64, n);
238 break;
239 case SbxSINGLE:
240 assignWithOverflowTo(p->nSingle, n);
241 break;
242 case SbxBYREF | SbxSINGLE:
243 assignWithOverflowTo(*p->pSingle, n);
244 break;
245
246 case SbxDECIMAL:
247 case SbxBYREF | SbxDECIMAL:
248 ImpCreateDecimal(p)->setWithOverflow(n);
249 break;
250
251 case SbxBYREF | SbxSTRING:
252 case SbxSTRING:
253 case SbxLPSTR:
254 {
255 #if HAVE_FEATURE_SCRIPTING
256 if( !p->pOUString )
257 {
258 p->pOUString = new OUString;
259 }
260 const Color* pColor;
261
262 LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType();
263 std::shared_ptr<SvNumberFormatter> pFormatter;
264 if (GetSbData()->pInst)
265 {
266 pFormatter = GetSbData()->pInst->GetNumberFormatter();
267 }
268 else
269 {
270 sal_uInt32 nDummy;
271 pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy );
272 }
273
274 sal_uInt32 nIndex;
275 sal_Int32 nCheckPos = 0;
276 SvNumFormatType nType;
277
278 SvtSysLocale aSysLocale;
279 DateOrder eDate = aSysLocale.GetLocaleData().getDateOrder();
280 OUString aStr;
281 // if the whole-number part is 0, we want no year!
282 if( n <= -1.0 || n >= 1.0 )
283 {
284 // Time only if != 00:00:00
285 if( rtl::math::approxEqual(floor( n ), n) )
286 {
287 switch( eDate )
288 {
289 default:
290 case DateOrder::MDY: aStr = "MM/DD/YYYY"; break;
291 case DateOrder::DMY: aStr = "DD/MM/YYYY"; break;
292 case DateOrder::YMD: aStr = "YYYY/MM/DD"; break;
293 }
294 }
295 else
296 {
297 switch( eDate )
298 {
299 default:
300 case DateOrder::MDY: aStr = "MM/DD/YYYY HH:MM:SS"; break;
301 case DateOrder::DMY: aStr = "DD/MM/YYYY HH:MM:SS"; break;
302 case DateOrder::YMD: aStr = "YYYY/MM/DD HH:MM:SS"; break;
303 }
304 }
305 }
306 else
307 {
308 aStr = "HH:MM:SS";
309 }
310 pFormatter->PutandConvertEntry( aStr,
311 nCheckPos,
312 nType,
313 nIndex,
314 LANGUAGE_ENGLISH_US,
315 eLangType, true);
316 pFormatter->GetOutputString( n, nIndex, *p->pOUString, &pColor );
317 #endif
318 break;
319 }
320
321 case SbxOBJECT:
322 if (auto pVal = dynamic_cast<SbxValue*>(p->pObj))
323 {
324 pVal->PutDate( n );
325 }
326 else
327 {
328 SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
329 }
330 break;
331
332 default:
333 SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
334 break;
335 }
336 }
337
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */