LCOV - code coverage report
Current view: top level - libreoffice/i18npool/source/calendar - calendar_gregorian.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 254 471 53.9 %
Date: 2012-12-27 Functions: 32 52 61.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10