1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <algorithm>
21 
22 #include <calendar_gregorian.hxx>
23 #include <localedata.hxx>
24 #include <nativenumbersupplier.hxx>
25 #include <com/sun/star/i18n/CalendarDisplayCode.hpp>
26 #include <com/sun/star/i18n/CalendarDisplayIndex.hpp>
27 #include <com/sun/star/i18n/NativeNumberMode.hpp>
28 #include <com/sun/star/i18n/reservedWords.hpp>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <rtl/math.hxx>
31 #include <sal/log.hxx>
32 
33 #include <stdio.h>
34 #include <string.h>
35 
36 #define erDUMP_ICU_CALENDAR 0
37 #define erDUMP_I18N_CALENDAR 0
38 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
39 // If both are used, DUMP_ICU_CAL_MSG() must be used before DUMP_I18N_CAL_MSG()
40 // to obtain internally set values from ICU, else Calendar::get() calls in
41 // DUMP_I18N_CAL_MSG() recalculate!
42 
43 // These pieces of macro are shamelessly borrowed from icu's olsontz.cpp, the
44 // double parens'ed approach to pass multiple parameters as one macro parameter
45 // is appealing.
debug_cal_loc(const char * f,int32_t l)46 static void debug_cal_loc(const char *f, int32_t l)
47 {
48     fprintf(stderr, "%s:%d: ", f, l);
49 }
50 # include <stdarg.h>
debug_cal_msg(const char * pat,...)51 static void debug_cal_msg(const char *pat, ...)
52 {
53     va_list ap;
54     va_start(ap, pat);
55     vfprintf(stderr, pat, ap);
56     va_end(ap);
57 }
58 
59 #if erDUMP_ICU_CALENDAR
60 // Make icu with
61 // DEFS = -DU_DEBUG_CALSVC -DUCAL_DEBUG_DUMP
62 // in workdir/UnpackedTarball/icu/source/icudefs.mk
63 // May need some patches to fix unmaintained things there.
64 extern void ucal_dump( const icu::Calendar & );
debug_icu_cal_dump(const::icu::Calendar & r)65 static void debug_icu_cal_dump( const ::icu::Calendar & r )
66 {
67     ucal_dump(r);
68     fflush(stderr);
69     // set a breakpoint here to pause display between dumps
70 }
71 // must use double parens, i.e.:  DUMP_ICU_CAL_MSG(("four is: %d",4));
72 #define DUMP_ICU_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_icu_cal_dump(*body);}
73 #else   // erDUMP_ICU_CALENDAR
74 #define DUMP_ICU_CAL_MSG(x)
75 #endif  // erDUMP_ICU_CALENDAR
76 
77 #if erDUMP_I18N_CALENDAR
debug_cal_millis_to_time(long nMillis,long & h,long & m,long & s,long & f)78 static void debug_cal_millis_to_time( long nMillis, long & h, long & m, long & s, long & f )
79 {
80     int sign = (nMillis < 0 ? -1 : 1);
81     nMillis = ::std::abs(nMillis);
82     h = sign * nMillis / (60 * 60 * 1000);
83     nMillis -= sign * h * (60 * 60 * 1000);
84     m = nMillis / (60 * 1000);
85     nMillis -= m * (60 * 1000);
86     s = nMillis / (1000);
87     nMillis -= s * (1000);
88     f = nMillis;
89 }
debug_i18n_cal_dump(const::icu::Calendar & r)90 static void debug_i18n_cal_dump( const ::icu::Calendar & r )
91 {
92     UErrorCode status;
93     long nMillis, h, m, s, f;
94     fprintf( stderr, " %04ld", (long)r.get( UCAL_YEAR, status = U_ZERO_ERROR));
95     fprintf( stderr, "-%02ld", (long)r.get( UCAL_MONTH, status = U_ZERO_ERROR)+1);
96     fprintf( stderr, "-%02ld", (long)r.get( UCAL_DATE, status = U_ZERO_ERROR));
97     fprintf( stderr, " %02ld", (long)r.get( UCAL_HOUR_OF_DAY, status = U_ZERO_ERROR));
98     fprintf( stderr, ":%02ld", (long)r.get( UCAL_MINUTE, status = U_ZERO_ERROR));
99     fprintf( stderr, ":%02ld", (long)r.get( UCAL_SECOND, status = U_ZERO_ERROR));
100     fprintf( stderr, "  zone: %ld", (long)(nMillis = r.get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR)));
101     fprintf( stderr, " (%f min)", (double)nMillis / 60000);
102     debug_cal_millis_to_time( nMillis, h, m, s, f);
103     fprintf( stderr, " (%ld:%02ld:%02ld.%ld)", h, m, s, f);
104     fprintf( stderr, "  DST: %ld", (long)(nMillis = r.get( UCAL_DST_OFFSET, status = U_ZERO_ERROR)));
105     fprintf( stderr, " (%f min)", (double)nMillis / 60000);
106     debug_cal_millis_to_time( nMillis, h, m, s, f);
107     fprintf( stderr, " (%ld:%02ld:%02ld.%ld)", h, m, s, f);
108     fprintf( stderr, "\n");
109     fflush(stderr);
110 }
111 // must use double parens, i.e.:  DUMP_I18N_CAL_MSG(("four is: %d",4));
112 #define DUMP_I18N_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_i18n_cal_dump(*body);}
113 #else   // erDUMP_I18N_CALENDAR
114 #define DUMP_I18N_CAL_MSG(x)
115 #endif  // erDUMP_I18N_CALENDAR
116 
117 #else   // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
118 #define DUMP_ICU_CAL_MSG(x)
119 #define DUMP_I18N_CAL_MSG(x)
120 #endif  // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
121 
122 
123 using namespace ::com::sun::star::uno;
124 using namespace ::com::sun::star::i18n;
125 using namespace ::com::sun::star::lang;
126 
127 
128 namespace i18npool {
129 
130 #define ERROR RuntimeException()
131 
Calendar_gregorian()132 Calendar_gregorian::Calendar_gregorian()
133     : mxNatNum(new NativeNumberSupplierService)
134 {
135     init(nullptr);
136 }
Calendar_gregorian(const Era * _earArray)137 Calendar_gregorian::Calendar_gregorian(const Era *_earArray)
138     : mxNatNum(new NativeNumberSupplierService)
139 {
140     init(_earArray);
141 }
142 void
init(const Era * _eraArray)143 Calendar_gregorian::init(const Era *_eraArray)
144 {
145     cCalendar = "com.sun.star.i18n.Calendar_gregorian";
146 
147     fieldSet = 0;
148 
149     // #i102356# With icu::Calendar::createInstance(UErrorCode) in a Thai
150     // th_TH system locale we accidentally used a Buddhist calendar. Though
151     // the ICU documentation says that should be the case only for
152     // th_TH_TRADITIONAL (and ja_JP_TRADITIONAL Gengou), a plain th_TH
153     // already triggers that behavior, ja_JP does not. Strange enough,
154     // passing a th_TH locale to the calendar creation doesn't trigger
155     // this.
156     // See also http://userguide.icu-project.org/datetime/calendar
157 
158     // Whatever ICU offers as the default calendar for a locale, ensure we
159     // have a Gregorian calendar as requested.
160 
161     /* XXX: with the current implementation the aLocale member variable is
162      * not set prior to loading a calendar from locale data. This
163      * creates an empty (root) locale for ICU, but at least the correct
164      * calendar is used. The language part must not be NULL (respectively
165      * not all, language and country and variant), otherwise the current
166      * default locale would be used again and the calendar keyword ignored.
167      * */
168     icu::Locale aIcuLocale( "", nullptr, nullptr, "calendar=gregorian");
169 
170     /* XXX: not specifying a timezone when creating a calendar assigns the
171      * system's timezone with all DST quirks, invalid times when switching
172      * to/from DST and so on. The XCalendar* interfaces are defined to support
173      * local time and UTC time so we can not override that here.
174      */
175     UErrorCode status = U_ZERO_ERROR;
176     body.reset( icu::Calendar::createInstance( aIcuLocale, status) );
177     if (!body || !U_SUCCESS(status)) throw ERROR;
178     eraArray=_eraArray;
179 }
180 
~Calendar_gregorian()181 Calendar_gregorian::~Calendar_gregorian()
182 {
183 }
184 
Calendar_hanja()185 Calendar_hanja::Calendar_hanja()
186 {
187     cCalendar = "com.sun.star.i18n.Calendar_hanja";
188 }
189 
190 OUString SAL_CALL
getDisplayName(sal_Int16 displayIndex,sal_Int16 idx,sal_Int16 nameType)191 Calendar_hanja::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
192 {
193     if ( displayIndex == CalendarDisplayIndex::AM_PM ) {
194         // Am/Pm string for Korean Hanja calendar will refer to Japanese locale
195         css::lang::Locale jaLocale("ja", OUString(), OUString());
196         if (idx == 0) return LocaleDataImpl::get()->getLocaleItem(jaLocale).timeAM;
197         else if (idx == 1) return LocaleDataImpl::get()->getLocaleItem(jaLocale).timePM;
198         else throw ERROR;
199     }
200     else
201         return Calendar_gregorian::getDisplayName( displayIndex, idx, nameType );
202 }
203 
204 void SAL_CALL
loadCalendar(const OUString &,const css::lang::Locale & rLocale)205 Calendar_hanja::loadCalendar( const OUString& /*uniqueID*/, const css::lang::Locale& rLocale )
206 {
207     // Since this class could be called by service name 'hanja_yoil', we have to
208     // rename uniqueID to get right calendar defined in locale data.
209     Calendar_gregorian::loadCalendar("hanja", rLocale);
210 }
211 
212 static const Era gengou_eraArray[] = {
213     {1868,  1,  1, 0},  // Meiji
214     {1912,  7, 30, 0},  // Taisho
215     {1926, 12, 25, 0},  // Showa
216     {1989,  1,  8, 0},  // Heisei
217     {2019,  5,  1, 0},  // Reiwa
218     {0, 0, 0, 0}
219 };
Calendar_gengou()220 Calendar_gengou::Calendar_gengou() : Calendar_gregorian(gengou_eraArray)
221 {
222     cCalendar = "com.sun.star.i18n.Calendar_gengou";
223 }
224 
225 static const Era ROC_eraArray[] = {
226     {1912, 1, 1, kDisplayEraForcedLongYear},    // #i116701#
227     {0, 0, 0, 0}
228 };
Calendar_ROC()229 Calendar_ROC::Calendar_ROC() : Calendar_gregorian(ROC_eraArray)
230 {
231     cCalendar = "com.sun.star.i18n.Calendar_ROC";
232 }
233 
234 /**
235 * The start year of the Korean traditional calendar (Dan-gi) is the inaugural
236 * year of Dan-gun (BC 2333).
237 */
238 static const Era dangi_eraArray[] = {
239     {-2332, 1, 1, 0},
240     {0, 0, 0, 0}
241 };
Calendar_dangi()242 Calendar_dangi::Calendar_dangi() : Calendar_gregorian(dangi_eraArray)
243 {
244     cCalendar = "com.sun.star.i18n.Calendar_dangi";
245 }
246 
247 static const Era buddhist_eraArray[] = {
248     {-542, 1, 1, 0},
249     {0, 0, 0, 0}
250 };
Calendar_buddhist()251 Calendar_buddhist::Calendar_buddhist() : Calendar_gregorian(buddhist_eraArray)
252 {
253     cCalendar = "com.sun.star.i18n.Calendar_buddhist";
254 }
255 
256 void SAL_CALL
loadCalendar(const OUString & uniqueID,const css::lang::Locale & rLocale)257 Calendar_gregorian::loadCalendar( const OUString& uniqueID, const css::lang::Locale& rLocale )
258 {
259     // init. fieldValue[]
260     getValue();
261 
262     aLocale = rLocale;
263     const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
264     for (const auto& rCal : xC)
265     {
266         if (uniqueID == rCal.Name)
267         {
268             aCalendar = rCal;
269             // setup minimalDaysInFirstWeek
270             setMinimumNumberOfDaysForFirstWeek(
271                     aCalendar.MinimumNumberOfDaysForFirstWeek);
272             // setup first day of week
273             for (sal_Int16 day = sal::static_int_cast<sal_Int16>(
274                         aCalendar.Days.getLength()-1); day>=0; day--)
275             {
276                 if (aCalendar.StartOfWeek == aCalendar.Days[day].ID)
277                 {
278                     setFirstDayOfWeek( day);
279                     return;
280                 }
281             }
282         }
283     }
284     // Calendar is not for the locale
285     throw ERROR;
286 }
287 
288 
289 css::i18n::Calendar2 SAL_CALL
getLoadedCalendar2()290 Calendar_gregorian::getLoadedCalendar2()
291 {
292     return aCalendar;
293 }
294 
295 css::i18n::Calendar SAL_CALL
getLoadedCalendar()296 Calendar_gregorian::getLoadedCalendar()
297 {
298     return LocaleDataImpl::downcastCalendar( aCalendar);
299 }
300 
301 OUString SAL_CALL
getUniqueID()302 Calendar_gregorian::getUniqueID()
303 {
304     return aCalendar.Name;
305 }
306 
307 void SAL_CALL
setDateTime(double fTimeInDays)308 Calendar_gregorian::setDateTime( double fTimeInDays )
309 {
310     // ICU handles dates in milliseconds as double values and uses floor()
311     // to obtain integer values, which may yield a date decremented by one
312     // for odd (historical) timezone values where the computed value due to
313     // rounding errors has a fractional part in milliseconds. Ensure we
314     // pass a value without fraction here. If not, that may lead to
315     // fdo#44286 or fdo#52619 and the like, e.g. when passing
316     // -2136315212000.000244 instead of -2136315212000.000000
317     double fM = fTimeInDays * U_MILLIS_PER_DAY;
318     double fR = rtl::math::round( fM );
319     SAL_INFO_IF( fM != fR, "i18npool",
320             "Calendar_gregorian::setDateTime: " << std::fixed << fM << " rounded to " << fR);
321     UErrorCode status = U_ZERO_ERROR;
322     body->setTime( fR, status);
323     if ( !U_SUCCESS(status) ) throw ERROR;
324     getValue();
325 }
326 
327 double SAL_CALL
getDateTime()328 Calendar_gregorian::getDateTime()
329 {
330     if (fieldSet) {
331         setValue();
332         getValue();
333     }
334     UErrorCode status = U_ZERO_ERROR;
335     double fR = body->getTime(status);
336     if ( !U_SUCCESS(status) ) throw ERROR;
337     return fR / U_MILLIS_PER_DAY;
338 }
339 
340 void SAL_CALL
setLocalDateTime(double fTimeInDays)341 Calendar_gregorian::setLocalDateTime( double fTimeInDays )
342 {
343     // See setDateTime() for why the rounding.
344     double fM = fTimeInDays * U_MILLIS_PER_DAY;
345     double fR = rtl::math::round( fM );
346     SAL_INFO_IF( fM != fR, "i18npool",
347             "Calendar_gregorian::setLocalDateTime: " << std::fixed << fM << " rounded to " << fR);
348     int32_t nZoneOffset, nDSTOffset;
349     UErrorCode status = U_ZERO_ERROR;
350     body->getTimeZone().getOffset( fR, true, nZoneOffset, nDSTOffset, status );
351     if ( !U_SUCCESS(status) ) throw ERROR;
352     status = U_ZERO_ERROR;
353     body->setTime( fR - (nZoneOffset + nDSTOffset), status );
354     if ( !U_SUCCESS(status) ) throw ERROR;
355     getValue();
356 }
357 
358 double SAL_CALL
getLocalDateTime()359 Calendar_gregorian::getLocalDateTime()
360 {
361     if (fieldSet) {
362         setValue();
363         getValue();
364     }
365     UErrorCode status = U_ZERO_ERROR;
366     double fTime = body->getTime( status );
367     if ( !U_SUCCESS(status) ) throw ERROR;
368     status = U_ZERO_ERROR;
369     int32_t nZoneOffset = body->get( UCAL_ZONE_OFFSET, status );
370     if ( !U_SUCCESS(status) ) throw ERROR;
371     status = U_ZERO_ERROR;
372     int32_t nDSTOffset = body->get( UCAL_DST_OFFSET, status );
373     if ( !U_SUCCESS(status) ) throw ERROR;
374     return (fTime + (nZoneOffset + nDSTOffset)) / U_MILLIS_PER_DAY;
375 }
376 
setTimeZone(const OUString & rTimeZone)377 bool Calendar_gregorian::setTimeZone( const OUString& rTimeZone )
378 {
379     if (fieldSet)
380     {
381         setValue();
382         getValue();
383     }
384     const icu::UnicodeString aID( reinterpret_cast<const UChar*>(rTimeZone.getStr()), rTimeZone.getLength());
385     const std::unique_ptr<const icu::TimeZone> pTZ( icu::TimeZone::createTimeZone(aID));
386     if (!pTZ)
387         return false;
388 
389     body->setTimeZone(*pTZ);
390     return true;
391 }
392 
393 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
394 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
mapFromGregorian()395 void Calendar_gregorian::mapFromGregorian()
396 {
397     if (eraArray) {
398         sal_Int16 e, y, m, d;
399 
400         e = fieldValue[CalendarFieldIndex::ERA];
401         y = fieldValue[CalendarFieldIndex::YEAR];
402         m = fieldValue[CalendarFieldIndex::MONTH] + 1;
403         d = fieldValue[CalendarFieldIndex::DAY_OF_MONTH];
404 
405         // since the year is reversed for first era, it is reversed again here for Era compare.
406         if (e == 0)
407             y = 1 - y;
408 
409         for (e = 0; eraArray[e].year; e++)
410             if ((y != eraArray[e].year) ? y < eraArray[e].year :
411                     (m != eraArray[e].month) ? m < eraArray[e].month : d < eraArray[e].day)
412                 break;
413 
414         fieldValue[CalendarFieldIndex::ERA] = e;
415         fieldValue[CalendarFieldIndex::YEAR] =
416             sal::static_int_cast<sal_Int16>( (e == 0) ? (eraArray[0].year - y) : (y - eraArray[e-1].year + 1) );
417     }
418 }
419 
420 #define FIELDS  ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
421 // map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
422 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
mapToGregorian()423 void Calendar_gregorian::mapToGregorian()
424 {
425     if (eraArray && (fieldSet & FIELDS)) {
426         sal_Int16 y, e = fieldValue[CalendarFieldIndex::ERA];
427         if (e == 0)
428             y = sal::static_int_cast<sal_Int16>( eraArray[0].year - fieldValue[CalendarFieldIndex::YEAR] );
429         else
430             y = sal::static_int_cast<sal_Int16>( eraArray[e-1].year + fieldValue[CalendarFieldIndex::YEAR] - 1 );
431 
432         fieldSetValue[CalendarFieldIndex::ERA] = y <= 0 ? 0 : 1;
433         fieldSetValue[CalendarFieldIndex::YEAR] = (y <= 0 ? 1 - y : y);
434         fieldSet |= FIELDS;
435     }
436 }
437 
438 /// @throws RuntimeException
fieldNameConverter(sal_Int16 fieldIndex)439 static UCalendarDateFields fieldNameConverter(sal_Int16 fieldIndex)
440 {
441     UCalendarDateFields f;
442 
443     switch (fieldIndex) {
444         case CalendarFieldIndex::AM_PM:             f = UCAL_AM_PM; break;
445         case CalendarFieldIndex::DAY_OF_MONTH:      f = UCAL_DATE; break;
446         case CalendarFieldIndex::DAY_OF_WEEK:       f = UCAL_DAY_OF_WEEK; break;
447         case CalendarFieldIndex::DAY_OF_YEAR:       f = UCAL_DAY_OF_YEAR; break;
448         case CalendarFieldIndex::DST_OFFSET:        f = UCAL_DST_OFFSET; break;
449         case CalendarFieldIndex::ZONE_OFFSET:       f = UCAL_ZONE_OFFSET; break;
450         case CalendarFieldIndex::HOUR:              f = UCAL_HOUR_OF_DAY; break;
451         case CalendarFieldIndex::MINUTE:            f = UCAL_MINUTE; break;
452         case CalendarFieldIndex::SECOND:            f = UCAL_SECOND; break;
453         case CalendarFieldIndex::MILLISECOND:       f = UCAL_MILLISECOND; break;
454         case CalendarFieldIndex::WEEK_OF_MONTH:     f = UCAL_WEEK_OF_MONTH; break;
455         case CalendarFieldIndex::WEEK_OF_YEAR:      f = UCAL_WEEK_OF_YEAR; break;
456         case CalendarFieldIndex::YEAR:              f = UCAL_YEAR; break;
457         case CalendarFieldIndex::MONTH:             f = UCAL_MONTH; break;
458         case CalendarFieldIndex::ERA:               f = UCAL_ERA; break;
459         default: throw ERROR;
460     }
461     return f;
462 }
463 
464 void SAL_CALL
setValue(sal_Int16 fieldIndex,sal_Int16 value)465 Calendar_gregorian::setValue( sal_Int16 fieldIndex, sal_Int16 value )
466 {
467     if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
468         throw ERROR;
469     fieldSet |= (1 << fieldIndex);
470     fieldValue[fieldIndex] = value;
471 }
472 
getCombinedOffset(sal_Int32 & o_nOffset,sal_Int16 nParentFieldIndex,sal_Int16 nChildFieldIndex) const473 bool Calendar_gregorian::getCombinedOffset( sal_Int32 & o_nOffset,
474         sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex ) const
475 {
476     o_nOffset = 0;
477     bool bFieldsSet = false;
478     if (fieldSet & (1 << nParentFieldIndex))
479     {
480         bFieldsSet = true;
481         o_nOffset = static_cast<sal_Int32>( fieldValue[nParentFieldIndex]) * 60000;
482     }
483     if (fieldSet & (1 << nChildFieldIndex))
484     {
485         bFieldsSet = true;
486         if (o_nOffset < 0)
487             o_nOffset -= static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
488         else
489             o_nOffset += static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
490     }
491     return bFieldsSet;
492 }
493 
getZoneOffset(sal_Int32 & o_nOffset) const494 bool Calendar_gregorian::getZoneOffset( sal_Int32 & o_nOffset ) const
495 {
496     return getCombinedOffset( o_nOffset, CalendarFieldIndex::ZONE_OFFSET,
497             CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
498 }
499 
getDSTOffset(sal_Int32 & o_nOffset) const500 bool Calendar_gregorian::getDSTOffset( sal_Int32 & o_nOffset ) const
501 {
502     return getCombinedOffset( o_nOffset, CalendarFieldIndex::DST_OFFSET,
503             CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
504 }
505 
submitFields()506 void Calendar_gregorian::submitFields()
507 {
508     for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
509     {
510         if (fieldSet & (1 << fieldIndex))
511         {
512             switch (fieldIndex)
513             {
514                 default:
515                     body->set(fieldNameConverter(fieldIndex), fieldSetValue[fieldIndex]);
516                     break;
517                 case CalendarFieldIndex::ZONE_OFFSET:
518                 case CalendarFieldIndex::DST_OFFSET:
519                 case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS:
520                 case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS:
521                     break;  // nothing, extra handling
522             }
523         }
524     }
525     sal_Int32 nZoneOffset, nDSTOffset;
526     if (getZoneOffset( nZoneOffset))
527         body->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET), nZoneOffset);
528     if (getDSTOffset( nDSTOffset))
529         body->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET), nDSTOffset);
530 }
531 
setValue()532 void Calendar_gregorian::setValue()
533 {
534     // Copy fields before calling submitFields() directly or indirectly below.
535     memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
536     // Possibly setup ERA and YEAR in fieldSetValue.
537     mapToGregorian();
538 
539     DUMP_ICU_CAL_MSG(("%s\n","setValue() before submission"));
540     DUMP_I18N_CAL_MSG(("%s\n","setValue() before submission"));
541 
542     submitFields();
543 
544     DUMP_ICU_CAL_MSG(("%s\n","setValue() after submission"));
545     DUMP_I18N_CAL_MSG(("%s\n","setValue() after submission"));
546 
547 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
548     {
549         // force icu::Calendar to recalculate
550         UErrorCode status;
551         sal_Int32 nTmp = body->get( UCAL_DATE, status = U_ZERO_ERROR);
552         DUMP_ICU_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
553         DUMP_I18N_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
554     }
555 #endif
556 }
557 
getValue()558 void Calendar_gregorian::getValue()
559 {
560     DUMP_ICU_CAL_MSG(("%s\n","getValue()"));
561     DUMP_I18N_CAL_MSG(("%s\n","getValue()"));
562     for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
563     {
564         if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS ||
565                 fieldIndex == CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS)
566             continue;   // not ICU fields
567 
568         UErrorCode status = U_ZERO_ERROR;
569         sal_Int32 value = body->get( fieldNameConverter(
570                     fieldIndex), status);
571         if ( !U_SUCCESS(status) ) throw ERROR;
572 
573         // Convert millisecond to minute for ZONE and DST and set remainder in
574         // second field.
575         if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET)
576         {
577             sal_Int32 nMinutes = value / 60000;
578             sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
579                         abs( value - nMinutes * 60000)));
580             fieldValue[CalendarFieldIndex::ZONE_OFFSET] = static_cast<sal_Int16>( nMinutes);
581             fieldValue[CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS] = nMillis;
582         }
583         else if (fieldIndex == CalendarFieldIndex::DST_OFFSET)
584         {
585             sal_Int32 nMinutes = value / 60000;
586             sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
587                         abs( value - nMinutes * 60000)));
588             fieldValue[CalendarFieldIndex::DST_OFFSET] = static_cast<sal_Int16>( nMinutes);
589             fieldValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] = nMillis;
590         }
591         else
592             fieldValue[fieldIndex] = static_cast<sal_Int16>(value);
593 
594         // offset 1 since the value for week start day SunDay is different between Calendar and Weekdays.
595         if ( fieldIndex == CalendarFieldIndex::DAY_OF_WEEK )
596             fieldValue[fieldIndex]--;   // UCAL_SUNDAY:/* == 1 */ ==> Weekdays::SUNDAY /* ==0 */
597     }
598     mapFromGregorian();
599     fieldSet = 0;
600 }
601 
602 sal_Int16 SAL_CALL
getValue(sal_Int16 fieldIndex)603 Calendar_gregorian::getValue( sal_Int16 fieldIndex )
604 {
605     if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
606         throw ERROR;
607 
608     if (fieldSet)  {
609         setValue();
610         getValue();
611     }
612 
613     return fieldValue[fieldIndex];
614 }
615 
616 void SAL_CALL
addValue(sal_Int16 fieldIndex,sal_Int32 value)617 Calendar_gregorian::addValue( sal_Int16 fieldIndex, sal_Int32 value )
618 {
619     // since ZONE and DST could not be add, we don't need to convert value here
620     UErrorCode status = U_ZERO_ERROR;
621     body->add(fieldNameConverter(fieldIndex), value, status);
622     if ( !U_SUCCESS(status) ) throw ERROR;
623     getValue();
624 }
625 
626 sal_Bool SAL_CALL
isValid()627 Calendar_gregorian::isValid()
628 {
629     if (fieldSet) {
630         sal_Int32 tmp = fieldSet;
631         setValue();
632         memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
633         getValue();
634         for ( sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++ ) {
635             // compare only with fields that are set and reset fieldSet[]
636             if (tmp & (1 << fieldIndex)) {
637                 if (fieldSetValue[fieldIndex] != fieldValue[fieldIndex])
638                     return false;
639             }
640         }
641     }
642     return true;
643 }
644 
645 // NativeNumberMode has different meaning between Number and Calendar for Asian locales.
646 // Here is the mapping table
647 // calendar(q/y/m/d)    zh_CN           zh_TW           ja              ko
648 // NatNum1              NatNum1/1/7/7   NatNum1/1/7/7   NatNum1/1/4/4   NatNum1/1/7/7
649 // NatNum2              NatNum2/2/8/8   NatNum2/2/8/8   NatNum2/2/5/5   NatNum2/2/8/8
650 // NatNum3              NatNum3/3/3/3   NatNum3/3/3/3   NatNum3/3/3/3   NatNum3/3/3/3
651 // NatNum4                                                              NatNum9/9/11/11
652 
NatNumForCalendar(const css::lang::Locale & aLocale,sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode,sal_Int16 value)653 static sal_Int16 NatNumForCalendar(const css::lang::Locale& aLocale,
654         sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode, sal_Int16 value )
655 {
656     bool isShort = ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
657         nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR) && value >= 100) ||
658         nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
659         nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER;
660     bool isChinese = aLocale.Language == "zh";
661     bool isJapanese = aLocale.Language == "ja";
662     bool isKorean = aLocale.Language == "ko";
663 
664     if (isChinese || isJapanese || isKorean) {
665         switch (nNativeNumberMode) {
666             case NativeNumberMode::NATNUM1:
667                 if (!isShort)
668                     nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM4 : NativeNumberMode::NATNUM7;
669                 break;
670             case NativeNumberMode::NATNUM2:
671                 if (!isShort)
672                     nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM5 : NativeNumberMode::NATNUM8;
673                 break;
674             case NativeNumberMode::NATNUM3:
675                 break;
676             case NativeNumberMode::NATNUM4:
677                 if (isKorean)
678                     return isShort ? NativeNumberMode::NATNUM9 : NativeNumberMode::NATNUM11;
679                 [[fallthrough]];
680             default: return 0;
681         }
682     }
683     return nNativeNumberMode;
684 }
685 
DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode)686 static sal_Int32 DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode)
687 {
688     switch( nCalendarDisplayCode ) {
689         case CalendarDisplayCode::SHORT_DAY:
690         case CalendarDisplayCode::LONG_DAY:
691             return CalendarFieldIndex::DAY_OF_MONTH;
692         case CalendarDisplayCode::SHORT_DAY_NAME:
693         case CalendarDisplayCode::LONG_DAY_NAME:
694         case CalendarDisplayCode::NARROW_DAY_NAME:
695             return CalendarFieldIndex::DAY_OF_WEEK;
696         case CalendarDisplayCode::SHORT_QUARTER:
697         case CalendarDisplayCode::LONG_QUARTER:
698         case CalendarDisplayCode::SHORT_MONTH:
699         case CalendarDisplayCode::LONG_MONTH:
700         case CalendarDisplayCode::SHORT_MONTH_NAME:
701         case CalendarDisplayCode::LONG_MONTH_NAME:
702         case CalendarDisplayCode::NARROW_MONTH_NAME:
703         case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME:
704         case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME:
705         case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME:
706         case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME:
707         case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME:
708         case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME:
709             return CalendarFieldIndex::MONTH;
710         case CalendarDisplayCode::SHORT_YEAR:
711         case CalendarDisplayCode::LONG_YEAR:
712             return CalendarFieldIndex::YEAR;
713         case CalendarDisplayCode::SHORT_ERA:
714         case CalendarDisplayCode::LONG_ERA:
715             return CalendarFieldIndex::ERA;
716         case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
717         case CalendarDisplayCode::LONG_YEAR_AND_ERA:
718             return CalendarFieldIndex::YEAR;
719         default:
720             return 0;
721     }
722 }
723 
724 sal_Int16 SAL_CALL
getFirstDayOfWeek()725 Calendar_gregorian::getFirstDayOfWeek()
726 {
727     // UCAL_SUNDAY == 1, Weekdays::SUNDAY == 0 => offset -1
728     // Check for underflow just in case we're called "out of sync".
729     return ::std::max( sal::static_int_cast<sal_Int16>(0),
730             sal::static_int_cast<sal_Int16>( static_cast<sal_Int16>(
731                     body->getFirstDayOfWeek()) - 1));
732 }
733 
734 void SAL_CALL
setFirstDayOfWeek(sal_Int16 day)735 Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day )
736 {
737     // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
738     body->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek>( day + 1));
739 }
740 
741 void SAL_CALL
setMinimumNumberOfDaysForFirstWeek(sal_Int16 days)742 Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
743 {
744     aCalendar.MinimumNumberOfDaysForFirstWeek = days;
745     body->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days));
746 }
747 
748 sal_Int16 SAL_CALL
getMinimumNumberOfDaysForFirstWeek()749 Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek()
750 {
751     return aCalendar.MinimumNumberOfDaysForFirstWeek;
752 }
753 
754 sal_Int16 SAL_CALL
getNumberOfMonthsInYear()755 Calendar_gregorian::getNumberOfMonthsInYear()
756 {
757     return static_cast<sal_Int16>(aCalendar.Months.getLength());
758 }
759 
760 
761 sal_Int16 SAL_CALL
getNumberOfDaysInWeek()762 Calendar_gregorian::getNumberOfDaysInWeek()
763 {
764     return static_cast<sal_Int16>(aCalendar.Days.getLength());
765 }
766 
767 
768 Sequence< CalendarItem > SAL_CALL
getDays()769 Calendar_gregorian::getDays()
770 {
771     return LocaleDataImpl::downcastCalendarItems( aCalendar.Days);
772 }
773 
774 
775 Sequence< CalendarItem > SAL_CALL
getMonths()776 Calendar_gregorian::getMonths()
777 {
778     return LocaleDataImpl::downcastCalendarItems( aCalendar.Months);
779 }
780 
781 
782 Sequence< CalendarItem2 > SAL_CALL
getDays2()783 Calendar_gregorian::getDays2()
784 {
785     return aCalendar.Days;
786 }
787 
788 
789 Sequence< CalendarItem2 > SAL_CALL
getMonths2()790 Calendar_gregorian::getMonths2()
791 {
792     return aCalendar.Months;
793 }
794 
795 
796 Sequence< CalendarItem2 > SAL_CALL
getGenitiveMonths2()797 Calendar_gregorian::getGenitiveMonths2()
798 {
799     return aCalendar.GenitiveMonths;
800 }
801 
802 
803 Sequence< CalendarItem2 > SAL_CALL
getPartitiveMonths2()804 Calendar_gregorian::getPartitiveMonths2()
805 {
806     return aCalendar.PartitiveMonths;
807 }
808 
809 
810 OUString SAL_CALL
getDisplayName(sal_Int16 displayIndex,sal_Int16 idx,sal_Int16 nameType)811 Calendar_gregorian::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
812 {
813     OUString aStr;
814 
815     switch( displayIndex ) {
816         case CalendarDisplayIndex::AM_PM:/* ==0 */
817             if (idx == 0) aStr = LocaleDataImpl::get()->getLocaleItem(aLocale).timeAM;
818             else if (idx == 1) aStr = LocaleDataImpl::get()->getLocaleItem(aLocale).timePM;
819             else throw ERROR;
820             break;
821         case CalendarDisplayIndex::DAY:
822             if( idx >= aCalendar.Days.getLength() ) throw ERROR;
823             if (nameType == 0) aStr = aCalendar.Days[idx].AbbrevName;
824             else if (nameType == 1) aStr = aCalendar.Days[idx].FullName;
825             else if (nameType == 2) aStr = aCalendar.Days[idx].NarrowName;
826             else throw ERROR;
827             break;
828         case CalendarDisplayIndex::MONTH:
829             if( idx >= aCalendar.Months.getLength() ) throw ERROR;
830             if (nameType == 0) aStr = aCalendar.Months[idx].AbbrevName;
831             else if (nameType == 1) aStr = aCalendar.Months[idx].FullName;
832             else if (nameType == 2) aStr = aCalendar.Months[idx].NarrowName;
833             else throw ERROR;
834             break;
835         case CalendarDisplayIndex::GENITIVE_MONTH:
836             if( idx >= aCalendar.GenitiveMonths.getLength() ) throw ERROR;
837             if (nameType == 0) aStr = aCalendar.GenitiveMonths[idx].AbbrevName;
838             else if (nameType == 1) aStr = aCalendar.GenitiveMonths[idx].FullName;
839             else if (nameType == 2) aStr = aCalendar.GenitiveMonths[idx].NarrowName;
840             else throw ERROR;
841             break;
842         case CalendarDisplayIndex::PARTITIVE_MONTH:
843             if( idx >= aCalendar.PartitiveMonths.getLength() ) throw ERROR;
844             if (nameType == 0) aStr = aCalendar.PartitiveMonths[idx].AbbrevName;
845             else if (nameType == 1) aStr = aCalendar.PartitiveMonths[idx].FullName;
846             else if (nameType == 2) aStr = aCalendar.PartitiveMonths[idx].NarrowName;
847             else throw ERROR;
848             break;
849         case CalendarDisplayIndex::ERA:
850             if( idx >= aCalendar.Eras.getLength() ) throw ERROR;
851             if (nameType == 0) aStr = aCalendar.Eras[idx].AbbrevName;
852             else if (nameType == 1) aStr = aCalendar.Eras[idx].FullName;
853             else throw ERROR;
854             break;
855         case CalendarDisplayIndex::YEAR:
856             break;
857         default:
858             throw ERROR;
859     }
860     return aStr;
861 }
862 
863 // Methods in XExtendedCalendar
864 OUString SAL_CALL
getDisplayString(sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode)865 Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
866 {
867     return getDisplayStringImpl( nCalendarDisplayCode, nNativeNumberMode, false);
868 }
869 
870 OUString
getDisplayStringImpl(sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode,bool bEraMode)871 Calendar_gregorian::getDisplayStringImpl( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode, bool bEraMode )
872 {
873     sal_Int16 value = getValue(sal::static_int_cast<sal_Int16>( DisplayCode2FieldIndex(nCalendarDisplayCode) ));
874     OUString aOUStr;
875 
876     if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
877             nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER) {
878         Sequence< OUString> xR = LocaleDataImpl::get()->getReservedWord(aLocale);
879         sal_Int16 quarter = value / 3;
880         // Since this base class method may be called by derived calendar
881         // classes where a year consists of more than 12 months we need a check
882         // to not run out of bounds of reserved quarter words. Perhaps a more
883         // clean way (instead of dividing by 3) would be to first get the
884         // number of months, divide by 4 and then use that result to divide the
885         // actual month value.
886         if ( quarter > 3 )
887             quarter = 3;
888         quarter = sal::static_int_cast<sal_Int16>( quarter +
889             ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER) ?
890             reservedWords::QUARTER1_ABBREVIATION : reservedWords::QUARTER1_WORD) );
891         aOUStr = xR[quarter];
892     } else {
893         // The "#100211# - checked" comments serve for detection of "use of
894         // sprintf is safe here" conditions. An sprintf encountered without
895         // having that comment triggers alarm ;-)
896         sal_Char aStr[10];
897         switch( nCalendarDisplayCode ) {
898             case CalendarDisplayCode::SHORT_MONTH:
899                 value += 1;     // month is zero based
900                 [[fallthrough]];
901             case CalendarDisplayCode::SHORT_DAY:
902                 sprintf(aStr, "%d", value);     // #100211# - checked
903                 break;
904             case CalendarDisplayCode::LONG_YEAR:
905                 if ( aCalendar.Name == "gengou" )
906                     sprintf(aStr, "%02d", value);     // #100211# - checked
907                 else
908                     sprintf(aStr, "%d", value);     // #100211# - checked
909                 break;
910             case CalendarDisplayCode::LONG_MONTH:
911                 value += 1;     // month is zero based
912                 sprintf(aStr, "%02d", value);   // #100211# - checked
913                 break;
914             case CalendarDisplayCode::SHORT_YEAR:
915                 // Take last 2 digits, or only one if value<10, for example,
916                 // in case of the Gengou calendar. For combined era+year always
917                 // the full year is displayed, without leading 0.
918                 // Workaround for non-combined calls in certain calendars is
919                 // the kDisplayEraForcedLongYear flag, but this also could get
920                 // called for YY not only E format codes, no differentiation
921                 // possible here; the good news is that usually the Gregorian
922                 // calendar is the default and hence YY calls for Gregorian and
923                 // E for the other calendar and currently (2013-02-28) ROC is
924                 // the only calendar using this.
925                 // See i#116701 and fdo#60915
926                 if (value < 100 || bEraMode || (eraArray && (eraArray[0].flags & kDisplayEraForcedLongYear)))
927                     sprintf(aStr, "%d", value); // #100211# - checked
928                 else
929                     sprintf(aStr, "%02d", value % 100); // #100211# - checked
930                 break;
931             case CalendarDisplayCode::LONG_DAY:
932                 sprintf(aStr, "%02d", value);   // #100211# - checked
933                 break;
934 
935             case CalendarDisplayCode::SHORT_DAY_NAME:
936                 return getDisplayName(CalendarDisplayIndex::DAY, value, 0);
937             case CalendarDisplayCode::LONG_DAY_NAME:
938                 return getDisplayName(CalendarDisplayIndex::DAY, value, 1);
939             case CalendarDisplayCode::NARROW_DAY_NAME:
940                 return getDisplayName(CalendarDisplayIndex::DAY, value, 2);
941             case CalendarDisplayCode::SHORT_MONTH_NAME:
942                 return getDisplayName(CalendarDisplayIndex::MONTH, value, 0);
943             case CalendarDisplayCode::LONG_MONTH_NAME:
944                 return getDisplayName(CalendarDisplayIndex::MONTH, value, 1);
945             case CalendarDisplayCode::NARROW_MONTH_NAME:
946                 return getDisplayName(CalendarDisplayIndex::MONTH, value, 2);
947             case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME:
948                 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 0);
949             case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME:
950                 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 1);
951             case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME:
952                 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 2);
953             case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME:
954                 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 0);
955             case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME:
956                 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 1);
957             case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME:
958                 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 2);
959             case CalendarDisplayCode::SHORT_ERA:
960                 return getDisplayName(CalendarDisplayIndex::ERA, value, 0);
961             case CalendarDisplayCode::LONG_ERA:
962                 return getDisplayName(CalendarDisplayIndex::ERA, value, 1);
963 
964             case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
965                 return  getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode, true ) +
966                     getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode, true );
967 
968             case CalendarDisplayCode::LONG_YEAR_AND_ERA:
969                 return  getDisplayStringImpl( CalendarDisplayCode::LONG_ERA, nNativeNumberMode, true ) +
970                     getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode, true );
971 
972             default:
973                 throw ERROR;
974         }
975         aOUStr = OUString::createFromAscii(aStr);
976     }
977     // NatNum12 used only for selected parts
978     if (nNativeNumberMode > 0 && nNativeNumberMode != 12) {
979         // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
980         if (eraArray == gengou_eraArray && value == 1
981             && (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
982                 nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR)
983             && (nNativeNumberMode == NativeNumberMode::NATNUM1 ||
984                 nNativeNumberMode == NativeNumberMode::NATNUM2)) {
985             static sal_Unicode gan = 0x5143;
986             return OUString(&gan, 1);
987         }
988         sal_Int16 nNatNum = NatNumForCalendar(aLocale, nCalendarDisplayCode, nNativeNumberMode, value);
989         if (nNatNum > 0)
990             return mxNatNum->getNativeNumberString(aOUStr, aLocale, nNatNum);
991     }
992     return aOUStr;
993 }
994 
995 // Methods in XExtendedCalendar
996 OUString SAL_CALL
getDisplayString(sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode)997 Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
998 {
999     // make year and era in different order for year before and after 0.
1000     if ((nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA ||
1001                 nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR_AND_ERA) &&
1002             getValue(CalendarFieldIndex::ERA) == 0) {
1003         if (nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA)
1004             return  getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode, true ) +
1005                 getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode, true );
1006         else
1007             return  getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode, true ) +
1008                 getDisplayStringImpl( CalendarDisplayCode::LONG_ERA, nNativeNumberMode, true );
1009     }
1010     return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
1011 }
1012 
1013 OUString SAL_CALL
getImplementationName()1014 Calendar_gregorian::getImplementationName()
1015 {
1016     return OUString::createFromAscii(cCalendar);
1017 }
1018 
1019 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)1020 Calendar_gregorian::supportsService(const OUString& rServiceName)
1021 {
1022     return cppu::supportsService(this, rServiceName);
1023 }
1024 
1025 Sequence< OUString > SAL_CALL
getSupportedServiceNames()1026 Calendar_gregorian::getSupportedServiceNames()
1027 {
1028     Sequence< OUString > aRet { OUString::createFromAscii(cCalendar) };
1029     return aRet;
1030 }
1031 
1032 }
1033 
1034 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1035