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.
debug_cal_loc(const char * f,int32_t l)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>
debug_cal_msg(const char * pat,...)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 & );
debug_icu_cal_dump(const::icu::Calendar & r)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
debug_cal_millis_to_time(long nMillis,long & h,long & m,long & s,long & f)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 }
debug_i18n_cal_dump(const::icu::Calendar & r)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
Calendar_gregorian()132 Calendar_gregorian::Calendar_gregorian()
133 {
134 init(NULL);
135 }
Calendar_gregorian(Era * _eraArray)136 Calendar_gregorian::Calendar_gregorian(Era *_eraArray)
137 {
138 init(_eraArray);
139 }
140 void SAL_CALL
init(Era * _eraArray)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
~Calendar_gregorian()183 Calendar_gregorian::~Calendar_gregorian()
184 {
185 delete body;
186 }
187
Calendar_hanja()188 Calendar_hanja::Calendar_hanja()
189 {
190 cCalendar = "com.sun.star.i18n.Calendar_hanja";
191 }
192
193 OUString SAL_CALL
getDisplayName(sal_Int16 displayIndex,sal_Int16 idx,sal_Int16 nameType)194 Calendar_hanja::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
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
loadCalendar(const OUString &,const com::sun::star::lang::Locale & rLocale)209 Calendar_hanja::loadCalendar( const OUString& /*uniqueID*/, const com::sun::star::lang::Locale& rLocale )
210 {
211 // Since this class could be called by service name 'hanja_yoil', we have to
212 // rename uniqueID to get right calendar defined in locale data.
213 Calendar_gregorian::loadCalendar(OUString::createFromAscii("hanja"), rLocale);
214 }
215
216 static Era gengou_eraArray[] = {
217 {1868, 1, 1},
218 {1912, 7, 30},
219 {1926, 12, 25},
220 {1989, 1, 8},
221 {2019, 5, 1},
222 {0, 0, 0}
223 };
Calendar_gengou()224 Calendar_gengou::Calendar_gengou() : Calendar_gregorian(gengou_eraArray)
225 {
226 cCalendar = "com.sun.star.i18n.Calendar_gengou";
227 }
228
229 static Era ROC_eraArray[] = {
230 {1912, 1, 1},
231 {0, 0, 0}
232 };
Calendar_ROC()233 Calendar_ROC::Calendar_ROC() : Calendar_gregorian(ROC_eraArray)
234 {
235 cCalendar = "com.sun.star.i18n.Calendar_ROC";
236 }
237
238 static Era buddhist_eraArray[] = {
239 {-542, 1, 1},
240 {0, 0, 0}
241 };
Calendar_buddhist()242 Calendar_buddhist::Calendar_buddhist() : Calendar_gregorian(buddhist_eraArray)
243 {
244 cCalendar = "com.sun.star.i18n.Calendar_buddhist";
245 }
246
247 void SAL_CALL
loadCalendar(const OUString & uniqueID,const com::sun::star::lang::Locale & rLocale)248 Calendar_gregorian::loadCalendar( const OUString& uniqueID, const com::sun::star::lang::Locale& rLocale )
249 {
250 // init. fieldValue[]
251 getValue();
252
253 aLocale = rLocale;
254 Sequence< Calendar> xC = LocaleData().getAllCalendars(rLocale);
255 for (sal_Int32 i = 0; i < xC.getLength(); i++)
256 {
257 if (uniqueID == xC[i].Name)
258 {
259 aCalendar = xC[i];
260 // setup minimalDaysInFirstWeek
261 setMinimumNumberOfDaysForFirstWeek(
262 aCalendar.MinimumNumberOfDaysForFirstWeek);
263 // setup first day of week
264 for (sal_Int16 day = sal::static_int_cast<sal_Int16>(
265 aCalendar.Days.getLength()-1); day>=0; day--)
266 {
267 if (aCalendar.StartOfWeek == aCalendar.Days[day].ID)
268 {
269 setFirstDayOfWeek( day);
270 return;
271 }
272 }
273 }
274 }
275 // Calendar is not for the locale
276 throw ERROR;
277 }
278
279
280 com::sun::star::i18n::Calendar SAL_CALL
getLoadedCalendar()281 Calendar_gregorian::getLoadedCalendar()
282 {
283 return aCalendar;
284 }
285
286 OUString SAL_CALL
getUniqueID()287 Calendar_gregorian::getUniqueID()
288 {
289 return aCalendar.Name;
290 }
291
292 void SAL_CALL
setDateTime(double timeInDays)293 Calendar_gregorian::setDateTime( double timeInDays )
294 {
295 UErrorCode status;
296 body->setTime(timeInDays * U_MILLIS_PER_DAY, status = U_ZERO_ERROR);
297 if ( !U_SUCCESS(status) ) throw ERROR;
298 getValue();
299 }
300
301 double SAL_CALL
getDateTime()302 Calendar_gregorian::getDateTime()
303 {
304 if (fieldSet) {
305 setValue();
306 getValue();
307 }
308 UErrorCode status;
309 double r = body->getTime(status = U_ZERO_ERROR);
310 if ( !U_SUCCESS(status) ) throw ERROR;
311 return r / U_MILLIS_PER_DAY;
312 }
313
314 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
315 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
mapFromGregorian()316 void Calendar_gregorian::mapFromGregorian()
317 {
318 if (eraArray) {
319 sal_Int16 e, y, m, d;
320
321 e = fieldValue[CalendarFieldIndex::ERA];
322 y = fieldValue[CalendarFieldIndex::YEAR];
323 m = fieldValue[CalendarFieldIndex::MONTH] + 1;
324 d = fieldValue[CalendarFieldIndex::DAY_OF_MONTH];
325
326 // since the year is reversed for first era, it is reversed again here for Era compare.
327 if (e == 0)
328 y = 1 - y;
329
330 for (e = 0; eraArray[e].year; e++)
331 if ((y != eraArray[e].year) ? y < eraArray[e].year :
332 (m != eraArray[e].month) ? m < eraArray[e].month : d < eraArray[e].day)
333 break;
334
335 fieldValue[CalendarFieldIndex::ERA] = e;
336 fieldValue[CalendarFieldIndex::YEAR] =
337 sal::static_int_cast<sal_Int16>( (e == 0) ? (eraArray[0].year - y) : (y - eraArray[e-1].year + 1) );
338 }
339 }
340
341 #define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
342 // map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
343 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
mapToGregorian()344 void Calendar_gregorian::mapToGregorian()
345 {
346 if (eraArray && (fieldSet & FIELDS)) {
347 sal_Int16 y, e = fieldValue[CalendarFieldIndex::ERA];
348 if (e == 0)
349 y = sal::static_int_cast<sal_Int16>( eraArray[0].year - fieldValue[CalendarFieldIndex::YEAR] );
350 else
351 y = sal::static_int_cast<sal_Int16>( eraArray[e-1].year + fieldValue[CalendarFieldIndex::YEAR] - 1 );
352
353 fieldSetValue[CalendarFieldIndex::ERA] = y <= 0 ? 0 : 1;
354 fieldSetValue[CalendarFieldIndex::YEAR] = (y <= 0 ? 1 - y : y);
355 fieldSet |= FIELDS;
356 }
357 }
358
fieldNameConverter(sal_Int16 fieldIndex)359 static UCalendarDateFields fieldNameConverter(sal_Int16 fieldIndex)
360 {
361 UCalendarDateFields f;
362
363 switch (fieldIndex) {
364 case CalendarFieldIndex::AM_PM: f = UCAL_AM_PM; break;
365 case CalendarFieldIndex::DAY_OF_MONTH: f = UCAL_DATE; break;
366 case CalendarFieldIndex::DAY_OF_WEEK: f = UCAL_DAY_OF_WEEK; break;
367 case CalendarFieldIndex::DAY_OF_YEAR: f = UCAL_DAY_OF_YEAR; break;
368 case CalendarFieldIndex::DST_OFFSET: f = UCAL_DST_OFFSET; break;
369 case CalendarFieldIndex::ZONE_OFFSET: f = UCAL_ZONE_OFFSET; break;
370 case CalendarFieldIndex::HOUR: f = UCAL_HOUR_OF_DAY; break;
371 case CalendarFieldIndex::MINUTE: f = UCAL_MINUTE; break;
372 case CalendarFieldIndex::SECOND: f = UCAL_SECOND; break;
373 case CalendarFieldIndex::MILLISECOND: f = UCAL_MILLISECOND; break;
374 case CalendarFieldIndex::WEEK_OF_MONTH: f = UCAL_WEEK_OF_MONTH; break;
375 case CalendarFieldIndex::WEEK_OF_YEAR: f = UCAL_WEEK_OF_YEAR; break;
376 case CalendarFieldIndex::YEAR: f = UCAL_YEAR; break;
377 case CalendarFieldIndex::MONTH: f = UCAL_MONTH; break;
378 case CalendarFieldIndex::ERA: f = UCAL_ERA; break;
379 default: throw ERROR;
380 }
381 return f;
382 }
383
384 void SAL_CALL
setValue(sal_Int16 fieldIndex,sal_Int16 value)385 Calendar_gregorian::setValue( sal_Int16 fieldIndex, sal_Int16 value )
386 {
387 if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
388 throw ERROR;
389 fieldSet |= (1 << fieldIndex);
390 fieldValue[fieldIndex] = value;
391 }
392
getCombinedOffset(sal_Int32 & o_nOffset,sal_Int16 nParentFieldIndex,sal_Int16 nChildFieldIndex) const393 bool Calendar_gregorian::getCombinedOffset( sal_Int32 & o_nOffset,
394 sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex ) const
395 {
396 o_nOffset = 0;
397 bool bFieldsSet = false;
398 if (fieldSet & (1 << nParentFieldIndex))
399 {
400 bFieldsSet = true;
401 o_nOffset = static_cast<sal_Int32>( fieldValue[nParentFieldIndex]) * 60000;
402 }
403 if (fieldSet & (1 << nChildFieldIndex))
404 {
405 bFieldsSet = true;
406 if (o_nOffset < 0)
407 o_nOffset -= static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
408 else
409 o_nOffset += static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
410 }
411 return bFieldsSet;
412 }
413
getZoneOffset(sal_Int32 & o_nOffset) const414 bool Calendar_gregorian::getZoneOffset( sal_Int32 & o_nOffset ) const
415 {
416 return getCombinedOffset( o_nOffset, CalendarFieldIndex::ZONE_OFFSET,
417 CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
418 }
419
getDSTOffset(sal_Int32 & o_nOffset) const420 bool Calendar_gregorian::getDSTOffset( sal_Int32 & o_nOffset ) const
421 {
422 return getCombinedOffset( o_nOffset, CalendarFieldIndex::DST_OFFSET,
423 CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
424 }
425
submitFields()426 void Calendar_gregorian::submitFields()
427 {
428 for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
429 {
430 if (fieldSet & (1 << fieldIndex))
431 {
432 switch (fieldIndex)
433 {
434 default:
435 body->set(fieldNameConverter(fieldIndex), fieldSetValue[fieldIndex]);
436 break;
437 case CalendarFieldIndex::ZONE_OFFSET:
438 case CalendarFieldIndex::DST_OFFSET:
439 case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS:
440 case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS:
441 break; // nothing, extra handling
442 }
443 }
444 }
445 sal_Int32 nZoneOffset, nDSTOffset;
446 if (getZoneOffset( nZoneOffset))
447 body->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET), nZoneOffset);
448 if (getDSTOffset( nDSTOffset))
449 body->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET), nDSTOffset);
450 }
451
submitValues(sal_Int32 nYear,sal_Int32 nMonth,sal_Int32 nDay,sal_Int32 nHour,sal_Int32 nMinute,sal_Int32 nSecond,sal_Int32 nMilliSecond,sal_Int32 nZone,sal_Int32 nDST)452 void Calendar_gregorian::submitValues( sal_Int32 nYear,
453 sal_Int32 nMonth, sal_Int32 nDay, sal_Int32 nHour, sal_Int32 nMinute,
454 sal_Int32 nSecond, sal_Int32 nMilliSecond, sal_Int32 nZone, sal_Int32 nDST )
455 {
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
lcl_setCombinedOffsetFieldValues(sal_Int32 nValue,sal_Int16 rFieldSetValue[],sal_Int16 rFieldValue[],sal_Int16 nParentFieldIndex,sal_Int16 nChildFieldIndex)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
setValue()489 void Calendar_gregorian::setValue()
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
getValue()703 void Calendar_gregorian::getValue()
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
getValue(sal_Int16 fieldIndex)747 Calendar_gregorian::getValue( sal_Int16 fieldIndex )
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
addValue(sal_Int16 fieldIndex,sal_Int32 value)761 Calendar_gregorian::addValue( sal_Int16 fieldIndex, sal_Int32 value )
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
isValid()771 Calendar_gregorian::isValid()
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
NatNumForCalendar(const com::sun::star::lang::Locale & aLocale,sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode,sal_Int16 value)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
DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode)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
getFirstDayOfWeek()861 Calendar_gregorian::getFirstDayOfWeek()
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
setFirstDayOfWeek(sal_Int16 day)871 Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day )
872 {
873 // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
874 body->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek>( day + 1));
875 }
876
877 void SAL_CALL
setMinimumNumberOfDaysForFirstWeek(sal_Int16 days)878 Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
879 {
880 aCalendar.MinimumNumberOfDaysForFirstWeek = days;
881 body->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days));
882 }
883
884 sal_Int16 SAL_CALL
getMinimumNumberOfDaysForFirstWeek()885 Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek()
886 {
887 return aCalendar.MinimumNumberOfDaysForFirstWeek;
888 }
889
890 sal_Int16 SAL_CALL
getNumberOfMonthsInYear()891 Calendar_gregorian::getNumberOfMonthsInYear()
892 {
893 return (sal_Int16) aCalendar.Months.getLength();
894 }
895
896
897 sal_Int16 SAL_CALL
getNumberOfDaysInWeek()898 Calendar_gregorian::getNumberOfDaysInWeek()
899 {
900 return (sal_Int16) aCalendar.Days.getLength();
901 }
902
903
904 Sequence< CalendarItem > SAL_CALL
getMonths()905 Calendar_gregorian::getMonths()
906 {
907 return aCalendar.Months;
908 }
909
910
911 Sequence< CalendarItem > SAL_CALL
getDays()912 Calendar_gregorian::getDays()
913 {
914 return aCalendar.Days;
915 }
916
917 OUString SAL_CALL
getDisplayName(sal_Int16 displayIndex,sal_Int16 idx,sal_Int16 nameType)918 Calendar_gregorian::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
919 {
920 OUString aStr;
921
922 switch( displayIndex ) {
923 case CalendarDisplayIndex::AM_PM:/* ==0 */
924 if (idx == 0) aStr = LocaleData().getLocaleItem(aLocale).timeAM;
925 else if (idx == 1) aStr = LocaleData().getLocaleItem(aLocale).timePM;
926 else throw ERROR;
927 break;
928 case CalendarDisplayIndex::DAY:
929 if( idx >= aCalendar.Days.getLength() ) throw ERROR;
930 if (nameType == 0) aStr = aCalendar.Days[idx].AbbrevName;
931 else if (nameType == 1) aStr = aCalendar.Days[idx].FullName;
932 else throw ERROR;
933 break;
934 case CalendarDisplayIndex::MONTH:
935 if( idx >= aCalendar.Months.getLength() ) throw ERROR;
936 if (nameType == 0) aStr = aCalendar.Months[idx].AbbrevName;
937 else if (nameType == 1) aStr = aCalendar.Months[idx].FullName;
938 else throw ERROR;
939 break;
940 case CalendarDisplayIndex::ERA:
941 if( idx >= aCalendar.Eras.getLength() ) throw ERROR;
942 if (nameType == 0) aStr = aCalendar.Eras[idx].AbbrevName;
943 else if (nameType == 1) aStr = aCalendar.Eras[idx].FullName;
944 else throw ERROR;
945 break;
946 case CalendarDisplayIndex::YEAR:
947 break;
948 default:
949 throw ERROR;
950 }
951 return aStr;
952 }
953
954 // Methods in XExtendedCalendar
955 OUString SAL_CALL
getDisplayString(sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode)956 Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
957 {
958 sal_Int16 value = getValue(sal::static_int_cast<sal_Int16>( DisplayCode2FieldIndex(nCalendarDisplayCode) ));
959 OUString aOUStr;
960
961 if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
962 nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER) {
963 Sequence< OUString> xR = LocaleData().getReservedWord(aLocale);
964 sal_Int16 quarter = value / 3;
965 // Since this base class method may be called by derived calendar
966 // classes where a year consists of more than 12 months we need a check
967 // to not run out of bounds of reserved quarter words. Perhaps a more
968 // clean way (instead of dividing by 3) would be to first get the
969 // number of months, divide by 4 and then use that result to divide the
970 // actual month value.
971 if ( quarter > 3 )
972 quarter = 3;
973 quarter = sal::static_int_cast<sal_Int16>( quarter +
974 ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER) ?
975 reservedWords::QUARTER1_ABBREVIATION : reservedWords::QUARTER1_WORD) );
976 aOUStr = xR[quarter];
977 } else {
978 // The "#100211# - checked" comments serve for detection of "use of
979 // sprintf is safe here" conditions. An sprintf encountered without
980 // having that comment triggers alarm ;-)
981 sal_Char aStr[10];
982 switch( nCalendarDisplayCode ) {
983 case CalendarDisplayCode::SHORT_MONTH:
984 value += 1; // month is zero based
985 // fall thru
986 case CalendarDisplayCode::SHORT_DAY:
987 sprintf(aStr, "%d", value); // #100211# - checked
988 break;
989 case CalendarDisplayCode::LONG_YEAR:
990 if (aCalendar.Name.equalsAscii("gengou"))
991 sprintf(aStr, "%02d", value); // #100211# - checked
992 else
993 sprintf(aStr, "%d", value); // #100211# - checked
994 break;
995 case CalendarDisplayCode::LONG_MONTH:
996 value += 1; // month is zero based
997 sprintf(aStr, "%02d", value); // #100211# - checked
998 break;
999 case CalendarDisplayCode::SHORT_YEAR:
1000 // Take last 2 digits, or only one if value<10, for example,
1001 // in case of the Gengou calendar.
1002 // #i116701# For values in non-Gregorian era years use all
1003 // digits.
1004 if (value < 100 || eraArray)
1005 sprintf(aStr, "%d", value); // #100211# - checked
1006 else
1007 sprintf(aStr, "%02d", value % 100); // #100211# - checked
1008 break;
1009 case CalendarDisplayCode::LONG_DAY:
1010 sprintf(aStr, "%02d", value); // #100211# - checked
1011 break;
1012
1013 case CalendarDisplayCode::SHORT_DAY_NAME:
1014 return getDisplayName(CalendarDisplayIndex::DAY, value, 0);
1015 case CalendarDisplayCode::LONG_DAY_NAME:
1016 return getDisplayName(CalendarDisplayIndex::DAY, value, 1);
1017 case CalendarDisplayCode::SHORT_MONTH_NAME:
1018 return getDisplayName(CalendarDisplayIndex::MONTH, value, 0);
1019 case CalendarDisplayCode::LONG_MONTH_NAME:
1020 return getDisplayName(CalendarDisplayIndex::MONTH, value, 1);
1021 case CalendarDisplayCode::SHORT_ERA:
1022 return getDisplayName(CalendarDisplayIndex::ERA, value, 0);
1023 case CalendarDisplayCode::LONG_ERA:
1024 return getDisplayName(CalendarDisplayIndex::ERA, value, 1);
1025
1026 case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
1027 return getDisplayString( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode ) +
1028 getDisplayString( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode );
1029
1030 case CalendarDisplayCode::LONG_YEAR_AND_ERA:
1031 return getDisplayString( CalendarDisplayCode::LONG_ERA, nNativeNumberMode ) +
1032 getDisplayString( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode );
1033
1034 default:
1035 throw ERROR;
1036 }
1037 aOUStr = OUString::createFromAscii(aStr);
1038 }
1039 if (nNativeNumberMode > 0) {
1040 // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
1041 if (eraArray == gengou_eraArray && value == 1
1042 && (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
1043 nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR)
1044 && (nNativeNumberMode == NativeNumberMode::NATNUM1 ||
1045 nNativeNumberMode == NativeNumberMode::NATNUM2)) {
1046 static sal_Unicode gan = 0x5143;
1047 return OUString(&gan, 1);
1048 }
1049 sal_Int16 nNatNum = NatNumForCalendar(aLocale, nCalendarDisplayCode, nNativeNumberMode, value);
1050 if (nNatNum > 0)
1051 return aNatNum.getNativeNumberString(aOUStr, aLocale, nNatNum);
1052 }
1053 return aOUStr;
1054 }
1055
1056 // Methods in XExtendedCalendar
1057 OUString SAL_CALL
getDisplayString(sal_Int32 nCalendarDisplayCode,sal_Int16 nNativeNumberMode)1058 Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
1059 {
1060 // make year and era in different order for year before and after 0.
1061 if ((nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA ||
1062 nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR_AND_ERA) &&
1063 getValue(CalendarFieldIndex::ERA) == 0) {
1064 if (nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA)
1065 return getDisplayString( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode ) +
1066 getDisplayString( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode );
1067 else
1068 return getDisplayString( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode ) +
1069 getDisplayString( CalendarDisplayCode::LONG_ERA, nNativeNumberMode );
1070 }
1071 return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
1072 }
1073
1074 OUString SAL_CALL
getImplementationName(void)1075 Calendar_gregorian::getImplementationName(void)
1076 {
1077 return OUString::createFromAscii(cCalendar);
1078 }
1079
1080 sal_Bool SAL_CALL
supportsService(const rtl::OUString & rServiceName)1081 Calendar_gregorian::supportsService(const rtl::OUString& rServiceName)
1082 {
1083 return !rServiceName.compareToAscii(cCalendar);
1084 }
1085
1086 Sequence< OUString > SAL_CALL
getSupportedServiceNames(void)1087 Calendar_gregorian::getSupportedServiceNames(void)
1088 {
1089 Sequence< OUString > aRet(1);
1090 aRet[0] = OUString::createFromAscii(cCalendar);
1091 return aRet;
1092 }
1093