1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: calendar_gregorian.cxx,v $
10 * $Revision: 1.34.24.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 #include "calendar_gregorian.hxx"
35 #include "localedata.hxx"
36 #include <com/sun/star/i18n/AmPmValue.hpp>
37 #include <com/sun/star/i18n/Months.hpp>
38 #include <com/sun/star/i18n/Weekdays.hpp>
39 #include <com/sun/star/i18n/reservedWords.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <comphelper/processfactory.hxx>
46 #define erDUMP_ICU_CALENDAR 0
47 #define erDUMP_I18N_CALENDAR 0
48 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
49 // If both are used, DUMP_ICU_CAL_MSG() must be used before DUMP_I18N_CAL_MSG()
50 // to obtain internally set values from ICU, else Calendar::get() calls in
51 // DUMP_I18N_CAL_MSG() recalculate!
53 // These pieces of macro are shamelessly borrowed from icu's olsontz.cpp, the
54 // double parens'ed approach to pass multiple parameters as one macro parameter
56 static void debug_cal_loc(const char *f
, int32_t l
)
58 fprintf(stderr
, "%s:%d: ", f
, l
);
61 static void debug_cal_msg(const char *pat
, ...)
65 vfprintf(stderr
, pat
, ap
);
68 #if erDUMP_ICU_CALENDAR
70 // DEFS = -DU_DEBUG_CALSVC -DUCAL_DEBUG_DUMP
71 // in icu/$(INPATH)/misc/build/icu/source/icudefs.mk
72 // May need some patches to fix unmaintained things there.
73 extern void ucal_dump( const icu::Calendar
& );
74 static void debug_icu_cal_dump( const ::icu::Calendar
& r
)
78 // set a breakpoint here to pause display between dumps
80 // must use double parens, i.e.: DUMP_ICU_CAL_MSG(("four is: %d",4));
81 #define DUMP_ICU_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_icu_cal_dump(*body);}
82 #else // erDUMP_ICU_CALENDAR
83 #define DUMP_ICU_CAL_MSG(x)
84 #endif // erDUMP_ICU_CALENDAR
86 #if erDUMP_I18N_CALENDAR
87 static void debug_cal_millis_to_time( long nMillis
, long & h
, long & m
, long & s
, long & f
)
89 int sign
= (nMillis
< 0 ? -1 : 1);
90 nMillis
= ::std::abs(nMillis
);
91 h
= sign
* nMillis
/ (60 * 60 * 1000);
92 nMillis
-= sign
* h
* (60 * 60 * 1000);
93 m
= nMillis
/ (60 * 1000);
94 nMillis
-= m
* (60 * 1000);
96 nMillis
-= s
* (1000);
99 static void debug_i18n_cal_dump( const ::icu::Calendar
& r
)
102 long nMillis
, h
, m
, s
, f
;
103 fprintf( stderr
, " %04ld", (long)r
.get( UCAL_YEAR
, status
= U_ZERO_ERROR
));
104 fprintf( stderr
, "-%02ld", (long)r
.get( UCAL_MONTH
, status
= U_ZERO_ERROR
)+1);
105 fprintf( stderr
, "-%02ld", (long)r
.get( UCAL_DATE
, status
= U_ZERO_ERROR
));
106 fprintf( stderr
, " %02ld", (long)r
.get( UCAL_HOUR_OF_DAY
, status
= U_ZERO_ERROR
));
107 fprintf( stderr
, ":%02ld", (long)r
.get( UCAL_MINUTE
, status
= U_ZERO_ERROR
));
108 fprintf( stderr
, ":%02ld", (long)r
.get( UCAL_SECOND
, status
= U_ZERO_ERROR
));
109 fprintf( stderr
, " zone: %ld", (long)(nMillis
= r
.get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
)));
110 fprintf( stderr
, " (%f min)", (double)nMillis
/ 60000);
111 debug_cal_millis_to_time( nMillis
, h
, m
, s
, f
);
112 fprintf( stderr
, " (%ld:%02ld:%02ld.%ld)", h
, m
, s
, f
);
113 fprintf( stderr
, " DST: %ld", (long)(nMillis
= r
.get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
)));
114 fprintf( stderr
, " (%f min)", (double)nMillis
/ 60000);
115 debug_cal_millis_to_time( nMillis
, h
, m
, s
, f
);
116 fprintf( stderr
, " (%ld:%02ld:%02ld.%ld)", h
, m
, s
, f
);
117 fprintf( stderr
, "\n");
120 // must use double parens, i.e.: DUMP_I18N_CAL_MSG(("four is: %d",4));
121 #define DUMP_I18N_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_i18n_cal_dump(*body);}
122 #else // erDUMP_I18N_CALENDAR
123 #define DUMP_I18N_CAL_MSG(x)
124 #endif // erDUMP_I18N_CALENDAR
126 #else // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
127 #define DUMP_ICU_CAL_MSG(x)
128 #define DUMP_I18N_CAL_MSG(x)
129 #endif // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
132 using namespace ::com::sun::star::uno
;
133 using namespace ::com::sun::star::lang
;
134 using namespace ::com::sun::star::i18n
;
135 using namespace ::rtl
;
137 #define ERROR RuntimeException()
139 Calendar_gregorian::Calendar_gregorian()
143 Calendar_gregorian::Calendar_gregorian(Era
*_earArray
)
148 Calendar_gregorian::init(Era
*_eraArray
)
150 cCalendar
= "com.sun.star.i18n.Calendar_gregorian";
152 // #i102356# With icu::Calendar::createInstance(UErrorCode) in a Thai
153 // th_TH system locale we accidentally used a Buddhist calendar. Though
154 // the ICU documentation says that should be the case only for
155 // th_TH_TRADITIONAL (and ja_JP_TRADITIONAL Gengou), a plain th_TH
156 // already triggers that behavior, ja_JP does not. Strange enough,
157 // passing a th_TH locale to the calendar creation doesn't trigger
159 // See also http://userguide.icu-project.org/datetime/calendar
161 // Whatever ICU offers as the default calendar for a locale, ensure we
162 // have a Gregorian calendar as requested.
164 /* XXX: with the current implementation the aLocale member variable is
165 * not set prior to loading a calendar from locale data. This
166 * creates an empty (root) locale for ICU, but at least the correct
167 * calendar is used. The language part must not be NULL (respectively
168 * not all, language and country and variant), otherwise the current
169 * default locale would be used again and the calendar keyword ignored.
171 icu::Locale
aIcuLocale( "", NULL
, NULL
, "calendar=gregorian");
174 body
= icu::Calendar::createInstance( aIcuLocale
, status
= U_ZERO_ERROR
);
175 if (!body
|| !U_SUCCESS(status
)) throw ERROR
;
180 loc
= body
->getLocale( ULOC_ACTUAL_LOCALE
, status
= U_ZERO_ERROR
);
181 fprintf( stderr
, "\nICU calendar actual locale: %s\n", loc
.getName());
182 loc
= body
->getLocale( ULOC_VALID_LOCALE
, status
= U_ZERO_ERROR
);
183 fprintf( stderr
, "ICU calendar valid locale: %s\n", loc
.getName());
190 Calendar_gregorian::~Calendar_gregorian()
195 Calendar_hanja::Calendar_hanja()
197 cCalendar
= "com.sun.star.i18n.Calendar_hanja";
201 Calendar_hanja::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
) throw(RuntimeException
)
203 if ( displayIndex
== CalendarDisplayIndex::AM_PM
) {
204 // Am/Pm string for Korean Hanja calendar will refer to Japanese locale
205 com::sun::star::lang::Locale jaLocale
=
206 com::sun::star::lang::Locale(OUString::createFromAscii("ja"), OUString(), OUString());
207 if (idx
== 0) return LocaleData().getLocaleItem(jaLocale
).timeAM
;
208 else if (idx
== 1) return LocaleData().getLocaleItem(jaLocale
).timePM
;
212 return Calendar_gregorian::getDisplayName( displayIndex
, idx
, nameType
);
216 Calendar_hanja::loadCalendar( const OUString
& /*uniqueID*/, const com::sun::star::lang::Locale
& rLocale
) throw(RuntimeException
)
218 // Since this class could be called by service name 'hanja_yoil', we have to
219 // rename uniqueID to get right calendar defined in locale data.
220 Calendar_gregorian::loadCalendar(OUString::createFromAscii("hanja"), rLocale
);
223 static Era gengou_eraArray
[] = {
230 Calendar_gengou::Calendar_gengou() : Calendar_gregorian(gengou_eraArray
)
232 cCalendar
= "com.sun.star.i18n.Calendar_gengou";
235 static Era ROC_eraArray
[] = {
239 Calendar_ROC::Calendar_ROC() : Calendar_gregorian(ROC_eraArray
)
241 cCalendar
= "com.sun.star.i18n.Calendar_ROC";
244 static Era buddhist_eraArray
[] = {
248 Calendar_buddhist::Calendar_buddhist() : Calendar_gregorian(buddhist_eraArray
)
250 cCalendar
= "com.sun.star.i18n.Calendar_buddhist";
254 Calendar_gregorian::loadCalendar( const OUString
& uniqueID
, const com::sun::star::lang::Locale
& rLocale
) throw(RuntimeException
)
256 // init. fieldValue[]
260 Sequence
< Calendar
> xC
= LocaleData().getAllCalendars(rLocale
);
261 for (sal_Int32 i
= 0; i
< xC
.getLength(); i
++)
263 if (uniqueID
== xC
[i
].Name
)
266 // setup minimalDaysInFirstWeek
267 setMinimumNumberOfDaysForFirstWeek(
268 aCalendar
.MinimumNumberOfDaysForFirstWeek
);
269 // setup first day of week
270 for (sal_Int16 day
= sal::static_int_cast
<sal_Int16
>(
271 aCalendar
.Days
.getLength()-1); day
>=0; day
--)
273 if (aCalendar
.StartOfWeek
== aCalendar
.Days
[day
].ID
)
275 setFirstDayOfWeek( day
);
281 // Calendar is not for the locale
286 com::sun::star::i18n::Calendar SAL_CALL
287 Calendar_gregorian::getLoadedCalendar() throw(RuntimeException
)
293 Calendar_gregorian::getUniqueID() throw(RuntimeException
)
295 return aCalendar
.Name
;
299 Calendar_gregorian::setDateTime( double timeInDays
) throw(RuntimeException
)
302 body
->setTime(timeInDays
* U_MILLIS_PER_DAY
, status
= U_ZERO_ERROR
);
303 if ( !U_SUCCESS(status
) ) throw ERROR
;
308 Calendar_gregorian::getDateTime() throw(RuntimeException
)
315 double r
= body
->getTime(status
= U_ZERO_ERROR
);
316 if ( !U_SUCCESS(status
) ) throw ERROR
;
317 return r
/ U_MILLIS_PER_DAY
;
320 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
321 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
322 void Calendar_gregorian::mapFromGregorian() throw(RuntimeException
)
325 sal_Int16 e
, y
, m
, d
;
327 e
= fieldValue
[CalendarFieldIndex::ERA
];
328 y
= fieldValue
[CalendarFieldIndex::YEAR
];
329 m
= fieldValue
[CalendarFieldIndex::MONTH
] + 1;
330 d
= fieldValue
[CalendarFieldIndex::DAY_OF_MONTH
];
332 // since the year is reversed for first era, it is reversed again here for Era compare.
336 for (e
= 0; eraArray
[e
].year
; e
++)
337 if ((y
!= eraArray
[e
].year
) ? y
< eraArray
[e
].year
:
338 (m
!= eraArray
[e
].month
) ? m
< eraArray
[e
].month
: d
< eraArray
[e
].day
)
341 fieldValue
[CalendarFieldIndex::ERA
] = e
;
342 fieldValue
[CalendarFieldIndex::YEAR
] =
343 sal::static_int_cast
<sal_Int16
>( (e
== 0) ? (eraArray
[0].year
- y
) : (y
- eraArray
[e
-1].year
+ 1) );
347 #define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
348 // map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
349 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
350 void Calendar_gregorian::mapToGregorian() throw(RuntimeException
)
352 if (eraArray
&& (fieldSet
& FIELDS
)) {
353 sal_Int16 y
, e
= fieldValue
[CalendarFieldIndex::ERA
];
355 y
= sal::static_int_cast
<sal_Int16
>( eraArray
[0].year
- fieldValue
[CalendarFieldIndex::YEAR
] );
357 y
= sal::static_int_cast
<sal_Int16
>( eraArray
[e
-1].year
+ fieldValue
[CalendarFieldIndex::YEAR
] - 1 );
359 fieldSetValue
[CalendarFieldIndex::ERA
] = y
<= 0 ? 0 : 1;
360 fieldSetValue
[CalendarFieldIndex::YEAR
] = (y
<= 0 ? 1 - y
: y
);
365 static UCalendarDateFields
fieldNameConverter(sal_Int16 fieldIndex
) throw(RuntimeException
)
367 UCalendarDateFields f
;
369 switch (fieldIndex
) {
370 case CalendarFieldIndex::AM_PM
: f
= UCAL_AM_PM
; break;
371 case CalendarFieldIndex::DAY_OF_MONTH
: f
= UCAL_DATE
; break;
372 case CalendarFieldIndex::DAY_OF_WEEK
: f
= UCAL_DAY_OF_WEEK
; break;
373 case CalendarFieldIndex::DAY_OF_YEAR
: f
= UCAL_DAY_OF_YEAR
; break;
374 case CalendarFieldIndex::DST_OFFSET
: f
= UCAL_DST_OFFSET
; break;
375 case CalendarFieldIndex::ZONE_OFFSET
: f
= UCAL_ZONE_OFFSET
; break;
376 case CalendarFieldIndex::HOUR
: f
= UCAL_HOUR_OF_DAY
; break;
377 case CalendarFieldIndex::MINUTE
: f
= UCAL_MINUTE
; break;
378 case CalendarFieldIndex::SECOND
: f
= UCAL_SECOND
; break;
379 case CalendarFieldIndex::MILLISECOND
: f
= UCAL_MILLISECOND
; break;
380 case CalendarFieldIndex::WEEK_OF_MONTH
: f
= UCAL_WEEK_OF_MONTH
; break;
381 case CalendarFieldIndex::WEEK_OF_YEAR
: f
= UCAL_WEEK_OF_YEAR
; break;
382 case CalendarFieldIndex::YEAR
: f
= UCAL_YEAR
; break;
383 case CalendarFieldIndex::MONTH
: f
= UCAL_MONTH
; break;
384 case CalendarFieldIndex::ERA
: f
= UCAL_ERA
; break;
385 default: throw ERROR
;
391 Calendar_gregorian::setValue( sal_Int16 fieldIndex
, sal_Int16 value
) throw(RuntimeException
)
393 if (fieldIndex
< 0 || FIELD_INDEX_COUNT
<= fieldIndex
)
395 fieldSet
|= (1 << fieldIndex
);
396 fieldValue
[fieldIndex
] = value
;
399 bool Calendar_gregorian::getCombinedOffset( sal_Int32
& o_nOffset
,
400 sal_Int16 nParentFieldIndex
, sal_Int16 nChildFieldIndex
) const
403 bool bFieldsSet
= false;
404 if (fieldSet
& (1 << nParentFieldIndex
))
407 o_nOffset
= static_cast<sal_Int32
>( fieldValue
[nParentFieldIndex
]) * 60000;
409 if (fieldSet
& (1 << nChildFieldIndex
))
413 o_nOffset
-= static_cast<sal_uInt16
>( fieldValue
[nChildFieldIndex
]);
415 o_nOffset
+= static_cast<sal_uInt16
>( fieldValue
[nChildFieldIndex
]);
420 bool Calendar_gregorian::getZoneOffset( sal_Int32
& o_nOffset
) const
422 return getCombinedOffset( o_nOffset
, CalendarFieldIndex::ZONE_OFFSET
,
423 CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
);
426 bool Calendar_gregorian::getDSTOffset( sal_Int32
& o_nOffset
) const
428 return getCombinedOffset( o_nOffset
, CalendarFieldIndex::DST_OFFSET
,
429 CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
);
432 void Calendar_gregorian::submitFields() throw(com::sun::star::uno::RuntimeException
)
434 for (sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++)
436 if (fieldSet
& (1 << fieldIndex
))
441 body
->set(fieldNameConverter(fieldIndex
), fieldSetValue
[fieldIndex
]);
443 case CalendarFieldIndex::ZONE_OFFSET
:
444 case CalendarFieldIndex::DST_OFFSET
:
445 case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
:
446 case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
:
447 break; // nothing, extra handling
451 sal_Int32 nZoneOffset
, nDSTOffset
;
452 if (getZoneOffset( nZoneOffset
))
453 body
->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET
), nZoneOffset
);
454 if (getDSTOffset( nDSTOffset
))
455 body
->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET
), nDSTOffset
);
458 void Calendar_gregorian::submitValues( sal_Int32 nYear
,
459 sal_Int32 nMonth
, sal_Int32 nDay
, sal_Int32 nHour
, sal_Int32 nMinute
,
460 sal_Int32 nSecond
, sal_Int32 nMilliSecond
, sal_Int32 nZone
, sal_Int32 nDST
)
461 throw(com::sun::star::uno::RuntimeException
)
465 body
->set( UCAL_YEAR
, nYear
);
467 body
->set( UCAL_MONTH
, nMonth
);
469 body
->set( UCAL_DATE
, nDay
);
471 body
->set( UCAL_HOUR_OF_DAY
, nHour
);
473 body
->set( UCAL_MINUTE
, nMinute
);
475 body
->set( UCAL_SECOND
, nSecond
);
476 if (nMilliSecond
>= 0)
477 body
->set( UCAL_MILLISECOND
, nMilliSecond
);
479 body
->set( UCAL_ZONE_OFFSET
, nZone
);
481 body
->set( UCAL_DST_OFFSET
, nDST
);
484 static void lcl_setCombinedOffsetFieldValues( sal_Int32 nValue
,
485 sal_Int16 rFieldSetValue
[], sal_Int16 rFieldValue
[],
486 sal_Int16 nParentFieldIndex
, sal_Int16 nChildFieldIndex
)
488 sal_Int32 nTrunc
= nValue
/ 60000;
489 rFieldSetValue
[nParentFieldIndex
] = rFieldValue
[nParentFieldIndex
] =
490 static_cast<sal_Int16
>( nTrunc
);
491 sal_uInt16 nMillis
= static_cast<sal_uInt16
>( abs( nValue
- nTrunc
* 60000));
492 rFieldSetValue
[nChildFieldIndex
] = rFieldValue
[nChildFieldIndex
] =
493 static_cast<sal_Int16
>( nMillis
);
496 void Calendar_gregorian::setValue() throw(RuntimeException
)
498 // Correct DST glitch, see also localtime/gmtime conversion pitfalls at
499 // http://www.erack.de/download/timetest.c
501 // #i24082# in order to make the DST correction work in all
502 // circumstances, the time values have to be always resubmitted,
503 // regardless whether specified by the caller or not. It is not
504 // sufficient to rely on the ICU internal values previously set, as the
505 // following may happen:
506 // - Let 2004-03-28T02:00 be the onsetRule.
507 // - On 2004-03-29 (calendar initialized with 2004-03-29T00:00 DST) set
508 // a date of 2004-03-28 => calendar results in 2004-03-27T23:00 no DST.
509 // - Correcting this with simply "2004-03-28 no DST" and no time
510 // specified results in 2004-03-29T00:00, the ICU internal 23:00 time
511 // being adjusted to 24:00 in this case, switching one day further.
512 // => submit 2004-03-28T00:00 no DST.
514 // This got even weirder since ICU incorporated also historical data,
515 // even the timezone may differ for different dates! It is necessary to
516 // let ICU choose the corresponding OlsonTimeZone transitions and adapt
518 // #i86094# gives examples where that went wrong:
519 // TZ=Europe/Moscow date <= 1919-07-01
520 // zone +2:30:48 (!) instead of +3h, DST +2h instead of +1h
521 // TZ=America/St_Johns date <= 1935-03-30
522 // zone -3:30:52 (!) instead of -3:30
524 // Copy fields before calling submitFields() directly or indirectly below.
525 memcpy(fieldSetValue
, fieldValue
, sizeof(fieldSetValue
));
526 // Possibly setup ERA and YEAR in fieldSetValue.
529 DUMP_ICU_CAL_MSG(("%s\n","setValue() before any submission"));
530 DUMP_I18N_CAL_MSG(("%s\n","setValue() before any submission"));
532 bool bNeedZone
= !(fieldSet
& (1 << CalendarFieldIndex::ZONE_OFFSET
));
533 bool bNeedDST
= !(fieldSet
& (1 << CalendarFieldIndex::DST_OFFSET
));
534 sal_Int32 nZone1
, nDST1
, nYear
, nMonth
, nDay
, nHour
, nMinute
, nSecond
, nMilliSecond
, nZone
, nDST
;
535 nZone1
= nDST1
= nZone
= nDST
= 0;
536 nYear
= nMonth
= nDay
= nHour
= nMinute
= nSecond
= nMilliSecond
= -1;
537 if ( bNeedZone
|| bNeedDST
)
540 if ( !(fieldSet
& (1 << CalendarFieldIndex::YEAR
)) )
542 nYear
= body
->get( UCAL_YEAR
, status
= U_ZERO_ERROR
);
543 if ( !U_SUCCESS(status
) )
546 if ( !(fieldSet
& (1 << CalendarFieldIndex::MONTH
)) )
548 nMonth
= body
->get( UCAL_MONTH
, status
= U_ZERO_ERROR
);
549 if ( !U_SUCCESS(status
) )
552 if ( !(fieldSet
& (1 << CalendarFieldIndex::DAY_OF_MONTH
)) )
554 nDay
= body
->get( UCAL_DATE
, status
= U_ZERO_ERROR
);
555 if ( !U_SUCCESS(status
) )
558 if ( !(fieldSet
& (1 << CalendarFieldIndex::HOUR
)) )
560 nHour
= body
->get( UCAL_HOUR_OF_DAY
, status
= U_ZERO_ERROR
);
561 if ( !U_SUCCESS(status
) )
564 if ( !(fieldSet
& (1 << CalendarFieldIndex::MINUTE
)) )
566 nMinute
= body
->get( UCAL_MINUTE
, status
= U_ZERO_ERROR
);
567 if ( !U_SUCCESS(status
) )
570 if ( !(fieldSet
& (1 << CalendarFieldIndex::SECOND
)) )
572 nSecond
= body
->get( UCAL_SECOND
, status
= U_ZERO_ERROR
);
573 if ( !U_SUCCESS(status
) )
576 if ( !(fieldSet
& (1 << CalendarFieldIndex::MILLISECOND
)) )
578 nMilliSecond
= body
->get( UCAL_MILLISECOND
, status
= U_ZERO_ERROR
);
579 if ( !U_SUCCESS(status
) )
582 if ( !(fieldSet
& (1 << CalendarFieldIndex::ZONE_OFFSET
)) )
584 nZone
= body
->get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
);
585 if ( !U_SUCCESS(status
) )
588 if ( !(fieldSet
& (1 << CalendarFieldIndex::DST_OFFSET
)) )
590 nDST
= body
->get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
);
591 if ( !U_SUCCESS(status
) )
595 // Submit values to obtain a time zone and DST corresponding to the date/time.
596 submitValues( nYear
, nMonth
, nDay
, nHour
, nMinute
, nSecond
, nMilliSecond
, nZone
, nDST
);
598 DUMP_ICU_CAL_MSG(("%s\n","setValue() in bNeedZone||bNeedDST after submitValues()"));
599 DUMP_I18N_CAL_MSG(("%s\n","setValue() in bNeedZone||bNeedDST after submitValues()"));
600 nZone1
= body
->get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
);
601 if ( !U_SUCCESS(status
) )
603 nDST1
= body
->get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
);
604 if ( !U_SUCCESS(status
) )
608 // The original submission, may lead to a different zone/DST.
610 DUMP_ICU_CAL_MSG(("%s\n","setValue() after original submission"));
611 DUMP_I18N_CAL_MSG(("%s\n","setValue() after original submission"));
613 if ( bNeedZone
|| bNeedDST
)
616 sal_Int32 nZone2
= body
->get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
);
617 if ( !U_SUCCESS(status
) )
619 sal_Int32 nDST2
= body
->get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
);
620 if ( !U_SUCCESS(status
) )
622 if ( nZone2
!= nZone1
|| nDST2
!= nDST1
)
624 // Due to different DSTs, resulting date values may differ if
625 // DST is onset at 00:00 and the very onsetRule date was
626 // submitted with DST off => date-1 23:00, for example, which
627 // is not what we want.
628 // Resubmit all values, this time including DST => date 01:00
629 // Similar for zone differences.
631 // Set field values accordingly in case they were used.
633 lcl_setCombinedOffsetFieldValues( nZone2
, fieldSetValue
,
634 fieldValue
, CalendarFieldIndex::ZONE_OFFSET
,
635 CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
);
637 lcl_setCombinedOffsetFieldValues( nDST2
, fieldSetValue
,
638 fieldValue
, CalendarFieldIndex::DST_OFFSET
,
639 CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
);
640 submitValues( nYear
, nMonth
, nDay
, nHour
, nMinute
, nSecond
, nMilliSecond
, nZone2
, nDST2
);
641 DUMP_ICU_CAL_MSG(("%s\n","setValue() after Zone/DST glitch resubmit"));
642 DUMP_I18N_CAL_MSG(("%s\n","setValue() after Zone/DST glitch resubmit"));
644 // Time zone transition => resubmit.
645 // TZ=America/St_Johns date <= 1935-03-30
646 // -3:30:52 (!) instead of -3:30
647 // if first submission included time zone -3:30 that would be wrong.
648 bool bResubmit
= false;
649 sal_Int32 nZone3
= body
->get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
);
650 if ( !U_SUCCESS(status
) )
652 if (nZone3
!= nZone2
)
656 lcl_setCombinedOffsetFieldValues( nZone3
, fieldSetValue
,
657 fieldValue
, CalendarFieldIndex::ZONE_OFFSET
,
658 CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
);
661 // If the DST onset rule says to switch from 00:00 to 01:00 and
662 // we tried to set onsetDay 00:00 with DST, the result was
663 // onsetDay-1 23:00 and no DST, which is not what we want. So
664 // once again without DST, resulting in onsetDay 01:00 and DST.
665 // Yes, this seems to be weird, but logically correct.
666 // It doesn't even have to be on an onsetDay as the DST is
667 // factored in all days by ICU and there seems to be some
669 // TZ=Asia/Tehran 1999-03-22 exposes this, for example.
670 sal_Int32 nDST3
= body
->get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
);
671 if ( !U_SUCCESS(status
) )
673 if (nDST2
!= nDST3
&& !nDST3
)
678 fieldSetValue
[CalendarFieldIndex::DST_OFFSET
] =
679 fieldValue
[CalendarFieldIndex::DST_OFFSET
] = 0;
680 fieldSetValue
[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
] =
681 fieldValue
[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
] = 0;
686 submitValues( nYear
, nMonth
, nDay
, nHour
, nMinute
, nSecond
, nMilliSecond
, nZone3
, nDST3
);
687 DUMP_ICU_CAL_MSG(("%s\n","setValue() after Zone/DST glitch 2nd resubmit"));
688 DUMP_I18N_CAL_MSG(("%s\n","setValue() after Zone/DST glitch 2nd resubmit"));
692 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
694 // force icu::Calendar to recalculate
696 sal_Int32 nTmp
= body
->get( UCAL_DATE
, status
= U_ZERO_ERROR
);
697 DUMP_ICU_CAL_MSG(("%s: %d\n","setValue() result day",nTmp
));
698 DUMP_I18N_CAL_MSG(("%s: %d\n","setValue() result day",nTmp
));
703 void Calendar_gregorian::getValue() throw(RuntimeException
)
705 DUMP_ICU_CAL_MSG(("%s\n","getValue()"));
706 DUMP_I18N_CAL_MSG(("%s\n","getValue()"));
707 for (sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++)
709 if (fieldIndex
== CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
||
710 fieldIndex
== CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
)
711 continue; // not ICU fields
713 UErrorCode status
; sal_Int32 value
= body
->get( fieldNameConverter(
714 fieldIndex
), status
= U_ZERO_ERROR
);
715 if ( !U_SUCCESS(status
) ) throw ERROR
;
717 // Convert millisecond to minute for ZONE and DST and set remainder in
719 if (fieldIndex
== CalendarFieldIndex::ZONE_OFFSET
)
721 sal_Int32 nMinutes
= value
/ 60000;
722 sal_Int16 nMillis
= static_cast<sal_Int16
>( static_cast<sal_uInt16
>(
723 abs( value
- nMinutes
* 60000)));
724 fieldValue
[CalendarFieldIndex::ZONE_OFFSET
] = static_cast<sal_Int16
>( nMinutes
);
725 fieldValue
[CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
] = nMillis
;
727 else if (fieldIndex
== CalendarFieldIndex::DST_OFFSET
)
729 sal_Int32 nMinutes
= value
/ 60000;
730 sal_Int16 nMillis
= static_cast<sal_Int16
>( static_cast<sal_uInt16
>(
731 abs( value
- nMinutes
* 60000)));
732 fieldValue
[CalendarFieldIndex::DST_OFFSET
] = static_cast<sal_Int16
>( nMinutes
);
733 fieldValue
[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
] = nMillis
;
736 fieldValue
[fieldIndex
] = (sal_Int16
) value
;
738 // offset 1 since the value for week start day SunDay is different between Calendar and Weekdays.
739 if ( fieldIndex
== CalendarFieldIndex::DAY_OF_WEEK
)
740 fieldValue
[fieldIndex
]--; // UCAL_SUNDAY:/* == 1 */ ==> Weekdays::SUNDAY /* ==0 */
747 Calendar_gregorian::getValue( sal_Int16 fieldIndex
) throw(RuntimeException
)
749 if (fieldIndex
< 0 || FIELD_INDEX_COUNT
<= fieldIndex
)
757 return fieldValue
[fieldIndex
];
761 Calendar_gregorian::addValue( sal_Int16 fieldIndex
, sal_Int32 value
) throw(RuntimeException
)
763 // since ZONE and DST could not be add, we don't need to convert value here
765 body
->add(fieldNameConverter(fieldIndex
), value
, status
= U_ZERO_ERROR
);
766 if ( !U_SUCCESS(status
) ) throw ERROR
;
771 Calendar_gregorian::isValid() throw(RuntimeException
)
774 sal_Int32 tmp
= fieldSet
;
776 memcpy(fieldSetValue
, fieldValue
, sizeof(fieldSetValue
));
778 for ( sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++ ) {
779 // compare only with fields that are set and reset fieldSet[]
780 if (tmp
& (1 << fieldIndex
)) {
781 if (fieldSetValue
[fieldIndex
] != fieldValue
[fieldIndex
])
789 // NativeNumberMode has different meaning between Number and Calendar for Asian locales.
790 // Here is the mapping table
791 // calendar(q/y/m/d) zh_CN zh_TW ja ko
792 // NatNum1 NatNum1/1/7/7 NatNum1/1/7/7 NatNum1/1/4/4 NatNum1/1/7/7
793 // NatNum2 NatNum2/2/8/8 NatNum2/2/8/8 NatNum2/2/5/5 NatNum2/2/8/8
794 // NatNum3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3
795 // NatNum4 NatNum9/9/11/11
797 static sal_Int16 SAL_CALL
NatNumForCalendar(const com::sun::star::lang::Locale
& aLocale
,
798 sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
, sal_Int16 value
)
800 sal_Bool isShort
= ((nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
||
801 nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR
) && value
>= 100) ||
802 nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
||
803 nCalendarDisplayCode
== CalendarDisplayCode::LONG_QUARTER
;
804 sal_Bool isChinese
= aLocale
.Language
.equalsAscii("zh");
805 sal_Bool isJapanese
= aLocale
.Language
.equalsAscii("ja");
806 sal_Bool isKorean
= aLocale
.Language
.equalsAscii("ko");
808 if (isChinese
|| isJapanese
|| isKorean
) {
809 switch (nNativeNumberMode
) {
810 case NativeNumberMode::NATNUM1
:
812 nNativeNumberMode
= isJapanese
? NativeNumberMode::NATNUM4
: NativeNumberMode::NATNUM7
;
814 case NativeNumberMode::NATNUM2
:
816 nNativeNumberMode
= isJapanese
? NativeNumberMode::NATNUM5
: NativeNumberMode::NATNUM8
;
818 case NativeNumberMode::NATNUM3
:
820 case NativeNumberMode::NATNUM4
:
822 return isShort
? NativeNumberMode::NATNUM9
: NativeNumberMode::NATNUM11
;
827 return nNativeNumberMode
;
830 static sal_Int32 SAL_CALL
DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode
)
832 switch( nCalendarDisplayCode
) {
833 case CalendarDisplayCode::SHORT_DAY
:
834 case CalendarDisplayCode::LONG_DAY
:
835 return CalendarFieldIndex::DAY_OF_MONTH
;
836 case CalendarDisplayCode::SHORT_DAY_NAME
:
837 case CalendarDisplayCode::LONG_DAY_NAME
:
838 return CalendarFieldIndex::DAY_OF_WEEK
;
839 case CalendarDisplayCode::SHORT_QUARTER
:
840 case CalendarDisplayCode::LONG_QUARTER
:
841 case CalendarDisplayCode::SHORT_MONTH
:
842 case CalendarDisplayCode::LONG_MONTH
:
843 case CalendarDisplayCode::SHORT_MONTH_NAME
:
844 case CalendarDisplayCode::LONG_MONTH_NAME
:
845 return CalendarFieldIndex::MONTH
;
846 case CalendarDisplayCode::SHORT_YEAR
:
847 case CalendarDisplayCode::LONG_YEAR
:
848 return CalendarFieldIndex::YEAR
;
849 case CalendarDisplayCode::SHORT_ERA
:
850 case CalendarDisplayCode::LONG_ERA
:
851 return CalendarFieldIndex::ERA
;
852 case CalendarDisplayCode::SHORT_YEAR_AND_ERA
:
853 case CalendarDisplayCode::LONG_YEAR_AND_ERA
:
854 return CalendarFieldIndex::YEAR
;
861 Calendar_gregorian::getFirstDayOfWeek() throw(RuntimeException
)
863 // UCAL_SUNDAY == 1, Weekdays::SUNDAY == 0 => offset -1
864 // Check for underflow just in case we're called "out of sync".
865 return ::std::max( sal::static_int_cast
<sal_Int16
>(0),
866 sal::static_int_cast
<sal_Int16
>( static_cast<sal_Int16
>(
867 body
->getFirstDayOfWeek()) - 1));
871 Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day
)
872 throw(RuntimeException
)
874 // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
875 body
->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek
>( day
+ 1));
879 Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days
) throw(RuntimeException
)
881 aCalendar
.MinimumNumberOfDaysForFirstWeek
= days
;
882 body
->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days
));
886 Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException
)
888 return aCalendar
.MinimumNumberOfDaysForFirstWeek
;
892 Calendar_gregorian::getNumberOfMonthsInYear() throw(RuntimeException
)
894 return (sal_Int16
) aCalendar
.Months
.getLength();
899 Calendar_gregorian::getNumberOfDaysInWeek() throw(RuntimeException
)
901 return (sal_Int16
) aCalendar
.Days
.getLength();
905 Sequence
< CalendarItem
> SAL_CALL
906 Calendar_gregorian::getMonths() throw(RuntimeException
)
908 return aCalendar
.Months
;
912 Sequence
< CalendarItem
> SAL_CALL
913 Calendar_gregorian::getDays() throw(RuntimeException
)
915 return aCalendar
.Days
;
919 Calendar_gregorian::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
) throw(RuntimeException
)
923 switch( displayIndex
) {
924 case CalendarDisplayIndex::AM_PM
:/* ==0 */
925 if (idx
== 0) aStr
= LocaleData().getLocaleItem(aLocale
).timeAM
;
926 else if (idx
== 1) aStr
= LocaleData().getLocaleItem(aLocale
).timePM
;
929 case CalendarDisplayIndex::DAY
:
930 if( idx
>= aCalendar
.Days
.getLength() ) throw ERROR
;
931 if (nameType
== 0) aStr
= aCalendar
.Days
[idx
].AbbrevName
;
932 else if (nameType
== 1) aStr
= aCalendar
.Days
[idx
].FullName
;
935 case CalendarDisplayIndex::MONTH
:
936 if( idx
>= aCalendar
.Months
.getLength() ) throw ERROR
;
937 if (nameType
== 0) aStr
= aCalendar
.Months
[idx
].AbbrevName
;
938 else if (nameType
== 1) aStr
= aCalendar
.Months
[idx
].FullName
;
941 case CalendarDisplayIndex::ERA
:
942 if( idx
>= aCalendar
.Eras
.getLength() ) throw ERROR
;
943 if (nameType
== 0) aStr
= aCalendar
.Eras
[idx
].AbbrevName
;
944 else if (nameType
== 1) aStr
= aCalendar
.Eras
[idx
].FullName
;
947 case CalendarDisplayIndex::YEAR
:
955 // Methods in XExtendedCalendar
957 Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
958 throw (RuntimeException
)
960 sal_Int16 value
= getValue(sal::static_int_cast
<sal_Int16
>( DisplayCode2FieldIndex(nCalendarDisplayCode
) ));
963 if (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
||
964 nCalendarDisplayCode
== CalendarDisplayCode::LONG_QUARTER
) {
965 Sequence
< OUString
> xR
= LocaleData().getReservedWord(aLocale
);
966 sal_Int16 quarter
= value
/ 3;
967 // Since this base class method may be called by derived calendar
968 // classes where a year consists of more than 12 months we need a check
969 // to not run out of bounds of reserved quarter words. Perhaps a more
970 // clean way (instead of dividing by 3) would be to first get the
971 // number of months, divide by 4 and then use that result to divide the
972 // actual month value.
975 quarter
= sal::static_int_cast
<sal_Int16
>( quarter
+
976 ((nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
) ?
977 reservedWords::QUARTER1_ABBREVIATION
: reservedWords::QUARTER1_WORD
) );
978 aOUStr
= xR
[quarter
];
980 // The "#100211# - checked" comments serve for detection of "use of
981 // sprintf is safe here" conditions. An sprintf encountered without
982 // having that comment triggers alarm ;-)
984 switch( nCalendarDisplayCode
) {
985 case CalendarDisplayCode::SHORT_MONTH
:
986 value
+= 1; // month is zero based
988 case CalendarDisplayCode::SHORT_DAY
:
989 sprintf(aStr
, "%d", value
); // #100211# - checked
991 case CalendarDisplayCode::LONG_YEAR
:
992 if (aCalendar
.Name
.equalsAscii("gengou"))
993 sprintf(aStr
, "%02d", value
); // #100211# - checked
995 sprintf(aStr
, "%d", value
); // #100211# - checked
997 case CalendarDisplayCode::LONG_MONTH
:
998 value
+= 1; // month is zero based
999 sprintf(aStr
, "%02d", value
); // #100211# - checked
1001 case CalendarDisplayCode::SHORT_YEAR
:
1002 // Take last 2 digits, or only one if vallue<10, for example,
1003 // in case of the Gengou calendar.
1005 sprintf(aStr
, "%d", value
); // #100211# - checked
1007 sprintf(aStr
, "%02d", value
% 100); // #100211# - checked
1009 case CalendarDisplayCode::LONG_DAY
:
1010 sprintf(aStr
, "%02d", value
); // #100211# - checked
1013 case CalendarDisplayCode::SHORT_DAY_NAME
:
1014 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 0);
1015 case CalendarDisplayCode::LONG_DAY_NAME
:
1016 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 1);
1017 case CalendarDisplayCode::SHORT_MONTH_NAME
:
1018 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 0);
1019 case CalendarDisplayCode::LONG_MONTH_NAME
:
1020 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 1);
1021 case CalendarDisplayCode::SHORT_ERA
:
1022 return getDisplayName(CalendarDisplayIndex::ERA
, value
, 0);
1023 case CalendarDisplayCode::LONG_ERA
:
1024 return getDisplayName(CalendarDisplayIndex::ERA
, value
, 1);
1026 case CalendarDisplayCode::SHORT_YEAR_AND_ERA
:
1027 return getDisplayString( CalendarDisplayCode::SHORT_ERA
, nNativeNumberMode
) +
1028 getDisplayString( CalendarDisplayCode::SHORT_YEAR
, nNativeNumberMode
);
1030 case CalendarDisplayCode::LONG_YEAR_AND_ERA
:
1031 return getDisplayString( CalendarDisplayCode::LONG_ERA
, nNativeNumberMode
) +
1032 getDisplayString( CalendarDisplayCode::LONG_YEAR
, nNativeNumberMode
);
1037 aOUStr
= OUString::createFromAscii(aStr
);
1039 if (nNativeNumberMode
> 0) {
1040 // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
1041 if (eraArray
== gengou_eraArray
&& value
== 1
1042 && (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
||
1043 nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR
)
1044 && (nNativeNumberMode
== NativeNumberMode::NATNUM1
||
1045 nNativeNumberMode
== NativeNumberMode::NATNUM2
)) {
1046 static sal_Unicode gan
= 0x5143;
1047 return OUString(&gan
, 1);
1049 sal_Int16 nNatNum
= NatNumForCalendar(aLocale
, nCalendarDisplayCode
, nNativeNumberMode
, value
);
1051 return aNatNum
.getNativeNumberString(aOUStr
, aLocale
, nNatNum
);
1056 // Methods in XExtendedCalendar
1058 Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
1059 throw (RuntimeException
)
1061 // make year and era in different order for year before and after 0.
1062 if ((nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR_AND_ERA
||
1063 nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR_AND_ERA
) &&
1064 getValue(CalendarFieldIndex::ERA
) == 0) {
1065 if (nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR_AND_ERA
)
1066 return getDisplayString( CalendarDisplayCode::SHORT_YEAR
, nNativeNumberMode
) +
1067 getDisplayString( CalendarDisplayCode::SHORT_ERA
, nNativeNumberMode
);
1069 return getDisplayString( CalendarDisplayCode::LONG_YEAR
, nNativeNumberMode
) +
1070 getDisplayString( CalendarDisplayCode::LONG_ERA
, nNativeNumberMode
);
1072 return Calendar_gregorian::getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);
1076 Calendar_gregorian::getImplementationName(void) throw( RuntimeException
)
1078 return OUString::createFromAscii(cCalendar
);
1082 Calendar_gregorian::supportsService(const rtl::OUString
& rServiceName
) throw( RuntimeException
)
1084 return !rServiceName
.compareToAscii(cCalendar
);
1087 Sequence
< OUString
> SAL_CALL
1088 Calendar_gregorian::getSupportedServiceNames(void) throw( RuntimeException
)
1090 Sequence
< OUString
> aRet(1);
1091 aRet
[0] = OUString::createFromAscii(cCalendar
);