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