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