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 <nativenumbersupplier.hxx>
25 #include <com/sun/star/i18n/CalendarDisplayCode.hpp>
26 #include <com/sun/star/i18n/CalendarDisplayIndex.hpp>
27 #include <com/sun/star/i18n/NativeNumberMode.hpp>
28 #include <com/sun/star/i18n/reservedWords.hpp>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <rtl/math.hxx>
31 #include <sal/log.hxx>
36 #define erDUMP_ICU_CALENDAR 0
37 #define erDUMP_I18N_CALENDAR 0
38 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
39 // If both are used, DUMP_ICU_CAL_MSG() must be used before DUMP_I18N_CAL_MSG()
40 // to obtain internally set values from ICU, else Calendar::get() calls in
41 // DUMP_I18N_CAL_MSG() recalculate!
43 // These pieces of macro are shamelessly borrowed from icu's olsontz.cpp, the
44 // double parens'ed approach to pass multiple parameters as one macro parameter
46 static void debug_cal_loc(const char *f
, int32_t l
)
48 fprintf(stderr
, "%s:%d: ", f
, l
);
51 static void debug_cal_msg(const char *pat
, ...)
55 vfprintf(stderr
, pat
, ap
);
59 #if erDUMP_ICU_CALENDAR
61 // DEFS = -DU_DEBUG_CALSVC -DUCAL_DEBUG_DUMP
62 // in workdir/UnpackedTarball/icu/source/icudefs.mk
63 // May need some patches to fix unmaintained things there.
64 extern void ucal_dump( const icu::Calendar
& );
65 static void debug_icu_cal_dump( const ::icu::Calendar
& r
)
69 // set a breakpoint here to pause display between dumps
71 // must use double parens, i.e.: DUMP_ICU_CAL_MSG(("four is: %d",4));
72 #define DUMP_ICU_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_icu_cal_dump(*body);}
73 #else // erDUMP_ICU_CALENDAR
74 #define DUMP_ICU_CAL_MSG(x)
75 #endif // erDUMP_ICU_CALENDAR
77 #if erDUMP_I18N_CALENDAR
78 static void debug_cal_millis_to_time( long nMillis
, long & h
, long & m
, long & s
, long & f
)
80 int sign
= (nMillis
< 0 ? -1 : 1);
81 nMillis
= ::std::abs(nMillis
);
82 h
= sign
* nMillis
/ (60 * 60 * 1000);
83 nMillis
-= sign
* h
* (60 * 60 * 1000);
84 m
= nMillis
/ (60 * 1000);
85 nMillis
-= m
* (60 * 1000);
87 nMillis
-= s
* (1000);
90 static void debug_i18n_cal_dump( const ::icu::Calendar
& r
)
93 long nMillis
, h
, m
, s
, f
;
94 fprintf( stderr
, " %04ld", (long)r
.get( UCAL_YEAR
, status
= U_ZERO_ERROR
));
95 fprintf( stderr
, "-%02ld", (long)r
.get( UCAL_MONTH
, status
= U_ZERO_ERROR
)+1);
96 fprintf( stderr
, "-%02ld", (long)r
.get( UCAL_DATE
, status
= U_ZERO_ERROR
));
97 fprintf( stderr
, " %02ld", (long)r
.get( UCAL_HOUR_OF_DAY
, status
= U_ZERO_ERROR
));
98 fprintf( stderr
, ":%02ld", (long)r
.get( UCAL_MINUTE
, status
= U_ZERO_ERROR
));
99 fprintf( stderr
, ":%02ld", (long)r
.get( UCAL_SECOND
, status
= U_ZERO_ERROR
));
100 fprintf( stderr
, " zone: %ld", (long)(nMillis
= r
.get( UCAL_ZONE_OFFSET
, status
= U_ZERO_ERROR
)));
101 fprintf( stderr
, " (%f min)", (double)nMillis
/ 60000);
102 debug_cal_millis_to_time( nMillis
, h
, m
, s
, f
);
103 fprintf( stderr
, " (%ld:%02ld:%02ld.%ld)", h
, m
, s
, f
);
104 fprintf( stderr
, " DST: %ld", (long)(nMillis
= r
.get( UCAL_DST_OFFSET
, status
= U_ZERO_ERROR
)));
105 fprintf( stderr
, " (%f min)", (double)nMillis
/ 60000);
106 debug_cal_millis_to_time( nMillis
, h
, m
, s
, f
);
107 fprintf( stderr
, " (%ld:%02ld:%02ld.%ld)", h
, m
, s
, f
);
108 fprintf( stderr
, "\n");
111 // must use double parens, i.e.: DUMP_I18N_CAL_MSG(("four is: %d",4));
112 #define DUMP_I18N_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_i18n_cal_dump(*body);}
113 #else // erDUMP_I18N_CALENDAR
114 #define DUMP_I18N_CAL_MSG(x)
115 #endif // erDUMP_I18N_CALENDAR
117 #else // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
118 #define DUMP_ICU_CAL_MSG(x)
119 #define DUMP_I18N_CAL_MSG(x)
120 #endif // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
123 using namespace ::com::sun::star::uno
;
124 using namespace ::com::sun::star::i18n
;
125 using namespace ::com::sun::star::lang
;
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");
170 UErrorCode status
= U_ZERO_ERROR
;
171 body
.reset( icu::Calendar::createInstance( aIcuLocale
, status
) );
172 if (!body
|| !U_SUCCESS(status
)) throw ERROR
;
176 Calendar_gregorian::~Calendar_gregorian()
180 Calendar_hanja::Calendar_hanja()
182 cCalendar
= "com.sun.star.i18n.Calendar_hanja";
186 Calendar_hanja::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
)
188 if ( displayIndex
== CalendarDisplayIndex::AM_PM
) {
189 // Am/Pm string for Korean Hanja calendar will refer to Japanese locale
190 css::lang::Locale
jaLocale("ja", OUString(), OUString());
191 if (idx
== 0) return LocaleDataImpl::get()->getLocaleItem(jaLocale
).timeAM
;
192 else if (idx
== 1) return LocaleDataImpl::get()->getLocaleItem(jaLocale
).timePM
;
196 return Calendar_gregorian::getDisplayName( displayIndex
, idx
, nameType
);
200 Calendar_hanja::loadCalendar( const OUString
& /*uniqueID*/, const css::lang::Locale
& rLocale
)
202 // Since this class could be called by service name 'hanja_yoil', we have to
203 // rename uniqueID to get right calendar defined in locale data.
204 Calendar_gregorian::loadCalendar("hanja", rLocale
);
207 static const Era gengou_eraArray
[] = {
208 {1868, 1, 1, 0}, // Meiji
209 {1912, 7, 30, 0}, // Taisho
210 {1926, 12, 25, 0}, // Showa
211 {1989, 1, 8, 0}, // Heisei
212 {2019, 5, 1, 0}, // Reiwa
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
)
241 // init. fieldValue[]
245 Sequence
< Calendar2
> xC
= LocaleDataImpl::get()->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()
277 css::i18n::Calendar SAL_CALL
278 Calendar_gregorian::getLoadedCalendar()
280 return LocaleDataImpl::downcastCalendar( aCalendar
);
284 Calendar_gregorian::getUniqueID()
286 return aCalendar
.Name
;
290 Calendar_gregorian::setDateTime( double fTimeInDays
)
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
);
303 UErrorCode status
= U_ZERO_ERROR
;
304 body
->setTime( fR
, status
);
305 if ( !U_SUCCESS(status
) ) throw ERROR
;
310 Calendar_gregorian::getDateTime()
316 UErrorCode status
= U_ZERO_ERROR
;
317 double fR
= body
->getTime(status
);
318 if ( !U_SUCCESS(status
) ) throw ERROR
;
319 return fR
/ U_MILLIS_PER_DAY
;
323 Calendar_gregorian::setLocalDateTime( double fTimeInDays
)
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
;
331 UErrorCode status
= U_ZERO_ERROR
;
332 body
->getTimeZone().getOffset( fR
, TRUE
, nZoneOffset
, nDSTOffset
, status
);
333 if ( !U_SUCCESS(status
) ) throw ERROR
;
334 status
= U_ZERO_ERROR
;
335 body
->setTime( fR
- (nZoneOffset
+ nDSTOffset
), status
);
336 if ( !U_SUCCESS(status
) ) throw ERROR
;
341 Calendar_gregorian::getLocalDateTime()
347 UErrorCode status
= U_ZERO_ERROR
;
348 double fTime
= body
->getTime( status
);
349 if ( !U_SUCCESS(status
) ) throw ERROR
;
350 status
= U_ZERO_ERROR
;
351 int32_t nZoneOffset
= body
->get( UCAL_ZONE_OFFSET
, status
);
352 if ( !U_SUCCESS(status
) ) throw ERROR
;
353 status
= U_ZERO_ERROR
;
354 int32_t nDSTOffset
= body
->get( UCAL_DST_OFFSET
, status
);
355 if ( !U_SUCCESS(status
) ) throw ERROR
;
356 return (fTime
+ (nZoneOffset
+ nDSTOffset
)) / U_MILLIS_PER_DAY
;
359 // map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
360 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
361 void Calendar_gregorian::mapFromGregorian()
364 sal_Int16 e
, y
, m
, d
;
366 e
= fieldValue
[CalendarFieldIndex::ERA
];
367 y
= fieldValue
[CalendarFieldIndex::YEAR
];
368 m
= fieldValue
[CalendarFieldIndex::MONTH
] + 1;
369 d
= fieldValue
[CalendarFieldIndex::DAY_OF_MONTH
];
371 // since the year is reversed for first era, it is reversed again here for Era compare.
375 for (e
= 0; eraArray
[e
].year
; e
++)
376 if ((y
!= eraArray
[e
].year
) ? y
< eraArray
[e
].year
:
377 (m
!= eraArray
[e
].month
) ? m
< eraArray
[e
].month
: d
< eraArray
[e
].day
)
380 fieldValue
[CalendarFieldIndex::ERA
] = e
;
381 fieldValue
[CalendarFieldIndex::YEAR
] =
382 sal::static_int_cast
<sal_Int16
>( (e
== 0) ? (eraArray
[0].year
- y
) : (y
- eraArray
[e
-1].year
+ 1) );
386 #define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
387 // map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
388 // By using eraArray, it can take care Japanese and Taiwan ROC calendar.
389 void Calendar_gregorian::mapToGregorian()
391 if (eraArray
&& (fieldSet
& FIELDS
)) {
392 sal_Int16 y
, e
= fieldValue
[CalendarFieldIndex::ERA
];
394 y
= sal::static_int_cast
<sal_Int16
>( eraArray
[0].year
- fieldValue
[CalendarFieldIndex::YEAR
] );
396 y
= sal::static_int_cast
<sal_Int16
>( eraArray
[e
-1].year
+ fieldValue
[CalendarFieldIndex::YEAR
] - 1 );
398 fieldSetValue
[CalendarFieldIndex::ERA
] = y
<= 0 ? 0 : 1;
399 fieldSetValue
[CalendarFieldIndex::YEAR
] = (y
<= 0 ? 1 - y
: y
);
404 /// @throws RuntimeException
405 static UCalendarDateFields
fieldNameConverter(sal_Int16 fieldIndex
)
407 UCalendarDateFields f
;
409 switch (fieldIndex
) {
410 case CalendarFieldIndex::AM_PM
: f
= UCAL_AM_PM
; break;
411 case CalendarFieldIndex::DAY_OF_MONTH
: f
= UCAL_DATE
; break;
412 case CalendarFieldIndex::DAY_OF_WEEK
: f
= UCAL_DAY_OF_WEEK
; break;
413 case CalendarFieldIndex::DAY_OF_YEAR
: f
= UCAL_DAY_OF_YEAR
; break;
414 case CalendarFieldIndex::DST_OFFSET
: f
= UCAL_DST_OFFSET
; break;
415 case CalendarFieldIndex::ZONE_OFFSET
: f
= UCAL_ZONE_OFFSET
; break;
416 case CalendarFieldIndex::HOUR
: f
= UCAL_HOUR_OF_DAY
; break;
417 case CalendarFieldIndex::MINUTE
: f
= UCAL_MINUTE
; break;
418 case CalendarFieldIndex::SECOND
: f
= UCAL_SECOND
; break;
419 case CalendarFieldIndex::MILLISECOND
: f
= UCAL_MILLISECOND
; break;
420 case CalendarFieldIndex::WEEK_OF_MONTH
: f
= UCAL_WEEK_OF_MONTH
; break;
421 case CalendarFieldIndex::WEEK_OF_YEAR
: f
= UCAL_WEEK_OF_YEAR
; break;
422 case CalendarFieldIndex::YEAR
: f
= UCAL_YEAR
; break;
423 case CalendarFieldIndex::MONTH
: f
= UCAL_MONTH
; break;
424 case CalendarFieldIndex::ERA
: f
= UCAL_ERA
; break;
425 default: throw ERROR
;
431 Calendar_gregorian::setValue( sal_Int16 fieldIndex
, sal_Int16 value
)
433 if (fieldIndex
< 0 || FIELD_INDEX_COUNT
<= fieldIndex
)
435 fieldSet
|= (1 << fieldIndex
);
436 fieldValue
[fieldIndex
] = value
;
439 bool Calendar_gregorian::getCombinedOffset( sal_Int32
& o_nOffset
,
440 sal_Int16 nParentFieldIndex
, sal_Int16 nChildFieldIndex
) const
443 bool bFieldsSet
= false;
444 if (fieldSet
& (1 << nParentFieldIndex
))
447 o_nOffset
= static_cast<sal_Int32
>( fieldValue
[nParentFieldIndex
]) * 60000;
449 if (fieldSet
& (1 << nChildFieldIndex
))
453 o_nOffset
-= static_cast<sal_uInt16
>( fieldValue
[nChildFieldIndex
]);
455 o_nOffset
+= static_cast<sal_uInt16
>( fieldValue
[nChildFieldIndex
]);
460 bool Calendar_gregorian::getZoneOffset( sal_Int32
& o_nOffset
) const
462 return getCombinedOffset( o_nOffset
, CalendarFieldIndex::ZONE_OFFSET
,
463 CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
);
466 bool Calendar_gregorian::getDSTOffset( sal_Int32
& o_nOffset
) const
468 return getCombinedOffset( o_nOffset
, CalendarFieldIndex::DST_OFFSET
,
469 CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
);
472 void Calendar_gregorian::submitFields()
474 for (sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++)
476 if (fieldSet
& (1 << fieldIndex
))
481 body
->set(fieldNameConverter(fieldIndex
), fieldSetValue
[fieldIndex
]);
483 case CalendarFieldIndex::ZONE_OFFSET
:
484 case CalendarFieldIndex::DST_OFFSET
:
485 case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
:
486 case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
:
487 break; // nothing, extra handling
491 sal_Int32 nZoneOffset
, nDSTOffset
;
492 if (getZoneOffset( nZoneOffset
))
493 body
->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET
), nZoneOffset
);
494 if (getDSTOffset( nDSTOffset
))
495 body
->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET
), nDSTOffset
);
498 void Calendar_gregorian::setValue()
500 // Copy fields before calling submitFields() directly or indirectly below.
501 memcpy(fieldSetValue
, fieldValue
, sizeof(fieldSetValue
));
502 // Possibly setup ERA and YEAR in fieldSetValue.
505 DUMP_ICU_CAL_MSG(("%s\n","setValue() before submission"));
506 DUMP_I18N_CAL_MSG(("%s\n","setValue() before submission"));
510 DUMP_ICU_CAL_MSG(("%s\n","setValue() after submission"));
511 DUMP_I18N_CAL_MSG(("%s\n","setValue() after submission"));
513 #if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
515 // force icu::Calendar to recalculate
517 sal_Int32 nTmp
= body
->get( UCAL_DATE
, status
= U_ZERO_ERROR
);
518 DUMP_ICU_CAL_MSG(("%s: %d\n","setValue() result day",nTmp
));
519 DUMP_I18N_CAL_MSG(("%s: %d\n","setValue() result day",nTmp
));
524 void Calendar_gregorian::getValue()
526 DUMP_ICU_CAL_MSG(("%s\n","getValue()"));
527 DUMP_I18N_CAL_MSG(("%s\n","getValue()"));
528 for (sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++)
530 if (fieldIndex
== CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
||
531 fieldIndex
== CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
)
532 continue; // not ICU fields
534 UErrorCode status
= U_ZERO_ERROR
;
535 sal_Int32 value
= body
->get( fieldNameConverter(
536 fieldIndex
), status
);
537 if ( !U_SUCCESS(status
) ) throw ERROR
;
539 // Convert millisecond to minute for ZONE and DST and set remainder in
541 if (fieldIndex
== CalendarFieldIndex::ZONE_OFFSET
)
543 sal_Int32 nMinutes
= value
/ 60000;
544 sal_Int16 nMillis
= static_cast<sal_Int16
>( static_cast<sal_uInt16
>(
545 abs( value
- nMinutes
* 60000)));
546 fieldValue
[CalendarFieldIndex::ZONE_OFFSET
] = static_cast<sal_Int16
>( nMinutes
);
547 fieldValue
[CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS
] = nMillis
;
549 else if (fieldIndex
== CalendarFieldIndex::DST_OFFSET
)
551 sal_Int32 nMinutes
= value
/ 60000;
552 sal_Int16 nMillis
= static_cast<sal_Int16
>( static_cast<sal_uInt16
>(
553 abs( value
- nMinutes
* 60000)));
554 fieldValue
[CalendarFieldIndex::DST_OFFSET
] = static_cast<sal_Int16
>( nMinutes
);
555 fieldValue
[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS
] = nMillis
;
558 fieldValue
[fieldIndex
] = static_cast<sal_Int16
>(value
);
560 // offset 1 since the value for week start day SunDay is different between Calendar and Weekdays.
561 if ( fieldIndex
== CalendarFieldIndex::DAY_OF_WEEK
)
562 fieldValue
[fieldIndex
]--; // UCAL_SUNDAY:/* == 1 */ ==> Weekdays::SUNDAY /* ==0 */
569 Calendar_gregorian::getValue( sal_Int16 fieldIndex
)
571 if (fieldIndex
< 0 || FIELD_INDEX_COUNT
<= fieldIndex
)
579 return fieldValue
[fieldIndex
];
583 Calendar_gregorian::addValue( sal_Int16 fieldIndex
, sal_Int32 value
)
585 // since ZONE and DST could not be add, we don't need to convert value here
586 UErrorCode status
= U_ZERO_ERROR
;
587 body
->add(fieldNameConverter(fieldIndex
), value
, status
);
588 if ( !U_SUCCESS(status
) ) throw ERROR
;
593 Calendar_gregorian::isValid()
596 sal_Int32 tmp
= fieldSet
;
598 memcpy(fieldSetValue
, fieldValue
, sizeof(fieldSetValue
));
600 for ( sal_Int16 fieldIndex
= 0; fieldIndex
< FIELD_INDEX_COUNT
; fieldIndex
++ ) {
601 // compare only with fields that are set and reset fieldSet[]
602 if (tmp
& (1 << fieldIndex
)) {
603 if (fieldSetValue
[fieldIndex
] != fieldValue
[fieldIndex
])
611 // NativeNumberMode has different meaning between Number and Calendar for Asian locales.
612 // Here is the mapping table
613 // calendar(q/y/m/d) zh_CN zh_TW ja ko
614 // NatNum1 NatNum1/1/7/7 NatNum1/1/7/7 NatNum1/1/4/4 NatNum1/1/7/7
615 // NatNum2 NatNum2/2/8/8 NatNum2/2/8/8 NatNum2/2/5/5 NatNum2/2/8/8
616 // NatNum3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3
617 // NatNum4 NatNum9/9/11/11
619 static sal_Int16
NatNumForCalendar(const css::lang::Locale
& aLocale
,
620 sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
, sal_Int16 value
)
622 bool isShort
= ((nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
||
623 nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR
) && value
>= 100) ||
624 nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
||
625 nCalendarDisplayCode
== CalendarDisplayCode::LONG_QUARTER
;
626 bool isChinese
= aLocale
.Language
== "zh";
627 bool isJapanese
= aLocale
.Language
== "ja";
628 bool isKorean
= aLocale
.Language
== "ko";
630 if (isChinese
|| isJapanese
|| isKorean
) {
631 switch (nNativeNumberMode
) {
632 case NativeNumberMode::NATNUM1
:
634 nNativeNumberMode
= isJapanese
? NativeNumberMode::NATNUM4
: NativeNumberMode::NATNUM7
;
636 case NativeNumberMode::NATNUM2
:
638 nNativeNumberMode
= isJapanese
? NativeNumberMode::NATNUM5
: NativeNumberMode::NATNUM8
;
640 case NativeNumberMode::NATNUM3
:
642 case NativeNumberMode::NATNUM4
:
644 return isShort
? NativeNumberMode::NATNUM9
: NativeNumberMode::NATNUM11
;
649 return nNativeNumberMode
;
652 static sal_Int32
DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode
)
654 switch( nCalendarDisplayCode
) {
655 case CalendarDisplayCode::SHORT_DAY
:
656 case CalendarDisplayCode::LONG_DAY
:
657 return CalendarFieldIndex::DAY_OF_MONTH
;
658 case CalendarDisplayCode::SHORT_DAY_NAME
:
659 case CalendarDisplayCode::LONG_DAY_NAME
:
660 case CalendarDisplayCode::NARROW_DAY_NAME
:
661 return CalendarFieldIndex::DAY_OF_WEEK
;
662 case CalendarDisplayCode::SHORT_QUARTER
:
663 case CalendarDisplayCode::LONG_QUARTER
:
664 case CalendarDisplayCode::SHORT_MONTH
:
665 case CalendarDisplayCode::LONG_MONTH
:
666 case CalendarDisplayCode::SHORT_MONTH_NAME
:
667 case CalendarDisplayCode::LONG_MONTH_NAME
:
668 case CalendarDisplayCode::NARROW_MONTH_NAME
:
669 case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME
:
670 case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME
:
671 case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME
:
672 case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME
:
673 case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME
:
674 case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME
:
675 return CalendarFieldIndex::MONTH
;
676 case CalendarDisplayCode::SHORT_YEAR
:
677 case CalendarDisplayCode::LONG_YEAR
:
678 return CalendarFieldIndex::YEAR
;
679 case CalendarDisplayCode::SHORT_ERA
:
680 case CalendarDisplayCode::LONG_ERA
:
681 return CalendarFieldIndex::ERA
;
682 case CalendarDisplayCode::SHORT_YEAR_AND_ERA
:
683 case CalendarDisplayCode::LONG_YEAR_AND_ERA
:
684 return CalendarFieldIndex::YEAR
;
691 Calendar_gregorian::getFirstDayOfWeek()
693 // UCAL_SUNDAY == 1, Weekdays::SUNDAY == 0 => offset -1
694 // Check for underflow just in case we're called "out of sync".
695 return ::std::max( sal::static_int_cast
<sal_Int16
>(0),
696 sal::static_int_cast
<sal_Int16
>( static_cast<sal_Int16
>(
697 body
->getFirstDayOfWeek()) - 1));
701 Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day
)
703 // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
704 body
->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek
>( day
+ 1));
708 Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days
)
710 aCalendar
.MinimumNumberOfDaysForFirstWeek
= days
;
711 body
->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days
));
715 Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek()
717 return aCalendar
.MinimumNumberOfDaysForFirstWeek
;
721 Calendar_gregorian::getNumberOfMonthsInYear()
723 return static_cast<sal_Int16
>(aCalendar
.Months
.getLength());
728 Calendar_gregorian::getNumberOfDaysInWeek()
730 return static_cast<sal_Int16
>(aCalendar
.Days
.getLength());
734 Sequence
< CalendarItem
> SAL_CALL
735 Calendar_gregorian::getDays()
737 return LocaleDataImpl::downcastCalendarItems( aCalendar
.Days
);
741 Sequence
< CalendarItem
> SAL_CALL
742 Calendar_gregorian::getMonths()
744 return LocaleDataImpl::downcastCalendarItems( aCalendar
.Months
);
748 Sequence
< CalendarItem2
> SAL_CALL
749 Calendar_gregorian::getDays2()
751 return aCalendar
.Days
;
755 Sequence
< CalendarItem2
> SAL_CALL
756 Calendar_gregorian::getMonths2()
758 return aCalendar
.Months
;
762 Sequence
< CalendarItem2
> SAL_CALL
763 Calendar_gregorian::getGenitiveMonths2()
765 return aCalendar
.GenitiveMonths
;
769 Sequence
< CalendarItem2
> SAL_CALL
770 Calendar_gregorian::getPartitiveMonths2()
772 return aCalendar
.PartitiveMonths
;
777 Calendar_gregorian::getDisplayName( sal_Int16 displayIndex
, sal_Int16 idx
, sal_Int16 nameType
)
781 switch( displayIndex
) {
782 case CalendarDisplayIndex::AM_PM
:/* ==0 */
783 if (idx
== 0) aStr
= LocaleDataImpl::get()->getLocaleItem(aLocale
).timeAM
;
784 else if (idx
== 1) aStr
= LocaleDataImpl::get()->getLocaleItem(aLocale
).timePM
;
787 case CalendarDisplayIndex::DAY
:
788 if( idx
>= aCalendar
.Days
.getLength() ) throw ERROR
;
789 if (nameType
== 0) aStr
= aCalendar
.Days
[idx
].AbbrevName
;
790 else if (nameType
== 1) aStr
= aCalendar
.Days
[idx
].FullName
;
791 else if (nameType
== 2) aStr
= aCalendar
.Days
[idx
].NarrowName
;
794 case CalendarDisplayIndex::MONTH
:
795 if( idx
>= aCalendar
.Months
.getLength() ) throw ERROR
;
796 if (nameType
== 0) aStr
= aCalendar
.Months
[idx
].AbbrevName
;
797 else if (nameType
== 1) aStr
= aCalendar
.Months
[idx
].FullName
;
798 else if (nameType
== 2) aStr
= aCalendar
.Months
[idx
].NarrowName
;
801 case CalendarDisplayIndex::GENITIVE_MONTH
:
802 if( idx
>= aCalendar
.GenitiveMonths
.getLength() ) throw ERROR
;
803 if (nameType
== 0) aStr
= aCalendar
.GenitiveMonths
[idx
].AbbrevName
;
804 else if (nameType
== 1) aStr
= aCalendar
.GenitiveMonths
[idx
].FullName
;
805 else if (nameType
== 2) aStr
= aCalendar
.GenitiveMonths
[idx
].NarrowName
;
808 case CalendarDisplayIndex::PARTITIVE_MONTH
:
809 if( idx
>= aCalendar
.PartitiveMonths
.getLength() ) throw ERROR
;
810 if (nameType
== 0) aStr
= aCalendar
.PartitiveMonths
[idx
].AbbrevName
;
811 else if (nameType
== 1) aStr
= aCalendar
.PartitiveMonths
[idx
].FullName
;
812 else if (nameType
== 2) aStr
= aCalendar
.PartitiveMonths
[idx
].NarrowName
;
815 case CalendarDisplayIndex::ERA
:
816 if( idx
>= aCalendar
.Eras
.getLength() ) throw ERROR
;
817 if (nameType
== 0) aStr
= aCalendar
.Eras
[idx
].AbbrevName
;
818 else if (nameType
== 1) aStr
= aCalendar
.Eras
[idx
].FullName
;
821 case CalendarDisplayIndex::YEAR
:
829 // Methods in XExtendedCalendar
831 Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
833 return getDisplayStringImpl( nCalendarDisplayCode
, nNativeNumberMode
, false);
837 Calendar_gregorian::getDisplayStringImpl( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
, bool bEraMode
)
839 sal_Int16 value
= getValue(sal::static_int_cast
<sal_Int16
>( DisplayCode2FieldIndex(nCalendarDisplayCode
) ));
842 if (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
||
843 nCalendarDisplayCode
== CalendarDisplayCode::LONG_QUARTER
) {
844 Sequence
< OUString
> xR
= LocaleDataImpl::get()->getReservedWord(aLocale
);
845 sal_Int16 quarter
= value
/ 3;
846 // Since this base class method may be called by derived calendar
847 // classes where a year consists of more than 12 months we need a check
848 // to not run out of bounds of reserved quarter words. Perhaps a more
849 // clean way (instead of dividing by 3) would be to first get the
850 // number of months, divide by 4 and then use that result to divide the
851 // actual month value.
854 quarter
= sal::static_int_cast
<sal_Int16
>( quarter
+
855 ((nCalendarDisplayCode
== CalendarDisplayCode::SHORT_QUARTER
) ?
856 reservedWords::QUARTER1_ABBREVIATION
: reservedWords::QUARTER1_WORD
) );
857 aOUStr
= xR
[quarter
];
859 // The "#100211# - checked" comments serve for detection of "use of
860 // sprintf is safe here" conditions. An sprintf encountered without
861 // having that comment triggers alarm ;-)
863 switch( nCalendarDisplayCode
) {
864 case CalendarDisplayCode::SHORT_MONTH
:
865 value
+= 1; // month is zero based
867 case CalendarDisplayCode::SHORT_DAY
:
868 sprintf(aStr
, "%d", value
); // #100211# - checked
870 case CalendarDisplayCode::LONG_YEAR
:
871 if ( aCalendar
.Name
== "gengou" )
872 sprintf(aStr
, "%02d", value
); // #100211# - checked
874 sprintf(aStr
, "%d", value
); // #100211# - checked
876 case CalendarDisplayCode::LONG_MONTH
:
877 value
+= 1; // month is zero based
878 sprintf(aStr
, "%02d", value
); // #100211# - checked
880 case CalendarDisplayCode::SHORT_YEAR
:
881 // Take last 2 digits, or only one if value<10, for example,
882 // in case of the Gengou calendar. For combined era+year always
883 // the full year is displayed, without leading 0.
884 // Workaround for non-combined calls in certain calendars is
885 // the kDisplayEraForcedLongYear flag, but this also could get
886 // called for YY not only E format codes, no differentiation
887 // possible here; the good news is that usually the Gregorian
888 // calendar is the default and hence YY calls for Gregorian and
889 // E for the other calendar and currently (2013-02-28) ROC is
890 // the only calendar using this.
891 // See i#116701 and fdo#60915
892 if (value
< 100 || bEraMode
|| (eraArray
&& (eraArray
[0].flags
& kDisplayEraForcedLongYear
)))
893 sprintf(aStr
, "%d", value
); // #100211# - checked
895 sprintf(aStr
, "%02d", value
% 100); // #100211# - checked
897 case CalendarDisplayCode::LONG_DAY
:
898 sprintf(aStr
, "%02d", value
); // #100211# - checked
901 case CalendarDisplayCode::SHORT_DAY_NAME
:
902 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 0);
903 case CalendarDisplayCode::LONG_DAY_NAME
:
904 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 1);
905 case CalendarDisplayCode::NARROW_DAY_NAME
:
906 return getDisplayName(CalendarDisplayIndex::DAY
, value
, 2);
907 case CalendarDisplayCode::SHORT_MONTH_NAME
:
908 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 0);
909 case CalendarDisplayCode::LONG_MONTH_NAME
:
910 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 1);
911 case CalendarDisplayCode::NARROW_MONTH_NAME
:
912 return getDisplayName(CalendarDisplayIndex::MONTH
, value
, 2);
913 case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME
:
914 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH
, value
, 0);
915 case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME
:
916 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH
, value
, 1);
917 case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME
:
918 return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH
, value
, 2);
919 case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME
:
920 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH
, value
, 0);
921 case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME
:
922 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH
, value
, 1);
923 case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME
:
924 return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH
, value
, 2);
925 case CalendarDisplayCode::SHORT_ERA
:
926 return getDisplayName(CalendarDisplayIndex::ERA
, value
, 0);
927 case CalendarDisplayCode::LONG_ERA
:
928 return getDisplayName(CalendarDisplayIndex::ERA
, value
, 1);
930 case CalendarDisplayCode::SHORT_YEAR_AND_ERA
:
931 return getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA
, nNativeNumberMode
, true ) +
932 getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR
, nNativeNumberMode
, true );
934 case CalendarDisplayCode::LONG_YEAR_AND_ERA
:
935 return getDisplayStringImpl( CalendarDisplayCode::LONG_ERA
, nNativeNumberMode
, true ) +
936 getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR
, nNativeNumberMode
, true );
941 aOUStr
= OUString::createFromAscii(aStr
);
943 // NatNum12 used only for selected parts
944 if (nNativeNumberMode
> 0 && nNativeNumberMode
!= 12) {
945 // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
946 if (eraArray
== gengou_eraArray
&& value
== 1
947 && (nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR
||
948 nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR
)
949 && (nNativeNumberMode
== NativeNumberMode::NATNUM1
||
950 nNativeNumberMode
== NativeNumberMode::NATNUM2
)) {
951 static sal_Unicode gan
= 0x5143;
952 return OUString(&gan
, 1);
954 sal_Int16 nNatNum
= NatNumForCalendar(aLocale
, nCalendarDisplayCode
, nNativeNumberMode
, value
);
956 return mxNatNum
->getNativeNumberString(aOUStr
, aLocale
, nNatNum
);
961 // Methods in XExtendedCalendar
963 Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode
, sal_Int16 nNativeNumberMode
)
965 // make year and era in different order for year before and after 0.
966 if ((nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR_AND_ERA
||
967 nCalendarDisplayCode
== CalendarDisplayCode::SHORT_YEAR_AND_ERA
) &&
968 getValue(CalendarFieldIndex::ERA
) == 0) {
969 if (nCalendarDisplayCode
== CalendarDisplayCode::LONG_YEAR_AND_ERA
)
970 return getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR
, nNativeNumberMode
, true ) +
971 getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA
, nNativeNumberMode
, true );
973 return getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR
, nNativeNumberMode
, true ) +
974 getDisplayStringImpl( CalendarDisplayCode::LONG_ERA
, nNativeNumberMode
, true );
976 return Calendar_gregorian::getDisplayString(nCalendarDisplayCode
, nNativeNumberMode
);
980 Calendar_gregorian::getImplementationName()
982 return OUString::createFromAscii(cCalendar
);
986 Calendar_gregorian::supportsService(const OUString
& rServiceName
)
988 return cppu::supportsService(this, rServiceName
);
991 Sequence
< OUString
> SAL_CALL
992 Calendar_gregorian::getSupportedServiceNames()
994 Sequence
< OUString
> aRet
{ OUString::createFromAscii(cCalendar
) };
1000 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */