1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include "calendar_gregorian.hxx"
23 #include "localedata.hxx"
24 #include <com/sun/star/i18n/AmPmValue.hpp>
25 #include <com/sun/star/i18n/Months.hpp>
26 #include <com/sun/star/i18n/Weekdays.hpp>
27 #include <com/sun/star/i18n/reservedWords.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <rtl/math.hxx>
32 #include <sal/log.hxx>
37 #define erDUMP_ICU_CALENDAR 0
38 #define erDUMP_I18N_CALENDAR 0
39 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
40 // If both are used, DUMP_ICU_CAL_MSG() must be used before DUMP_I18N_CAL_MSG()
41 // to obtain internally set values from ICU, else Calendar::get() calls in
42 // DUMP_I18N_CAL_MSG() recalculate!
44 // These pieces of macro are shamelessly borrowed from icu's olsontz.cpp, the
45 // double parens'ed approach to pass multiple parameters as one macro parameter
47 static void debug_cal_loc(const char *f
, int32_t l
)
49 fprintf(stderr
, "%s:%d: ", f
, l
);
52 static void debug_cal_msg(const char *pat
, ...)
56 vfprintf(stderr
, pat
, ap
);
60 #if erDUMP_ICU_CALENDAR
62 // DEFS = -DU_DEBUG_CALSVC -DUCAL_DEBUG_DUMP
63 // in workdir/UnpackedTarball/icu/source/icudefs.mk
64 // May need some patches to fix unmaintained things there.
65 extern void ucal_dump( const icu::Calendar
& );
66 static void debug_icu_cal_dump( const ::icu::Calendar
& r
)
70 // set a breakpoint here to pause display between dumps
72 // must use double parens, i.e.: DUMP_ICU_CAL_MSG(("four is: %d",4));
73 #define DUMP_ICU_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_icu_cal_dump(*body);}
74 #else // erDUMP_ICU_CALENDAR
75 #define DUMP_ICU_CAL_MSG(x)
76 #endif // erDUMP_ICU_CALENDAR
78 #if erDUMP_I18N_CALENDAR
79 static void debug_cal_millis_to_time( long nMillis
, long & h
, long & m
, long & s
, long & f
)
81 int sign
= (nMillis
< 0 ? -1 : 1);
82 nMillis
= ::std::abs(nMillis
);
83 h
= sign
* nMillis
/ (60 * 60 * 1000);
84 nMillis
-= sign
* h
* (60 * 60 * 1000);
85 m
= nMillis
/ (60 * 1000);
86 nMillis
-= m
* (60 * 1000);
88 nMillis
-= s
* (1000);
91 static void debug_i18n_cal_dump( const ::icu::Calendar
& r
)
94 long nMillis
, h
, m
, s
, f
;
95 fprintf( stderr
, " %04ld", (long)r
.get( UCAL_YEAR
, status
= U_ZERO_ERROR
));
96 fprintf( stderr
, "-%02ld", (long)r
.get( UCAL_MONTH
, status
= U_ZERO_ERROR
)+1);
97 fprintf( stderr
, "-%02ld", (long)r
.get( UCAL_DATE
, status
= U_ZERO_ERROR
));
98 fprintf( stderr
, " %02ld", (long)r
.get( UCAL_HOUR_OF_DAY
, status
= U_ZERO_ERROR
));
99 fprintf( stderr
, ":%02ld", (long)r
.get( UCAL_MINUTE
, status
= U_ZERO_ERROR
));
100 fprintf( stderr
, ":%02ld", (long)r
.get( UCAL_SECOND
, status
= U_ZERO_ERROR
));
101 fprintf( stderr
, " zone: %ld", (long)(nMillis
= r
.get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
)));
102 fprintf( stderr
, " (%f min)", (double)nMillis
/ 60000);
103 debug_cal_millis_to_time( nMillis
, h
, m
, s
, f
);
104 fprintf( stderr
, " (%ld:%02ld:%02ld.%ld)", h
, m
, s
, f
);
105 fprintf( stderr
, " DST: %ld", (long)(nMillis
= r
.get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
)));
106 fprintf( stderr
, " (%f min)", (double)nMillis
/ 60000);
107 debug_cal_millis_to_time( nMillis
, h
, m
, s
, f
);
108 fprintf( stderr
, " (%ld:%02ld:%02ld.%ld)", h
, m
, s
, f
);
109 fprintf( stderr
, "\n");
112 // must use double parens, i.e.: DUMP_I18N_CAL_MSG(("four is: %d",4));
113 #define DUMP_I18N_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_i18n_cal_dump(*body);}
114 #else // erDUMP_I18N_CALENDAR
115 #define DUMP_I18N_CAL_MSG(x)
116 #endif // erDUMP_I18N_CALENDAR
118 #else // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
119 #define DUMP_ICU_CAL_MSG(x)
120 #define DUMP_I18N_CAL_MSG(x)
121 #endif // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
124 using namespace ::com::sun::star::uno
;
125 using namespace ::com::sun::star::lang
;
128 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
130 #define ERROR RuntimeException()
132 Calendar_gregorian::Calendar_gregorian()
133 : mxNatNum(new NativeNumberSupplierService
)
137 Calendar_gregorian::Calendar_gregorian(const Era
*_earArray
)
138 : mxNatNum(new NativeNumberSupplierService
)
143 Calendar_gregorian::init(const Era
*_eraArray
)
145 cCalendar
= "com.sun.star.i18n.Calendar_gregorian";
149 // #i102356# With icu::Calendar::createInstance(UErrorCode) in a Thai
150 // th_TH system locale we accidentally used a Buddhist calendar. Though
151 // the ICU documentation says that should be the case only for
152 // th_TH_TRADITIONAL (and ja_JP_TRADITIONAL Gengou), a plain th_TH
153 // already triggers that behavior, ja_JP does not. Strange enough,
154 // passing a th_TH locale to the calendar creation doesn't trigger
156 // See also http://userguide.icu-project.org/datetime/calendar
158 // Whatever ICU offers as the default calendar for a locale, ensure we
159 // have a Gregorian calendar as requested.
161 /* XXX: with the current implementation the aLocale member variable is
162 * not set prior to loading a calendar from locale data. This
163 * creates an empty (root) locale for ICU, but at least the correct
164 * calendar is used. The language part must not be NULL (respectively
165 * not all, language and country and variant), otherwise the current
166 * default locale would be used again and the calendar keyword ignored.
168 icu::Locale
aIcuLocale( "", nullptr, nullptr, "calendar=gregorian");
171 body
= icu::Calendar::createInstance( aIcuLocale
, status
= U_ZERO_ERROR
);
172 if (!body
|| !U_SUCCESS(status
)) throw ERROR
;
176 Calendar_gregorian::~Calendar_gregorian()
181 Calendar_hanja::Calendar_hanja()
183 cCalendar
= "com.sun.star.i18n.Calendar_hanja";
187 Calendar_hanja::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
) throw(RuntimeException
, std::exception
)
189 if ( displayIndex
== CalendarDisplayIndex::AM_PM
) {
190 // Am/Pm string for Korean Hanja calendar will refer to Japanese locale
191 css::lang::Locale
jaLocale(OUString("ja"), OUString(), OUString());
192 if (idx
== 0) return LocaleDataImpl().getLocaleItem(jaLocale
).timeAM
;
193 else if (idx
== 1) return LocaleDataImpl().getLocaleItem(jaLocale
).timePM
;
197 return Calendar_gregorian::getDisplayName( displayIndex
, idx
, nameType
);
201 Calendar_hanja::loadCalendar( const OUString
& /*uniqueID*/, const css::lang::Locale
& rLocale
) throw(RuntimeException
, std::exception
)
203 // Since this class could be called by service name 'hanja_yoil', we have to
204 // rename uniqueID to get right calendar defined in locale data.
205 Calendar_gregorian::loadCalendar("hanja", rLocale
);
208 static const Era gengou_eraArray
[] = {
215 Calendar_gengou::Calendar_gengou() : Calendar_gregorian(gengou_eraArray
)
217 cCalendar
= "com.sun.star.i18n.Calendar_gengou";
220 static const Era ROC_eraArray
[] = {
221 {1912, 1, 1, kDisplayEraForcedLongYear
}, // #i116701#
224 Calendar_ROC::Calendar_ROC() : Calendar_gregorian(ROC_eraArray
)
226 cCalendar
= "com.sun.star.i18n.Calendar_ROC";
229 static const Era buddhist_eraArray
[] = {
233 Calendar_buddhist::Calendar_buddhist() : Calendar_gregorian(buddhist_eraArray
)
235 cCalendar
= "com.sun.star.i18n.Calendar_buddhist";
239 Calendar_gregorian::loadCalendar( const OUString
& uniqueID
, const css::lang::Locale
& rLocale
) throw(RuntimeException
, std::exception
)
241 // init. fieldValue[]
245 Sequence
< Calendar2
> xC
= LocaleDataImpl().getAllCalendars2(rLocale
);
246 for (sal_Int32 i
= 0; i
< xC
.getLength(); i
++)
248 if (uniqueID
== xC
[i
].Name
)
251 // setup minimalDaysInFirstWeek
252 setMinimumNumberOfDaysForFirstWeek(
253 aCalendar
.MinimumNumberOfDaysForFirstWeek
);
254 // setup first day of week
255 for (sal_Int16 day
= sal::static_int_cast
<sal_Int16
>(
256 aCalendar
.Days
.getLength()-1); day
>=0; day
--)
258 if (aCalendar
.StartOfWeek
== aCalendar
.Days
[day
].ID
)
260 setFirstDayOfWeek( day
);
266 // Calendar is not for the locale
271 css::i18n::Calendar2 SAL_CALL
272 Calendar_gregorian::getLoadedCalendar2() throw(RuntimeException
, std::exception
)
277 css::i18n::Calendar SAL_CALL
278 Calendar_gregorian::getLoadedCalendar() throw(RuntimeException
, std::exception
)
280 return LocaleDataImpl::downcastCalendar( aCalendar
);
284 Calendar_gregorian::getUniqueID() throw(RuntimeException
, std::exception
)
286 return aCalendar
.Name
;
290 Calendar_gregorian::setDateTime( double fTimeInDays
) throw(RuntimeException
, std::exception
)
292 // ICU handles dates in milliseconds as double values and uses floor()
293 // to obtain integer values, which may yield a date decremented by one
294 // for odd (historical) timezone values where the computed value due to
295 // rounding errors has a fractional part in milliseconds. Ensure we
296 // pass a value without fraction here. If not, that may lead to
297 // fdo#44286 or fdo#52619 and the like, e.g. when passing
298 // -2136315212000.000244 instead of -2136315212000.000000
299 double fM
= fTimeInDays
* U_MILLIS_PER_DAY
;
300 double fR
= rtl::math::round( fM
);
301 SAL_INFO_IF( fM
!= fR
, "i18npool",
302 "Calendar_gregorian::setDateTime: " << std::fixed
<< fM
<< " rounded to " << fR
);
304 body
->setTime( fR
, status
= U_ZERO_ERROR
);
305 if ( !U_SUCCESS(status
) ) throw ERROR
;
310 Calendar_gregorian::getDateTime() throw(RuntimeException
, std::exception
)
317 double fR
= body
->getTime(status
= U_ZERO_ERROR
);
318 if ( !U_SUCCESS(status
) ) throw ERROR
;
319 return fR
/ U_MILLIS_PER_DAY
;
323 Calendar_gregorian::setLocalDateTime( double fTimeInDays
) throw(RuntimeException
, std::exception
)
325 // See setDateTime() for why the rounding.
326 double fM
= fTimeInDays
* U_MILLIS_PER_DAY
;
327 double fR
= rtl::math::round( fM
);
328 SAL_INFO_IF( fM
!= fR
, "i18npool",
329 "Calendar_gregorian::setLocalDateTime: " << std::fixed
<< fM
<< " rounded to " << fR
);
330 int32_t nZoneOffset
, nDSTOffset
;
332 body
->getTimeZone().getOffset( fR
, TRUE
, nZoneOffset
, nDSTOffset
, status
= U_ZERO_ERROR
);
333 if ( !U_SUCCESS(status
) ) throw ERROR
;
334 body
->setTime( fR
- (nZoneOffset
+ nDSTOffset
), status
= U_ZERO_ERROR
);
335 if ( !U_SUCCESS(status
) ) throw ERROR
;
340 Calendar_gregorian::getLocalDateTime() throw(RuntimeException
, std::exception
)
347 double fTime
= body
->getTime( status
= U_ZERO_ERROR
);
348 if ( !U_SUCCESS(status
) ) throw ERROR
;
349 int32_t nZoneOffset
= body
->get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
);
350 if ( !U_SUCCESS(status
) ) throw ERROR
;
351 int32_t nDSTOffset
= body
->get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
);
352 if ( !U_SUCCESS(status
) ) throw ERROR
;
353 return (fTime
+ (nZoneOffset
+ nDSTOffset
)) / U_MILLIS_PER_DAY
;
356 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
357 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
358 void Calendar_gregorian::mapFromGregorian() throw(RuntimeException
)
361 sal_Int16 e
, y
, m
, d
;
363 e
= fieldValue
[CalendarFieldIndex::ERA
];
364 y
= fieldValue
[CalendarFieldIndex::YEAR
];
365 m
= fieldValue
[CalendarFieldIndex::MONTH
] + 1;
366 d
= fieldValue
[CalendarFieldIndex::DAY_OF_MONTH
];
368 // since the year is reversed for first era, it is reversed again here for Era compare.
372 for (e
= 0; eraArray
[e
].year
; e
++)
373 if ((y
!= eraArray
[e
].year
) ? y
< eraArray
[e
].year
:
374 (m
!= eraArray
[e
].month
) ? m
< eraArray
[e
].month
: d
< eraArray
[e
].day
)
377 fieldValue
[CalendarFieldIndex::ERA
] = e
;
378 fieldValue
[CalendarFieldIndex::YEAR
] =
379 sal::static_int_cast
<sal_Int16
>( (e
== 0) ? (eraArray
[0].year
- y
) : (y
- eraArray
[e
-1].year
+ 1) );
383 #define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
384 // map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
385 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
386 void Calendar_gregorian::mapToGregorian() throw(RuntimeException
)
388 if (eraArray
&& (fieldSet
& FIELDS
)) {
389 sal_Int16 y
, e
= fieldValue
[CalendarFieldIndex::ERA
];
391 y
= sal::static_int_cast
<sal_Int16
>( eraArray
[0].year
- fieldValue
[CalendarFieldIndex::YEAR
] );
393 y
= sal::static_int_cast
<sal_Int16
>( eraArray
[e
-1].year
+ fieldValue
[CalendarFieldIndex::YEAR
] - 1 );
395 fieldSetValue
[CalendarFieldIndex::ERA
] = y
<= 0 ? 0 : 1;
396 fieldSetValue
[CalendarFieldIndex::YEAR
] = (y
<= 0 ? 1 - y
: y
);
401 static UCalendarDateFields
fieldNameConverter(sal_Int16 fieldIndex
) throw(RuntimeException
)
403 UCalendarDateFields f
;
405 switch (fieldIndex
) {
406 case CalendarFieldIndex::AM_PM
: f
= UCAL_AM_PM
; break;
407 case CalendarFieldIndex::DAY_OF_MONTH
: f
= UCAL_DATE
; break;
408 case CalendarFieldIndex::DAY_OF_WEEK
: f
= UCAL_DAY_OF_WEEK
; break;
409 case CalendarFieldIndex::DAY_OF_YEAR
: f
= UCAL_DAY_OF_YEAR
; break;
410 case CalendarFieldIndex::DST_OFFSET
: f
= UCAL_DST_OFFSET
; break;
411 case CalendarFieldIndex::ZONE_OFFSET
: f
= UCAL_ZONE_OFFSET
; break;
412 case CalendarFieldIndex::HOUR
: f
= UCAL_HOUR_OF_DAY
; break;
413 case CalendarFieldIndex::MINUTE
: f
= UCAL_MINUTE
; break;
414 case CalendarFieldIndex::SECOND
: f
= UCAL_SECOND
; break;
415 case CalendarFieldIndex::MILLISECOND
: f
= UCAL_MILLISECOND
; break;
416 case CalendarFieldIndex::WEEK_OF_MONTH
: f
= UCAL_WEEK_OF_MONTH
; break;
417 case CalendarFieldIndex::WEEK_OF_YEAR
: f
= UCAL_WEEK_OF_YEAR
; break;
418 case CalendarFieldIndex::YEAR
: f
= UCAL_YEAR
; break;
419 case CalendarFieldIndex::MONTH
: f
= UCAL_MONTH
; break;
420 case CalendarFieldIndex::ERA
: f
= UCAL_ERA
; break;
421 default: throw ERROR
;
427 Calendar_gregorian::setValue( sal_Int16 fieldIndex
, sal_Int16 value
) throw(RuntimeException
, std::exception
)
429 if (fieldIndex
< 0 || FIELD_INDEX_COUNT
<= fieldIndex
)
431 fieldSet
|= (1 << fieldIndex
);
432 fieldValue
[fieldIndex
] = value
;
435 bool Calendar_gregorian::getCombinedOffset( sal_Int32
& o_nOffset
,
436 sal_Int16 nParentFieldIndex
, sal_Int16 nChildFieldIndex
) const
439 bool bFieldsSet
= false;
440 if (fieldSet
& (1 << nParentFieldIndex
))
443 o_nOffset
= static_cast<sal_Int32
>( fieldValue
[nParentFieldIndex
]) * 60000;
445 if (fieldSet
& (1 << nChildFieldIndex
))
449 o_nOffset
-= static_cast<sal_uInt16
>( fieldValue
[nChildFieldIndex
]);
451 o_nOffset
+= static_cast<sal_uInt16
>( fieldValue
[nChildFieldIndex
]);
456 bool Calendar_gregorian::getZoneOffset( sal_Int32
& o_nOffset
) const
458 return getCombinedOffset( o_nOffset
, CalendarFieldIndex::ZONE_OFFSET
,
459 CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
);
462 bool Calendar_gregorian::getDSTOffset( sal_Int32
& o_nOffset
) const
464 return getCombinedOffset( o_nOffset
, CalendarFieldIndex::DST_OFFSET
,
465 CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
);
468 void Calendar_gregorian::submitFields() throw(css::uno::RuntimeException
)
470 for (sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++)
472 if (fieldSet
& (1 << fieldIndex
))
477 body
->set(fieldNameConverter(fieldIndex
), fieldSetValue
[fieldIndex
]);
479 case CalendarFieldIndex::ZONE_OFFSET
:
480 case CalendarFieldIndex::DST_OFFSET
:
481 case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
:
482 case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
:
483 break; // nothing, extra handling
487 sal_Int32 nZoneOffset
, nDSTOffset
;
488 if (getZoneOffset( nZoneOffset
))
489 body
->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET
), nZoneOffset
);
490 if (getDSTOffset( nDSTOffset
))
491 body
->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET
), nDSTOffset
);
494 void Calendar_gregorian::setValue() throw(RuntimeException
)
496 // Copy fields before calling submitFields() directly or indirectly below.
497 memcpy(fieldSetValue
, fieldValue
, sizeof(fieldSetValue
));
498 // Possibly setup ERA and YEAR in fieldSetValue.
501 DUMP_ICU_CAL_MSG(("%s\n","setValue() before submission"));
502 DUMP_I18N_CAL_MSG(("%s\n","setValue() before submission"));
506 DUMP_ICU_CAL_MSG(("%s\n","setValue() after submission"));
507 DUMP_I18N_CAL_MSG(("%s\n","setValue() after submission"));
509 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
511 // force icu::Calendar to recalculate
513 sal_Int32 nTmp
= body
->get( UCAL_DATE
, status
= U_ZERO_ERROR
);
514 DUMP_ICU_CAL_MSG(("%s: %d\n","setValue() result day",nTmp
));
515 DUMP_I18N_CAL_MSG(("%s: %d\n","setValue() result day",nTmp
));
520 void Calendar_gregorian::getValue() throw(RuntimeException
)
522 DUMP_ICU_CAL_MSG(("%s\n","getValue()"));
523 DUMP_I18N_CAL_MSG(("%s\n","getValue()"));
524 for (sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++)
526 if (fieldIndex
== CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
||
527 fieldIndex
== CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
)
528 continue; // not ICU fields
530 UErrorCode status
; sal_Int32 value
= body
->get( fieldNameConverter(
531 fieldIndex
), status
= U_ZERO_ERROR
);
532 if ( !U_SUCCESS(status
) ) throw ERROR
;
534 // Convert millisecond to minute for ZONE and DST and set remainder in
536 if (fieldIndex
== CalendarFieldIndex::ZONE_OFFSET
)
538 sal_Int32 nMinutes
= value
/ 60000;
539 sal_Int16 nMillis
= static_cast<sal_Int16
>( static_cast<sal_uInt16
>(
540 abs( value
- nMinutes
* 60000)));
541 fieldValue
[CalendarFieldIndex::ZONE_OFFSET
] = static_cast<sal_Int16
>( nMinutes
);
542 fieldValue
[CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
] = nMillis
;
544 else if (fieldIndex
== CalendarFieldIndex::DST_OFFSET
)
546 sal_Int32 nMinutes
= value
/ 60000;
547 sal_Int16 nMillis
= static_cast<sal_Int16
>( static_cast<sal_uInt16
>(
548 abs( value
- nMinutes
* 60000)));
549 fieldValue
[CalendarFieldIndex::DST_OFFSET
] = static_cast<sal_Int16
>( nMinutes
);
550 fieldValue
[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
] = nMillis
;
553 fieldValue
[fieldIndex
] = (sal_Int16
) value
;
555 // offset 1 since the value for week start day SunDay is different between Calendar and Weekdays.
556 if ( fieldIndex
== CalendarFieldIndex::DAY_OF_WEEK
)
557 fieldValue
[fieldIndex
]--; // UCAL_SUNDAY:/* == 1 */ ==> Weekdays::SUNDAY /* ==0 */
564 Calendar_gregorian::getValue( sal_Int16 fieldIndex
) throw(RuntimeException
, std::exception
)
566 if (fieldIndex
< 0 || FIELD_INDEX_COUNT
<= fieldIndex
)
574 return fieldValue
[fieldIndex
];
578 Calendar_gregorian::addValue( sal_Int16 fieldIndex
, sal_Int32 value
) throw(RuntimeException
, std::exception
)
580 // since ZONE and DST could not be add, we don't need to convert value here
582 body
->add(fieldNameConverter(fieldIndex
), value
, status
= U_ZERO_ERROR
);
583 if ( !U_SUCCESS(status
) ) throw ERROR
;
588 Calendar_gregorian::isValid() throw(RuntimeException
, std::exception
)
591 sal_Int32 tmp
= fieldSet
;
593 memcpy(fieldSetValue
, fieldValue
, sizeof(fieldSetValue
));
595 for ( sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++ ) {
596 // compare only with fields that are set and reset fieldSet[]
597 if (tmp
& (1 << fieldIndex
)) {
598 if (fieldSetValue
[fieldIndex
] != fieldValue
[fieldIndex
])
606 // NativeNumberMode has different meaning between Number and Calendar for Asian locales.
607 // Here is the mapping table
608 // calendar(q/y/m/d) zh_CN zh_TW ja ko
609 // NatNum1 NatNum1/1/7/7 NatNum1/1/7/7 NatNum1/1/4/4 NatNum1/1/7/7
610 // NatNum2 NatNum2/2/8/8 NatNum2/2/8/8 NatNum2/2/5/5 NatNum2/2/8/8
611 // NatNum3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3
612 // NatNum4 NatNum9/9/11/11
614 static sal_Int16 SAL_CALL
NatNumForCalendar(const css::lang::Locale
& aLocale
,
615 sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
, sal_Int16 value
)
617 bool isShort
= ((nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
||
618 nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR
) && value
>= 100) ||
619 nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
||
620 nCalendarDisplayCode
== CalendarDisplayCode::LONG_QUARTER
;
621 bool isChinese
= aLocale
.Language
== "zh";
622 bool isJapanese
= aLocale
.Language
== "ja";
623 bool isKorean
= aLocale
.Language
== "ko";
625 if (isChinese
|| isJapanese
|| isKorean
) {
626 switch (nNativeNumberMode
) {
627 case NativeNumberMode::NATNUM1
:
629 nNativeNumberMode
= isJapanese
? NativeNumberMode::NATNUM4
: NativeNumberMode::NATNUM7
;
631 case NativeNumberMode::NATNUM2
:
633 nNativeNumberMode
= isJapanese
? NativeNumberMode::NATNUM5
: NativeNumberMode::NATNUM8
;
635 case NativeNumberMode::NATNUM3
:
637 case NativeNumberMode::NATNUM4
:
639 return isShort
? NativeNumberMode::NATNUM9
: NativeNumberMode::NATNUM11
;
644 return nNativeNumberMode
;
647 static sal_Int32 SAL_CALL
DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode
)
649 switch( nCalendarDisplayCode
) {
650 case CalendarDisplayCode::SHORT_DAY
:
651 case CalendarDisplayCode::LONG_DAY
:
652 return CalendarFieldIndex::DAY_OF_MONTH
;
653 case CalendarDisplayCode::SHORT_DAY_NAME
:
654 case CalendarDisplayCode::LONG_DAY_NAME
:
655 case CalendarDisplayCode::NARROW_DAY_NAME
:
656 return CalendarFieldIndex::DAY_OF_WEEK
;
657 case CalendarDisplayCode::SHORT_QUARTER
:
658 case CalendarDisplayCode::LONG_QUARTER
:
659 case CalendarDisplayCode::SHORT_MONTH
:
660 case CalendarDisplayCode::LONG_MONTH
:
661 case CalendarDisplayCode::SHORT_MONTH_NAME
:
662 case CalendarDisplayCode::LONG_MONTH_NAME
:
663 case CalendarDisplayCode::NARROW_MONTH_NAME
:
664 case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME
:
665 case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME
:
666 case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME
:
667 case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME
:
668 case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME
:
669 case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME
:
670 return CalendarFieldIndex::MONTH
;
671 case CalendarDisplayCode::SHORT_YEAR
:
672 case CalendarDisplayCode::LONG_YEAR
:
673 return CalendarFieldIndex::YEAR
;
674 case CalendarDisplayCode::SHORT_ERA
:
675 case CalendarDisplayCode::LONG_ERA
:
676 return CalendarFieldIndex::ERA
;
677 case CalendarDisplayCode::SHORT_YEAR_AND_ERA
:
678 case CalendarDisplayCode::LONG_YEAR_AND_ERA
:
679 return CalendarFieldIndex::YEAR
;
686 Calendar_gregorian::getFirstDayOfWeek() throw(RuntimeException
, std::exception
)
688 // UCAL_SUNDAY == 1, Weekdays::SUNDAY == 0 => offset -1
689 // Check for underflow just in case we're called "out of sync".
690 return ::std::max( sal::static_int_cast
<sal_Int16
>(0),
691 sal::static_int_cast
<sal_Int16
>( static_cast<sal_Int16
>(
692 body
->getFirstDayOfWeek()) - 1));
696 Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day
)
697 throw(RuntimeException
, std::exception
)
699 // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
700 body
->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek
>( day
+ 1));
704 Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days
) throw(RuntimeException
, std::exception
)
706 aCalendar
.MinimumNumberOfDaysForFirstWeek
= days
;
707 body
->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days
));
711 Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek() throw(RuntimeException
, std::exception
)
713 return aCalendar
.MinimumNumberOfDaysForFirstWeek
;
717 Calendar_gregorian::getNumberOfMonthsInYear() throw(RuntimeException
, std::exception
)
719 return (sal_Int16
) aCalendar
.Months
.getLength();
724 Calendar_gregorian::getNumberOfDaysInWeek() throw(RuntimeException
, std::exception
)
726 return (sal_Int16
) aCalendar
.Days
.getLength();
730 Sequence
< CalendarItem
> SAL_CALL
731 Calendar_gregorian::getDays() throw(RuntimeException
, std::exception
)
733 return LocaleDataImpl::downcastCalendarItems( aCalendar
.Days
);
737 Sequence
< CalendarItem
> SAL_CALL
738 Calendar_gregorian::getMonths() throw(RuntimeException
, std::exception
)
740 return LocaleDataImpl::downcastCalendarItems( aCalendar
.Months
);
744 Sequence
< CalendarItem2
> SAL_CALL
745 Calendar_gregorian::getDays2() throw(RuntimeException
, std::exception
)
747 return aCalendar
.Days
;
751 Sequence
< CalendarItem2
> SAL_CALL
752 Calendar_gregorian::getMonths2() throw(RuntimeException
, std::exception
)
754 return aCalendar
.Months
;
758 Sequence
< CalendarItem2
> SAL_CALL
759 Calendar_gregorian::getGenitiveMonths2() throw(RuntimeException
, std::exception
)
761 return aCalendar
.GenitiveMonths
;
765 Sequence
< CalendarItem2
> SAL_CALL
766 Calendar_gregorian::getPartitiveMonths2() throw(RuntimeException
, std::exception
)
768 return aCalendar
.PartitiveMonths
;
773 Calendar_gregorian::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
) throw(RuntimeException
, std::exception
)
777 switch( displayIndex
) {
778 case CalendarDisplayIndex::AM_PM
:/* ==0 */
779 if (idx
== 0) aStr
= LocaleDataImpl().getLocaleItem(aLocale
).timeAM
;
780 else if (idx
== 1) aStr
= LocaleDataImpl().getLocaleItem(aLocale
).timePM
;
783 case CalendarDisplayIndex::DAY
:
784 if( idx
>= aCalendar
.Days
.getLength() ) throw ERROR
;
785 if (nameType
== 0) aStr
= aCalendar
.Days
[idx
].AbbrevName
;
786 else if (nameType
== 1) aStr
= aCalendar
.Days
[idx
].FullName
;
787 else if (nameType
== 2) aStr
= aCalendar
.Days
[idx
].NarrowName
;
790 case CalendarDisplayIndex::MONTH
:
791 if( idx
>= aCalendar
.Months
.getLength() ) throw ERROR
;
792 if (nameType
== 0) aStr
= aCalendar
.Months
[idx
].AbbrevName
;
793 else if (nameType
== 1) aStr
= aCalendar
.Months
[idx
].FullName
;
794 else if (nameType
== 2) aStr
= aCalendar
.Months
[idx
].NarrowName
;
797 case CalendarDisplayIndex::GENITIVE_MONTH
:
798 if( idx
>= aCalendar
.GenitiveMonths
.getLength() ) throw ERROR
;
799 if (nameType
== 0) aStr
= aCalendar
.GenitiveMonths
[idx
].AbbrevName
;
800 else if (nameType
== 1) aStr
= aCalendar
.GenitiveMonths
[idx
].FullName
;
801 else if (nameType
== 2) aStr
= aCalendar
.GenitiveMonths
[idx
].NarrowName
;
804 case CalendarDisplayIndex::PARTITIVE_MONTH
:
805 if( idx
>= aCalendar
.PartitiveMonths
.getLength() ) throw ERROR
;
806 if (nameType
== 0) aStr
= aCalendar
.PartitiveMonths
[idx
].AbbrevName
;
807 else if (nameType
== 1) aStr
= aCalendar
.PartitiveMonths
[idx
].FullName
;
808 else if (nameType
== 2) aStr
= aCalendar
.PartitiveMonths
[idx
].NarrowName
;
811 case CalendarDisplayIndex::ERA
:
812 if( idx
>= aCalendar
.Eras
.getLength() ) throw ERROR
;
813 if (nameType
== 0) aStr
= aCalendar
.Eras
[idx
].AbbrevName
;
814 else if (nameType
== 1) aStr
= aCalendar
.Eras
[idx
].FullName
;
817 case CalendarDisplayIndex::YEAR
:
825 // Methods in XExtendedCalendar
827 Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
828 throw (RuntimeException
, std::exception
)
830 return getDisplayStringImpl( nCalendarDisplayCode
, nNativeNumberMode
, false);
834 Calendar_gregorian::getDisplayStringImpl( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
, bool bEraMode
)
835 throw (RuntimeException
)
837 sal_Int16 value
= getValue(sal::static_int_cast
<sal_Int16
>( DisplayCode2FieldIndex(nCalendarDisplayCode
) ));
840 if (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
||
841 nCalendarDisplayCode
== CalendarDisplayCode::LONG_QUARTER
) {
842 Sequence
< OUString
> xR
= LocaleDataImpl().getReservedWord(aLocale
);
843 sal_Int16 quarter
= value
/ 3;
844 // Since this base class method may be called by derived calendar
845 // classes where a year consists of more than 12 months we need a check
846 // to not run out of bounds of reserved quarter words. Perhaps a more
847 // clean way (instead of dividing by 3) would be to first get the
848 // number of months, divide by 4 and then use that result to divide the
849 // actual month value.
852 quarter
= sal::static_int_cast
<sal_Int16
>( quarter
+
853 ((nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
) ?
854 reservedWords::QUARTER1_ABBREVIATION
: reservedWords::QUARTER1_WORD
) );
855 aOUStr
= xR
[quarter
];
857 // The "#100211# - checked" comments serve for detection of "use of
858 // sprintf is safe here" conditions. An sprintf encountered without
859 // having that comment triggers alarm ;-)
861 switch( nCalendarDisplayCode
) {
862 case CalendarDisplayCode::SHORT_MONTH
:
863 value
+= 1; // month is zero based
865 case CalendarDisplayCode::SHORT_DAY
:
866 sprintf(aStr
, "%d", value
); // #100211# - checked
868 case CalendarDisplayCode::LONG_YEAR
:
869 if ( aCalendar
.Name
== "gengou" )
870 sprintf(aStr
, "%02d", value
); // #100211# - checked
872 sprintf(aStr
, "%d", value
); // #100211# - checked
874 case CalendarDisplayCode::LONG_MONTH
:
875 value
+= 1; // month is zero based
876 sprintf(aStr
, "%02d", value
); // #100211# - checked
878 case CalendarDisplayCode::SHORT_YEAR
:
879 // Take last 2 digits, or only one if value<10, for example,
880 // in case of the Gengou calendar. For combined era+year always
881 // the full year is displayed, without leading 0.
882 // Workaround for non-combined calls in certain calendars is
883 // the kDisplayEraForcedLongYear flag, but this also could get
884 // called for YY not only E format codes, no differentiation
885 // possible here; the good news is that usually the Gregorian
886 // calendar is the default and hence YY calls for Gregorian and
887 // E for the other calendar and currently (2013-02-28) ROC is
888 // the only calendar using this.
889 // See i#116701 and fdo#60915
890 if (value
< 100 || bEraMode
|| (eraArray
&& (eraArray
[0].flags
& kDisplayEraForcedLongYear
)))
891 sprintf(aStr
, "%d", value
); // #100211# - checked
893 sprintf(aStr
, "%02d", value
% 100); // #100211# - checked
895 case CalendarDisplayCode::LONG_DAY
:
896 sprintf(aStr
, "%02d", value
); // #100211# - checked
899 case CalendarDisplayCode::SHORT_DAY_NAME
:
900 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 0);
901 case CalendarDisplayCode::LONG_DAY_NAME
:
902 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 1);
903 case CalendarDisplayCode::NARROW_DAY_NAME
:
904 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 2);
905 case CalendarDisplayCode::SHORT_MONTH_NAME
:
906 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 0);
907 case CalendarDisplayCode::LONG_MONTH_NAME
:
908 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 1);
909 case CalendarDisplayCode::NARROW_MONTH_NAME
:
910 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 2);
911 case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME
:
912 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH
, value
, 0);
913 case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME
:
914 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH
, value
, 1);
915 case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME
:
916 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH
, value
, 2);
917 case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME
:
918 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH
, value
, 0);
919 case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME
:
920 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH
, value
, 1);
921 case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME
:
922 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH
, value
, 2);
923 case CalendarDisplayCode::SHORT_ERA
:
924 return getDisplayName(CalendarDisplayIndex::ERA
, value
, 0);
925 case CalendarDisplayCode::LONG_ERA
:
926 return getDisplayName(CalendarDisplayIndex::ERA
, value
, 1);
928 case CalendarDisplayCode::SHORT_YEAR_AND_ERA
:
929 return getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA
, nNativeNumberMode
, true ) +
930 getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR
, nNativeNumberMode
, true );
932 case CalendarDisplayCode::LONG_YEAR_AND_ERA
:
933 return getDisplayStringImpl( CalendarDisplayCode::LONG_ERA
, nNativeNumberMode
, true ) +
934 getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR
, nNativeNumberMode
, true );
939 aOUStr
= OUString::createFromAscii(aStr
);
941 if (nNativeNumberMode
> 0) {
942 // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
943 if (eraArray
== gengou_eraArray
&& value
== 1
944 && (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
||
945 nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR
)
946 && (nNativeNumberMode
== NativeNumberMode::NATNUM1
||
947 nNativeNumberMode
== NativeNumberMode::NATNUM2
)) {
948 static sal_Unicode gan
= 0x5143;
949 return OUString(&gan
, 1);
951 sal_Int16 nNatNum
= NatNumForCalendar(aLocale
, nCalendarDisplayCode
, nNativeNumberMode
, value
);
953 return mxNatNum
->getNativeNumberString(aOUStr
, aLocale
, nNatNum
);
958 // Methods in XExtendedCalendar
960 Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
961 throw (RuntimeException
, std::exception
)
963 // make year and era in different order for year before and after 0.
964 if ((nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR_AND_ERA
||
965 nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR_AND_ERA
) &&
966 getValue(CalendarFieldIndex::ERA
) == 0) {
967 if (nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR_AND_ERA
)
968 return getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR
, nNativeNumberMode
, true ) +
969 getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA
, nNativeNumberMode
, true );
971 return getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR
, nNativeNumberMode
, true ) +
972 getDisplayStringImpl( CalendarDisplayCode::LONG_ERA
, nNativeNumberMode
, true );
974 return Calendar_gregorian::getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);
978 Calendar_gregorian::getImplementationName() throw( RuntimeException
, std::exception
)
980 return OUString::createFromAscii(cCalendar
);
984 Calendar_gregorian::supportsService(const OUString
& rServiceName
) throw( RuntimeException
, std::exception
)
986 return cppu::supportsService(this, rServiceName
);
989 Sequence
< OUString
> SAL_CALL
990 Calendar_gregorian::getSupportedServiceNames() throw( RuntimeException
, std::exception
)
992 Sequence
< OUString
> aRet
{ OUString::createFromAscii(cCalendar
) };
998 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */