ethpccal.cpp (4887B)
1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2003 - 2013, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ******************************************************************************* 8 */ 9 10 #include "unicode/utypes.h" 11 12 #if !UCONFIG_NO_FORMATTING 13 14 #include "gregoimp.h" 15 #include "umutex.h" 16 #include "ethpccal.h" 17 #include "cecal.h" 18 #include <float.h> 19 20 U_NAMESPACE_BEGIN 21 22 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicCalendar) 23 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicAmeteAlemCalendar) 24 25 static const int32_t JD_EPOCH_OFFSET_AMETE_ALEM = -285019; 26 static const int32_t JD_EPOCH_OFFSET_AMETE_MIHRET = 1723856; 27 static const int32_t AMETE_MIHRET_DELTA = 5500; // 5501 - 1 (Amete Alem 5501 = Amete Mihret 1) 28 29 //------------------------------------------------------------------------- 30 // Constructors... 31 //------------------------------------------------------------------------- 32 33 EthiopicCalendar::EthiopicCalendar(const Locale& aLocale, 34 UErrorCode& success) 35 : CECalendar(aLocale, success) 36 { 37 } 38 39 EthiopicCalendar::~EthiopicCalendar() 40 { 41 } 42 43 EthiopicCalendar* 44 EthiopicCalendar::clone() const 45 { 46 return new EthiopicCalendar(*this); 47 } 48 49 const char * 50 EthiopicCalendar::getType() const 51 { 52 return "ethiopic"; 53 } 54 55 //------------------------------------------------------------------------- 56 // Calendar framework 57 //------------------------------------------------------------------------- 58 59 int32_t 60 EthiopicCalendar::handleGetExtendedYear(UErrorCode& status) 61 { 62 if (U_FAILURE(status)) { 63 return 0; 64 } 65 // Ethiopic calendar uses EXTENDED_YEAR aligned to 66 // Amelete Hihret year always. 67 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { 68 return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 69 } 70 // The year defaults to the epoch start, the era to AMETE_MIHRET 71 if (internalGet(UCAL_ERA, AMETE_MIHRET) == AMETE_MIHRET) { 72 return internalGet(UCAL_YEAR, 1); // Default to year 1 73 } 74 int32_t year = internalGet(UCAL_YEAR, 1); 75 if (uprv_add32_overflow(year, -AMETE_MIHRET_DELTA, &year)) { 76 status = U_ILLEGAL_ARGUMENT_ERROR; 77 return 0; 78 } 79 return year; 80 } 81 82 IMPL_SYSTEM_DEFAULT_CENTURY(EthiopicCalendar, "@calendar=ethiopic") 83 84 int32_t 85 EthiopicCalendar::getJDEpochOffset() const 86 { 87 return JD_EPOCH_OFFSET_AMETE_MIHRET; 88 } 89 90 int32_t EthiopicCalendar::extendedYearToEra(int32_t extendedYear) const { 91 return extendedYear <= 0 ? AMETE_ALEM : AMETE_MIHRET; 92 } 93 94 int32_t EthiopicCalendar::extendedYearToYear(int32_t extendedYear) const { 95 return extendedYear <= 0 ? extendedYear + AMETE_MIHRET_DELTA : extendedYear; 96 } 97 98 int32_t EthiopicCalendar::getRelatedYearDifference() const { 99 constexpr int32_t kEthiopicCalendarRelatedYearDifference = 8; 100 return kEthiopicCalendarRelatedYearDifference; 101 } 102 103 //------------------------------------------------------------------------- 104 // Constructors... 105 //------------------------------------------------------------------------- 106 107 EthiopicAmeteAlemCalendar::EthiopicAmeteAlemCalendar(const Locale& aLocale, 108 UErrorCode& success) 109 : EthiopicCalendar(aLocale, success) 110 { 111 } 112 113 EthiopicAmeteAlemCalendar::~EthiopicAmeteAlemCalendar() 114 { 115 } 116 117 EthiopicAmeteAlemCalendar* 118 EthiopicAmeteAlemCalendar::clone() const 119 { 120 return new EthiopicAmeteAlemCalendar(*this); 121 } 122 123 //------------------------------------------------------------------------- 124 // Calendar framework 125 //------------------------------------------------------------------------- 126 127 const char * 128 EthiopicAmeteAlemCalendar::getType() const 129 { 130 return "ethiopic-amete-alem"; 131 } 132 133 int32_t 134 EthiopicAmeteAlemCalendar::handleGetExtendedYear(UErrorCode& status) 135 { 136 if (U_FAILURE(status)) { 137 return 0; 138 } 139 // Ethiopic calendar uses EXTENDED_YEAR aligned to 140 // Amelete Hihret year always. 141 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { 142 return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 143 } 144 // Default to year 1 145 return internalGet(UCAL_YEAR, 1); 146 } 147 148 int32_t 149 EthiopicAmeteAlemCalendar::getJDEpochOffset() const 150 { 151 return JD_EPOCH_OFFSET_AMETE_ALEM; 152 } 153 154 int32_t EthiopicAmeteAlemCalendar::extendedYearToEra(int32_t /* extendedYear */) const { 155 return AMETE_ALEM; 156 } 157 158 int32_t EthiopicAmeteAlemCalendar::extendedYearToYear(int32_t extendedYear) const { 159 return extendedYear; 160 } 161 162 163 int32_t 164 EthiopicAmeteAlemCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const 165 { 166 if (field == UCAL_ERA) { 167 return 0; // Only one era in this mode, era is always 0 168 } 169 return EthiopicCalendar::handleGetLimit(field, limitType); 170 } 171 172 U_NAMESPACE_END 173 174 #endif