summaryrefslogtreecommitdiffstats
path: root/i18npool/source/calendar
diff options
context:
space:
mode:
Diffstat (limited to 'i18npool/source/calendar')
-rw-r--r--i18npool/source/calendar/calendarImpl.cxx370
-rw-r--r--i18npool/source/calendar/calendar_gregorian.cxx1082
-rw-r--r--i18npool/source/calendar/calendar_hijri.cxx317
-rw-r--r--i18npool/source/calendar/calendar_jewish.cxx316
4 files changed, 2085 insertions, 0 deletions
diff --git a/i18npool/source/calendar/calendarImpl.cxx b/i18npool/source/calendar/calendarImpl.cxx
new file mode 100644
index 0000000000..4e364696cf
--- /dev/null
+++ b/i18npool/source/calendar/calendarImpl.cxx
@@ -0,0 +1,370 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <calendarImpl.hxx>
+#include <calendar_gregorian.hxx>
+#include <localedata.hxx>
+#include <comphelper/processfactory.hxx>
+#include <cppuhelper/supportsservice.hxx>
+
+#include <com/sun/star/uno/XComponentContext.hpp>
+
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::i18n;
+
+namespace i18npool {
+
+CalendarImpl::CalendarImpl() : m_xContext(comphelper::getProcessComponentContext())
+{
+}
+
+CalendarImpl::CalendarImpl(const Reference< XComponentContext > &rxContext) : m_xContext(rxContext)
+{
+ if (!m_xContext.is())
+ throw RuntimeException("CalendarImpl::CalendarImpl: empty m_xContext");
+}
+
+CalendarImpl::~CalendarImpl()
+{
+}
+
+void SAL_CALL
+CalendarImpl::loadDefaultCalendarTZ( const css::lang::Locale& rLocale, const OUString& rTimeZone )
+{
+ const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
+ auto pCal = std::find_if(xC.begin(), xC.end(), [](const Calendar2& rCal) { return rCal.Default; });
+ if (pCal == xC.end())
+ throw RuntimeException("CalendarImpl::loadDefaultCalendarTZ: no default calendar found for this locale");
+ loadCalendarTZ(pCal->Name, rLocale, rTimeZone);
+}
+
+void SAL_CALL
+CalendarImpl::loadCalendarTZ( const OUString& uniqueID, const css::lang::Locale& rLocale, const OUString& rTimeZone )
+{
+ Reference < XCalendar4 > xOldCalendar( xCalendar ); // backup
+ const OUString aCacheID( uniqueID + "_" + rTimeZone);
+ bool bTimeZone = true;
+ sal_Int32 i;
+
+ for (i = 0; i < sal::static_int_cast<sal_Int32>(lookupTable.size()); i++) {
+ lookupTableItem &listItem = lookupTable[i];
+ if (aCacheID == listItem.m_aCacheID) {
+ xCalendar = listItem.xCalendar;
+ break;
+ }
+ }
+
+ if (i >= sal::static_int_cast<sal_Int32>(lookupTable.size())) {
+ Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
+ "com.sun.star.i18n.Calendar_" + uniqueID, m_xContext);
+
+ if ( ! xI.is() ) {
+ // check if the calendar is defined in localedata, load gregorian calendar service.
+ const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
+ if (std::any_of(xC.begin(), xC.end(), [&uniqueID](const Calendar2& rCal) { return uniqueID == rCal.Name; }))
+ xI = m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Calendar_gregorian", m_xContext);
+ }
+
+ if ( !xI.is() )
+ throw RuntimeException("CalendarImpl::loadCalendarTZ: no calendar found for this locale");
+ xCalendar.set(xI, UNO_QUERY);
+
+ if (!rTimeZone.isEmpty())
+ {
+ /* XXX NOTE: currently (2019-06-19) calendar implementations derive
+ * from Calendar_gregorian, even Hijri and Jewish. If that should
+ * change in future this should be adapted. */
+ Calendar_gregorian* pCal = dynamic_cast<Calendar_gregorian*>(xCalendar.get());
+ bTimeZone = (pCal && pCal->setTimeZone(rTimeZone));
+ }
+
+ lookupTable.emplace_back( aCacheID, xCalendar );
+ }
+
+ if ( !xCalendar.is() )
+ {
+ xCalendar = xOldCalendar;
+ throw RuntimeException("CalendarImpl::loadCalendarTZ: no calendar found for this locale, should use old one?");
+ }
+
+ try
+ {
+ xCalendar->loadCalendar(uniqueID, rLocale);
+ }
+ catch ( Exception& )
+ { // restore previous calendar and re-throw
+ xCalendar = xOldCalendar;
+ throw;
+ }
+
+ if (!bTimeZone)
+ // The calendar is usable but is not in the expected time zone.
+ throw RuntimeException("CalendarImpl::loadCalendarTZ: the calendar is usable but is not in the expected time zone");
+}
+
+Calendar2 SAL_CALL
+CalendarImpl::getLoadedCalendar2()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getLoadedCalendar2: no calendar");
+ return xCalendar->getLoadedCalendar2();
+}
+
+::css::i18n::Calendar SAL_CALL
+CalendarImpl::getLoadedCalendar()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getLoadedCalendar: no calendar");
+ return xCalendar->getLoadedCalendar();
+}
+
+Sequence< OUString > SAL_CALL
+CalendarImpl::getAllCalendars( const css::lang::Locale& rLocale )
+{
+ const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
+ Sequence< OUString > xSeq( xC.getLength() );
+ std::transform(xC.begin(), xC.end(), xSeq.getArray(),
+ [](const Calendar2& rCal) { return rCal.Name; });
+ return xSeq;
+}
+
+void SAL_CALL
+CalendarImpl::setDateTime( double fTimeInDays )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::setDateTime: no calendar");
+ xCalendar->setDateTime( fTimeInDays );
+}
+
+double SAL_CALL
+CalendarImpl::getDateTime()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getDateTime: no calendar");
+ return xCalendar->getDateTime();
+}
+
+void SAL_CALL
+CalendarImpl::setLocalDateTime( double fTimeInDays )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::setLocalDateTime: no calendar");
+ xCalendar->setLocalDateTime( fTimeInDays );
+}
+
+double SAL_CALL
+CalendarImpl::getLocalDateTime()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getLocalDateTime: no calendar");
+ return xCalendar->getLocalDateTime();
+}
+
+void SAL_CALL CalendarImpl::loadDefaultCalendar( const css::lang::Locale& rLocale )
+{
+ loadDefaultCalendarTZ( rLocale, OUString());
+}
+
+void SAL_CALL CalendarImpl::loadCalendar( const OUString& uniqueID, const css::lang::Locale& rLocale )
+{
+ loadCalendarTZ( uniqueID, rLocale, OUString());
+}
+
+OUString SAL_CALL
+CalendarImpl::getUniqueID()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getUniqueID: no calendar");
+ return xCalendar->getUniqueID();
+}
+
+void SAL_CALL
+CalendarImpl::setValue( sal_Int16 fieldIndex, sal_Int16 value )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::setValue: no calendar");
+ xCalendar->setValue( fieldIndex, value );
+}
+
+sal_Int16 SAL_CALL
+CalendarImpl::getValue( sal_Int16 fieldIndex )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getValue: no calendar");
+ return xCalendar->getValue( fieldIndex );
+}
+
+void SAL_CALL
+CalendarImpl::addValue( sal_Int16 fieldIndex, sal_Int32 amount )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::addValue: no calendar");
+ xCalendar->addValue( fieldIndex, amount);
+}
+
+sal_Int16 SAL_CALL
+CalendarImpl::getFirstDayOfWeek()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getFirstDayOfWeek: no calendar");
+ return xCalendar->getFirstDayOfWeek();
+}
+
+void SAL_CALL
+CalendarImpl::setFirstDayOfWeek( sal_Int16 day )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::setFirstDayOfWeek: no calendar");
+ xCalendar->setFirstDayOfWeek(day);
+}
+
+void SAL_CALL
+CalendarImpl::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::setMinimumNumberOfDaysForFirstWeek: no calendar");
+ xCalendar->setMinimumNumberOfDaysForFirstWeek(days);
+}
+
+sal_Int16 SAL_CALL
+CalendarImpl::getMinimumNumberOfDaysForFirstWeek()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getMinimumNumberOfDaysForFirstWeek: no calendar");
+ return xCalendar->getMinimumNumberOfDaysForFirstWeek();
+}
+
+
+OUString SAL_CALL
+CalendarImpl::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getDisplayName: no calendar");
+ return xCalendar->getDisplayName( displayIndex, idx, nameType );
+}
+
+sal_Int16 SAL_CALL
+CalendarImpl::getNumberOfMonthsInYear()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::setDisplayName: no calendar");
+ return xCalendar->getNumberOfMonthsInYear();
+}
+
+
+sal_Int16 SAL_CALL
+CalendarImpl::getNumberOfDaysInWeek()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getNumberOfDaysInWeek: no calendar");
+ return xCalendar->getNumberOfDaysInWeek();
+}
+
+
+Sequence< CalendarItem > SAL_CALL
+CalendarImpl::getDays()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::setNumberOfDaysInWeek: no calendar");
+ return xCalendar->getDays();
+}
+
+
+Sequence< CalendarItem > SAL_CALL
+CalendarImpl::getMonths()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getMonths: no calendar");
+ return xCalendar->getMonths();
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+CalendarImpl::getDays2()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getDays2: no calendar");
+ return xCalendar->getDays2();
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+CalendarImpl::getMonths2()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getMonths2: no calendar");
+ return xCalendar->getMonths2();
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+CalendarImpl::getGenitiveMonths2()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getGenitiveMonths2: no calendar");
+ return xCalendar->getGenitiveMonths2();
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+CalendarImpl::getPartitiveMonths2()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getPartitiveMonths2: no calendar");
+ return xCalendar->getPartitiveMonths2();
+}
+
+
+sal_Bool SAL_CALL
+CalendarImpl::isValid()
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::isValid: no calendar");
+ return xCalendar->isValid();
+}
+
+OUString SAL_CALL
+CalendarImpl::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
+{
+ if (!xCalendar.is())
+ throw RuntimeException("CalendarImpl::getDisplayString: no calendar");
+ return xCalendar->getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
+}
+
+OUString SAL_CALL
+CalendarImpl::getImplementationName()
+{
+ return "com.sun.star.i18n.CalendarImpl";
+}
+
+sal_Bool SAL_CALL
+CalendarImpl::supportsService(const OUString& rServiceName)
+{
+ return cppu::supportsService(this, rServiceName);
+}
+
+Sequence< OUString > SAL_CALL
+CalendarImpl::getSupportedServiceNames()
+{
+ return { "com.sun.star.i18n.LocaleCalendar", "com.sun.star.i18n.LocaleCalendar2" };
+}
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/i18npool/source/calendar/calendar_gregorian.cxx b/i18npool/source/calendar/calendar_gregorian.cxx
new file mode 100644
index 0000000000..0dbe964e5b
--- /dev/null
+++ b/i18npool/source/calendar/calendar_gregorian.cxx
@@ -0,0 +1,1082 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <algorithm>
+
+#include <calendar_gregorian.hxx>
+#include <localedata.hxx>
+#include <nativenumbersupplier.hxx>
+#include <com/sun/star/i18n/CalendarDisplayCode.hpp>
+#include <com/sun/star/i18n/CalendarDisplayIndex.hpp>
+#include <com/sun/star/i18n/NativeNumberMode.hpp>
+#include <com/sun/star/i18n/reservedWords.hpp>
+#include <cppuhelper/supportsservice.hxx>
+#include <o3tl/sprintf.hxx>
+#include <rtl/math.hxx>
+#include <sal/log.hxx>
+
+#include <stdio.h>
+#include <string.h>
+
+#define erDUMP_ICU_CALENDAR 0
+#define erDUMP_I18N_CALENDAR 0
+#if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
+// If both are used, DUMP_ICU_CAL_MSG() must be used before DUMP_I18N_CAL_MSG()
+// to obtain internally set values from ICU, else Calendar::get() calls in
+// DUMP_I18N_CAL_MSG() recalculate!
+
+// These pieces of macro are shamelessly borrowed from icu's olsontz.cpp, the
+// double parens'ed approach to pass multiple parameters as one macro parameter
+// is appealing.
+static void debug_cal_loc(const char *f, int32_t l)
+{
+ fprintf(stderr, "%s:%d: ", f, l);
+}
+# include <stdarg.h>
+static void debug_cal_msg(const char *pat, ...)
+{
+ va_list ap;
+ va_start(ap, pat);
+ vfprintf(stderr, pat, ap);
+ va_end(ap);
+}
+
+#if erDUMP_ICU_CALENDAR
+// Make icu with
+// DEFS = -DU_DEBUG_CALSVC -DUCAL_DEBUG_DUMP
+// in workdir/UnpackedTarball/icu/source/icudefs.mk
+// May need some patches to fix unmaintained things there.
+extern void ucal_dump( const icu::Calendar & );
+static void debug_icu_cal_dump( const ::icu::Calendar & r )
+{
+ ucal_dump(r);
+ fflush(stderr);
+ // set a breakpoint here to pause display between dumps
+}
+// must use double parens, i.e.: DUMP_ICU_CAL_MSG(("four is: %d",4));
+#define DUMP_ICU_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_icu_cal_dump(*body);}
+#else // erDUMP_ICU_CALENDAR
+#define DUMP_ICU_CAL_MSG(x)
+#endif // erDUMP_ICU_CALENDAR
+
+#if erDUMP_I18N_CALENDAR
+static void debug_cal_millis_to_time( long nMillis, long & h, long & m, long & s, long & f )
+{
+ int sign = (nMillis < 0 ? -1 : 1);
+ nMillis = ::std::abs(nMillis);
+ h = sign * nMillis / (60 * 60 * 1000);
+ nMillis -= sign * h * (60 * 60 * 1000);
+ m = nMillis / (60 * 1000);
+ nMillis -= m * (60 * 1000);
+ s = nMillis / (1000);
+ nMillis -= s * (1000);
+ f = nMillis;
+}
+static void debug_i18n_cal_dump( const ::icu::Calendar & r )
+{
+ UErrorCode status;
+ long nMillis, h, m, s, f;
+ fprintf( stderr, " %04ld", (long)r.get( UCAL_YEAR, status = U_ZERO_ERROR));
+ fprintf( stderr, "-%02ld", (long)r.get( UCAL_MONTH, status = U_ZERO_ERROR)+1);
+ fprintf( stderr, "-%02ld", (long)r.get( UCAL_DATE, status = U_ZERO_ERROR));
+ fprintf( stderr, " %02ld", (long)r.get( UCAL_HOUR_OF_DAY, status = U_ZERO_ERROR));
+ fprintf( stderr, ":%02ld", (long)r.get( UCAL_MINUTE, status = U_ZERO_ERROR));
+ fprintf( stderr, ":%02ld", (long)r.get( UCAL_SECOND, status = U_ZERO_ERROR));
+ fprintf( stderr, " zone: %ld", (long)(nMillis = r.get( UCAL_ZONE_OFFSET, status = U_ZERO_ERROR)));
+ fprintf( stderr, " (%f min)", (double)nMillis / 60000);
+ debug_cal_millis_to_time( nMillis, h, m, s, f);
+ fprintf( stderr, " (%ld:%02ld:%02ld.%ld)", h, m, s, f);
+ fprintf( stderr, " DST: %ld", (long)(nMillis = r.get( UCAL_DST_OFFSET, status = U_ZERO_ERROR)));
+ fprintf( stderr, " (%f min)", (double)nMillis / 60000);
+ debug_cal_millis_to_time( nMillis, h, m, s, f);
+ fprintf( stderr, " (%ld:%02ld:%02ld.%ld)", h, m, s, f);
+ fprintf( stderr, "\n");
+ fflush(stderr);
+}
+// must use double parens, i.e.: DUMP_I18N_CAL_MSG(("four is: %d",4));
+#define DUMP_I18N_CAL_MSG(x) {debug_cal_loc(__FILE__,__LINE__);debug_cal_msg x;debug_i18n_cal_dump(*body);}
+#else // erDUMP_I18N_CALENDAR
+#define DUMP_I18N_CAL_MSG(x)
+#endif // erDUMP_I18N_CALENDAR
+
+#else // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
+#define DUMP_ICU_CAL_MSG(x)
+#define DUMP_I18N_CAL_MSG(x)
+#endif // erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
+
+
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::i18n;
+using namespace ::com::sun::star::lang;
+
+
+namespace i18npool {
+
+// Cast positive int32 values to signed int16, needed for
+// fieldValue[CalendarFieldIndex::YEAR] where absolute value of an era year may
+// be 32768 that with static_cast<sal_Int16> becomes -32768, which we also
+// do here but indicating places in a controlled way to map back.
+static inline sal_Int16 cast32To16( sal_Int32 a )
+{
+ // More than uint16 can't be handled, should not occur.
+ assert(a >= 0 && a < static_cast<sal_Int32>(SAL_MAX_INT16) - static_cast<sal_Int32>(SAL_MIN_INT16));
+ return static_cast<sal_Int16>(a);
+}
+// And back.
+static inline sal_Int32 cast16To32( sal_Int16 i )
+{
+ return static_cast<sal_Int32>(static_cast<sal_uInt16>(i));
+}
+
+Calendar_gregorian::Calendar_gregorian()
+ : mxNatNum(new NativeNumberSupplierService)
+{
+ init(nullptr);
+}
+Calendar_gregorian::Calendar_gregorian(const Era *_earArray)
+ : mxNatNum(new NativeNumberSupplierService)
+{
+ init(_earArray);
+}
+void
+Calendar_gregorian::init(const Era *_eraArray)
+{
+ cCalendar = "com.sun.star.i18n.Calendar_gregorian";
+
+ fieldSet = 0;
+
+ // #i102356# With icu::Calendar::createInstance(UErrorCode) in a Thai
+ // th_TH system locale we accidentally used a Buddhist calendar. Though
+ // the ICU documentation says that should be the case only for
+ // th_TH_TRADITIONAL (and ja_JP_TRADITIONAL Gengou), a plain th_TH
+ // already triggers that behavior, ja_JP does not. Strange enough,
+ // passing a th_TH locale to the calendar creation doesn't trigger
+ // this.
+ // See also http://userguide.icu-project.org/datetime/calendar
+
+ // Whatever ICU offers as the default calendar for a locale, ensure we
+ // have a Gregorian calendar as requested.
+
+ /* XXX: with the current implementation the aLocale member variable is
+ * not set prior to loading a calendar from locale data. This
+ * creates an empty (root) locale for ICU, but at least the correct
+ * calendar is used. The language part must not be NULL (respectively
+ * not all, language and country and variant), otherwise the current
+ * default locale would be used again and the calendar keyword ignored.
+ * */
+ icu::Locale aIcuLocale( "", nullptr, nullptr, "calendar=gregorian");
+
+ /* XXX: not specifying a timezone when creating a calendar assigns the
+ * system's timezone with all DST quirks, invalid times when switching
+ * to/from DST and so on. The XCalendar* interfaces are defined to support
+ * local time and UTC time so we can not override that here.
+ */
+ UErrorCode status = U_ZERO_ERROR;
+ body.reset( icu::Calendar::createInstance( aIcuLocale, status) );
+ if (!body || !U_SUCCESS(status)) throw RuntimeException();
+ eraArray=_eraArray;
+}
+
+Calendar_gregorian::~Calendar_gregorian()
+{
+}
+
+Calendar_hanja::Calendar_hanja()
+{
+ cCalendar = "com.sun.star.i18n.Calendar_hanja";
+}
+
+OUString SAL_CALL
+Calendar_hanja::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
+{
+ if ( displayIndex == CalendarDisplayIndex::AM_PM ) {
+ // Am/Pm string for Korean Hanja calendar will refer to Japanese locale
+ css::lang::Locale jaLocale("ja", OUString(), OUString());
+ if (idx == 0) return LocaleDataImpl::get()->getLocaleItem(jaLocale).timeAM;
+ else if (idx == 1) return LocaleDataImpl::get()->getLocaleItem(jaLocale).timePM;
+ else throw RuntimeException();
+ }
+ else
+ return Calendar_gregorian::getDisplayName( displayIndex, idx, nameType );
+}
+
+
+Calendar_hanja_yoil::Calendar_hanja_yoil()
+{
+ cCalendar = "com.sun.star.i18n.Calendar_hanja_yoil";
+}
+
+OUString SAL_CALL
+Calendar_hanja_yoil::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
+{
+ if ( displayIndex == CalendarDisplayIndex::AM_PM ) {
+ // Am/Pm string for Korean Hanja calendar will refer to Japanese locale
+ css::lang::Locale jaLocale("ja", OUString(), OUString());
+ if (idx == 0) return LocaleDataImpl::get()->getLocaleItem(jaLocale).timeAM;
+ else if (idx == 1) return LocaleDataImpl::get()->getLocaleItem(jaLocale).timePM;
+ else throw RuntimeException();
+ }
+ else
+ return Calendar_gregorian::getDisplayName( displayIndex, idx, nameType );
+}
+
+
+const Era gengou_eraArray[] = {
+ {1868, 1, 1, 0}, // Meiji
+ {1912, 7, 30, 0}, // Taisho
+ {1926, 12, 25, 0}, // Showa
+ {1989, 1, 8, 0}, // Heisei
+ {2019, 5, 1, 0}, // Reiwa
+ {0, 0, 0, 0}
+};
+Calendar_gengou::Calendar_gengou() : Calendar_gregorian(gengou_eraArray)
+{
+ cCalendar = "com.sun.star.i18n.Calendar_gengou";
+}
+
+const Era ROC_eraArray[] = {
+ {1912, 1, 1, kDisplayEraForcedLongYear}, // #i116701#
+ {0, 0, 0, 0}
+};
+Calendar_ROC::Calendar_ROC() : Calendar_gregorian(ROC_eraArray)
+{
+ cCalendar = "com.sun.star.i18n.Calendar_ROC";
+}
+
+/**
+* The start year of the Korean traditional calendar (Dan-gi) is the inaugural
+* year of Dan-gun (BC 2333).
+*/
+const Era dangi_eraArray[] = {
+ {-2332, 1, 1, 0},
+ {0, 0, 0, 0}
+};
+Calendar_dangi::Calendar_dangi() : Calendar_gregorian(dangi_eraArray)
+{
+ cCalendar = "com.sun.star.i18n.Calendar_dangi";
+}
+
+const Era buddhist_eraArray[] = {
+ {-542, 1, 1, 0},
+ {0, 0, 0, 0}
+};
+Calendar_buddhist::Calendar_buddhist() : Calendar_gregorian(buddhist_eraArray)
+{
+ cCalendar = "com.sun.star.i18n.Calendar_buddhist";
+}
+
+void SAL_CALL
+Calendar_gregorian::loadCalendar( const OUString& uniqueID, const css::lang::Locale& rLocale )
+{
+ // init. fieldValue[]
+ getValue();
+
+ aLocale = rLocale;
+ const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale);
+ for (const auto& rCal : xC)
+ {
+ if (uniqueID == rCal.Name)
+ {
+ aCalendar = rCal;
+ // setup minimalDaysInFirstWeek
+ setMinimumNumberOfDaysForFirstWeek(
+ aCalendar.MinimumNumberOfDaysForFirstWeek);
+ // setup first day of week
+ for (sal_Int16 day = sal::static_int_cast<sal_Int16>(
+ aCalendar.Days.getLength()-1); day>=0; day--)
+ {
+ if (aCalendar.StartOfWeek == aCalendar.Days[day].ID)
+ {
+ setFirstDayOfWeek( day);
+ return;
+ }
+ }
+ }
+ }
+ // Calendar is not for the locale
+ throw RuntimeException();
+}
+
+
+css::i18n::Calendar2 SAL_CALL
+Calendar_gregorian::getLoadedCalendar2()
+{
+ return aCalendar;
+}
+
+css::i18n::Calendar SAL_CALL
+Calendar_gregorian::getLoadedCalendar()
+{
+ return LocaleDataImpl::downcastCalendar( aCalendar);
+}
+
+OUString SAL_CALL
+Calendar_gregorian::getUniqueID()
+{
+ return aCalendar.Name;
+}
+
+void SAL_CALL
+Calendar_gregorian::setDateTime( double fTimeInDays )
+{
+ // ICU handles dates in milliseconds as double values and uses floor()
+ // to obtain integer values, which may yield a date decremented by one
+ // for odd (historical) timezone values where the computed value due to
+ // rounding errors has a fractional part in milliseconds. Ensure we
+ // pass a value without fraction here. If not, that may lead to
+ // fdo#44286 or fdo#52619 and the like, e.g. when passing
+ // -2136315212000.000244 instead of -2136315212000.000000
+ double fM = fTimeInDays * U_MILLIS_PER_DAY;
+ double fR = rtl::math::round( fM );
+ SAL_INFO_IF( fM != fR, "i18npool",
+ "Calendar_gregorian::setDateTime: " << std::fixed << fM << " rounded to " << fR);
+ UErrorCode status = U_ZERO_ERROR;
+ body->setTime( fR, status);
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ getValue();
+}
+
+double SAL_CALL
+Calendar_gregorian::getDateTime()
+{
+ if (fieldSet) {
+ setValue();
+ getValue();
+ }
+ UErrorCode status = U_ZERO_ERROR;
+ double fR = body->getTime(status);
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ return fR / U_MILLIS_PER_DAY;
+}
+
+void SAL_CALL
+Calendar_gregorian::setLocalDateTime( double fTimeInDays )
+{
+ // See setDateTime() for why the rounding.
+ double fM = fTimeInDays * U_MILLIS_PER_DAY;
+ double fR = rtl::math::round( fM );
+ SAL_INFO_IF( fM != fR, "i18npool",
+ "Calendar_gregorian::setLocalDateTime: " << std::fixed << fM << " rounded to " << fR);
+ int32_t nZoneOffset, nDSTOffset;
+ UErrorCode status = U_ZERO_ERROR;
+ body->getTimeZone().getOffset( fR, true, nZoneOffset, nDSTOffset, status );
+
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ status = U_ZERO_ERROR;
+ body->setTime( fR - (nZoneOffset + nDSTOffset), status );
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ getValue();
+}
+
+double SAL_CALL
+Calendar_gregorian::getLocalDateTime()
+{
+ if (fieldSet) {
+ setValue();
+ getValue();
+ }
+ UErrorCode status = U_ZERO_ERROR;
+ double fTime = body->getTime( status );
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ status = U_ZERO_ERROR;
+ int32_t nZoneOffset = body->get( UCAL_ZONE_OFFSET, status );
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ status = U_ZERO_ERROR;
+ int32_t nDSTOffset = body->get( UCAL_DST_OFFSET, status );
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ return (fTime + (nZoneOffset + nDSTOffset)) / U_MILLIS_PER_DAY;
+}
+
+bool Calendar_gregorian::setTimeZone( const OUString& rTimeZone )
+{
+ if (fieldSet)
+ {
+ setValue();
+ getValue();
+ }
+ const icu::UnicodeString aID( reinterpret_cast<const UChar*>(rTimeZone.getStr()), rTimeZone.getLength());
+ const std::unique_ptr<const icu::TimeZone> pTZ( icu::TimeZone::createTimeZone(aID));
+ if (!pTZ)
+ return false;
+
+ body->setTimeZone(*pTZ);
+ return true;
+}
+
+// map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
+// By using eraArray, it can take care Japanese and Taiwan ROC calendar.
+void Calendar_gregorian::mapFromGregorian()
+{
+ if (!eraArray)
+ return;
+
+ sal_Int16 e, m, d;
+ sal_Int32 y;
+
+ e = fieldValue[CalendarFieldIndex::ERA];
+ y = cast16To32(fieldValue[CalendarFieldIndex::YEAR]);
+ m = fieldValue[CalendarFieldIndex::MONTH] + 1;
+ d = fieldValue[CalendarFieldIndex::DAY_OF_MONTH];
+
+ // since the year is reversed for first era, it is reversed again here for Era compare.
+ if (e == 0)
+ y = 1 - y;
+
+ for (e = 0; eraArray[e].year; e++)
+ if ((y != eraArray[e].year) ? y < eraArray[e].year :
+ (m != eraArray[e].month) ? m < eraArray[e].month : d < eraArray[e].day)
+ break;
+
+ fieldValue[CalendarFieldIndex::ERA] = e;
+ fieldValue[CalendarFieldIndex::YEAR] =
+ cast32To16( (e == 0) ? (eraArray[0].year - y) : (y - eraArray[e-1].year + 1) );
+}
+
+#define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR))
+// map field value from other calendar to gregorian calendar, it can be overwritten by derived class.
+// By using eraArray, it can take care Japanese and Taiwan ROC calendar.
+void Calendar_gregorian::mapToGregorian()
+{
+ if (!eraArray || !(fieldSet & FIELDS))
+ return;
+
+ sal_Int16 e = fieldValue[CalendarFieldIndex::ERA];
+ sal_Int32 y;
+ if (e == 0)
+ y = eraArray[0].year - cast16To32(fieldValue[CalendarFieldIndex::YEAR]);
+ else
+ y = eraArray[e-1].year + cast16To32(fieldValue[CalendarFieldIndex::YEAR] - 1);
+
+ fieldSetValue[CalendarFieldIndex::ERA] = y <= 0 ? 0 : 1;
+ fieldSetValue[CalendarFieldIndex::YEAR] = cast32To16(y <= 0 ? 1 - y : y);
+ fieldSet |= FIELDS;
+}
+
+/// @throws RuntimeException
+static UCalendarDateFields fieldNameConverter(sal_Int16 fieldIndex)
+{
+ UCalendarDateFields f;
+
+ switch (fieldIndex) {
+ case CalendarFieldIndex::AM_PM: f = UCAL_AM_PM; break;
+ case CalendarFieldIndex::DAY_OF_MONTH: f = UCAL_DATE; break;
+ case CalendarFieldIndex::DAY_OF_WEEK: f = UCAL_DAY_OF_WEEK; break;
+ case CalendarFieldIndex::DAY_OF_YEAR: f = UCAL_DAY_OF_YEAR; break;
+ case CalendarFieldIndex::DST_OFFSET: f = UCAL_DST_OFFSET; break;
+ case CalendarFieldIndex::ZONE_OFFSET: f = UCAL_ZONE_OFFSET; break;
+ case CalendarFieldIndex::HOUR: f = UCAL_HOUR_OF_DAY; break;
+ case CalendarFieldIndex::MINUTE: f = UCAL_MINUTE; break;
+ case CalendarFieldIndex::SECOND: f = UCAL_SECOND; break;
+ case CalendarFieldIndex::MILLISECOND: f = UCAL_MILLISECOND; break;
+ case CalendarFieldIndex::WEEK_OF_MONTH: f = UCAL_WEEK_OF_MONTH; break;
+ case CalendarFieldIndex::WEEK_OF_YEAR: f = UCAL_WEEK_OF_YEAR; break;
+ case CalendarFieldIndex::YEAR: f = UCAL_YEAR; break;
+ case CalendarFieldIndex::MONTH: f = UCAL_MONTH; break;
+ case CalendarFieldIndex::ERA: f = UCAL_ERA; break;
+ default: throw RuntimeException();
+ }
+ return f;
+}
+
+void SAL_CALL
+Calendar_gregorian::setValue( sal_Int16 fieldIndex, sal_Int16 value )
+{
+ if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
+ throw RuntimeException();
+ fieldSet |= (1 << fieldIndex);
+ fieldValue[fieldIndex] = value;
+}
+
+bool Calendar_gregorian::getCombinedOffset( sal_Int32 & o_nOffset,
+ sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex ) const
+{
+ o_nOffset = 0;
+ bool bFieldsSet = false;
+ if (fieldSet & (1 << nParentFieldIndex))
+ {
+ bFieldsSet = true;
+ o_nOffset = static_cast<sal_Int32>( fieldValue[nParentFieldIndex]) * 60000;
+ }
+ if (fieldSet & (1 << nChildFieldIndex))
+ {
+ bFieldsSet = true;
+ if (o_nOffset < 0)
+ o_nOffset -= static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
+ else
+ o_nOffset += static_cast<sal_uInt16>( fieldValue[nChildFieldIndex]);
+ }
+ return bFieldsSet;
+}
+
+bool Calendar_gregorian::getZoneOffset( sal_Int32 & o_nOffset ) const
+{
+ return getCombinedOffset( o_nOffset, CalendarFieldIndex::ZONE_OFFSET,
+ CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
+}
+
+bool Calendar_gregorian::getDSTOffset( sal_Int32 & o_nOffset ) const
+{
+ return getCombinedOffset( o_nOffset, CalendarFieldIndex::DST_OFFSET,
+ CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
+}
+
+void Calendar_gregorian::submitFields()
+{
+ for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
+ {
+ if (fieldSet & (1 << fieldIndex))
+ {
+ switch (fieldIndex)
+ {
+ default:
+ body->set(fieldNameConverter(fieldIndex), fieldSetValue[fieldIndex]);
+ break;
+ case CalendarFieldIndex::ZONE_OFFSET:
+ case CalendarFieldIndex::DST_OFFSET:
+ case CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS:
+ case CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS:
+ break; // nothing, extra handling
+ }
+ }
+ }
+ sal_Int32 nZoneOffset, nDSTOffset;
+ if (getZoneOffset( nZoneOffset))
+ body->set( fieldNameConverter( CalendarFieldIndex::ZONE_OFFSET), nZoneOffset);
+ if (getDSTOffset( nDSTOffset))
+ body->set( fieldNameConverter( CalendarFieldIndex::DST_OFFSET), nDSTOffset);
+}
+
+void Calendar_gregorian::setValue()
+{
+ // Copy fields before calling submitFields() directly or indirectly below.
+ memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
+ // Possibly setup ERA and YEAR in fieldSetValue.
+ mapToGregorian();
+
+ DUMP_ICU_CAL_MSG(("%s\n","setValue() before submission"));
+ DUMP_I18N_CAL_MSG(("%s\n","setValue() before submission"));
+
+ submitFields();
+
+ DUMP_ICU_CAL_MSG(("%s\n","setValue() after submission"));
+ DUMP_I18N_CAL_MSG(("%s\n","setValue() after submission"));
+
+#if erDUMP_ICU_CALENDAR || erDUMP_I18N_CALENDAR
+ {
+ // force icu::Calendar to recalculate
+ UErrorCode status;
+ sal_Int32 nTmp = body->get( UCAL_DATE, status = U_ZERO_ERROR);
+ DUMP_ICU_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
+ DUMP_I18N_CAL_MSG(("%s: %d\n","setValue() result day",nTmp));
+ }
+#endif
+}
+
+void Calendar_gregorian::getValue()
+{
+ DUMP_ICU_CAL_MSG(("%s\n","getValue()"));
+ DUMP_I18N_CAL_MSG(("%s\n","getValue()"));
+ for (sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++)
+ {
+ if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS ||
+ fieldIndex == CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS)
+ continue; // not ICU fields
+
+ UErrorCode status = U_ZERO_ERROR;
+ sal_Int32 value = body->get( fieldNameConverter(
+ fieldIndex), status);
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+
+ // Convert millisecond to minute for ZONE and DST and set remainder in
+ // second field.
+ if (fieldIndex == CalendarFieldIndex::ZONE_OFFSET)
+ {
+ sal_Int32 nMinutes = value / 60000;
+ sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
+ abs( value - nMinutes * 60000)));
+ fieldValue[CalendarFieldIndex::ZONE_OFFSET] = static_cast<sal_Int16>( nMinutes);
+ fieldValue[CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS] = nMillis;
+ }
+ else if (fieldIndex == CalendarFieldIndex::DST_OFFSET)
+ {
+ sal_Int32 nMinutes = value / 60000;
+ sal_Int16 nMillis = static_cast<sal_Int16>( static_cast<sal_uInt16>(
+ abs( value - nMinutes * 60000)));
+ fieldValue[CalendarFieldIndex::DST_OFFSET] = static_cast<sal_Int16>( nMinutes);
+ fieldValue[CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS] = nMillis;
+ }
+ else
+ fieldValue[fieldIndex] = static_cast<sal_Int16>(value);
+
+ // offset 1 since the value for week start day SunDay is different between Calendar and Weekdays.
+ if ( fieldIndex == CalendarFieldIndex::DAY_OF_WEEK )
+ fieldValue[fieldIndex]--; // UCAL_SUNDAY:/* == 1 */ ==> Weekdays::SUNDAY /* ==0 */
+ }
+ mapFromGregorian();
+ fieldSet = 0;
+}
+
+sal_Int16 SAL_CALL
+Calendar_gregorian::getValue( sal_Int16 fieldIndex )
+{
+ if (fieldIndex < 0 || FIELD_INDEX_COUNT <= fieldIndex)
+ throw RuntimeException();
+
+ if (fieldSet) {
+ setValue();
+ getValue();
+ }
+
+ return fieldValue[fieldIndex];
+}
+
+void SAL_CALL
+Calendar_gregorian::addValue( sal_Int16 fieldIndex, sal_Int32 value )
+{
+ // since ZONE and DST could not be add, we don't need to convert value here
+ UErrorCode status = U_ZERO_ERROR;
+ body->add(fieldNameConverter(fieldIndex), value, status);
+ if ( !U_SUCCESS(status) ) throw RuntimeException();
+ getValue();
+}
+
+sal_Bool SAL_CALL
+Calendar_gregorian::isValid()
+{
+ if (fieldSet) {
+ sal_Int32 tmp = fieldSet;
+ setValue();
+ memcpy(fieldSetValue, fieldValue, sizeof(fieldSetValue));
+ getValue();
+ for ( sal_Int16 fieldIndex = 0; fieldIndex < FIELD_INDEX_COUNT; fieldIndex++ ) {
+ // compare only with fields that are set and reset fieldSet[]
+ if (tmp & (1 << fieldIndex)) {
+ if (fieldSetValue[fieldIndex] != fieldValue[fieldIndex])
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+// NativeNumberMode has different meaning between Number and Calendar for Asian locales.
+// Here is the mapping table
+// calendar(q/y/m/d) zh_CN zh_TW ja ko
+// NatNum1 NatNum1/1/7/7 NatNum1/1/7/7 NatNum1/1/4/4 NatNum1/1/7/7
+// NatNum2 NatNum2/2/8/8 NatNum2/2/8/8 NatNum2/2/5/5 NatNum2/2/8/8
+// NatNum3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3 NatNum3/3/3/3
+// NatNum4 NatNum9/9/11/11
+
+static sal_Int16 NatNumForCalendar(const css::lang::Locale& aLocale,
+ sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode, sal_Int32 value )
+{
+ bool isShort = ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
+ nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR) && value >= 100) ||
+ nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
+ nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER;
+ bool isChinese = aLocale.Language == "zh";
+ bool isJapanese = aLocale.Language == "ja";
+ bool isKorean = aLocale.Language == "ko";
+
+ if (isChinese || isJapanese || isKorean) {
+ switch (nNativeNumberMode) {
+ case NativeNumberMode::NATNUM1:
+ if (!isShort)
+ nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM4 : NativeNumberMode::NATNUM7;
+ break;
+ case NativeNumberMode::NATNUM2:
+ if (!isShort)
+ nNativeNumberMode = isJapanese ? NativeNumberMode::NATNUM5 : NativeNumberMode::NATNUM8;
+ break;
+ case NativeNumberMode::NATNUM3:
+ break;
+ case NativeNumberMode::NATNUM4:
+ if (isKorean)
+ return isShort ? NativeNumberMode::NATNUM9 : NativeNumberMode::NATNUM11;
+ [[fallthrough]];
+ default: return 0;
+ }
+ }
+ return nNativeNumberMode;
+}
+
+static sal_Int32 DisplayCode2FieldIndex(sal_Int32 nCalendarDisplayCode)
+{
+ switch( nCalendarDisplayCode ) {
+ case CalendarDisplayCode::SHORT_DAY:
+ case CalendarDisplayCode::LONG_DAY:
+ return CalendarFieldIndex::DAY_OF_MONTH;
+ case CalendarDisplayCode::SHORT_DAY_NAME:
+ case CalendarDisplayCode::LONG_DAY_NAME:
+ case CalendarDisplayCode::NARROW_DAY_NAME:
+ return CalendarFieldIndex::DAY_OF_WEEK;
+ case CalendarDisplayCode::SHORT_QUARTER:
+ case CalendarDisplayCode::LONG_QUARTER:
+ case CalendarDisplayCode::SHORT_MONTH:
+ case CalendarDisplayCode::LONG_MONTH:
+ case CalendarDisplayCode::SHORT_MONTH_NAME:
+ case CalendarDisplayCode::LONG_MONTH_NAME:
+ case CalendarDisplayCode::NARROW_MONTH_NAME:
+ case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME:
+ case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME:
+ case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME:
+ case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME:
+ case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME:
+ case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME:
+ return CalendarFieldIndex::MONTH;
+ case CalendarDisplayCode::SHORT_YEAR:
+ case CalendarDisplayCode::LONG_YEAR:
+ return CalendarFieldIndex::YEAR;
+ case CalendarDisplayCode::SHORT_ERA:
+ case CalendarDisplayCode::LONG_ERA:
+ return CalendarFieldIndex::ERA;
+ case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
+ case CalendarDisplayCode::LONG_YEAR_AND_ERA:
+ return CalendarFieldIndex::YEAR;
+ default:
+ return 0;
+ }
+}
+
+sal_Int16 SAL_CALL
+Calendar_gregorian::getFirstDayOfWeek()
+{
+ // UCAL_SUNDAY == 1, Weekdays::SUNDAY == 0 => offset -1
+ // Check for underflow just in case we're called "out of sync".
+ return ::std::max( sal::static_int_cast<sal_Int16>(0),
+ sal::static_int_cast<sal_Int16>( static_cast<sal_Int16>(
+ body->getFirstDayOfWeek()) - 1));
+}
+
+void SAL_CALL
+Calendar_gregorian::setFirstDayOfWeek( sal_Int16 day )
+{
+ // Weekdays::SUNDAY == 0, UCAL_SUNDAY == 1 => offset +1
+ body->setFirstDayOfWeek( static_cast<UCalendarDaysOfWeek>( day + 1));
+}
+
+void SAL_CALL
+Calendar_gregorian::setMinimumNumberOfDaysForFirstWeek( sal_Int16 days )
+{
+ aCalendar.MinimumNumberOfDaysForFirstWeek = days;
+ body->setMinimalDaysInFirstWeek( static_cast<uint8_t>( days));
+}
+
+sal_Int16 SAL_CALL
+Calendar_gregorian::getMinimumNumberOfDaysForFirstWeek()
+{
+ return aCalendar.MinimumNumberOfDaysForFirstWeek;
+}
+
+sal_Int16 SAL_CALL
+Calendar_gregorian::getNumberOfMonthsInYear()
+{
+ return static_cast<sal_Int16>(aCalendar.Months.getLength());
+}
+
+
+sal_Int16 SAL_CALL
+Calendar_gregorian::getNumberOfDaysInWeek()
+{
+ return static_cast<sal_Int16>(aCalendar.Days.getLength());
+}
+
+
+Sequence< CalendarItem > SAL_CALL
+Calendar_gregorian::getDays()
+{
+ return LocaleDataImpl::downcastCalendarItems( aCalendar.Days);
+}
+
+
+Sequence< CalendarItem > SAL_CALL
+Calendar_gregorian::getMonths()
+{
+ return LocaleDataImpl::downcastCalendarItems( aCalendar.Months);
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+Calendar_gregorian::getDays2()
+{
+ return aCalendar.Days;
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+Calendar_gregorian::getMonths2()
+{
+ return aCalendar.Months;
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+Calendar_gregorian::getGenitiveMonths2()
+{
+ return aCalendar.GenitiveMonths;
+}
+
+
+Sequence< CalendarItem2 > SAL_CALL
+Calendar_gregorian::getPartitiveMonths2()
+{
+ return aCalendar.PartitiveMonths;
+}
+
+
+OUString SAL_CALL
+Calendar_gregorian::getDisplayName( sal_Int16 displayIndex, sal_Int16 idx, sal_Int16 nameType )
+{
+ OUString aStr;
+
+ switch( displayIndex ) {
+ case CalendarDisplayIndex::AM_PM:/* ==0 */
+ if (idx == 0) aStr = LocaleDataImpl::get()->getLocaleItem(aLocale).timeAM;
+ else if (idx == 1) aStr = LocaleDataImpl::get()->getLocaleItem(aLocale).timePM;
+ else throw RuntimeException();
+ break;
+ case CalendarDisplayIndex::DAY:
+ if( idx >= aCalendar.Days.getLength() ) throw RuntimeException();
+ if (nameType == 0) aStr = aCalendar.Days[idx].AbbrevName;
+ else if (nameType == 1) aStr = aCalendar.Days[idx].FullName;
+ else if (nameType == 2) aStr = aCalendar.Days[idx].NarrowName;
+ else throw RuntimeException();
+ break;
+ case CalendarDisplayIndex::MONTH:
+ if( idx >= aCalendar.Months.getLength() ) throw RuntimeException();
+ if (nameType == 0) aStr = aCalendar.Months[idx].AbbrevName;
+ else if (nameType == 1) aStr = aCalendar.Months[idx].FullName;
+ else if (nameType == 2) aStr = aCalendar.Months[idx].NarrowName;
+ else throw RuntimeException();
+ break;
+ case CalendarDisplayIndex::GENITIVE_MONTH:
+ if( idx >= aCalendar.GenitiveMonths.getLength() ) throw RuntimeException();
+ if (nameType == 0) aStr = aCalendar.GenitiveMonths[idx].AbbrevName;
+ else if (nameType == 1) aStr = aCalendar.GenitiveMonths[idx].FullName;
+ else if (nameType == 2) aStr = aCalendar.GenitiveMonths[idx].NarrowName;
+ else throw RuntimeException();
+ break;
+ case CalendarDisplayIndex::PARTITIVE_MONTH:
+ if( idx >= aCalendar.PartitiveMonths.getLength() ) throw RuntimeException();
+ if (nameType == 0) aStr = aCalendar.PartitiveMonths[idx].AbbrevName;
+ else if (nameType == 1) aStr = aCalendar.PartitiveMonths[idx].FullName;
+ else if (nameType == 2) aStr = aCalendar.PartitiveMonths[idx].NarrowName;
+ else throw RuntimeException();
+ break;
+ case CalendarDisplayIndex::ERA:
+ if( idx >= aCalendar.Eras.getLength() ) throw RuntimeException();
+ if (nameType == 0) aStr = aCalendar.Eras[idx].AbbrevName;
+ else if (nameType == 1) aStr = aCalendar.Eras[idx].FullName;
+ else throw RuntimeException();
+ break;
+ case CalendarDisplayIndex::YEAR:
+ break;
+ default:
+ throw RuntimeException();
+ }
+ return aStr;
+}
+
+// Methods in XExtendedCalendar
+OUString SAL_CALL
+Calendar_gregorian::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
+{
+ return getDisplayStringImpl( nCalendarDisplayCode, nNativeNumberMode, false);
+}
+
+OUString
+Calendar_gregorian::getDisplayStringImpl( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode, bool bEraMode )
+{
+ sal_Int32 value = cast16To32( getValue( sal::static_int_cast<sal_Int16>(
+ DisplayCode2FieldIndex(nCalendarDisplayCode))));
+ OUString aOUStr;
+
+ if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER ||
+ nCalendarDisplayCode == CalendarDisplayCode::LONG_QUARTER) {
+ Sequence< OUString> xR = LocaleDataImpl::get()->getReservedWord(aLocale);
+ sal_Int16 quarter = value / 3;
+ // Since this base class method may be called by derived calendar
+ // classes where a year consists of more than 12 months we need a check
+ // to not run out of bounds of reserved quarter words. Perhaps a more
+ // clean way (instead of dividing by 3) would be to first get the
+ // number of months, divide by 4 and then use that result to divide the
+ // actual month value.
+ if ( quarter > 3 )
+ quarter = 3;
+ quarter = sal::static_int_cast<sal_Int16>( quarter +
+ ((nCalendarDisplayCode == CalendarDisplayCode::SHORT_QUARTER) ?
+ reservedWords::QUARTER1_ABBREVIATION : reservedWords::QUARTER1_WORD) );
+ aOUStr = xR[quarter];
+ } else {
+ // The "#100211# - checked" comments serve for detection of "use of
+ // sprintf is safe here" conditions. An sprintf encountered without
+ // having that comment triggers alarm ;-)
+ switch( nCalendarDisplayCode ) {
+ case CalendarDisplayCode::SHORT_MONTH:
+ value += 1; // month is zero based
+ [[fallthrough]];
+ case CalendarDisplayCode::SHORT_DAY:
+ aOUStr = OUString::number(value);
+ break;
+ case CalendarDisplayCode::LONG_YEAR:
+ if ( aCalendar.Name == "gengou" )
+ {
+ char aStr[12]; // "-2147483648" and \0
+ o3tl::sprintf(aStr, "%02" SAL_PRIdINT32, value); // #100211# - checked
+ aOUStr = OUString::createFromAscii(aStr);
+ }
+ else
+ aOUStr = OUString::number(value);
+ break;
+ case CalendarDisplayCode::LONG_MONTH:
+ {
+ value += 1; // month is zero based
+ char aStr[12]; // "-2147483648" and \0
+ o3tl::sprintf(aStr, "%02" SAL_PRIdINT32, value); // #100211# - checked
+ aOUStr = OUString::createFromAscii(aStr);
+ break;
+ }
+ case CalendarDisplayCode::SHORT_YEAR:
+ // Take last 2 digits, or only one if value<10, for example,
+ // in case of the Gengou calendar. For combined era+year always
+ // the full year is displayed, without leading 0.
+ // Workaround for non-combined calls in certain calendars is
+ // the kDisplayEraForcedLongYear flag, but this also could get
+ // called for YY not only E format codes, no differentiation
+ // possible here; the good news is that usually the Gregorian
+ // calendar is the default and hence YY calls for Gregorian and
+ // E for the other calendar and currently (2013-02-28) ROC is
+ // the only calendar using this.
+ // See i#116701 and fdo#60915
+ if (value < 100 || bEraMode || (eraArray && (eraArray[0].flags & kDisplayEraForcedLongYear)))
+ aOUStr = OUString::number(value);
+ else
+ {
+ char aStr[12]; // "-2147483648" and \0
+ o3tl::sprintf(aStr, "%02" SAL_PRIdINT32, value % 100); // #100211# - checked
+ aOUStr = OUString::createFromAscii(aStr);
+ }
+ break;
+ case CalendarDisplayCode::LONG_DAY:
+ {
+ char aStr[12]; // "-2147483648" and \0
+ o3tl::sprintf(aStr, "%02" SAL_PRIdINT32, value); // #100211# - checked
+ aOUStr = OUString::createFromAscii(aStr);
+ break;
+ }
+
+ case CalendarDisplayCode::SHORT_DAY_NAME:
+ return getDisplayName(CalendarDisplayIndex::DAY, value, 0);
+ case CalendarDisplayCode::LONG_DAY_NAME:
+ return getDisplayName(CalendarDisplayIndex::DAY, value, 1);
+ case CalendarDisplayCode::NARROW_DAY_NAME:
+ return getDisplayName(CalendarDisplayIndex::DAY, value, 2);
+ case CalendarDisplayCode::SHORT_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::MONTH, value, 0);
+ case CalendarDisplayCode::LONG_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::MONTH, value, 1);
+ case CalendarDisplayCode::NARROW_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::MONTH, value, 2);
+ case CalendarDisplayCode::SHORT_GENITIVE_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 0);
+ case CalendarDisplayCode::LONG_GENITIVE_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 1);
+ case CalendarDisplayCode::NARROW_GENITIVE_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::GENITIVE_MONTH, value, 2);
+ case CalendarDisplayCode::SHORT_PARTITIVE_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 0);
+ case CalendarDisplayCode::LONG_PARTITIVE_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 1);
+ case CalendarDisplayCode::NARROW_PARTITIVE_MONTH_NAME:
+ return getDisplayName(CalendarDisplayIndex::PARTITIVE_MONTH, value, 2);
+ case CalendarDisplayCode::SHORT_ERA:
+ return getDisplayName(CalendarDisplayIndex::ERA, value, 0);
+ case CalendarDisplayCode::LONG_ERA:
+ return getDisplayName(CalendarDisplayIndex::ERA, value, 1);
+
+ case CalendarDisplayCode::SHORT_YEAR_AND_ERA:
+ return getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode, true ) +
+ getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode, true );
+
+ case CalendarDisplayCode::LONG_YEAR_AND_ERA:
+ return getDisplayStringImpl( CalendarDisplayCode::LONG_ERA, nNativeNumberMode, true ) +
+ getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode, true );
+
+ default:
+ throw RuntimeException();
+ }
+ }
+ // NatNum12 used only for selected parts
+ if (nNativeNumberMode > 0 && nNativeNumberMode != 12) {
+ // For Japanese calendar, first year calls GAN, see bug 111668 for detail.
+ if (eraArray == gengou_eraArray && value == 1
+ && (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||
+ nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR)
+ && (nNativeNumberMode == NativeNumberMode::NATNUM1 ||
+ nNativeNumberMode == NativeNumberMode::NATNUM2)) {
+ static sal_Unicode gan = 0x5143;
+ return OUString(&gan, 1);
+ }
+ sal_Int16 nNatNum = NatNumForCalendar(aLocale, nCalendarDisplayCode, nNativeNumberMode, value);
+ if (nNatNum > 0)
+ return mxNatNum->getNativeNumberString(aOUStr, aLocale, nNatNum);
+ }
+ return aOUStr;
+}
+
+// Methods in XExtendedCalendar
+OUString SAL_CALL
+Calendar_buddhist::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode )
+{
+ // make year and era in different order for year before and after 0.
+ if ((nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA ||
+ nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR_AND_ERA) &&
+ getValue(CalendarFieldIndex::ERA) == 0) {
+ if (nCalendarDisplayCode == CalendarDisplayCode::LONG_YEAR_AND_ERA)
+ return getDisplayStringImpl( CalendarDisplayCode::SHORT_YEAR, nNativeNumberMode, true ) +
+ getDisplayStringImpl( CalendarDisplayCode::SHORT_ERA, nNativeNumberMode, true );
+ else
+ return getDisplayStringImpl( CalendarDisplayCode::LONG_YEAR, nNativeNumberMode, true ) +
+ getDisplayStringImpl( CalendarDisplayCode::LONG_ERA, nNativeNumberMode, true );
+ }
+ return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode);
+}
+
+OUString SAL_CALL
+Calendar_gregorian::getImplementationName()
+{
+ return OUString::createFromAscii(cCalendar);
+}
+
+sal_Bool SAL_CALL
+Calendar_gregorian::supportsService(const OUString& rServiceName)
+{
+ return cppu::supportsService(this, rServiceName);
+}
+
+Sequence< OUString > SAL_CALL
+Calendar_gregorian::getSupportedServiceNames()
+{
+ Sequence< OUString > aRet { OUString::createFromAscii(cCalendar) };
+ return aRet;
+}
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/i18npool/source/calendar/calendar_hijri.cxx b/i18npool/source/calendar/calendar_hijri.cxx
new file mode 100644
index 0000000000..ed64c80db6
--- /dev/null
+++ b/i18npool/source/calendar/calendar_hijri.cxx
@@ -0,0 +1,317 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <sal/config.h>
+#include <i18nutil/calendar.hxx>
+#include <cmath>
+#include <stdlib.h>
+
+#include <calendar_hijri.hxx>
+#include <tools/long.hxx>
+#include <basegfx/numeric/ftools.hxx>
+
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::lang;
+using namespace ::com::sun::star::i18n;
+
+namespace i18npool {
+
+// Synodic Period (mean time between 2 successive new moon: 29d, 12 hr, 44min, 3sec
+constexpr double SynPeriod = 29.53058868;
+
+// Julian day on Jan 1, 1900
+constexpr double jd1900 = 2415020.75933;
+
+// Reference point: March 26, 2001 == 1422 Hijri == 1252 Synodial month from 1900
+constexpr sal_Int32 SynRef = 1252;
+constexpr sal_Int32 GregRef = 1422;
+
+Calendar_hijri::Calendar_hijri()
+{
+ cCalendar = "com.sun.star.i18n.Calendar_hijri";
+}
+
+#define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR) | (1 << CalendarFieldIndex::MONTH) | (1 << CalendarFieldIndex::DAY_OF_MONTH))
+
+// map field value from hijri calendar to gregorian calendar
+void Calendar_hijri::mapToGregorian()
+{
+ if (!(fieldSet & FIELDS))
+ return;
+
+ sal_Int32 day = static_cast<sal_Int32>(fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH]);
+ sal_Int32 month = static_cast<sal_Int32>(fieldSetValue[CalendarFieldIndex::MONTH]) + 1;
+ sal_Int32 year = static_cast<sal_Int32>(fieldSetValue[CalendarFieldIndex::YEAR]);
+ if (fieldSetValue[CalendarFieldIndex::ERA] == 0)
+ year *= -1;
+
+ ToGregorian(&day, &month, &year);
+
+ fieldSetValue[CalendarFieldIndex::ERA] = year <= 0 ? 0 : 1;
+ fieldSetValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>(month - 1);
+ fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(day);
+ fieldSetValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(abs(year));
+ fieldSet |= FIELDS;
+}
+
+// map field value from gregorian calendar to hijri calendar
+void Calendar_hijri::mapFromGregorian()
+{
+ sal_Int32 month, day, year;
+
+ day = static_cast<sal_Int32>(fieldValue[CalendarFieldIndex::DAY_OF_MONTH]);
+ month = static_cast<sal_Int32>(fieldValue[CalendarFieldIndex::MONTH]) + 1;
+ year = static_cast<sal_Int32>(fieldValue[CalendarFieldIndex::YEAR]);
+ if (fieldValue[CalendarFieldIndex::ERA] == 0)
+ year *= -1;
+
+ // Get Hijri date
+ getHijri(&day, &month, &year);
+
+ fieldValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(day);
+ fieldValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>(month - 1);
+ fieldValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(abs(year));
+ fieldValue[CalendarFieldIndex::ERA] = static_cast<sal_Int16>(year) < 1 ? 0 : 1;
+}
+
+
+// This function returns the Julian date/time of the Nth new moon since
+// January 1900. The synodic month is passed as parameter.
+
+// Adapted from "Astronomical Formulae for Calculators" by
+// Jean Meeus, Third Edition, Willmann-Bell, 1985.
+
+double
+Calendar_hijri::NewMoon(sal_Int32 n)
+{
+ double jd, t, t2, t3, k, ma, sa, tf, xtra;
+ k = n;
+ t = k/1236.85; // Time in Julian centuries from 1900 January 0.5
+ t2 = t * t;
+ t3 = t2 * t;
+
+ // Mean time of phase
+ jd = jd1900
+ + SynPeriod * k
+ - 0.0001178 * t2
+ - 0.000000155 * t3
+ + 0.00033 * sin(basegfx::deg2rad(166.56 + 132.87 * t - 0.009173 * t2));
+
+ // Sun's mean anomaly in radian
+ sa = basegfx::deg2rad(359.2242
+ + 29.10535608 * k
+ - 0.0000333 * t2
+ - 0.00000347 * t3);
+
+ // Moon's mean anomaly
+ ma = basegfx::deg2rad(306.0253
+ + 385.81691806 * k
+ + 0.0107306 * t2
+ + 0.00001236 * t3);
+
+ // Moon's argument of latitude
+ tf = 2.0 * basegfx::deg2rad(21.2964
+ + 390.67050646 * k
+ - 0.0016528 * t2
+ - 0.00000239 * t3);
+
+ // should reduce to interval between 0 to 1.0 before calculating further
+ // Corrections for New Moon
+ xtra = (0.1734 - 0.000393 * t) * sin(sa)
+ + 0.0021 * sin(sa * 2)
+ - 0.4068 * sin(ma)
+ + 0.0161 * sin(2 * ma)
+ - 0.0004 * sin(3 * ma)
+ + 0.0104 * sin(tf)
+ - 0.0051 * sin(sa + ma)
+ - 0.0074 * sin(sa - ma)
+ + 0.0004 * sin(tf + sa)
+ - 0.0004 * sin(tf - sa)
+ - 0.0006 * sin(tf + ma)
+ + 0.0010 * sin(tf - ma)
+ + 0.0005 * sin(sa + 2 * ma);
+
+ // convert from Ephemeris Time (ET) to (approximate) Universal Time (UT)
+ jd += xtra - (0.41 + 1.2053 * t + 0.4992 * t2)/1440;
+
+ return jd;
+}
+
+// Get Hijri Date
+void
+Calendar_hijri::getHijri(sal_Int32 *day, sal_Int32 *month, sal_Int32 *year)
+{
+ double prevday;
+ sal_Int32 syndiff;
+ sal_Int32 newsyn;
+ double newjd;
+ sal_Int32 synmonth;
+
+ // Get Julian Day from Gregorian
+ sal_Int32 const julday = getJulianDay(*day, *month, *year);
+
+ // obtain approx. of how many Synodic months since the beginning of the year 1900
+ synmonth = static_cast<sal_Int32>(0.5 + (julday - jd1900)/SynPeriod);
+
+ newsyn = synmonth;
+ prevday = julday - 0.5;
+
+ do {
+ newjd = NewMoon(newsyn);
+
+ // Decrement syntonic months
+ newsyn--;
+ } while (newjd > prevday);
+ newsyn++;
+
+ // difference from reference point
+ syndiff = newsyn - SynRef;
+
+ // Round up the day
+ *day = static_cast<sal_Int32>(julday - newjd + 0.5);
+ *month = (syndiff % 12) + 1;
+
+ // currently not supported
+ //dayOfYear = (sal_Int32)(month * SynPeriod + day);
+ *year = GregRef + static_cast<sal_Int32>(syndiff / 12);
+
+ // If month negative, consider it previous year
+ if (syndiff != 0 && *month <= 0) {
+ *month += 12;
+ (*year)--;
+ }
+
+ // If Before Hijri subtract 1
+ if (*year <= 0) (*year)--;
+}
+
+void
+Calendar_hijri::ToGregorian(sal_Int32 *day, sal_Int32 *month, sal_Int32 *year)
+{
+ sal_Int32 nmonth;
+ double jday;
+
+ if ( *year < 0 ) (*year)++;
+
+ // Number of month from reference point
+ nmonth = *month + *year * 12 - (GregRef * 12 + 1);
+
+ // Add Synodic Reference point
+ nmonth += SynRef;
+
+ // Get Julian days add time too
+ jday = NewMoon(nmonth) + *day;
+
+ // Round-up
+ jday = std::trunc(jday + 0.5);
+
+ // Use algorithm from "Numerical Recipes in C"
+ getGregorianDay(static_cast<sal_Int32>(jday), day, month, year);
+
+ // Julian -> Gregorian only works for non-negative year
+ if ( *year <= 0 ) {
+ *day = -1;
+ *month = -1;
+ *year = -1;
+ }
+}
+
+/* this algorithm is taken from "Numerical Recipes in C", 2nd ed, pp 14-15. */
+/* this algorithm only valid for non-negative gregorian year */
+void
+Calendar_hijri::getGregorianDay(sal_Int32 lJulianDay, sal_Int32 *pnDay, sal_Int32 *pnMonth, sal_Int32 *pnYear)
+{
+ /* working variables */
+ tools::Long lFactorA, lFactorB, lFactorC, lFactorD, lFactorE;
+
+ constexpr sal_Int32 GREGORIAN_CROSSOVER = 2299161;
+
+ /* test whether to adjust for the Gregorian calendar crossover */
+ if (lJulianDay >= GREGORIAN_CROSSOVER) {
+ /* calculate a small adjustment */
+ tools::Long lAdjust = static_cast<tools::Long>((static_cast<float>(lJulianDay - 1867216) - 0.25) / 36524.25);
+
+ lFactorA = lJulianDay + 1 + lAdjust - static_cast<tools::Long>(0.25 * lAdjust);
+
+ } else {
+ /* no adjustment needed */
+ lFactorA = lJulianDay;
+ }
+
+ lFactorB = lFactorA + 1524;
+ lFactorC = static_cast<tools::Long>(6680.0 + (static_cast<float>(lFactorB - 2439870) - 122.1) / 365.25);
+ lFactorD = static_cast<tools::Long>(365 * lFactorC + (0.25 * lFactorC));
+ lFactorE = static_cast<tools::Long>((lFactorB - lFactorD) / i18nutil::monthDaysWithoutJanFeb);
+
+ /* now, pull out the day number */
+ *pnDay = lFactorB - lFactorD - static_cast<tools::Long>(i18nutil::monthDaysWithoutJanFeb * lFactorE);
+
+ /* ...and the month, adjusting it if necessary */
+ *pnMonth = lFactorE - 1;
+ if (*pnMonth > 12)
+ (*pnMonth) -= 12;
+
+ /* ...and similarly for the year */
+ *pnYear = lFactorC - 4715;
+ if (*pnMonth > 2)
+ (*pnYear)--;
+
+ // Negative year adjustments
+ if (*pnYear <= 0)
+ (*pnYear)--;
+}
+
+sal_Int32
+Calendar_hijri::getJulianDay(sal_Int32 day, sal_Int32 month, sal_Int32 year)
+{
+ double jy, jm;
+
+ if( year == 0 ) {
+ return -1;
+ }
+
+ if( year == 1582 && month == 10 && day > 4 && day < 15 ) {
+ return -1;
+ }
+
+ if( month > 2 ) {
+ jy = year;
+ jm = month + 1;
+ } else {
+ jy = year - 1;
+ jm = month + 13;
+ }
+
+ sal_Int32 intgr = static_cast<sal_Int32>(static_cast<sal_Int32>(365.25 * jy) + static_cast<sal_Int32>(i18nutil::monthDaysWithoutJanFeb * jm) + day + 1720995 );
+
+ //check for switch to Gregorian calendar
+ double const gregcal = 15 + 31 * ( 10 + 12 * 1582 );
+
+ if( day + 31 * (month + 12 * year) >= gregcal ) {
+ double ja;
+ ja = std::trunc(0.01 * jy);
+ intgr += static_cast<sal_Int32>(2 - ja + std::trunc(0.25 * ja));
+ }
+
+ return intgr;
+}
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/i18npool/source/calendar/calendar_jewish.cxx b/i18npool/source/calendar/calendar_jewish.cxx
new file mode 100644
index 0000000000..19b55eaf87
--- /dev/null
+++ b/i18npool/source/calendar/calendar_jewish.cxx
@@ -0,0 +1,316 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+
+#include <calendar_jewish.hxx>
+#include <nativenumbersupplier.hxx>
+
+#include <com/sun/star/i18n/CalendarDisplayCode.hpp>
+#include <com/sun/star/i18n/NativeNumberMode.hpp>
+
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::i18n;
+using namespace ::com::sun::star::lang;
+
+namespace i18npool {
+
+// not used
+//static UErrorCode status; // status is shared in all calls to Calendar, it has to be reset for each call.
+
+Calendar_jewish::Calendar_jewish()
+{
+ cCalendar = "com.sun.star.i18n.Calendar_jewish";
+}
+
+// The following C++ code is translated from the Lisp code in
+// ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
+// Software---Practice & Experience, vol. 20, no. 9 (September, 1990),
+// pp. 899--928.
+
+// This code is in the public domain, but any use of it
+// should acknowledge its source.
+// https://web.archive.org/web/20010222054702/http://www.ntu.edu.sg/home/ayxyan/date1.txt
+
+// Hebrew dates
+
+const int HebrewEpoch = -1373429; // Absolute date of start of Hebrew calendar
+
+// True if year is an Hebrew leap year
+static bool HebrewLeapYear(sal_Int32 year) {
+ return ((((7 * year) + 1) % 19) < 7);
+}
+
+// Last month of Hebrew year.
+static sal_Int32 LastMonthOfHebrewYear(sal_Int32 year) {
+ return (HebrewLeapYear(year)) ? 13 : 12;
+}
+
+// Number of days elapsed from the Sunday prior to the start of the
+// Hebrew calendar to the mean conjunction of Tishri of Hebrew year.
+static sal_Int32 HebrewCalendarElapsedDays(sal_Int32 year) {
+ sal_Int32 MonthsElapsed =
+ (235 * ((year - 1) / 19)) // Months in complete cycles so far.
+ + (12 * ((year - 1) % 19)) // Regular months in this cycle.
+ + (7 * ((year - 1) % 19) + 1) / 19; // Leap months this cycle
+ sal_Int32 PartsElapsed = 204 + 793 * (MonthsElapsed % 1080);
+ int HoursElapsed =
+ 5 + 12 * MonthsElapsed + 793 * (MonthsElapsed / 1080)
+ + PartsElapsed / 1080;
+ sal_Int32 ConjunctionDay = 1 + 29 * MonthsElapsed + HoursElapsed / 24;
+ sal_Int32 ConjunctionParts = 1080 * (HoursElapsed % 24) + PartsElapsed % 1080;
+ sal_Int32 AlternativeDay;
+
+ if ((ConjunctionParts >= 19440) // If new moon is at or after midday,
+ || (((ConjunctionDay % 7) == 2) // ...or is on a Tuesday...
+ && (ConjunctionParts >= 9924) // at 9 hours, 204 parts or later...
+ && !(HebrewLeapYear(year))) // ...of a common year,
+ || (((ConjunctionDay % 7) == 1) // ...or is on a Monday at...
+ && (ConjunctionParts >= 16789) // 15 hours, 589 parts or later...
+ && (HebrewLeapYear(year - 1))))// at the end of a leap year
+ // Then postpone Rosh HaShanah one day
+ AlternativeDay = ConjunctionDay + 1;
+ else
+ AlternativeDay = ConjunctionDay;
+
+ if (((AlternativeDay % 7) == 0)// If Rosh HaShanah would occur on Sunday,
+ || ((AlternativeDay % 7) == 3) // or Wednesday,
+ || ((AlternativeDay % 7) == 5)) // or Friday
+ // Then postpone it one (more) day
+ return (1+ AlternativeDay);
+ else
+ return AlternativeDay;
+}
+
+// Number of days in Hebrew year.
+static sal_Int32 DaysInHebrewYear(sal_Int32 year) {
+ return ((HebrewCalendarElapsedDays(year + 1)) -
+ (HebrewCalendarElapsedDays(year)));
+}
+
+// True if Heshvan is long in Hebrew year.
+static bool LongHeshvan(sal_Int32 year) {
+ return ((DaysInHebrewYear(year) % 10) == 5);
+}
+
+// True if Kislev is short in Hebrew year.
+static bool ShortKislev(sal_Int32 year) {
+ return ((DaysInHebrewYear(year) % 10) == 3);
+}
+
+// Last day of month in Hebrew year.
+static sal_Int32 LastDayOfHebrewMonth(sal_Int32 month, sal_Int32 year) {
+ if ((month == 2)
+ || (month == 4)
+ || (month == 6)
+ || ((month == 8) && !(LongHeshvan(year)))
+ || ((month == 9) && ShortKislev(year))
+ || (month == 10)
+ || ((month == 12) && !(HebrewLeapYear(year)))
+ || (month == 13))
+ return 29;
+ else
+ return 30;
+}
+
+namespace {
+
+class HebrewDate {
+private:
+ sal_Int32 year; // 1...
+ sal_Int32 month; // 1..LastMonthOfHebrewYear(year)
+ sal_Int32 day; // 1..LastDayOfHebrewMonth(month, year)
+
+public:
+ HebrewDate(sal_Int32 m, sal_Int32 d, sal_Int32 y) : year(y), month(m), day(d) { }
+
+ explicit HebrewDate(sal_Int32 d) { // Computes the Hebrew date from the absolute date.
+
+ if (d == 730119) // 1999-12-31 : a very common date used in calc
+ {
+ year = 5760;
+ month = 10;
+ day = 22;
+ return;
+ }
+
+ year = (d + HebrewEpoch) / 366; // Approximation from below.
+ // Search forward for year from the approximation.
+ while (d >= HebrewDate(7,1,year + 1).GetAbsoluteDate())
+ year++;
+ // Search forward for month from either Tishri or Nisan.
+ if (d < HebrewDate(1, 1, year).GetAbsoluteDate())
+ month = 7; // Start at Tishri
+ else
+ month = 1; // Start at Nisan
+ while (d > HebrewDate(month, (LastDayOfHebrewMonth(month,year)), year).GetAbsoluteDate())
+ month++;
+ // Calculate the day by subtraction.
+ day = d - HebrewDate(month, 1, year).GetAbsoluteDate() + 1;
+ }
+
+ int GetAbsoluteDate() const { // Computes the absolute date of Hebrew date.
+ sal_Int32 DayInYear = day; // Days so far this month.
+ if (month < 7) { // Before Tishri, so add days in prior months
+ // this year before and after Nisan.
+ sal_Int32 m = 7;
+ while (m <= (LastMonthOfHebrewYear(year))) {
+ DayInYear = DayInYear + LastDayOfHebrewMonth(m, year);
+ m++;
+ };
+ m = 1;
+ while (m < month) {
+ DayInYear = DayInYear + LastDayOfHebrewMonth(m, year);
+ m++;
+ }
+ }
+ else { // Add days in prior months this year
+ sal_Int32 m = 7;
+ while (m < month) {
+ DayInYear = DayInYear + LastDayOfHebrewMonth(m, year);
+ m++;
+ }
+ }
+ return (DayInYear +
+ (HebrewCalendarElapsedDays(year)// Days in prior years.
+ + HebrewEpoch)); // Days elapsed before absolute date 1.
+ }
+
+ sal_Int32 GetMonth() const { return month; }
+ sal_Int32 GetDay() const { return day; }
+ sal_Int32 GetYear() const { return year; }
+
+};
+
+}
+
+// Gregorian dates
+
+static int LastDayOfGregorianMonth(int month, int year) {
+// Compute the last date of the month for the Gregorian calendar.
+
+ switch (month) {
+ case 2:
+ if ((((year % 4) == 0) && ((year % 100) != 0))
+ || ((year % 400) == 0))
+ return 29;
+ else
+ return 28;
+ case 4:
+ case 6:
+ case 9:
+ case 11: return 30;
+ default: return 31;
+ }
+}
+
+namespace {
+
+class GregorianDate {
+private:
+ int year; // 1...
+ int month; // 1 == January, ..., 12 == December
+ int day; // 1..LastDayOfGregorianMonth(month, year)
+
+public:
+ GregorianDate(int m, int d, int y) { month = m; day = d; year = y; }
+
+ explicit GregorianDate(int d) { // Computes the Gregorian date from the absolute date.
+ // Search forward year by year from approximate year
+ year = d/366;
+ while (d >= GregorianDate(1,1,year+1).GetAbsoluteDate())
+ year++;
+ // Search forward month by month from January
+ month = 1;
+ while (d > GregorianDate(month, LastDayOfGregorianMonth(month,year), year).GetAbsoluteDate())
+ month++;
+ day = d - GregorianDate(month,1,year).GetAbsoluteDate() + 1;
+ }
+
+ int GetAbsoluteDate() const { // Computes the absolute date from the Gregorian date.
+ int N = day; // days this month
+ for (int m = month - 1; m > 0; m--) // days in prior months this year
+ N = N + LastDayOfGregorianMonth(m, year);
+ return
+ (N // days this year
+ + 365 * (year - 1) // days in previous years ignoring leap days
+ + (year - 1)/4 // Julian leap days before this year...
+ - (year - 1)/100 // ...minus prior century years...
+ + (year - 1)/400); // ...plus prior years divisible by 400
+ }
+
+ int GetMonth() const { return month; }
+ int GetDay() const { return day; }
+ int GetYear() const { return year; }
+
+};
+
+}
+
+// map field value from gregorian calendar to other calendar, it can be overwritten by derived class.
+void Calendar_jewish::mapFromGregorian()
+{
+ int y = fieldValue[CalendarFieldIndex::YEAR];
+ if (fieldValue[CalendarFieldIndex::ERA] == 0)
+ y = 1 - y;
+ GregorianDate Temp(fieldValue[CalendarFieldIndex::MONTH] + 1, fieldValue[CalendarFieldIndex::DAY_OF_MONTH], y);
+ HebrewDate hd(Temp.GetAbsoluteDate());
+
+ fieldValue[CalendarFieldIndex::ERA] = hd.GetYear() <= 0 ? 0 : 1;
+ fieldValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>( hd.GetMonth() - 1 );
+ fieldValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(hd.GetDay());
+ fieldValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(hd.GetYear() <= 0 ? 1 - hd.GetYear() : hd.GetYear());
+}
+
+#define FIELDS ((1 << CalendarFieldIndex::ERA) | (1 << CalendarFieldIndex::YEAR) | (1 << CalendarFieldIndex::MONTH) | (1 << CalendarFieldIndex::DAY_OF_MONTH))
+// map field value from other calendar to gregorian calendar, it should be implemented.
+void Calendar_jewish::mapToGregorian()
+{
+ if (!(fieldSet & FIELDS))
+ return;
+
+ sal_Int16 y = fieldSetValue[CalendarFieldIndex::YEAR];
+ if (fieldSetValue[CalendarFieldIndex::ERA] == 0)
+ y = 1 - y;
+ HebrewDate Temp(fieldSetValue[CalendarFieldIndex::MONTH] + 1, fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH], y);
+ GregorianDate gd(Temp.GetAbsoluteDate());
+
+ fieldSetValue[CalendarFieldIndex::ERA] = gd.GetYear() <= 0 ? 0 : 1;
+ fieldSetValue[CalendarFieldIndex::MONTH] = sal::static_int_cast<sal_Int16>( gd.GetMonth() - 1 );
+ fieldSetValue[CalendarFieldIndex::DAY_OF_MONTH] = static_cast<sal_Int16>(gd.GetDay());
+ fieldSetValue[CalendarFieldIndex::YEAR] = static_cast<sal_Int16>(gd.GetYear() <= 0 ? 1 - gd.GetYear() : gd.GetYear());
+ fieldSet |= FIELDS;
+}
+
+// Methods in XExtendedCalendar
+OUString SAL_CALL
+Calendar_jewish::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 /*nNativeNumberMode*/ )
+{
+ const sal_Int16 nNativeNumberMode = NativeNumberMode::NATNUM2; // make Hebrew number for Jewish calendar
+
+ if (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR) {
+ sal_Int32 value = getValue(CalendarFieldIndex::YEAR) % 1000; // take last 3 digits
+ return mxNatNum->getNativeNumberString(OUString::number(value), aLocale, nNativeNumberMode );
+ }
+ else
+ return Calendar_gregorian::getDisplayString(nCalendarDisplayCode, nNativeNumberMode );
+}
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */