Line data Source code
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 <unotools/datetime.hxx>
21 : #include <tools/date.hxx>
22 : #include <tools/time.hxx>
23 : #include <tools/datetime.hxx>
24 : #include <rtl/ustrbuf.hxx>
25 : #include <rtl/math.hxx>
26 : #include <osl/diagnose.h>
27 :
28 : namespace
29 : {
30 : /** convert string to number with optional min and max values */
31 : template <typename T>
32 54 : bool convertNumber( T& rValue,
33 : const OUString& rString,
34 : T /*nMin*/ = -1, T /*nMax*/ = -1)
35 : {
36 54 : bool bNeg = false;
37 54 : rValue = 0;
38 :
39 54 : sal_Int32 nPos = 0L;
40 54 : sal_Int32 nLen = rString.getLength();
41 :
42 : // skip white space
43 108 : while( nPos < nLen && ' ' == rString[nPos] )
44 0 : nPos++;
45 :
46 54 : if( nPos < nLen && '-' == rString[nPos] )
47 : {
48 0 : bNeg = true;
49 0 : nPos++;
50 : }
51 :
52 : // get number
53 486 : while( nPos < nLen &&
54 126 : '0' <= rString[nPos] &&
55 126 : '9' >= rString[nPos] )
56 : {
57 : // TODO: check overflow!
58 126 : rValue *= 10;
59 126 : rValue += (rString[nPos] - sal_Unicode('0'));
60 126 : nPos++;
61 : }
62 :
63 54 : if( bNeg )
64 0 : rValue *= -1;
65 :
66 54 : return nPos == nLen;
67 : }
68 :
69 : // although the standard calls for fixed-length (zero-padded) tokens
70 : // (in their integer part), we are here liberal and allow shorter tokens
71 : // (when there are separators, else it is ambiguous).
72 : // Note that:
73 : // the token separator is OPTIONAL
74 : // empty string is a valid token! (to recognise hh or hhmm or hh:mm formats)
75 : // returns: success / failure
76 : // in case of failure, no reference argument is changed
77 : // arguments:
78 : // i_str: string to extract token from
79 : // index: index in i_str where to start tokenizing
80 : // after return, start of *next* token (if any)
81 : // if this was the last token, then the value is UNDEFINED
82 : // o_strInt: output; integer part of token
83 : // o_bFraction: output; was there a fractional part?
84 : // o_strFrac: output; fractional part of token
85 27 : bool impl_getISO8601TimeToken(const OUString &i_str, sal_Int32 &nPos, OUString &resInt, bool &bFraction, OUString &resFrac)
86 : {
87 27 : bFraction = false;
88 : // all tokens are of length 2
89 27 : const sal_Int32 nEndPos = nPos + 2;
90 27 : const sal_Unicode c0 = '0';
91 27 : const sal_Unicode c9 = '9';
92 27 : const sal_Unicode sep = ':';
93 81 : for (;nPos < nEndPos && nPos < i_str.getLength(); ++nPos)
94 : {
95 54 : const sal_Unicode c = i_str[nPos];
96 54 : if (c == sep)
97 0 : return true;
98 54 : if (c < c0 || c > c9)
99 0 : return false;
100 54 : resInt += OUString(c);
101 : }
102 27 : if (nPos == i_str.getLength() || i_str[nPos] == sep)
103 18 : return true;
104 9 : if (i_str[nPos] == ',' || i_str[nPos] == '.')
105 : {
106 0 : bFraction = true;
107 0 : ++nPos;
108 0 : for (; nPos < i_str.getLength(); ++nPos)
109 : {
110 0 : const sal_Unicode c = i_str[nPos];
111 0 : if (c == 'Z' || c == '+' || c == '-')
112 : {
113 0 : --nPos; // we don't want to skip the tz separator
114 0 : return true;
115 : }
116 0 : if (c == sep)
117 : // fractional part allowed only in *last* token
118 0 : return false;
119 0 : if (c < c0 || c > c9)
120 0 : return false;
121 0 : resFrac += OUString(c);
122 : }
123 : OSL_ENSURE(nPos == i_str.getLength(), "impl_getISO8601TimeToken internal error; expected to be at end of string");
124 0 : return true;
125 : }
126 9 : if (i_str[nPos] == 'Z' || i_str[nPos] == '+' || i_str[nPos] == '-')
127 : {
128 9 : --nPos; // we don't want to skip the tz separator
129 9 : return true;
130 : }
131 : else
132 0 : return false;
133 : }
134 27 : inline bool getISO8601TimeToken(const OUString &i_str, sal_Int32 &io_index, OUString &o_strInt, bool &o_bFraction, OUString &o_strFrac)
135 : {
136 27 : OUString resInt;
137 54 : OUString resFrac;
138 27 : bool bFraction = false;
139 27 : sal_Int32 index = io_index;
140 27 : if(!impl_getISO8601TimeToken(i_str, index, resInt, bFraction, resFrac))
141 0 : return false;
142 : else
143 : {
144 27 : io_index = index+1;
145 27 : o_strInt = resInt;
146 27 : o_strFrac = resFrac;
147 27 : o_bFraction = bFraction;
148 27 : return true;
149 27 : }
150 : }
151 9 : inline bool getISO8601TimeZoneToken(const OUString &i_str, sal_Int32 &io_index, OUString &o_strInt)
152 : {
153 9 : const sal_Unicode c0 = '0';
154 9 : const sal_Unicode c9 = '9';
155 9 : const sal_Unicode sep = ':';
156 9 : if (i_str[io_index] == 'Z') // UTC timezone indicator
157 : {
158 9 : ++io_index;
159 9 : o_strInt = "Z";
160 9 : return true;
161 : }
162 0 : else if (i_str[io_index] == '+' || i_str[io_index] == '-') // other timezones indicator
163 : {
164 0 : ++io_index;
165 0 : o_strInt.clear();
166 0 : for (; io_index < i_str.getLength(); ++io_index)
167 : {
168 0 : const sal_Unicode c = i_str[io_index];
169 0 : if ((c < c0 || c > c9) && c != sep)
170 0 : return false;
171 0 : o_strInt += OUString(c);
172 : }
173 0 : return true;
174 : }
175 : else
176 0 : return false;
177 : }
178 : }
179 :
180 : namespace utl
181 : {
182 :
183 0 : void typeConvert(const Date& _rDate, css::util::Date& _rOut)
184 : {
185 0 : _rOut.Day = _rDate.GetDay();
186 0 : _rOut.Month = _rDate.GetMonth();
187 0 : _rOut.Year = _rDate.GetYear();
188 0 : }
189 :
190 0 : void typeConvert(const css::util::Date& _rDate, Date& _rOut)
191 : {
192 0 : _rOut = Date(_rDate.Day, _rDate.Month, _rDate.Year);
193 0 : }
194 :
195 0 : void typeConvert(const DateTime& _rDateTime, css::util::DateTime& _rOut)
196 : {
197 0 : _rOut.Year = _rDateTime.GetYear();
198 0 : _rOut.Month = _rDateTime.GetMonth();
199 0 : _rOut.Day = _rDateTime.GetDay();
200 0 : _rOut.Hours = _rDateTime.GetHour();
201 0 : _rOut.Minutes = _rDateTime.GetMin();
202 0 : _rOut.Seconds = _rDateTime.GetSec();
203 0 : _rOut.NanoSeconds = _rDateTime.GetNanoSec();
204 0 : }
205 :
206 0 : void typeConvert(const css::util::DateTime& _rDateTime, DateTime& _rOut)
207 : {
208 0 : Date aDate(_rDateTime.Day, _rDateTime.Month, _rDateTime.Year);
209 0 : tools::Time aTime(_rDateTime.Hours, _rDateTime.Minutes, _rDateTime.Seconds, _rDateTime.NanoSeconds);
210 0 : _rOut = DateTime(aDate, aTime);
211 0 : }
212 :
213 9 : void extractDate(const css::util::DateTime& _rDateTime, css::util::Date& _rOut)
214 : {
215 9 : _rOut.Day = _rDateTime.Day;
216 9 : _rOut.Month = _rDateTime.Month;
217 9 : _rOut.Year = _rDateTime.Year;
218 9 : }
219 :
220 0 : OUString toISO8601(const css::util::DateTime& rDateTime)
221 : {
222 0 : OUStringBuffer rBuffer;
223 0 : rBuffer.append((sal_Int32) rDateTime.Year);
224 0 : rBuffer.append('-');
225 0 : if( rDateTime.Month < 10 )
226 0 : rBuffer.append('0');
227 0 : rBuffer.append((sal_Int32) rDateTime.Month);
228 0 : rBuffer.append('-');
229 0 : if( rDateTime.Day < 10 )
230 0 : rBuffer.append('0');
231 0 : rBuffer.append((sal_Int32) rDateTime.Day);
232 :
233 0 : if( rDateTime.NanoSeconds != 0 ||
234 0 : rDateTime.Seconds != 0 ||
235 0 : rDateTime.Minutes != 0 ||
236 0 : rDateTime.Hours != 0 )
237 : {
238 0 : rBuffer.append('T');
239 0 : if( rDateTime.Hours < 10 )
240 0 : rBuffer.append('0');
241 0 : rBuffer.append((sal_Int32) rDateTime.Hours);
242 0 : rBuffer.append(':');
243 0 : if( rDateTime.Minutes < 10 )
244 0 : rBuffer.append('0');
245 0 : rBuffer.append((sal_Int32) rDateTime.Minutes);
246 0 : rBuffer.append(':');
247 0 : if( rDateTime.Seconds < 10 )
248 0 : rBuffer.append('0');
249 0 : rBuffer.append((sal_Int32) rDateTime.Seconds);
250 0 : if ( rDateTime.NanoSeconds > 0)
251 : {
252 : OSL_ENSURE(rDateTime.NanoSeconds < 1000000000,"NanoSeconds cannot be more than 999 999 999");
253 0 : rBuffer.append(',');
254 0 : std::ostringstream ostr;
255 0 : ostr.fill('0');
256 0 : ostr.width(9);
257 0 : ostr << rDateTime.NanoSeconds;
258 0 : rBuffer.append(OUString::createFromAscii(ostr.str().c_str()));
259 : }
260 : }
261 0 : return rBuffer.makeStringAndClear();
262 : }
263 :
264 : /** convert ISO8601 DateTime String to util::DateTime */
265 18 : bool ISO8601parseDateTime(const OUString &rString, css::util::DateTime& rDateTime)
266 : {
267 18 : bool bSuccess = true;
268 :
269 36 : rtl::OUString aDateStr, aTimeStr;
270 18 : css::util::Date aDate;
271 18 : css::util::Time aTime;
272 18 : sal_Int32 nPos = rString.indexOf( 'T' );
273 18 : if ( nPos >= 0 )
274 : {
275 9 : aDateStr = rString.copy( 0, nPos );
276 9 : aTimeStr = rString.copy( nPos + 1 );
277 : }
278 : else
279 9 : aDateStr = rString; // no separator: only date part
280 :
281 18 : bSuccess = ISO8601parseDate(aDateStr, aDate);
282 :
283 18 : if ( bSuccess && !aTimeStr.isEmpty() ) // time is optional
284 : {
285 9 : bSuccess = ISO8601parseTime(aTimeStr, aTime);
286 : }
287 :
288 18 : if (bSuccess)
289 : {
290 : rDateTime = css::util::DateTime(aTime.NanoSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours,
291 9 : aDate.Day, aDate.Month, aDate.Year, false);
292 : }
293 :
294 36 : return bSuccess;
295 : }
296 :
297 : /** convert ISO8601 Date String to util::Date */
298 : // TODO: supports only calendar dates YYYY-MM-DD
299 : // MISSING: calendar dates YYYYMMDD YYYY-MM
300 : // year, week date, ordinal date
301 18 : bool ISO8601parseDate(const OUString &aDateStr, css::util::Date& rDate)
302 : {
303 18 : bool bSuccess = true;
304 :
305 18 : sal_Int32 nYear = 1899;
306 18 : sal_Int32 nMonth = 12;
307 18 : sal_Int32 nDay = 30;
308 :
309 18 : const sal_Unicode* pStr = aDateStr.getStr();
310 18 : sal_Int32 nDateTokens = 1;
311 126 : while ( *pStr )
312 : {
313 90 : if ( *pStr == '-' )
314 18 : nDateTokens++;
315 90 : pStr++;
316 : }
317 18 : if ( nDateTokens > 3 || aDateStr.isEmpty() )
318 9 : bSuccess = false;
319 : else
320 : {
321 9 : sal_Int32 n = 0;
322 9 : if ( !convertNumber<sal_Int32>( nYear, aDateStr.getToken( 0, '-', n ), 0, 9999 ) )
323 0 : bSuccess = false;
324 9 : if ( nDateTokens >= 2 )
325 9 : if ( !convertNumber<sal_Int32>( nMonth, aDateStr.getToken( 0, '-', n ), 0, 12 ) )
326 0 : bSuccess = false;
327 9 : if ( nDateTokens >= 3 )
328 9 : if ( !convertNumber<sal_Int32>( nDay, aDateStr.getToken( 0, '-', n ), 0, 31 ) )
329 0 : bSuccess = false;
330 : }
331 :
332 18 : if (bSuccess)
333 : {
334 9 : rDate.Year = (sal_uInt16)nYear;
335 9 : rDate.Month = (sal_uInt16)nMonth;
336 9 : rDate.Day = (sal_uInt16)nDay;
337 : }
338 :
339 18 : return bSuccess;
340 : }
341 :
342 : /** convert ISO8601 Time String to util::Time */
343 9 : bool ISO8601parseTime(const OUString &aTimeStr, css::util::Time& rTime)
344 : {
345 9 : bool bSuccess = true;
346 :
347 9 : sal_Int32 nHour = 0;
348 9 : sal_Int32 nMin = 0;
349 9 : sal_Int32 nSec = 0;
350 9 : sal_Int32 nNanoSec = 0;
351 :
352 9 : sal_Int32 n = 0;
353 9 : OUString tokInt;
354 18 : OUString tokFrac;
355 18 : OUString tokTz;
356 9 : bool bFrac = false;
357 : // hours
358 9 : if (bSuccess && (bSuccess = getISO8601TimeToken(aTimeStr, n, tokInt, bFrac, tokFrac)))
359 : {
360 9 : if ( bFrac && n < aTimeStr.getLength())
361 : // is it junk or the timezone?
362 0 : bSuccess = getISO8601TimeZoneToken(aTimeStr, n, tokTz);
363 9 : if (bSuccess && (bSuccess = convertNumber<sal_Int32>( nHour, tokInt, 0, 23 )) )
364 : {
365 9 : if (bFrac)
366 : {
367 : sal_Int64 fracNumerator;
368 0 : if ( (bSuccess = convertNumber(fracNumerator, tokFrac)) )
369 : {
370 0 : double frac = static_cast<double>(fracNumerator) / pow(static_cast<double>(10), static_cast<double>(tokFrac.getLength()));
371 : // minutes
372 : OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac hours (of hours) not between 0 and 1");
373 0 : frac *= 60;
374 0 : nMin = floor(frac);
375 0 : frac -= nMin;
376 : // seconds
377 : OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac minutes (of hours) not between 0 and 1");
378 0 : frac *= 60;
379 0 : nSec = floor(frac);
380 0 : frac -= nSec;
381 : // nanoseconds
382 : OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac seconds (of hours) not between 0 and 1");
383 0 : frac *= 1000000000;
384 0 : nNanoSec = ::rtl::math::round(frac);
385 : }
386 0 : goto end;
387 : }
388 9 : if(n >= aTimeStr.getLength())
389 0 : goto end;
390 : }
391 : }
392 :
393 : // minutes
394 9 : if (bSuccess && (bSuccess = getISO8601TimeToken(aTimeStr, n, tokInt, bFrac, tokFrac)))
395 : {
396 9 : if ( bFrac && n < aTimeStr.getLength())
397 : // is it junk or the timezone?
398 0 : bSuccess = getISO8601TimeZoneToken(aTimeStr, n, tokTz);
399 9 : if (bSuccess && (bSuccess = convertNumber<sal_Int32>( nMin, tokInt, 0, 59 )) )
400 : {
401 9 : if (bFrac)
402 : {
403 : sal_Int64 fracNumerator;
404 0 : if ( (bSuccess = convertNumber(fracNumerator, tokFrac)) )
405 : {
406 0 : double frac = static_cast<double>(fracNumerator) / pow(static_cast<double>(10), static_cast<double>(tokFrac.getLength()));
407 : // seconds
408 : OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac minutes (of minutes) not between 0 and 1");
409 0 : frac *= 60;
410 0 : nSec = floor(frac);
411 0 : frac -= nSec;
412 : // nanoseconds
413 : OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac seconds (of minutes) not between 0 and 1");
414 0 : frac *= 1000000000;
415 0 : nNanoSec = ::rtl::math::round(frac);
416 : }
417 0 : goto end;
418 : }
419 9 : if(n >= aTimeStr.getLength())
420 0 : goto end;
421 : }
422 : }
423 : // seconds
424 9 : if (bSuccess && (bSuccess = getISO8601TimeToken(aTimeStr, n, tokInt, bFrac, tokFrac)))
425 : {
426 9 : if (n < aTimeStr.getLength())
427 : // is it junk or the timezone?
428 9 : bSuccess = getISO8601TimeZoneToken(aTimeStr, n, tokTz);
429 : // max 60 for leap seconds
430 9 : if (bSuccess && (bSuccess = convertNumber<sal_Int32>( nSec, tokInt, 0, 60 )) )
431 : {
432 9 : if (bFrac)
433 : {
434 : sal_Int64 fracNumerator;
435 0 : if ( (bSuccess = convertNumber(fracNumerator, tokFrac)) )
436 : {
437 0 : double frac = static_cast<double>(fracNumerator) / pow(static_cast<double>(10), static_cast<double>(tokFrac.getLength()));
438 : // nanoseconds
439 : OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac seconds (of seconds) not between 0 and 1");
440 0 : frac *= 1000000000;
441 0 : nNanoSec = ::rtl::math::round(frac);
442 : }
443 0 : goto end;
444 : }
445 : }
446 : }
447 :
448 : end:
449 9 : if (bSuccess)
450 : {
451 : // normalise time
452 9 : const int secondsOverflow = (nSec == 60) ? 61 : 60;
453 9 : if (nNanoSec == 1000000000)
454 : {
455 0 : nNanoSec = 0;
456 0 : ++nSec;
457 : }
458 9 : if(nSec == secondsOverflow)
459 : {
460 0 : nSec = 0;
461 0 : ++nMin;
462 : }
463 9 : if(nMin == 60)
464 : {
465 0 : nMin = 0;
466 0 : ++nHour;
467 : }
468 9 : if(!tokTz.isEmpty())
469 9 : rTime.IsUTC = (tokTz == "Z");
470 :
471 9 : rTime.Hours = (sal_uInt16)nHour;
472 9 : rTime.Minutes = (sal_uInt16)nMin;
473 9 : rTime.Seconds = (sal_uInt16)nSec;
474 9 : rTime.NanoSeconds = nNanoSec;
475 : }
476 :
477 18 : return bSuccess;
478 : }
479 :
480 : } // namespace utl
481 :
482 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|