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     {0, 0,  0}
222 };
223 Calendar_gengou::Calendar_gengou() : Calendar_gregorian(gengou_eraArray)
224 {
225         cCalendar = "com.sun.star.i18n.Calendar_gengou";
226 }
227 
228 static Era ROC_eraArray[] = {
229     {1912, 1, 1},
230     {0, 0,  0}
231 };
232 Calendar_ROC::Calendar_ROC() : Calendar_gregorian(ROC_eraArray)
233 {
234         cCalendar = "com.sun.star.i18n.Calendar_ROC";
235 }
236 
237 static Era buddhist_eraArray[] = {
238     {-542, 1, 1},
239     {0, 0,  0}
240 };
241 Calendar_buddhist::Calendar_buddhist() : Calendar_gregorian(buddhist_eraArray)
242 {
243         cCalendar = "com.sun.star.i18n.Calendar_buddhist";
244 }
245 
246 void SAL_CALL
247 Calendar_gregorian::loadCalendar( const OUString& uniqueID, const com::sun::star::lang::Locale& rLocale ) throw(RuntimeException)
248 {
249         // init. fieldValue[]
250         getValue();
251 
252         aLocale = rLocale;
253         Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
254         for (sal_Int32 i = 0; i < xC.getLength(); i++)
255         {
256             if (uniqueID == xC[i].Name)
257             {
258                 aCalendar = xC[i];
259                 // setup minimalDaysInFirstWeek
260                 setMinimumNumberOfDaysForFirstWeek(
261                         aCalendar.MinimumNumberOfDaysForFirstWeek);
262                 // setup first day of week
263                 for (sal_Int16 day = sal::static_int_cast<sal_Int16>(
264                             aCalendar.Days.getLength()-1); day>=0; day--)
265                 {
266                     if (aCalendar.StartOfWeek == aCalendar.Days[day].ID)
267                     {
268                         setFirstDayOfWeek( day);
269                         return;
270                     }
271                 }
272             }
273         }
274         // Calendar is not for the locale
275         throw ERROR;
276 }
277 
278 
279 com::sun::star::i18n::Calendar SAL_CALL
280 Calendar_gregorian::getLoadedCalendar() throw(RuntimeException)
281 {
282         return aCalendar;
283 }
284 
285 OUString SAL_CALL
286 Calendar_gregorian::getUniqueID() throw(RuntimeException)
287 {
288         return aCalendar.Name;
289 }
290 
291 void SAL_CALL
292 Calendar_gregorian::setDateTime( double timeInDays ) throw(RuntimeException)
293 {
294         UErrorCode status;
295         body->setTime(timeInDays * U_MILLIS_PER_DAY, status = U_ZERO_ERROR);
296         if ( !U_SUCCESS(status) ) throw ERROR;
297         getValue();
298 }
299 
300 double SAL_CALL
301 Calendar_gregorian::getDateTime() throw(RuntimeException)
302 {
303         if (fieldSet) {
304             setValue();
305             getValue();
306         }
307         UErrorCode status;
308         double r = body->getTime(status = U_ZERO_ERROR);
309         if ( !U_SUCCESS(status) ) throw ERROR;
310         return r / U_MILLIS_PER_DAY;
311 }
312 
313 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
314 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
315 void Calendar_gregorian::mapFromGregorian() throw(RuntimeException)
316 {
317         if (eraArray) {
318             sal_Int16 e, y, m, d;
319 
320             e = fieldValue[CalendarFieldIndex::ERA];
321             y = fieldValue[CalendarFieldIndex::YEAR];
322             m = fieldValue[CalendarFieldIndex::MONTH] + 1;
323             d = fieldValue[CalendarFieldIndex::DAY_OF_MONTH];
324 
325             // since the year is reversed for first era, it is reversed again here for Era compare.
326             if (e == 0)
327                 y = 1 - y;
328 
329             for (e = 0; eraArray[e].year; e++)
330                 if ((y != eraArray[e].year) ? y < eraArray[e].year :
331                     (m != eraArray[e].month) ? m < eraArray[e].month : d < eraArray[e].day)
332                     break;
333 
334             fieldValue[CalendarFieldIndex::ERA] = e;
335             fieldValue[CalendarFieldIndex::YEAR] =
336                 sal::static_int_cast<sal_Int16>( (e == 0) ? (eraArray[0].year - y) : (y - eraArray[e-1].year + 1) );
337         }
338 }
339 
340 #define FIELDS  ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
341 // map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
342 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
343 void Calendar_gregorian::mapToGregorian() throw(RuntimeException)
344 {
345         if (eraArray && (fieldSet & FIELDS)) {
346             sal_Int16 y, e = fieldValue[CalendarFieldIndex::ERA];
347             if (e == 0)
348                 y = sal::static_int_cast<sal_Int16>( eraArray[0].year - fieldValue[CalendarFieldIndex::YEAR] );
349             else
350                 y = sal::static_int_cast<sal_Int16>( eraArray[e-1].year + fieldValue[CalendarFieldIndex::YEAR] - 1 );
351 
352             fieldSetValue[CalendarFieldIndex::ERA] = y <= 0 ? 0 : 1;
353             fieldSetValue[CalendarFieldIndex::YEAR] = (y <= 0 ? 1 - y : y);
354             fieldSet |= FIELDS;
355         }
356 }
357 
358 static UCalendarDateFields fieldNameConverter(sal_Int16 fieldIndex) throw(RuntimeException)
359 {
360         UCalendarDateFields f;
361 
362         switch (fieldIndex) {
363             case CalendarFieldIndex::AM_PM:             f = UCAL_AM_PM; break;
364             case CalendarFieldIndex::DAY_OF_MONTH:      f = UCAL_DATE; break;
365             case CalendarFieldIndex::DAY_OF_WEEK:       f = UCAL_DAY_OF_WEEK; break;
366             case CalendarFieldIndex::DAY_OF_YEAR:       f = UCAL_DAY_OF_YEAR; break;
367             case CalendarFieldIndex::DST_OFFSET:        f = UCAL_DST_OFFSET; break;
368             case CalendarFieldIndex::ZONE_OFFSET:       f = UCAL_ZONE_OFFSET; break;
369             case CalendarFieldIndex::HOUR:              f = UCAL_HOUR_OF_DAY; break;
370             case CalendarFieldIndex::MINUTE:            f = UCAL_MINUTE; break;
371             case CalendarFieldIndex::SECOND:            f = UCAL_SECOND; break;
372             case CalendarFieldIndex::MILLISECOND:       f = UCAL_MILLISECOND; break;
373             case CalendarFieldIndex::WEEK_OF_MONTH:     f = UCAL_WEEK_OF_MONTH; break;
374             case CalendarFieldIndex::WEEK_OF_YEAR:      f = UCAL_WEEK_OF_YEAR; break;
375             case CalendarFieldIndex::YEAR:              f = UCAL_YEAR; break;
376             case CalendarFieldIndex::MONTH:             f = UCAL_MONTH; break;
377             case CalendarFieldIndex::ERA:               f = UCAL_ERA; break;
378             default: throw ERROR;
379         }
380         return f;
381 }
382 
383 void SAL_CALL
384 Calendar_gregorian::setValue( sal_Int16 fieldIndex, sal_Int16 value ) throw(RuntimeException)
385 {
386     if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
387         throw ERROR;
388     fieldSet |= (1 << fieldIndex);
389     fieldValue[fieldIndex] = value;
390 }
391 
392 bool Calendar_gregorian::getCombinedOffset( sal_Int32 & o_nOffset,
393         sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex ) const
394 {
395     o_nOffset = 0;
396     bool bFieldsSet = false;
397     if (fieldSet & (1 << nParentFieldIndex))
398     {
399         bFieldsSet = true;
400         o_nOffset = static_cast<sal_Int32>( fieldValue[nParentFieldIndex]) * 60000;
401     }
402     if (fieldSet & (1 << nChildFieldIndex))
403     {
404         bFieldsSet = true;
405         if (o_nOffset < 0)
406             o_nOffset -= static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
407         else
408             o_nOffset += static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
409     }
410     return bFieldsSet;
411 }
412 
413 bool Calendar_gregorian::getZoneOffset( sal_Int32 & o_nOffset ) const
414 {
415     return getCombinedOffset( o_nOffset, CalendarFieldIndex::ZONE_OFFSET,
416             CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
417 }
418 
419 bool Calendar_gregorian::getDSTOffset( sal_Int32 & o_nOffset ) const
420 {
421     return getCombinedOffset( o_nOffset, CalendarFieldIndex::DST_OFFSET,
422             CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
423 }
424 
425 void Calendar_gregorian::submitFields() throw(com::sun::star::uno::RuntimeException)
426 {
427     for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
428     {
429         if (fieldSet & (1 << fieldIndex))
430         {
431             switch (fieldIndex)
432             {
433                 default:
434                     body->set(fieldNameConverter(fieldIndex), fieldSetValue[fieldIndex]);
435                     break;
436                 case CalendarFieldIndex::ZONE_OFFSET:
437                 case CalendarFieldIndex::DST_OFFSET:
438                 case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS:
439                 case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS:
440                     break;  // nothing, extra handling
441             }
442         }
443     }
444     sal_Int32 nZoneOffset, nDSTOffset;
445     if (getZoneOffset( nZoneOffset))
446         body->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET), nZoneOffset);
447     if (getDSTOffset( nDSTOffset))
448         body->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET), nDSTOffset);
449 }
450 
451 void Calendar_gregorian::submitValues( sal_Int32 nYear,
452         sal_Int32 nMonth, sal_Int32 nDay, sal_Int32 nHour, sal_Int32 nMinute,
453         sal_Int32 nSecond, sal_Int32 nMilliSecond, sal_Int32 nZone, sal_Int32 nDST )
454             throw(com::sun::star::uno::RuntimeException)
455 {
456     submitFields();
457     if (nYear >= 0)
458         body->set( UCAL_YEAR, nYear);
459     if (nMonth >= 0)
460         body->set( UCAL_MONTH, nMonth);
461     if (nDay >= 0)
462         body->set( UCAL_DATE, nDay);
463     if (nHour >= 0)
464         body->set( UCAL_HOUR_OF_DAY, nHour);
465     if (nMinute >= 0)
466         body->set( UCAL_MINUTE, nMinute);
467     if (nSecond >= 0)
468         body->set( UCAL_SECOND, nSecond);
469     if (nMilliSecond >= 0)
470         body->set( UCAL_MILLISECOND, nMilliSecond);
471     if (nZone != 0)
472         body->set( UCAL_ZONE_OFFSET, nZone);
473     if (nDST != 0)
474         body->set( UCAL_DST_OFFSET, nDST);
475 }
476 
477 static void lcl_setCombinedOffsetFieldValues( sal_Int32 nValue,
478         sal_Int16 rFieldSetValue[], sal_Int16 rFieldValue[],
479         sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex )
480 {
481     sal_Int32 nTrunc = nValue / 60000;
482     rFieldSetValue[nParentFieldIndex] = rFieldValue[nParentFieldIndex] =
483         static_cast<sal_Int16>( nTrunc);
484     sal_uInt16 nMillis = static_cast<sal_uInt16>( abs( nValue - nTrunc * 60000));
485     rFieldSetValue[nChildFieldIndex] = rFieldValue[nChildFieldIndex] =
486         static_cast<sal_Int16>( nMillis);
487 }
488 
489 void Calendar_gregorian::setValue() throw(RuntimeException)
490 {
491         // Correct DST glitch, see also localtime/gmtime conversion pitfalls at
492         // http://www.erack.de/download/timetest.c
493         //
494         // #i24082# in order to make the DST correction work in all
495         // circumstances, the time values have to be always resubmitted,
496         // regardless whether specified by the caller or not. It is not
497         // sufficient to rely on the ICU internal values previously set, as the
498         // following may happen:
499         // - Let 2004-03-28T02:00 be the onsetRule.
500         // - On 2004-03-29 (calendar initialized with 2004-03-29T00:00 DST) set
501         //   a date of 2004-03-28 => calendar results in 2004-03-27T23:00 no DST.
502         // - Correcting this with simply "2004-03-28 no DST" and no time
503         //   specified results in 2004-03-29T00:00, the ICU internal 23:00 time
504         //   being adjusted to 24:00 in this case, switching one day further.
505         // => submit 2004-03-28T00:00 no DST.
506 
507         // This got even weirder since ICU incorporated also historical data,
508         // even the timezone may differ for different dates! It is necessary to
509         // let ICU choose the corresponding OlsonTimeZone transitions and adapt
510         // values.
511         // #i86094# gives examples where that went wrong:
512         // TZ=Europe/Moscow date <= 1919-07-01
513         //      zone +2:30:48 (!) instead of +3h, DST +2h instead of +1h
514         // TZ=America/St_Johns date <= 1935-03-30
515         //      zone -3:30:52 (!) instead of -3:30
516 
517         // Copy fields before calling submitFields() directly or indirectly below.
518         memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
519         // Possibly setup ERA and YEAR in fieldSetValue.
520         mapToGregorian();
521 
522         DUMP_ICU_CAL_MSG(("%s\n","setValue() before any submission"));
523         DUMP_I18N_CAL_MSG(("%s\n","setValue() before any submission"));
524 
525         bool bNeedZone = !(fieldSet & (1 << CalendarFieldIndex::ZONE_OFFSET));
526         bool bNeedDST  = !(fieldSet & (1 << CalendarFieldIndex::DST_OFFSET));
527         sal_Int32 nZone1, nDST1, nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone0, nDST0;
528         nZone1 = nDST1 = nZone0 = nDST0 = 0;
529         nYear = nMonth = nDay = nHour = nMinute = nSecond = nMilliSecond = -1;
530         if ( bNeedZone || bNeedDST )
531         {
532             UErrorCode status;
533             if ( !(fieldSet & (1 << CalendarFieldIndex::YEAR)) )
534             {
535                 nYear = body->get( UCAL_YEAR, status = U_ZERO_ERROR);
536                 if ( !U_SUCCESS(status) )
537                     nYear = -1;
538             }
539             if ( !(fieldSet & (1 << CalendarFieldIndex::MONTH)) )
540             {
541                 nMonth = body->get( UCAL_MONTH, status = U_ZERO_ERROR);
542                 if ( !U_SUCCESS(status) )
543                     nMonth = -1;
544             }
545             if ( !(fieldSet & (1 << CalendarFieldIndex::DAY_OF_MONTH)) )
546             {
547                 nDay = body->get( UCAL_DATE, status = U_ZERO_ERROR);
548                 if ( !U_SUCCESS(status) )
549                     nDay = -1;
550             }
551             if ( !(fieldSet & (1 << CalendarFieldIndex::HOUR)) )
552             {
553                 nHour = body->get( UCAL_HOUR_OF_DAY, status = U_ZERO_ERROR);
554                 if ( !U_SUCCESS(status) )
555                     nHour = -1;
556             }
557             if ( !(fieldSet & (1 << CalendarFieldIndex::MINUTE)) )
558             {
559                 nMinute = body->get( UCAL_MINUTE, status = U_ZERO_ERROR);
560                 if ( !U_SUCCESS(status) )
561                     nMinute = -1;
562             }
563             if ( !(fieldSet & (1 << CalendarFieldIndex::SECOND)) )
564             {
565                 nSecond = body->get( UCAL_SECOND, status = U_ZERO_ERROR);
566                 if ( !U_SUCCESS(status) )
567                     nSecond = -1;
568             }
569             if ( !(fieldSet & (1 << CalendarFieldIndex::MILLISECOND)) )
570             {
571                 nMilliSecond = body->get( UCAL_MILLISECOND, status = U_ZERO_ERROR);
572                 if ( !U_SUCCESS(status) )
573                     nMilliSecond = -1;
574             }
575             if ( !(fieldSet & (1 << CalendarFieldIndex::ZONE_OFFSET)) )
576             {
577                 nZone0 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
578                 if ( !U_SUCCESS(status) )
579                     nZone0 = 0;
580             }
581             if ( !(fieldSet & (1 << CalendarFieldIndex::DST_OFFSET)) )
582             {
583                 nDST0 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
584                 if ( !U_SUCCESS(status) )
585                     nDST0 = 0;
586             }
587 
588             // Submit values to obtain a time zone and DST corresponding to the date/time.
589             submitValues( nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone0, nDST0);
590 
591             DUMP_ICU_CAL_MSG(("%s\n","setValue() in bNeedZone||bNeedDST after submitValues()"));
592             DUMP_I18N_CAL_MSG(("%s\n","setValue() in bNeedZone||bNeedDST after submitValues()"));
593             nZone1 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
594             if ( !U_SUCCESS(status) )
595                 nZone1 = 0;
596             nDST1 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
597             if ( !U_SUCCESS(status) )
598                 nDST1 = 0;
599         }
600 
601         // The original submission, may lead to a different zone/DST and
602         // different date.
603         submitFields();
604         DUMP_ICU_CAL_MSG(("%s\n","setValue() after original submission"));
605         DUMP_I18N_CAL_MSG(("%s\n","setValue() after original submission"));
606 
607         if ( bNeedZone || bNeedDST )
608         {
609             UErrorCode status;
610             sal_Int32 nZone2 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
611             if ( !U_SUCCESS(status) )
612                 nZone2 = nZone1;
613             sal_Int32 nDST2 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
614             if ( !U_SUCCESS(status) )
615                 nDST2 = nDST1;
616             if ( nZone0 != nZone1 || nZone2 != nZone1 || nDST0 != nDST1 || nDST2 != nDST1 )
617             {
618                 // Due to different DSTs, resulting date values may differ if
619                 // DST is onset at 00:00 and the very onsetRule date was
620                 // submitted with DST off => date-1 23:00, for example, which
621                 // is not what we want.
622                 // Resubmit all values, this time including DST => date 01:00
623                 // Similar for zone differences.
624                 // If already the first full submission with nZone0 and nDST0
625                 // lead to date-1 23:00, the original submission was based on
626                 // that date if it wasn't a full date (nDST0 set, nDST1 not
627                 // set, nDST2==nDST1). If it was January 1st without year we're
628                 // even off by one year now. Resubmit all values including new
629                 // DST => date 00:00.
630 
631                 // Set field values accordingly in case they were used.
632                 if (!bNeedZone)
633                     lcl_setCombinedOffsetFieldValues( nZone2, fieldSetValue,
634                             fieldValue, CalendarFieldIndex::ZONE_OFFSET,
635                             CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
636                 if (!bNeedDST)
637                     lcl_setCombinedOffsetFieldValues( nDST2, fieldSetValue,
638                             fieldValue, CalendarFieldIndex::DST_OFFSET,
639                             CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
640                 submitValues( nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone2, nDST2);
641                 DUMP_ICU_CAL_MSG(("%s\n","setValue() after Zone/DST glitch resubmit"));
642                 DUMP_I18N_CAL_MSG(("%s\n","setValue() after Zone/DST glitch resubmit"));
643 
644                 // Time zone transition => resubmit.
645                 // TZ=America/St_Johns date <= 1935-03-30
646                 //      -3:30:52 (!) instead of -3:30
647                 //      if first submission included time zone -3:30 that would be wrong.
648                 bool bResubmit = false;
649                 sal_Int32 nZone3 = body->get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR);
650                 if ( !U_SUCCESS(status) )
651                     nZone3 = nZone2;
652                 if (nZone3 != nZone2)
653                 {
654                     bResubmit = true;
655                     if (!bNeedZone)
656                         lcl_setCombinedOffsetFieldValues( nZone3, fieldSetValue,
657                                 fieldValue, CalendarFieldIndex::ZONE_OFFSET,
658                                 CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
659                 }
660 
661                 // If the DST onset rule says to switch from 00:00 to 01:00 and
662                 // we tried to set onsetDay 00:00 with DST, the result was
663                 // onsetDay-1 23:00 and no DST, which is not what we want. So
664                 // once again without DST, resulting in onsetDay 01:00 and DST.
665                 // Yes, this seems to be weird, but logically correct.
666                 // It doesn't even have to be on an onsetDay as the DST is
667                 // factored in all days by ICU and there seems to be some
668                 // unknown behavior.
669                 // TZ=Asia/Tehran 1999-03-22 exposes this, for example.
670                 sal_Int32 nDST3 = body->get( UCAL_DST_OFFSET, status = U_ZERO_ERROR);
671                 if ( !U_SUCCESS(status) )
672                     nDST3 = nDST2;
673                 if (nDST2 != nDST3 && !nDST3)
674                 {
675                     bResubmit = true;
676                     if (!bNeedDST)
677                     {
678                         fieldSetValue[CalendarFieldIndex::DST_OFFSET] =
679                             fieldValue[CalendarFieldIndex::DST_OFFSET] = 0;
680                         fieldSetValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] =
681                             fieldValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] = 0;
682                     }
683                 }
684                 if (bResubmit)
685                 {
686                     submitValues( nYear, nMonth, nDay, nHour, nMinute, nSecond, nMilliSecond, nZone3, nDST3);
687                     DUMP_ICU_CAL_MSG(("%s\n","setValue() after Zone/DST glitch 2nd resubmit"));
688                     DUMP_I18N_CAL_MSG(("%s\n","setValue() after Zone/DST glitch 2nd resubmit"));
689                 }
690             }
691         }
692 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
693         {
694             // force icu::Calendar to recalculate
695             UErrorCode status;
696             sal_Int32 nTmp = body->get( UCAL_DATE, status = U_ZERO_ERROR);
697             DUMP_ICU_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
698             DUMP_I18N_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
699         }
700 #endif
701 }
702 
703 void Calendar_gregorian::getValue() throw(RuntimeException)
704 {
705     DUMP_ICU_CAL_MSG(("%s\n","getValue()"));
706     DUMP_I18N_CAL_MSG(("%s\n","getValue()"));
707     for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
708     {
709         if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS ||
710                 fieldIndex == CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS)
711             continue;   // not ICU fields
712 
713         UErrorCode status; sal_Int32 value = body->get( fieldNameConverter(
714                     fieldIndex), status = U_ZERO_ERROR);
715         if ( !U_SUCCESS(status) ) throw ERROR;
716 
717         // Convert millisecond to minute for ZONE and DST and set remainder in
718         // second field.
719         if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET)
720         {
721             sal_Int32 nMinutes = value / 60000;
722             sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
723                         abs( value - nMinutes * 60000)));
724             fieldValue[CalendarFieldIndex::ZONE_OFFSET] = static_cast<sal_Int16>( nMinutes);
725             fieldValue[CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS] = nMillis;
726         }
727         else if (fieldIndex == CalendarFieldIndex::DST_OFFSET)
728         {
729             sal_Int32 nMinutes = value / 60000;
730             sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
731                         abs( value - nMinutes * 60000)));
732             fieldValue[CalendarFieldIndex::DST_OFFSET] = static_cast<sal_Int16>( nMinutes);
733             fieldValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] = nMillis;
734         }
735         else
736             fieldValue[fieldIndex] = (sal_Int16) value;
737 
738         // offset 1 since the value for week start day SunDay is different between Calendar and Weekdays.
739         if ( fieldIndex == CalendarFieldIndex::DAY_OF_WEEK )
740             fieldValue[fieldIndex]--;   // UCAL_SUNDAY:/* == 1 */ ==> Weekdays::SUNDAY /* ==0 */
741     }
742     mapFromGregorian();
743     fieldSet = 0;
744 }
745 
746 sal_Int16 SAL_CALL
747 Calendar_gregorian::getValue( sal_Int16 fieldIndex ) throw(RuntimeException)
748 {
749     if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
750         throw ERROR;
751 
752     if (fieldSet)  {
753         setValue();
754         getValue();
755     }
756 
757     return fieldValue[fieldIndex];
758 }
759 
760 void SAL_CALL
761 Calendar_gregorian::addValue( sal_Int16 fieldIndex, sal_Int32 value ) throw(RuntimeException)
762 {
763         // since ZONE and DST could not be add, we don't need to convert value here
764         UErrorCode status;
765         body->add(fieldNameConverter(fieldIndex), value, status = U_ZERO_ERROR);
766         if ( !U_SUCCESS(status) ) throw ERROR;
767         getValue();
768 }
769 
770 sal_Bool SAL_CALL
771 Calendar_gregorian::isValid() throw(RuntimeException)
772 {
773         if (fieldSet) {
774             sal_Int32 tmp = fieldSet;
775             setValue();
776             memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
777             getValue();
778             for ( sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++ ) {
779                 // compare only with fields that are set and reset fieldSet[]
780                 if (tmp & (1 << fieldIndex)) {
781                     if (fieldSetValue[fieldIndex] != fieldValue[fieldIndex])
782                         return sal_False;
783                 }
784             }
785         }
786         return true;
787 }
788 
789 // NativeNumberMode has different meaning between Number and Calendar for Asian locales.
790 // Here is the mapping table
791 // calendar(q/y/m/d)    zh_CN           zh_TW           ja              ko
792 // NatNum1              NatNum1/1/7/7   NatNum1/1/7/7   NatNum1/1/4/4   NatNum1/1/7/7
793 // NatNum2              NatNum2/2/8/8   NatNum2/2/8/8   NatNum2/2/5/5   NatNum2/2/8/8
794 // NatNum3              NatNum3/3/3/3   NatNum3/3/3/3   NatNum3/3/3/3   NatNum3/3/3/3
795 // NatNum4                                                              NatNum9/9/11/11
796 
797 static sal_Int16 SAL_CALL NatNumForCalendar(const com::sun::star::lang::Locale& aLocale,
798         sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode, sal_Int16 value )
799 {
800     sal_Bool isShort = ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
801         nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR) && value >= 100) ||
802         nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
803         nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER;
804     sal_Bool isChinese = aLocale.Language.equalsAscii("zh");
805     sal_Bool isJapanese = aLocale.Language.equalsAscii("ja");
806     sal_Bool isKorean = aLocale.Language.equalsAscii("ko");
807 
808     if (isChinese || isJapanese || isKorean) {
809         switch (nNativeNumberMode) {
810             case NativeNumberMode::NATNUM1:
811                 if (!isShort)
812                     nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM4 : NativeNumberMode::NATNUM7;
813                 break;
814             case NativeNumberMode::NATNUM2:
815                 if (!isShort)
816                     nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM5 : NativeNumberMode::NATNUM8;
817                 break;
818             case NativeNumberMode::NATNUM3:
819                 break;
820             case NativeNumberMode::NATNUM4:
821                 if (isKorean)
822                     return isShort ? NativeNumberMode::NATNUM9 : NativeNumberMode::NATNUM11;
823                 // fall through
824             default: return 0;
825         }
826     }
827     return nNativeNumberMode;
828 }
829 
830 static sal_Int32 SAL_CALL DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode)
831 {
832     switch( nCalendarDisplayCode ) {
833         case CalendarDisplayCode::SHORT_DAY:
834         case CalendarDisplayCode::LONG_DAY:
835             return CalendarFieldIndex::DAY_OF_MONTH;
836         case CalendarDisplayCode::SHORT_DAY_NAME:
837         case CalendarDisplayCode::LONG_DAY_NAME:
838             return CalendarFieldIndex::DAY_OF_WEEK;
839         case CalendarDisplayCode::SHORT_QUARTER:
840         case CalendarDisplayCode::LONG_QUARTER:
841         case CalendarDisplayCode::SHORT_MONTH:
842         case CalendarDisplayCode::LONG_MONTH:
843         case CalendarDisplayCode::SHORT_MONTH_NAME:
844         case CalendarDisplayCode::LONG_MONTH_NAME:
845             return CalendarFieldIndex::MONTH;
846         case CalendarDisplayCode::SHORT_YEAR:
847         case CalendarDisplayCode::LONG_YEAR:
848             return CalendarFieldIndex::YEAR;
849         case CalendarDisplayCode::SHORT_ERA:
850         case CalendarDisplayCode::LONG_ERA:
851             return CalendarFieldIndex::ERA;
852         case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
853         case CalendarDisplayCode::LONG_YEAR_AND_ERA:
854             return CalendarFieldIndex::YEAR;
855         default:
856             return 0;
857     }
858 }
859 
860 sal_Int16 SAL_CALL
861 Calendar_gregorian::getFirstDayOfWeek() throw(RuntimeException)
862 {
863     // UCAL_SUNDAY == 1, Weekdays::SUNDAY == 0 => offset -1
864     // Check for underflow just in case we're called "out of sync".
865     return ::std::max( sal::static_int_cast<sal_Int16>(0),
866             sal::static_int_cast<sal_Int16>( static_cast<sal_Int16>(
867                     body->getFirstDayOfWeek()) - 1));
868 }
869 
870 void SAL_CALL
871 Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day )
872 throw(RuntimeException)
873 {
874     // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
875     body->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek>( day + 1));
876 }
877 
878 void SAL_CALL
879 Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days ) throw(RuntimeException)
880 {
881         aCalendar.MinimumNumberOfDaysForFirstWeek = days;
882         body->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days));
883 }
884 
885 sal_Int16 SAL_CALL
886 Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException)
887 {
888         return aCalendar.MinimumNumberOfDaysForFirstWeek;
889 }
890 
891 sal_Int16 SAL_CALL
892 Calendar_gregorian::getNumberOfMonthsInYear() throw(RuntimeException)
893 {
894         return (sal_Int16) aCalendar.Months.getLength();
895 }
896 
897 
898 sal_Int16 SAL_CALL
899 Calendar_gregorian::getNumberOfDaysInWeek() throw(RuntimeException)
900 {
901         return (sal_Int16) aCalendar.Days.getLength();
902 }
903 
904 
905 Sequence< CalendarItem > SAL_CALL
906 Calendar_gregorian::getMonths() throw(RuntimeException)
907 {
908         return aCalendar.Months;
909 }
910 
911 
912 Sequence< CalendarItem > SAL_CALL
913 Calendar_gregorian::getDays() throw(RuntimeException)
914 {
915         return aCalendar.Days;
916 }
917 
918 OUString SAL_CALL
919 Calendar_gregorian::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType ) throw(RuntimeException)
920 {
921         OUString aStr;
922 
923         switch( displayIndex ) {
924             case CalendarDisplayIndex::AM_PM:/* ==0 */
925                 if (idx == 0) aStr = LocaleData().getLocaleItem(aLocale).timeAM;
926                 else if (idx == 1) aStr = LocaleData().getLocaleItem(aLocale).timePM;
927                 else throw ERROR;
928                 break;
929             case CalendarDisplayIndex::DAY:
930                 if( idx >= aCalendar.Days.getLength() ) throw ERROR;
931                 if (nameType == 0) aStr = aCalendar.Days[idx].AbbrevName;
932                 else if (nameType == 1) aStr = aCalendar.Days[idx].FullName;
933                 else throw ERROR;
934                 break;
935             case CalendarDisplayIndex::MONTH:
936                 if( idx >= aCalendar.Months.getLength() ) throw ERROR;
937                 if (nameType == 0) aStr = aCalendar.Months[idx].AbbrevName;
938                 else if (nameType == 1) aStr = aCalendar.Months[idx].FullName;
939                 else throw ERROR;
940                 break;
941             case CalendarDisplayIndex::ERA:
942                 if( idx >= aCalendar.Eras.getLength() ) throw ERROR;
943                 if (nameType == 0) aStr = aCalendar.Eras[idx].AbbrevName;
944                 else if (nameType == 1) aStr = aCalendar.Eras[idx].FullName;
945                 else throw ERROR;
946                 break;
947             case CalendarDisplayIndex::YEAR:
948                 break;
949             default:
950                 throw ERROR;
951         }
952         return aStr;
953 }
954 
955 // Methods in XExtendedCalendar
956 OUString SAL_CALL
957 Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
958         throw (RuntimeException)
959 {
960     sal_Int16 value = getValue(sal::static_int_cast<sal_Int16>( DisplayCode2FieldIndex(nCalendarDisplayCode) ));
961     OUString aOUStr;
962 
963     if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
964             nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER) {
965         Sequence< OUString> xR = LocaleData().getReservedWord(aLocale);
966         sal_Int16 quarter = value / 3;
967         // Since this base class method may be called by derived calendar
968         // classes where a year consists of more than 12 months we need a check
969         // to not run out of bounds of reserved quarter words. Perhaps a more
970         // clean way (instead of dividing by 3) would be to first get the
971         // number of months, divide by 4 and then use that result to divide the
972         // actual month value.
973         if ( quarter > 3 )
974             quarter = 3;
975         quarter = sal::static_int_cast<sal_Int16>( quarter +
976             ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER) ?
977             reservedWords::QUARTER1_ABBREVIATION : reservedWords::QUARTER1_WORD) );
978         aOUStr = xR[quarter];
979     } else {
980         // The "#100211# - checked" comments serve for detection of "use of
981         // sprintf is safe here" conditions. An sprintf encountered without
982         // having that comment triggers alarm ;-)
983         sal_Char aStr[10];
984         switch( nCalendarDisplayCode ) {
985             case CalendarDisplayCode::SHORT_MONTH:
986                 value += 1;     // month is zero based
987                 // fall thru
988             case CalendarDisplayCode::SHORT_DAY:
989                 sprintf(aStr, "%d", value);     // #100211# - checked
990                 break;
991             case CalendarDisplayCode::LONG_YEAR:
992                 if (aCalendar.Name.equalsAscii("gengou"))
993                     sprintf(aStr, "%02d", value);     // #100211# - checked
994                 else
995                     sprintf(aStr, "%d", value);     // #100211# - checked
996                 break;
997             case CalendarDisplayCode::LONG_MONTH:
998                 value += 1;     // month is zero based
999                 sprintf(aStr, "%02d", value);   // #100211# - checked
1000                 break;
1001             case CalendarDisplayCode::SHORT_YEAR:
1002                 // Take last 2 digits, or only one if value<10, for example,
1003                 // in case of the Gengou calendar.
1004                 // #i116701# For values in non-Gregorian era years use all
1005                 // digits.
1006                 if (value < 100 || eraArray)
1007                     sprintf(aStr, "%d", value); // #100211# - checked
1008                 else
1009                     sprintf(aStr, "%02d", value % 100); // #100211# - checked
1010                 break;
1011             case CalendarDisplayCode::LONG_DAY:
1012                 sprintf(aStr, "%02d", value);   // #100211# - checked
1013                 break;
1014 
1015             case CalendarDisplayCode::SHORT_DAY_NAME:
1016                 return getDisplayName(CalendarDisplayIndex::DAY, value, 0);
1017             case CalendarDisplayCode::LONG_DAY_NAME:
1018                 return getDisplayName(CalendarDisplayIndex::DAY, value, 1);
1019             case CalendarDisplayCode::SHORT_MONTH_NAME:
1020                 return getDisplayName(CalendarDisplayIndex::MONTH, value, 0);
1021             case CalendarDisplayCode::LONG_MONTH_NAME:
1022                 return getDisplayName(CalendarDisplayIndex::MONTH, value, 1);
1023             case CalendarDisplayCode::SHORT_ERA:
1024                 return getDisplayName(CalendarDisplayIndex::ERA, value, 0);
1025             case CalendarDisplayCode::LONG_ERA:
1026                 return getDisplayName(CalendarDisplayIndex::ERA, value, 1);
1027 
1028             case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
1029                 return  getDisplayString( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode ) +
1030                     getDisplayString( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode );
1031 
1032             case CalendarDisplayCode::LONG_YEAR_AND_ERA:
1033                 return  getDisplayString( CalendarDisplayCode::LONG_ERA, nNativeNumberMode ) +
1034                     getDisplayString( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode );
1035 
1036             default:
1037                 throw ERROR;
1038         }
1039         aOUStr = OUString::createFromAscii(aStr);
1040     }
1041     if (nNativeNumberMode > 0) {
1042         // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
1043 		if (eraArray == gengou_eraArray && value == 1
1044 			&& (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
1045 				nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR)
1046 			&& (nNativeNumberMode == NativeNumberMode::NATNUM1 ||
1047 				nNativeNumberMode == NativeNumberMode::NATNUM2)) {
1048 			static sal_Unicode gan = 0x5143;
1049 			return OUString(&gan, 1);
1050 		}
1051         sal_Int16 nNatNum = NatNumForCalendar(aLocale, nCalendarDisplayCode, nNativeNumberMode, value);
1052         if (nNatNum > 0)
1053             return aNatNum.getNativeNumberString(aOUStr, aLocale, nNatNum);
1054     }
1055     return aOUStr;
1056 }
1057 
1058 // Methods in XExtendedCalendar
1059 OUString SAL_CALL
1060 Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
1061         throw (RuntimeException)
1062 {
1063     // make year and era in different order for year before and after 0.
1064     if ((nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA ||
1065                 nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR_AND_ERA) &&
1066             getValue(CalendarFieldIndex::ERA) == 0) {
1067         if (nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA)
1068             return  getDisplayString( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode ) +
1069                 getDisplayString( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode );
1070         else
1071             return  getDisplayString( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode ) +
1072                 getDisplayString( CalendarDisplayCode::LONG_ERA, nNativeNumberMode );
1073     }
1074     return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
1075 }
1076 
1077 OUString SAL_CALL
1078 Calendar_gregorian::getImplementationName(void) throw( RuntimeException )
1079 {
1080         return OUString::createFromAscii(cCalendar);
1081 }
1082 
1083 sal_Bool SAL_CALL
1084 Calendar_gregorian::supportsService(const rtl::OUString& rServiceName) throw( RuntimeException )
1085 {
1086         return !rServiceName.compareToAscii(cCalendar);
1087 }
1088 
1089 Sequence< OUString > SAL_CALL
1090 Calendar_gregorian::getSupportedServiceNames(void) throw( RuntimeException )
1091 {
1092         Sequence< OUString > aRet(1);
1093         aRet[0] = OUString::createFromAscii(cCalendar);
1094         return aRet;
1095 }
1096 
1097