michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2003 - 2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "umutex.h" michael@0: #include "ethpccal.h" michael@0: #include "cecal.h" michael@0: #include michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicCalendar) michael@0: michael@0: //static const int32_t JD_EPOCH_OFFSET_AMETE_ALEM = -285019; michael@0: static const int32_t JD_EPOCH_OFFSET_AMETE_MIHRET = 1723856; michael@0: static const int32_t AMETE_MIHRET_DELTA = 5500; // 5501 - 1 (Amete Alem 5501 = Amete Mihret 1) michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: EthiopicCalendar::EthiopicCalendar(const Locale& aLocale, michael@0: UErrorCode& success, michael@0: EEraType type /*= AMETE_MIHRET_ERA*/) michael@0: : CECalendar(aLocale, success), michael@0: eraType(type) michael@0: { michael@0: } michael@0: michael@0: EthiopicCalendar::EthiopicCalendar(const EthiopicCalendar& other) michael@0: : CECalendar(other), michael@0: eraType(other.eraType) michael@0: { michael@0: } michael@0: michael@0: EthiopicCalendar::~EthiopicCalendar() michael@0: { michael@0: } michael@0: michael@0: Calendar* michael@0: EthiopicCalendar::clone() const michael@0: { michael@0: return new EthiopicCalendar(*this); michael@0: } michael@0: michael@0: const char * michael@0: EthiopicCalendar::getType() const michael@0: { michael@0: if (isAmeteAlemEra()) { michael@0: return "ethiopic-amete-alem"; michael@0: } michael@0: return "ethiopic"; michael@0: } michael@0: michael@0: void michael@0: EthiopicCalendar::setAmeteAlemEra(UBool onOff) michael@0: { michael@0: eraType = onOff ? AMETE_ALEM_ERA : AMETE_MIHRET_ERA; michael@0: } michael@0: michael@0: UBool michael@0: EthiopicCalendar::isAmeteAlemEra() const michael@0: { michael@0: return (eraType == AMETE_ALEM_ERA); michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Calendar framework michael@0: //------------------------------------------------------------------------- michael@0: michael@0: int32_t michael@0: EthiopicCalendar::handleGetExtendedYear() michael@0: { michael@0: // Ethiopic calendar uses EXTENDED_YEAR aligned to michael@0: // Amelete Hihret year always. michael@0: int32_t eyear; michael@0: if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { michael@0: eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 michael@0: } else if (isAmeteAlemEra()) { michael@0: eyear = internalGet(UCAL_YEAR, 1 + AMETE_MIHRET_DELTA) michael@0: - AMETE_MIHRET_DELTA; // Default to year 1 of Amelete Mihret michael@0: } else { michael@0: // The year defaults to the epoch start, the era to AMETE_MIHRET michael@0: int32_t era = internalGet(UCAL_ERA, AMETE_MIHRET); michael@0: if (era == AMETE_MIHRET) { michael@0: eyear = internalGet(UCAL_YEAR, 1); // Default to year 1 michael@0: } else { michael@0: eyear = internalGet(UCAL_YEAR, 1) - AMETE_MIHRET_DELTA; michael@0: } michael@0: } michael@0: return eyear; michael@0: } michael@0: michael@0: void michael@0: EthiopicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) michael@0: { michael@0: int32_t eyear, month, day, era, year; michael@0: jdToCE(julianDay, getJDEpochOffset(), eyear, month, day); michael@0: michael@0: if (isAmeteAlemEra()) { michael@0: era = AMETE_ALEM; michael@0: year = eyear + AMETE_MIHRET_DELTA; michael@0: } else { michael@0: if (eyear > 0) { michael@0: era = AMETE_MIHRET; michael@0: year = eyear; michael@0: } else { michael@0: era = AMETE_ALEM; michael@0: year = eyear + AMETE_MIHRET_DELTA; michael@0: } michael@0: } michael@0: michael@0: internalSet(UCAL_EXTENDED_YEAR, eyear); michael@0: internalSet(UCAL_ERA, era); michael@0: internalSet(UCAL_YEAR, year); michael@0: internalSet(UCAL_MONTH, month); michael@0: internalSet(UCAL_DATE, day); michael@0: internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day); michael@0: } michael@0: michael@0: int32_t michael@0: EthiopicCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const michael@0: { michael@0: if (isAmeteAlemEra() && field == UCAL_ERA) { michael@0: return 0; // Only one era in this mode, era is always 0 michael@0: } michael@0: return CECalendar::handleGetLimit(field, limitType); michael@0: } michael@0: michael@0: /** michael@0: * The system maintains a static default century start date and Year. They are michael@0: * initialized the first time they are used. Once the system default century date michael@0: * and year are set, they do not change. michael@0: */ michael@0: static UDate gSystemDefaultCenturyStart = DBL_MIN; michael@0: static int32_t gSystemDefaultCenturyStartYear = -1; michael@0: static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; michael@0: michael@0: static void U_CALLCONV initializeSystemDefaultCentury() michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: EthiopicCalendar calendar(Locale("@calendar=ethiopic"), status); michael@0: if (U_SUCCESS(status)) { michael@0: calendar.setTime(Calendar::getNow(), status); michael@0: calendar.add(UCAL_YEAR, -80, status); michael@0: michael@0: gSystemDefaultCenturyStart = calendar.getTime(status); michael@0: gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); michael@0: } michael@0: // We have no recourse upon failure unless we want to propagate the failure michael@0: // out. michael@0: } michael@0: michael@0: UDate michael@0: EthiopicCalendar::defaultCenturyStart() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t michael@0: EthiopicCalendar::defaultCenturyStartYear() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStartYear michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: if (isAmeteAlemEra()) { michael@0: return gSystemDefaultCenturyStartYear + AMETE_MIHRET_DELTA; michael@0: } michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: michael@0: int32_t michael@0: EthiopicCalendar::getJDEpochOffset() const michael@0: { michael@0: return JD_EPOCH_OFFSET_AMETE_MIHRET; michael@0: } michael@0: michael@0: michael@0: #if 0 michael@0: // We do not want to introduce this API in ICU4C. michael@0: // It was accidentally introduced in ICU4J as a public API. michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Calendar system Conversion methods... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: int32_t michael@0: EthiopicCalendar::ethiopicToJD(int32_t year, int32_t month, int32_t date) michael@0: { michael@0: return ceToJD(year, month, date, JD_EPOCH_OFFSET_AMETE_MIHRET); michael@0: } michael@0: #endif michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif