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 <algorithm>
21 :
22 : #include "calendar_gregorian.hxx"
23 : #include "localedata.hxx"
24 : #include <com/sun/star/i18n/AmPmValue.hpp>
25 : #include <com/sun/star/i18n/Months.hpp>
26 : #include <com/sun/star/i18n/Weekdays.hpp>
27 : #include <com/sun/star/i18n/reservedWords.hpp>
28 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 : #include <comphelper/processfactory.hxx>
30 : #include <cppuhelper/supportsservice.hxx>
31 : #include <rtl/math.hxx>
32 :
33 : #include <stdio.h>
34 : #include <string.h>
35 :
36 : #define erDUMP_ICU_CALENDAR 0
37 : #define erDUMP_I18N_CALENDAR 0
38 : #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
39 : // If both are used, DUMP_ICU_CAL_MSG() must be used before DUMP_I18N_CAL_MSG()
40 : // to obtain internally set values from ICU, else Calendar::get() calls in
41 : // DUMP_I18N_CAL_MSG() recalculate!
42 :
43 : // These pieces of macro are shamelessly borrowed from icu's olsontz.cpp, the
44 : // double parens'ed approach to pass multiple parameters as one macro parameter
45 : // is appealing.
46 : static void debug_cal_loc(const char *f, int32_t l)
47 : {
48 : fprintf(stderr, "%s:%d: ", f, l);
49 : }
50 : # include <stdarg.h>
51 : static void debug_cal_msg(const char *pat, ...)
52 : {
53 : va_list ap;
54 : va_start(ap, pat);
55 : vfprintf(stderr, pat, ap);
56 : va_end(ap);
57 : }
58 :
59 : #if erDUMP_ICU_CALENDAR
60 : // Make icu with
61 : // DEFS = -DU_DEBUG_CALSVC -DUCAL_DEBUG_DUMP
62 : // in workdir/UnpackedTarball/icu/source/icudefs.mk
63 : // May need some patches to fix unmaintained things there.
64 : extern void ucal_dump( const icu::Calendar & );
65 : static void debug_icu_cal_dump( const ::icu::Calendar & r )
66 : {
67 : ucal_dump(r);
68 : fflush(stderr);
69 : // set a breakpoint here to pause display between dumps
70 : }
71 : // must use double parens, i.e.: DUMP_ICU_CAL_MSG(("four is: %d",4));
72 : #define DUMP_ICU_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_icu_cal_dump(*body);}
73 : #else // erDUMP_ICU_CALENDAR
74 : #define DUMP_ICU_CAL_MSG(x)
75 : #endif // erDUMP_ICU_CALENDAR
76 :
77 : #if erDUMP_I18N_CALENDAR
78 : static void debug_cal_millis_to_time( long nMillis, long & h, long & m, long & s, long & f )
79 : {
80 : int sign = (nMillis < 0 ? -1 : 1);
81 : nMillis = ::std::abs(nMillis);
82 : h = sign * nMillis / (60 * 60 * 1000);
83 : nMillis -= sign * h * (60 * 60 * 1000);
84 : m = nMillis / (60 * 1000);
85 : nMillis -= m * (60 * 1000);
86 : s = nMillis / (1000);
87 : nMillis -= s * (1000);
88 : f = nMillis;
89 : }
90 : static void debug_i18n_cal_dump( const ::icu::Calendar & r )
91 : {
92 : UErrorCode status;
93 : long nMillis, h, m, s, f;
94 : fprintf( stderr, " %04ld", (long)r.get( UCAL_YEAR, status = U_ZERO_ERROR));
95 : fprintf( stderr, "-%02ld", (long)r.get( UCAL_MONTH, status = U_ZERO_ERROR)+1);
96 : fprintf( stderr, "-%02ld", (long)r.get( UCAL_DATE, status = U_ZERO_ERROR));
97 : fprintf( stderr, " %02ld", (long)r.get( UCAL_HOUR_OF_DAY, status = U_ZERO_ERROR));
98 : fprintf( stderr, ":%02ld", (long)r.get( UCAL_MINUTE, status = U_ZERO_ERROR));
99 : fprintf( stderr, ":%02ld", (long)r.get( UCAL_SECOND, status = U_ZERO_ERROR));
100 : fprintf( stderr, " zone: %ld", (long)(nMillis = r.get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR)));
101 : fprintf( stderr, " (%f min)", (double)nMillis / 60000);
102 : debug_cal_millis_to_time( nMillis, h, m, s, f);
103 : fprintf( stderr, " (%ld:%02ld:%02ld.%ld)", h, m, s, f);
104 : fprintf( stderr, " DST: %ld", (long)(nMillis = r.get( UCAL_DST_OFFSET, status = U_ZERO_ERROR)));
105 : fprintf( stderr, " (%f min)", (double)nMillis / 60000);
106 : debug_cal_millis_to_time( nMillis, h, m, s, f);
107 : fprintf( stderr, " (%ld:%02ld:%02ld.%ld)", h, m, s, f);
108 : fprintf( stderr, "\n");
109 : fflush(stderr);
110 : }
111 : // must use double parens, i.e.: DUMP_I18N_CAL_MSG(("four is: %d",4));
112 : #define DUMP_I18N_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_i18n_cal_dump(*body);}
113 : #else // erDUMP_I18N_CALENDAR
114 : #define DUMP_I18N_CAL_MSG(x)
115 : #endif // erDUMP_I18N_CALENDAR
116 :
117 : #else // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
118 : #define DUMP_ICU_CAL_MSG(x)
119 : #define DUMP_I18N_CAL_MSG(x)
120 : #endif // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
121 :
122 :
123 : using namespace ::com::sun::star::uno;
124 : using namespace ::com::sun::star::lang;
125 :
126 :
127 : namespace com { namespace sun { namespace star { namespace i18n {
128 :
129 : #define ERROR RuntimeException()
130 :
131 400 : Calendar_gregorian::Calendar_gregorian()
132 : {
133 400 : init(NULL);
134 400 : }
135 4 : Calendar_gregorian::Calendar_gregorian(const Era *_earArray)
136 : {
137 4 : init(_earArray);
138 4 : }
139 : void SAL_CALL
140 404 : Calendar_gregorian::init(const Era *_eraArray)
141 : {
142 404 : cCalendar = "com.sun.star.i18n.Calendar_gregorian";
143 :
144 404 : fieldSet = 0;
145 :
146 : // #i102356# With icu::Calendar::createInstance(UErrorCode) in a Thai
147 : // th_TH system locale we accidentally used a Buddhist calendar. Though
148 : // the ICU documentation says that should be the case only for
149 : // th_TH_TRADITIONAL (and ja_JP_TRADITIONAL Gengou), a plain th_TH
150 : // already triggers that behavior, ja_JP does not. Strange enough,
151 : // passing a th_TH locale to the calendar creation doesn't trigger
152 : // this.
153 : // See also http://userguide.icu-project.org/datetime/calendar
154 :
155 : // Whatever ICU offers as the default calendar for a locale, ensure we
156 : // have a Gregorian calendar as requested.
157 :
158 : /* XXX: with the current implementation the aLocale member variable is
159 : * not set prior to loading a calendar from locale data. This
160 : * creates an empty (root) locale for ICU, but at least the correct
161 : * calendar is used. The language part must not be NULL (respectively
162 : * not all, language and country and variant), otherwise the current
163 : * default locale would be used again and the calendar keyword ignored.
164 : * */
165 404 : icu::Locale aIcuLocale( "", NULL, NULL, "calendar=gregorian");
166 :
167 : UErrorCode status;
168 404 : body = icu::Calendar::createInstance( aIcuLocale, status = U_ZERO_ERROR);
169 404 : if (!body || !U_SUCCESS(status)) throw ERROR;
170 404 : eraArray=_eraArray;
171 404 : }
172 :
173 1178 : Calendar_gregorian::~Calendar_gregorian()
174 : {
175 394 : delete body;
176 784 : }
177 :
178 0 : Calendar_hanja::Calendar_hanja()
179 : {
180 0 : cCalendar = "com.sun.star.i18n.Calendar_hanja";
181 0 : }
182 :
183 : OUString SAL_CALL
184 0 : Calendar_hanja::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType ) throw(RuntimeException, std::exception)
185 : {
186 0 : if ( displayIndex == CalendarDisplayIndex::AM_PM ) {
187 : // Am/Pm string for Korean Hanja calendar will refer to Japanese locale
188 : com::sun::star::lang::Locale jaLocale =
189 0 : com::sun::star::lang::Locale(OUString("ja"), OUString(), OUString());
190 0 : if (idx == 0) return LocaleDataImpl().getLocaleItem(jaLocale).timeAM;
191 0 : else if (idx == 1) return LocaleDataImpl().getLocaleItem(jaLocale).timePM;
192 0 : else throw ERROR;
193 : }
194 : else
195 0 : return Calendar_gregorian::getDisplayName( displayIndex, idx, nameType );
196 : }
197 :
198 : void SAL_CALL
199 0 : Calendar_hanja::loadCalendar( const OUString& /*uniqueID*/, const com::sun::star::lang::Locale& rLocale ) throw(RuntimeException, std::exception)
200 : {
201 : // Since this class could be called by service name 'hanja_yoil', we have to
202 : // rename uniqueID to get right calendar defined in locale data.
203 0 : Calendar_gregorian::loadCalendar(OUString("hanja"), rLocale);
204 0 : }
205 :
206 : static const Era gengou_eraArray[] = {
207 : {1868, 1, 1, 0},
208 : {1912, 7, 30, 0},
209 : {1926, 12, 25, 0},
210 : {1989, 1, 8, 0},
211 : {0, 0, 0, 0}
212 : };
213 0 : Calendar_gengou::Calendar_gengou() : Calendar_gregorian(gengou_eraArray)
214 : {
215 0 : cCalendar = "com.sun.star.i18n.Calendar_gengou";
216 0 : }
217 :
218 : static const Era ROC_eraArray[] = {
219 : {1912, 1, 1, kDisplayEraForcedLongYear}, // #i116701#
220 : {0, 0, 0, 0}
221 : };
222 2 : Calendar_ROC::Calendar_ROC() : Calendar_gregorian(ROC_eraArray)
223 : {
224 2 : cCalendar = "com.sun.star.i18n.Calendar_ROC";
225 2 : }
226 :
227 : static const Era buddhist_eraArray[] = {
228 : {-542, 1, 1, 0},
229 : {0, 0, 0, 0}
230 : };
231 2 : Calendar_buddhist::Calendar_buddhist() : Calendar_gregorian(buddhist_eraArray)
232 : {
233 2 : cCalendar = "com.sun.star.i18n.Calendar_buddhist";
234 2 : }
235 :
236 : void SAL_CALL
237 1382 : Calendar_gregorian::loadCalendar( const OUString& uniqueID, const com::sun::star::lang::Locale& rLocale ) throw(RuntimeException, std::exception)
238 : {
239 : // init. fieldValue[]
240 1382 : getValue();
241 :
242 1382 : aLocale = rLocale;
243 1382 : Sequence< Calendar2 > xC = LocaleDataImpl().getAllCalendars2(rLocale);
244 1394 : for (sal_Int32 i = 0; i < xC.getLength(); i++)
245 : {
246 1394 : if (uniqueID == xC[i].Name)
247 : {
248 1382 : aCalendar = xC[i];
249 : // setup minimalDaysInFirstWeek
250 : setMinimumNumberOfDaysForFirstWeek(
251 1382 : aCalendar.MinimumNumberOfDaysForFirstWeek);
252 : // setup first day of week
253 9662 : for (sal_Int16 day = sal::static_int_cast<sal_Int16>(
254 1382 : aCalendar.Days.getLength()-1); day>=0; day--)
255 : {
256 9662 : if (aCalendar.StartOfWeek == aCalendar.Days[day].ID)
257 : {
258 1382 : setFirstDayOfWeek( day);
259 2764 : return;
260 : }
261 : }
262 : }
263 : }
264 : // Calendar is not for the locale
265 0 : throw ERROR;
266 : }
267 :
268 :
269 : com::sun::star::i18n::Calendar2 SAL_CALL
270 0 : Calendar_gregorian::getLoadedCalendar2() throw(RuntimeException, std::exception)
271 : {
272 0 : return aCalendar;
273 : }
274 :
275 : com::sun::star::i18n::Calendar SAL_CALL
276 0 : Calendar_gregorian::getLoadedCalendar() throw(RuntimeException, std::exception)
277 : {
278 0 : return LocaleDataImpl::downcastCalendar( aCalendar);
279 : }
280 :
281 : OUString SAL_CALL
282 4508 : Calendar_gregorian::getUniqueID() throw(RuntimeException, std::exception)
283 : {
284 4508 : return aCalendar.Name;
285 : }
286 :
287 : void SAL_CALL
288 5688 : Calendar_gregorian::setDateTime( double fTimeInDays ) throw(RuntimeException, std::exception)
289 : {
290 : // ICU handles dates in milliseconds as double values and uses floor()
291 : // to obtain integer values, which may yield a date decremented by one
292 : // for odd (historical) timezone values where the computed value due to
293 : // rounding errors has a fractional part in milliseconds. Ensure we
294 : // pass a value without fraction here. If not, that may lead to
295 : // fdo#44286 or fdo#52619 and the like, e.g. when passing
296 : // -2136315212000.000244 instead of -2136315212000.000000
297 5688 : double fM = fTimeInDays * U_MILLIS_PER_DAY;
298 5688 : double fR = rtl::math::round( fM );
299 : SAL_INFO_IF( fM != fR, "i18npool",
300 : "Calendar_gregorian::setDateTime: " << std::fixed << fM << " rounded to " << fR);
301 : UErrorCode status;
302 5688 : body->setTime( fR, status = U_ZERO_ERROR);
303 5688 : if ( !U_SUCCESS(status) ) throw ERROR;
304 5688 : getValue();
305 5688 : }
306 :
307 : double SAL_CALL
308 260 : Calendar_gregorian::getDateTime() throw(RuntimeException, std::exception)
309 : {
310 260 : if (fieldSet) {
311 0 : setValue();
312 0 : getValue();
313 : }
314 : UErrorCode status;
315 260 : double fR = body->getTime(status = U_ZERO_ERROR);
316 260 : if ( !U_SUCCESS(status) ) throw ERROR;
317 260 : return fR / U_MILLIS_PER_DAY;
318 : }
319 :
320 : // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
321 : // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
322 7318 : void Calendar_gregorian::mapFromGregorian() throw(RuntimeException)
323 : {
324 7318 : if (eraArray) {
325 : sal_Int16 e, y, m, d;
326 :
327 24 : e = fieldValue[CalendarFieldIndex::ERA];
328 24 : y = fieldValue[CalendarFieldIndex::YEAR];
329 24 : m = fieldValue[CalendarFieldIndex::MONTH] + 1;
330 24 : d = fieldValue[CalendarFieldIndex::DAY_OF_MONTH];
331 :
332 : // since the year is reversed for first era, it is reversed again here for Era compare.
333 24 : if (e == 0)
334 0 : y = 1 - y;
335 :
336 48 : for (e = 0; eraArray[e].year; e++)
337 24 : if ((y != eraArray[e].year) ? y < eraArray[e].year :
338 0 : (m != eraArray[e].month) ? m < eraArray[e].month : d < eraArray[e].day)
339 0 : break;
340 :
341 24 : fieldValue[CalendarFieldIndex::ERA] = e;
342 : fieldValue[CalendarFieldIndex::YEAR] =
343 24 : sal::static_int_cast<sal_Int16>( (e == 0) ? (eraArray[0].year - y) : (y - eraArray[e-1].year + 1) );
344 : }
345 7318 : }
346 :
347 : #define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
348 : // map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
349 : // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
350 248 : void Calendar_gregorian::mapToGregorian() throw(RuntimeException)
351 : {
352 248 : if (eraArray && (fieldSet & FIELDS)) {
353 0 : sal_Int16 y, e = fieldValue[CalendarFieldIndex::ERA];
354 0 : if (e == 0)
355 0 : y = sal::static_int_cast<sal_Int16>( eraArray[0].year - fieldValue[CalendarFieldIndex::YEAR] );
356 : else
357 0 : y = sal::static_int_cast<sal_Int16>( eraArray[e-1].year + fieldValue[CalendarFieldIndex::YEAR] - 1 );
358 :
359 0 : fieldSetValue[CalendarFieldIndex::ERA] = y <= 0 ? 0 : 1;
360 0 : fieldSetValue[CalendarFieldIndex::YEAR] = (y <= 0 ? 1 - y : y);
361 0 : fieldSet |= FIELDS;
362 : }
363 248 : }
364 :
365 111492 : static UCalendarDateFields fieldNameConverter(sal_Int16 fieldIndex) throw(RuntimeException)
366 : {
367 : UCalendarDateFields f;
368 :
369 111492 : switch (fieldIndex) {
370 7318 : case CalendarFieldIndex::AM_PM: f = UCAL_AM_PM; break;
371 7892 : case CalendarFieldIndex::DAY_OF_MONTH: f = UCAL_DATE; break;
372 7318 : case CalendarFieldIndex::DAY_OF_WEEK: f = UCAL_DAY_OF_WEEK; break;
373 7318 : case CalendarFieldIndex::DAY_OF_YEAR: f = UCAL_DAY_OF_YEAR; break;
374 7318 : case CalendarFieldIndex::DST_OFFSET: f = UCAL_DST_OFFSET; break;
375 7318 : case CalendarFieldIndex::ZONE_OFFSET: f = UCAL_ZONE_OFFSET; break;
376 7318 : case CalendarFieldIndex::HOUR: f = UCAL_HOUR_OF_DAY; break;
377 7318 : case CalendarFieldIndex::MINUTE: f = UCAL_MINUTE; break;
378 7318 : case CalendarFieldIndex::SECOND: f = UCAL_SECOND; break;
379 7318 : case CalendarFieldIndex::MILLISECOND: f = UCAL_MILLISECOND; break;
380 7318 : case CalendarFieldIndex::WEEK_OF_MONTH: f = UCAL_WEEK_OF_MONTH; break;
381 7318 : case CalendarFieldIndex::WEEK_OF_YEAR: f = UCAL_WEEK_OF_YEAR; break;
382 7892 : case CalendarFieldIndex::YEAR: f = UCAL_YEAR; break;
383 7892 : case CalendarFieldIndex::MONTH: f = UCAL_MONTH; break;
384 7318 : case CalendarFieldIndex::ERA: f = UCAL_ERA; break;
385 0 : default: throw ERROR;
386 : }
387 111492 : return f;
388 : }
389 :
390 : void SAL_CALL
391 992 : Calendar_gregorian::setValue( sal_Int16 fieldIndex, sal_Int16 value ) throw(RuntimeException, std::exception)
392 : {
393 992 : if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
394 0 : throw ERROR;
395 992 : fieldSet |= (1 << fieldIndex);
396 992 : fieldValue[fieldIndex] = value;
397 992 : }
398 :
399 1148 : bool Calendar_gregorian::getCombinedOffset( sal_Int32 & o_nOffset,
400 : sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex ) const
401 : {
402 1148 : o_nOffset = 0;
403 1148 : bool bFieldsSet = false;
404 1148 : if (fieldSet & (1 << nParentFieldIndex))
405 : {
406 0 : bFieldsSet = true;
407 0 : o_nOffset = static_cast<sal_Int32>( fieldValue[nParentFieldIndex]) * 60000;
408 : }
409 1148 : if (fieldSet & (1 << nChildFieldIndex))
410 : {
411 0 : bFieldsSet = true;
412 0 : if (o_nOffset < 0)
413 0 : o_nOffset -= static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
414 : else
415 0 : o_nOffset += static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
416 : }
417 1148 : return bFieldsSet;
418 : }
419 :
420 574 : bool Calendar_gregorian::getZoneOffset( sal_Int32 & o_nOffset ) const
421 : {
422 : return getCombinedOffset( o_nOffset, CalendarFieldIndex::ZONE_OFFSET,
423 574 : CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
424 : }
425 :
426 574 : bool Calendar_gregorian::getDSTOffset( sal_Int32 & o_nOffset ) const
427 : {
428 : return getCombinedOffset( o_nOffset, CalendarFieldIndex::DST_OFFSET,
429 574 : CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
430 : }
431 :
432 574 : void Calendar_gregorian::submitFields() throw(com::sun::star::uno::RuntimeException)
433 : {
434 10332 : for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
435 : {
436 9758 : if (fieldSet & (1 << fieldIndex))
437 : {
438 1722 : switch (fieldIndex)
439 : {
440 : default:
441 1722 : body->set(fieldNameConverter(fieldIndex), fieldSetValue[fieldIndex]);
442 1722 : break;
443 : case CalendarFieldIndex::ZONE_OFFSET:
444 : case CalendarFieldIndex::DST_OFFSET:
445 : case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS:
446 : case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS:
447 0 : break; // nothing, extra handling
448 : }
449 : }
450 : }
451 : sal_Int32 nZoneOffset, nDSTOffset;
452 574 : if (getZoneOffset( nZoneOffset))
453 0 : body->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET), nZoneOffset);
454 574 : if (getDSTOffset( nDSTOffset))
455 0 : body->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET), nDSTOffset);
456 574 : }
457 :
458 326 : void Calendar_gregorian::submitValues( sal_Int32 nYear,
459 : sal_Int32 nMonth, sal_Int32 nDay, sal_Int32 nHour, sal_Int32 nMinute,
460 : sal_Int32 nSecond, sal_Int32 nMilliSecond, sal_Int32 nZone, sal_Int32 nDST )
461 : throw(com::sun::star::uno::RuntimeException)
462 : {
463 326 : submitFields();
464 326 : if (nYear >= 0)
465 0 : body->set( UCAL_YEAR, nYear);
466 326 : if (nMonth >= 0)
467 0 : body->set( UCAL_MONTH, nMonth);
468 326 : if (nDay >= 0)
469 0 : body->set( UCAL_DATE, nDay);
470 326 : if (nHour >= 0)
471 326 : body->set( UCAL_HOUR_OF_DAY, nHour);
472 326 : if (nMinute >= 0)
473 326 : body->set( UCAL_MINUTE, nMinute);
474 326 : if (nSecond >= 0)
475 326 : body->set( UCAL_SECOND, nSecond);
476 326 : if (nMilliSecond >= 0)
477 326 : body->set( UCAL_MILLISECOND, nMilliSecond);
478 326 : if (nZone != 0)
479 326 : body->set( UCAL_ZONE_OFFSET, nZone);
480 326 : if (nDST != 0)
481 78 : body->set( UCAL_DST_OFFSET, nDST);
482 326 : }
483 :
484 0 : static void lcl_setCombinedOffsetFieldValues( sal_Int32 nValue,
485 : sal_Int16 rFieldSetValue[], sal_Int16 rFieldValue[],
486 : sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex )
487 : {
488 0 : sal_Int32 nTrunc = nValue / 60000;
489 0 : rFieldSetValue[nParentFieldIndex] = rFieldValue[nParentFieldIndex] =
490 0 : static_cast<sal_Int16>( nTrunc);
491 0 : sal_uInt16 nMillis = static_cast<sal_uInt16>( abs( nValue - nTrunc * 60000));
492 0 : rFieldSetValue[nChildFieldIndex] = rFieldValue[nChildFieldIndex] =
493 0 : static_cast<sal_Int16>( nMillis);
494 0 : }
495 :
496 248 : void Calendar_gregorian::setValue() throw(RuntimeException)
497 : {
498 : // Correct DST glitch, see also localtime/gmtime conversion pitfalls at
499 : // http://www.erack.de/download/timetest.c
500 :
501 : // #i24082# in order to make the DST correction work in all
502 : // circumstances, the time values have to be always resubmitted,
503 : // regardless whether specified by the caller or not. It is not
504 : // sufficient to rely on the ICU internal values previously set, as the
505 : // following may happen:
506 : // - Let 2004-03-28T02:00 be the onsetRule.
507 : // - On 2004-03-29 (calendar initialized with 2004-03-29T00:00 DST) set
508 : // a date of 2004-03-28 => calendar results in 2004-03-27T23:00 no DST.
509 : // - Correcting this with simply "2004-03-28 no DST" and no time
510 : // specified results in 2004-03-29T00:00, the ICU internal 23:00 time
511 : // being adjusted to 24:00 in this case, switching one day further.
512 : // => submit 2004-03-28T00:00 no DST.
513 :
514 : // This got even weirder since ICU incorporated also historical data,
515 : // even the timezone may differ for different dates! It is necessary to
516 : // let ICU choose the corresponding OlsonTimeZone transitions and adapt
517 : // values.
518 : // #i86094# gives examples where that went wrong:
519 : // TZ=Europe/Moscow date <= 1919-07-01
520 : // zone +2:30:48 (!) instead of +3h, DST +2h instead of +1h
521 : // TZ=America/St_Johns date <= 1935-03-30
522 : // zone -3:30:52 (!) instead of -3:30
523 :
524 : // Copy fields before calling submitFields() directly or indirectly below.
525 248 : memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
526 : // Possibly setup ERA and YEAR in fieldSetValue.
527 248 : mapToGregorian();
528 :
529 : DUMP_ICU_CAL_MSG(("%s\n","setValue() before any submission"));
530 : DUMP_I18N_CAL_MSG(("%s\n","setValue() before any submission"));
531 :
532 248 : bool bNeedZone = !(fieldSet & (1 << CalendarFieldIndex::ZONE_OFFSET));
533 248 : bool bNeedDST = !(fieldSet & (1 << CalendarFieldIndex::DST_OFFSET));
534 : sal_Int32 nZone1, nDST1, nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone0, nDST0;
535 248 : nZone1 = nDST1 = nZone0 = nDST0 = 0;
536 248 : nYear = nMonth = nDay = nHour = nMinute = nSecond = nMilliSecond = -1;
537 248 : if ( bNeedZone || bNeedDST )
538 : {
539 : UErrorCode status;
540 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::YEAR)) )
541 : {
542 0 : nYear = body->get( UCAL_YEAR, status = U_ZERO_ERROR);
543 0 : if ( !U_SUCCESS(status) )
544 0 : nYear = -1;
545 : }
546 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::MONTH)) )
547 : {
548 0 : nMonth = body->get( UCAL_MONTH, status = U_ZERO_ERROR);
549 0 : if ( !U_SUCCESS(status) )
550 0 : nMonth = -1;
551 : }
552 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::DAY_OF_MONTH)) )
553 : {
554 0 : nDay = body->get( UCAL_DATE, status = U_ZERO_ERROR);
555 0 : if ( !U_SUCCESS(status) )
556 0 : nDay = -1;
557 : }
558 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::HOUR)) )
559 : {
560 248 : nHour = body->get( UCAL_HOUR_OF_DAY, status = U_ZERO_ERROR);
561 248 : if ( !U_SUCCESS(status) )
562 0 : nHour = -1;
563 : }
564 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::MINUTE)) )
565 : {
566 248 : nMinute = body->get( UCAL_MINUTE, status = U_ZERO_ERROR);
567 248 : if ( !U_SUCCESS(status) )
568 0 : nMinute = -1;
569 : }
570 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::SECOND)) )
571 : {
572 248 : nSecond = body->get( UCAL_SECOND, status = U_ZERO_ERROR);
573 248 : if ( !U_SUCCESS(status) )
574 0 : nSecond = -1;
575 : }
576 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::MILLISECOND)) )
577 : {
578 248 : nMilliSecond = body->get( UCAL_MILLISECOND, status = U_ZERO_ERROR);
579 248 : if ( !U_SUCCESS(status) )
580 0 : nMilliSecond = -1;
581 : }
582 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::ZONE_OFFSET)) )
583 : {
584 248 : nZone0 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
585 248 : if ( !U_SUCCESS(status) )
586 0 : nZone0 = 0;
587 : }
588 248 : if ( !(fieldSet & (1 << CalendarFieldIndex::DST_OFFSET)) )
589 : {
590 248 : nDST0 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
591 248 : if ( !U_SUCCESS(status) )
592 0 : nDST0 = 0;
593 : }
594 :
595 : // Submit values to obtain a time zone and DST corresponding to the date/time.
596 248 : submitValues( nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone0, nDST0);
597 :
598 : DUMP_ICU_CAL_MSG(("%s\n","setValue() in bNeedZone||bNeedDST after submitValues()"));
599 : DUMP_I18N_CAL_MSG(("%s\n","setValue() in bNeedZone||bNeedDST after submitValues()"));
600 248 : nZone1 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
601 248 : if ( !U_SUCCESS(status) )
602 0 : nZone1 = 0;
603 248 : nDST1 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
604 248 : if ( !U_SUCCESS(status) )
605 0 : nDST1 = 0;
606 : }
607 :
608 : // The original submission, may lead to a different zone/DST and
609 : // different date.
610 248 : submitFields();
611 : DUMP_ICU_CAL_MSG(("%s\n","setValue() after original submission"));
612 : DUMP_I18N_CAL_MSG(("%s\n","setValue() after original submission"));
613 :
614 248 : if ( bNeedZone || bNeedDST )
615 : {
616 : UErrorCode status;
617 248 : sal_Int32 nZone2 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
618 248 : if ( !U_SUCCESS(status) )
619 0 : nZone2 = nZone1;
620 248 : sal_Int32 nDST2 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
621 248 : if ( !U_SUCCESS(status) )
622 0 : nDST2 = nDST1;
623 248 : if ( nZone0 != nZone1 || nZone2 != nZone1 || nDST0 != nDST1 || nDST2 != nDST1 )
624 : {
625 : // Due to different DSTs, resulting date values may differ if
626 : // DST is onset at 00:00 and the very onsetRule date was
627 : // submitted with DST off => date-1 23:00, for example, which
628 : // is not what we want.
629 : // Resubmit all values, this time including DST => date 01:00
630 : // Similar for zone differences.
631 : // If already the first full submission with nZone0 and nDST0
632 : // lead to date-1 23:00, the original submission was based on
633 : // that date if it wasn't a full date (nDST0 set, nDST1 not
634 : // set, nDST2==nDST1). If it was January 1st without year we're
635 : // even off by one year now. Resubmit all values including new
636 : // DST => date 00:00.
637 :
638 : // Set field values accordingly in case they were used.
639 78 : if (!bNeedZone)
640 : lcl_setCombinedOffsetFieldValues( nZone2, fieldSetValue,
641 : fieldValue, CalendarFieldIndex::ZONE_OFFSET,
642 0 : CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
643 78 : if (!bNeedDST)
644 : lcl_setCombinedOffsetFieldValues( nDST2, fieldSetValue,
645 : fieldValue, CalendarFieldIndex::DST_OFFSET,
646 0 : CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
647 78 : submitValues( nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone2, nDST2);
648 : DUMP_ICU_CAL_MSG(("%s\n","setValue() after Zone/DST glitch resubmit"));
649 : DUMP_I18N_CAL_MSG(("%s\n","setValue() after Zone/DST glitch resubmit"));
650 :
651 : // Time zone transition => resubmit.
652 : // TZ=America/St_Johns date <= 1935-03-30
653 : // -3:30:52 (!) instead of -3:30
654 : // if first submission included time zone -3:30 that would be wrong.
655 78 : bool bResubmit = false;
656 78 : sal_Int32 nZone3 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
657 78 : if ( !U_SUCCESS(status) )
658 0 : nZone3 = nZone2;
659 78 : if (nZone3 != nZone2)
660 : {
661 0 : bResubmit = true;
662 0 : if (!bNeedZone)
663 : lcl_setCombinedOffsetFieldValues( nZone3, fieldSetValue,
664 : fieldValue, CalendarFieldIndex::ZONE_OFFSET,
665 0 : CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
666 : }
667 :
668 : // If the DST onset rule says to switch from 00:00 to 01:00 and
669 : // we tried to set onsetDay 00:00 with DST, the result was
670 : // onsetDay-1 23:00 and no DST, which is not what we want. So
671 : // once again without DST, resulting in onsetDay 01:00 and DST.
672 : // Yes, this seems to be weird, but logically correct.
673 : // It doesn't even have to be on an onsetDay as the DST is
674 : // factored in all days by ICU and there seems to be some
675 : // unknown behavior.
676 : // TZ=Asia/Tehran 1999-03-22 exposes this, for example.
677 78 : sal_Int32 nDST3 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
678 78 : if ( !U_SUCCESS(status) )
679 0 : nDST3 = nDST2;
680 78 : if (nDST2 != nDST3 && !nDST3)
681 : {
682 0 : bResubmit = true;
683 0 : if (!bNeedDST)
684 : {
685 : fieldSetValue[CalendarFieldIndex::DST_OFFSET] =
686 0 : fieldValue[CalendarFieldIndex::DST_OFFSET] = 0;
687 : fieldSetValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] =
688 0 : fieldValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] = 0;
689 : }
690 : }
691 78 : if (bResubmit)
692 : {
693 0 : submitValues( nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone3, nDST3);
694 : DUMP_ICU_CAL_MSG(("%s\n","setValue() after Zone/DST glitch 2nd resubmit"));
695 : DUMP_I18N_CAL_MSG(("%s\n","setValue() after Zone/DST glitch 2nd resubmit"));
696 : }
697 : SAL_INFO( "i18npool", "Calendar_gregorian::setValue:"
698 : " nZone0 " << nZone0 << ", nDST0 " << nDST0 <<
699 : ", nZone1 " << nZone1 << ", nDST1 " << nDST1 <<
700 : ", nZone2 " << nZone2 << ", nDST2 " << nDST2 <<
701 : ", nZone3 " << nZone3 << ", nDST3 " << nDST3);
702 : }
703 : }
704 : #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
705 : {
706 : // force icu::Calendar to recalculate
707 : UErrorCode status;
708 : sal_Int32 nTmp = body->get( UCAL_DATE, status = U_ZERO_ERROR);
709 : DUMP_ICU_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
710 : DUMP_I18N_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
711 : }
712 : #endif
713 248 : }
714 :
715 7318 : void Calendar_gregorian::getValue() throw(RuntimeException)
716 : {
717 : DUMP_ICU_CAL_MSG(("%s\n","getValue()"));
718 : DUMP_I18N_CAL_MSG(("%s\n","getValue()"));
719 131724 : for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
720 : {
721 124406 : if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS ||
722 : fieldIndex == CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS)
723 14636 : continue; // not ICU fields
724 :
725 : UErrorCode status; sal_Int32 value = body->get( fieldNameConverter(
726 109770 : fieldIndex), status = U_ZERO_ERROR);
727 109770 : if ( !U_SUCCESS(status) ) throw ERROR;
728 :
729 : // Convert millisecond to minute for ZONE and DST and set remainder in
730 : // second field.
731 109770 : if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET)
732 : {
733 7318 : sal_Int32 nMinutes = value / 60000;
734 : sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
735 7318 : abs( value - nMinutes * 60000)));
736 7318 : fieldValue[CalendarFieldIndex::ZONE_OFFSET] = static_cast<sal_Int16>( nMinutes);
737 7318 : fieldValue[CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS] = nMillis;
738 : }
739 102452 : else if (fieldIndex == CalendarFieldIndex::DST_OFFSET)
740 : {
741 7318 : sal_Int32 nMinutes = value / 60000;
742 : sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
743 7318 : abs( value - nMinutes * 60000)));
744 7318 : fieldValue[CalendarFieldIndex::DST_OFFSET] = static_cast<sal_Int16>( nMinutes);
745 7318 : fieldValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] = nMillis;
746 : }
747 : else
748 95134 : fieldValue[fieldIndex] = (sal_Int16) value;
749 :
750 : // offset 1 since the value for week start day SunDay is different between Calendar and Weekdays.
751 109770 : if ( fieldIndex == CalendarFieldIndex::DAY_OF_WEEK )
752 7318 : fieldValue[fieldIndex]--; // UCAL_SUNDAY:/* == 1 */ ==> Weekdays::SUNDAY /* ==0 */
753 : }
754 7318 : mapFromGregorian();
755 7318 : fieldSet = 0;
756 7318 : }
757 :
758 : sal_Int16 SAL_CALL
759 29238 : Calendar_gregorian::getValue( sal_Int16 fieldIndex ) throw(RuntimeException, std::exception)
760 : {
761 29238 : if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
762 0 : throw ERROR;
763 :
764 29238 : if (fieldSet) {
765 0 : setValue();
766 0 : getValue();
767 : }
768 :
769 29238 : return fieldValue[fieldIndex];
770 : }
771 :
772 : void SAL_CALL
773 0 : Calendar_gregorian::addValue( sal_Int16 fieldIndex, sal_Int32 value ) throw(RuntimeException, std::exception)
774 : {
775 : // since ZONE and DST could not be add, we don't need to convert value here
776 : UErrorCode status;
777 0 : body->add(fieldNameConverter(fieldIndex), value, status = U_ZERO_ERROR);
778 0 : if ( !U_SUCCESS(status) ) throw ERROR;
779 0 : getValue();
780 0 : }
781 :
782 : sal_Bool SAL_CALL
783 248 : Calendar_gregorian::isValid() throw(RuntimeException, std::exception)
784 : {
785 248 : if (fieldSet) {
786 248 : sal_Int32 tmp = fieldSet;
787 248 : setValue();
788 248 : memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
789 248 : getValue();
790 4464 : for ( sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++ ) {
791 : // compare only with fields that are set and reset fieldSet[]
792 4216 : if (tmp & (1 << fieldIndex)) {
793 744 : if (fieldSetValue[fieldIndex] != fieldValue[fieldIndex])
794 0 : return sal_False;
795 : }
796 : }
797 : }
798 248 : return true;
799 : }
800 :
801 : // NativeNumberMode has different meaning between Number and Calendar for Asian locales.
802 : // Here is the mapping table
803 : // calendar(q/y/m/d) zh_CN zh_TW ja ko
804 : // NatNum1 NatNum1/1/7/7 NatNum1/1/7/7 NatNum1/1/4/4 NatNum1/1/7/7
805 : // NatNum2 NatNum2/2/8/8 NatNum2/2/8/8 NatNum2/2/5/5 NatNum2/2/8/8
806 : // NatNum3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3
807 : // NatNum4 NatNum9/9/11/11
808 :
809 12 : static sal_Int16 SAL_CALL NatNumForCalendar(const com::sun::star::lang::Locale& aLocale,
810 : sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode, sal_Int16 value )
811 : {
812 10 : bool isShort = ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
813 4 : nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR) && value >= 100) ||
814 20 : nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
815 12 : nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER;
816 12 : bool isChinese = aLocale.Language == "zh";
817 12 : bool isJapanese = aLocale.Language == "ja";
818 12 : bool isKorean = aLocale.Language == "ko";
819 :
820 12 : if (isChinese || isJapanese || isKorean) {
821 0 : switch (nNativeNumberMode) {
822 : case NativeNumberMode::NATNUM1:
823 0 : if (!isShort)
824 0 : nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM4 : NativeNumberMode::NATNUM7;
825 0 : break;
826 : case NativeNumberMode::NATNUM2:
827 0 : if (!isShort)
828 0 : nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM5 : NativeNumberMode::NATNUM8;
829 0 : break;
830 : case NativeNumberMode::NATNUM3:
831 0 : break;
832 : case NativeNumberMode::NATNUM4:
833 0 : if (isKorean)
834 0 : return isShort ? NativeNumberMode::NATNUM9 : NativeNumberMode::NATNUM11;
835 : // fall through
836 0 : default: return 0;
837 : }
838 : }
839 12 : return nNativeNumberMode;
840 : }
841 :
842 5250 : static sal_Int32 SAL_CALL DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode)
843 : {
844 5250 : switch( nCalendarDisplayCode ) {
845 : case CalendarDisplayCode::SHORT_DAY:
846 : case CalendarDisplayCode::LONG_DAY:
847 2230 : return CalendarFieldIndex::DAY_OF_MONTH;
848 : case CalendarDisplayCode::SHORT_DAY_NAME:
849 : case CalendarDisplayCode::LONG_DAY_NAME:
850 : case CalendarDisplayCode::NARROW_DAY_NAME:
851 0 : return CalendarFieldIndex::DAY_OF_WEEK;
852 : case CalendarDisplayCode::SHORT_QUARTER:
853 : case CalendarDisplayCode::LONG_QUARTER:
854 : case CalendarDisplayCode::SHORT_MONTH:
855 : case CalendarDisplayCode::LONG_MONTH:
856 : case CalendarDisplayCode::SHORT_MONTH_NAME:
857 : case CalendarDisplayCode::LONG_MONTH_NAME:
858 : case CalendarDisplayCode::NARROW_MONTH_NAME:
859 : case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME:
860 : case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME:
861 : case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME:
862 : case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME:
863 : case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME:
864 : case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME:
865 2242 : return CalendarFieldIndex::MONTH;
866 : case CalendarDisplayCode::SHORT_YEAR:
867 : case CalendarDisplayCode::LONG_YEAR:
868 774 : return CalendarFieldIndex::YEAR;
869 : case CalendarDisplayCode::SHORT_ERA:
870 : case CalendarDisplayCode::LONG_ERA:
871 4 : return CalendarFieldIndex::ERA;
872 : case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
873 : case CalendarDisplayCode::LONG_YEAR_AND_ERA:
874 0 : return CalendarFieldIndex::YEAR;
875 : default:
876 0 : return 0;
877 : }
878 : }
879 :
880 : sal_Int16 SAL_CALL
881 0 : Calendar_gregorian::getFirstDayOfWeek() throw(RuntimeException, std::exception)
882 : {
883 : // UCAL_SUNDAY == 1, Weekdays::SUNDAY == 0 => offset -1
884 : // Check for underflow just in case we're called "out of sync".
885 0 : return ::std::max( sal::static_int_cast<sal_Int16>(0),
886 : sal::static_int_cast<sal_Int16>( static_cast<sal_Int16>(
887 0 : body->getFirstDayOfWeek()) - 1));
888 : }
889 :
890 : void SAL_CALL
891 1382 : Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day )
892 : throw(RuntimeException, std::exception)
893 : {
894 : // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
895 1382 : body->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek>( day + 1));
896 1382 : }
897 :
898 : void SAL_CALL
899 1382 : Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days ) throw(RuntimeException, std::exception)
900 : {
901 1382 : aCalendar.MinimumNumberOfDaysForFirstWeek = days;
902 1382 : body->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days));
903 1382 : }
904 :
905 : sal_Int16 SAL_CALL
906 0 : Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException, std::exception)
907 : {
908 0 : return aCalendar.MinimumNumberOfDaysForFirstWeek;
909 : }
910 :
911 : sal_Int16 SAL_CALL
912 7398 : Calendar_gregorian::getNumberOfMonthsInYear() throw(RuntimeException, std::exception)
913 : {
914 7398 : return (sal_Int16) aCalendar.Months.getLength();
915 : }
916 :
917 :
918 : sal_Int16 SAL_CALL
919 3864 : Calendar_gregorian::getNumberOfDaysInWeek() throw(RuntimeException, std::exception)
920 : {
921 3864 : return (sal_Int16) aCalendar.Days.getLength();
922 : }
923 :
924 :
925 : Sequence< CalendarItem > SAL_CALL
926 0 : Calendar_gregorian::getDays() throw(RuntimeException, std::exception)
927 : {
928 0 : return LocaleDataImpl::downcastCalendarItems( aCalendar.Days);
929 : }
930 :
931 :
932 : Sequence< CalendarItem > SAL_CALL
933 0 : Calendar_gregorian::getMonths() throw(RuntimeException, std::exception)
934 : {
935 0 : return LocaleDataImpl::downcastCalendarItems( aCalendar.Months);
936 : }
937 :
938 :
939 : Sequence< CalendarItem2 > SAL_CALL
940 1106 : Calendar_gregorian::getDays2() throw(RuntimeException, std::exception)
941 : {
942 1106 : return aCalendar.Days;
943 : }
944 :
945 :
946 : Sequence< CalendarItem2 > SAL_CALL
947 2842 : Calendar_gregorian::getMonths2() throw(RuntimeException, std::exception)
948 : {
949 2842 : return aCalendar.Months;
950 : }
951 :
952 :
953 : Sequence< CalendarItem2 > SAL_CALL
954 1106 : Calendar_gregorian::getGenitiveMonths2() throw(RuntimeException, std::exception)
955 : {
956 1106 : return aCalendar.GenitiveMonths;
957 : }
958 :
959 :
960 : Sequence< CalendarItem2 > SAL_CALL
961 1106 : Calendar_gregorian::getPartitiveMonths2() throw(RuntimeException, std::exception)
962 : {
963 1106 : return aCalendar.PartitiveMonths;
964 : }
965 :
966 :
967 : OUString SAL_CALL
968 1992 : Calendar_gregorian::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType ) throw(RuntimeException, std::exception)
969 : {
970 1992 : OUString aStr;
971 :
972 1992 : switch( displayIndex ) {
973 : case CalendarDisplayIndex::AM_PM:/* ==0 */
974 304 : if (idx == 0) aStr = LocaleDataImpl().getLocaleItem(aLocale).timeAM;
975 104 : else if (idx == 1) aStr = LocaleDataImpl().getLocaleItem(aLocale).timePM;
976 0 : else throw ERROR;
977 304 : break;
978 : case CalendarDisplayIndex::DAY:
979 70 : if( idx >= aCalendar.Days.getLength() ) throw ERROR;
980 70 : if (nameType == 0) aStr = aCalendar.Days[idx].AbbrevName;
981 70 : else if (nameType == 1) aStr = aCalendar.Days[idx].FullName;
982 0 : else if (nameType == 2) aStr = aCalendar.Days[idx].NarrowName;
983 0 : else throw ERROR;
984 70 : break;
985 : case CalendarDisplayIndex::MONTH:
986 146 : if( idx >= aCalendar.Months.getLength() ) throw ERROR;
987 146 : if (nameType == 0) aStr = aCalendar.Months[idx].AbbrevName;
988 70 : else if (nameType == 1) aStr = aCalendar.Months[idx].FullName;
989 0 : else if (nameType == 2) aStr = aCalendar.Months[idx].NarrowName;
990 0 : else throw ERROR;
991 146 : break;
992 : case CalendarDisplayIndex::GENITIVE_MONTH:
993 1464 : if( idx >= aCalendar.GenitiveMonths.getLength() ) throw ERROR;
994 1464 : if (nameType == 0) aStr = aCalendar.GenitiveMonths[idx].AbbrevName;
995 0 : else if (nameType == 1) aStr = aCalendar.GenitiveMonths[idx].FullName;
996 0 : else if (nameType == 2) aStr = aCalendar.GenitiveMonths[idx].NarrowName;
997 0 : else throw ERROR;
998 1464 : break;
999 : case CalendarDisplayIndex::PARTITIVE_MONTH:
1000 4 : if( idx >= aCalendar.PartitiveMonths.getLength() ) throw ERROR;
1001 4 : if (nameType == 0) aStr = aCalendar.PartitiveMonths[idx].AbbrevName;
1002 4 : else if (nameType == 1) aStr = aCalendar.PartitiveMonths[idx].FullName;
1003 0 : else if (nameType == 2) aStr = aCalendar.PartitiveMonths[idx].NarrowName;
1004 0 : else throw ERROR;
1005 4 : break;
1006 : case CalendarDisplayIndex::ERA:
1007 4 : if( idx >= aCalendar.Eras.getLength() ) throw ERROR;
1008 4 : if (nameType == 0) aStr = aCalendar.Eras[idx].AbbrevName;
1009 4 : else if (nameType == 1) aStr = aCalendar.Eras[idx].FullName;
1010 0 : else throw ERROR;
1011 4 : break;
1012 : case CalendarDisplayIndex::YEAR:
1013 0 : break;
1014 : default:
1015 0 : throw ERROR;
1016 : }
1017 1992 : return aStr;
1018 : }
1019 :
1020 : // Methods in XExtendedCalendar
1021 : OUString SAL_CALL
1022 5250 : Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
1023 : throw (RuntimeException, std::exception)
1024 : {
1025 5250 : return getDisplayStringImpl( nCalendarDisplayCode, nNativeNumberMode, false);
1026 : }
1027 :
1028 : OUString
1029 5250 : Calendar_gregorian::getDisplayStringImpl( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode, bool bEraMode )
1030 : throw (RuntimeException)
1031 : {
1032 5250 : sal_Int16 value = getValue(sal::static_int_cast<sal_Int16>( DisplayCode2FieldIndex(nCalendarDisplayCode) ));
1033 5250 : OUString aOUStr;
1034 :
1035 5250 : if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
1036 : nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER) {
1037 0 : Sequence< OUString> xR = LocaleDataImpl().getReservedWord(aLocale);
1038 0 : sal_Int16 quarter = value / 3;
1039 : // Since this base class method may be called by derived calendar
1040 : // classes where a year consists of more than 12 months we need a check
1041 : // to not run out of bounds of reserved quarter words. Perhaps a more
1042 : // clean way (instead of dividing by 3) would be to first get the
1043 : // number of months, divide by 4 and then use that result to divide the
1044 : // actual month value.
1045 0 : if ( quarter > 3 )
1046 0 : quarter = 3;
1047 : quarter = sal::static_int_cast<sal_Int16>( quarter +
1048 : ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER) ?
1049 0 : reservedWords::QUARTER1_ABBREVIATION : reservedWords::QUARTER1_WORD) );
1050 0 : aOUStr = xR[quarter];
1051 : } else {
1052 : // The "#100211# - checked" comments serve for detection of "use of
1053 : // sprintf is safe here" conditions. An sprintf encountered without
1054 : // having that comment triggers alarm ;-)
1055 : sal_Char aStr[10];
1056 5250 : switch( nCalendarDisplayCode ) {
1057 : case CalendarDisplayCode::SHORT_MONTH:
1058 118 : value += 1; // month is zero based
1059 : // fall thru
1060 : case CalendarDisplayCode::SHORT_DAY:
1061 248 : sprintf(aStr, "%d", value); // #100211# - checked
1062 248 : break;
1063 : case CalendarDisplayCode::LONG_YEAR:
1064 340 : if ( aCalendar.Name == "gengou" )
1065 0 : sprintf(aStr, "%02d", value); // #100211# - checked
1066 : else
1067 340 : sprintf(aStr, "%d", value); // #100211# - checked
1068 340 : break;
1069 : case CalendarDisplayCode::LONG_MONTH:
1070 644 : value += 1; // month is zero based
1071 644 : sprintf(aStr, "%02d", value); // #100211# - checked
1072 644 : break;
1073 : case CalendarDisplayCode::SHORT_YEAR:
1074 : // Take last 2 digits, or only one if value<10, for example,
1075 : // in case of the Gengou calendar. For combined era+year always
1076 : // the full year is displayed, without leading 0.
1077 : // Workaround for non-combined calls in certain calendars is
1078 : // the kDisplayEraForcedLongYear flag, but this also could get
1079 : // called for YY not only E format codes, no differentiation
1080 : // possible here; the good news is that usually the Gregorian
1081 : // calendar is the default and hence YY calls for Gregorian and
1082 : // E for the other calendar and currently (2013-02-28) ROC is
1083 : // the only calendar using this.
1084 : // See i#116701 and fdo#60915
1085 434 : if (value < 100 || bEraMode || (eraArray && (eraArray[0].flags & kDisplayEraForcedLongYear)))
1086 2 : sprintf(aStr, "%d", value); // #100211# - checked
1087 : else
1088 432 : sprintf(aStr, "%02d", value % 100); // #100211# - checked
1089 434 : break;
1090 : case CalendarDisplayCode::LONG_DAY:
1091 2100 : sprintf(aStr, "%02d", value); // #100211# - checked
1092 2100 : break;
1093 :
1094 : case CalendarDisplayCode::SHORT_DAY_NAME:
1095 1484 : return getDisplayName(CalendarDisplayIndex::DAY, value, 0);
1096 : case CalendarDisplayCode::LONG_DAY_NAME:
1097 0 : return getDisplayName(CalendarDisplayIndex::DAY, value, 1);
1098 : case CalendarDisplayCode::NARROW_DAY_NAME:
1099 0 : return getDisplayName(CalendarDisplayIndex::DAY, value, 2);
1100 : case CalendarDisplayCode::SHORT_MONTH_NAME:
1101 12 : return getDisplayName(CalendarDisplayIndex::MONTH, value, 0);
1102 : case CalendarDisplayCode::LONG_MONTH_NAME:
1103 0 : return getDisplayName(CalendarDisplayIndex::MONTH, value, 1);
1104 : case CalendarDisplayCode::NARROW_MONTH_NAME:
1105 0 : return getDisplayName(CalendarDisplayIndex::MONTH, value, 2);
1106 : case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME:
1107 1464 : return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 0);
1108 : case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME:
1109 0 : return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 1);
1110 : case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME:
1111 0 : return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 2);
1112 : case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME:
1113 0 : return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 0);
1114 : case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME:
1115 4 : return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 1);
1116 : case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME:
1117 0 : return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 2);
1118 : case CalendarDisplayCode::SHORT_ERA:
1119 0 : return getDisplayName(CalendarDisplayIndex::ERA, value, 0);
1120 : case CalendarDisplayCode::LONG_ERA:
1121 4 : return getDisplayName(CalendarDisplayIndex::ERA, value, 1);
1122 :
1123 : case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
1124 0 : return getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode, true ) +
1125 0 : getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode, true );
1126 :
1127 : case CalendarDisplayCode::LONG_YEAR_AND_ERA:
1128 0 : return getDisplayStringImpl( CalendarDisplayCode::LONG_ERA, nNativeNumberMode, true ) +
1129 0 : getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode, true );
1130 :
1131 : default:
1132 0 : throw ERROR;
1133 : }
1134 3766 : aOUStr = OUString::createFromAscii(aStr);
1135 : }
1136 3766 : if (nNativeNumberMode > 0) {
1137 : // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
1138 12 : if (eraArray == gengou_eraArray && value == 1
1139 0 : && (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
1140 : nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR)
1141 0 : && (nNativeNumberMode == NativeNumberMode::NATNUM1 ||
1142 : nNativeNumberMode == NativeNumberMode::NATNUM2)) {
1143 : static sal_Unicode gan = 0x5143;
1144 0 : return OUString(&gan, 1);
1145 : }
1146 12 : sal_Int16 nNatNum = NatNumForCalendar(aLocale, nCalendarDisplayCode, nNativeNumberMode, value);
1147 12 : if (nNatNum > 0)
1148 12 : return aNatNum.getNativeNumberString(aOUStr, aLocale, nNatNum);
1149 : }
1150 3754 : return aOUStr;
1151 : }
1152 :
1153 : // Methods in XExtendedCalendar
1154 : OUString SAL_CALL
1155 24 : Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
1156 : throw (RuntimeException, std::exception)
1157 : {
1158 : // make year and era in different order for year before and after 0.
1159 48 : if ((nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA ||
1160 24 : nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR_AND_ERA) &&
1161 0 : getValue(CalendarFieldIndex::ERA) == 0) {
1162 0 : if (nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA)
1163 0 : return getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode, true ) +
1164 0 : getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode, true );
1165 : else
1166 0 : return getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode, true ) +
1167 0 : getDisplayStringImpl( CalendarDisplayCode::LONG_ERA, nNativeNumberMode, true );
1168 : }
1169 24 : return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
1170 : }
1171 :
1172 : OUString SAL_CALL
1173 0 : Calendar_gregorian::getImplementationName(void) throw( RuntimeException, std::exception )
1174 : {
1175 0 : return OUString::createFromAscii(cCalendar);
1176 : }
1177 :
1178 : sal_Bool SAL_CALL
1179 0 : Calendar_gregorian::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
1180 : {
1181 0 : return cppu::supportsService(this, rServiceName);
1182 : }
1183 :
1184 : Sequence< OUString > SAL_CALL
1185 0 : Calendar_gregorian::getSupportedServiceNames(void) throw( RuntimeException, std::exception )
1186 : {
1187 0 : Sequence< OUString > aRet(1);
1188 0 : aRet[0] = OUString::createFromAscii(cCalendar);
1189 0 : return aRet;
1190 : }
1191 :
1192 : }}}}
1193 :
1194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|