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