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