| 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 | void |
| 83 | EthiopicCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) |
| 84 | { |
| 85 | int32_t eyear, month, day; |
| 86 | jdToCE(julianDay, getJDEpochOffset(), eyear, month, day, status); |
| 87 | if (U_FAILURE(status)) return; |
| 88 | |
| 89 | internalSet(UCAL_EXTENDED_YEAR, eyear); |
| 90 | internalSet(UCAL_ERA, (eyear > 0) ? AMETE_MIHRET : AMETE_ALEM); |
| 91 | internalSet(UCAL_YEAR, (eyear > 0) ? eyear : (eyear + AMETE_MIHRET_DELTA)); |
| 92 | internalSet(UCAL_MONTH, month); |
| 93 | internalSet(UCAL_ORDINAL_MONTH, month); |
| 94 | internalSet(UCAL_DATE, day); |
| 95 | internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day); |
| 96 | } |
| 97 | |
| 98 | constexpr uint32_t kEthiopicRelatedYearDiff = 8; |
| 99 | |
| 100 | int32_t EthiopicCalendar::getRelatedYear(UErrorCode &status) const |
| 101 | { |
| 102 | int32_t year = get(UCAL_EXTENDED_YEAR, status); |
| 103 | if (U_FAILURE(status)) { |
| 104 | return 0; |
| 105 | } |
| 106 | return year + kEthiopicRelatedYearDiff; |
| 107 | } |
| 108 | |
| 109 | void EthiopicCalendar::setRelatedYear(int32_t year) |
| 110 | { |
| 111 | // set extended year |
| 112 | set(UCAL_EXTENDED_YEAR, year - kEthiopicRelatedYearDiff); |
| 113 | } |
| 114 | |
| 115 | IMPL_SYSTEM_DEFAULT_CENTURY(EthiopicCalendar, "@calendar=ethiopic") |
| 116 | |
| 117 | int32_t |
| 118 | EthiopicCalendar::getJDEpochOffset() const |
| 119 | { |
| 120 | return JD_EPOCH_OFFSET_AMETE_MIHRET; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | //------------------------------------------------------------------------- |
| 125 | // Constructors... |
| 126 | //------------------------------------------------------------------------- |
| 127 | |
| 128 | EthiopicAmeteAlemCalendar::EthiopicAmeteAlemCalendar(const Locale& aLocale, |
| 129 | UErrorCode& success) |
| 130 | : EthiopicCalendar(aLocale, success) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | EthiopicAmeteAlemCalendar::~EthiopicAmeteAlemCalendar() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | EthiopicAmeteAlemCalendar* |
| 139 | EthiopicAmeteAlemCalendar::clone() const |
| 140 | { |
| 141 | return new EthiopicAmeteAlemCalendar(*this); |
| 142 | } |
| 143 | |
| 144 | //------------------------------------------------------------------------- |
| 145 | // Calendar framework |
| 146 | //------------------------------------------------------------------------- |
| 147 | |
| 148 | const char * |
| 149 | EthiopicAmeteAlemCalendar::getType() const |
| 150 | { |
| 151 | return "ethiopic-amete-alem"; |
| 152 | } |
| 153 | |
| 154 | int32_t |
| 155 | EthiopicAmeteAlemCalendar::handleGetExtendedYear(UErrorCode& status) |
| 156 | { |
| 157 | if (U_FAILURE(status)) { |
| 158 | return 0; |
| 159 | } |
| 160 | // Ethiopic calendar uses EXTENDED_YEAR aligned to |
| 161 | // Amelete Hihret year always. |
| 162 | if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { |
| 163 | return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 |
| 164 | } |
| 165 | // Default to year 1 of Amelete Mihret |
| 166 | int32_t year = internalGet(UCAL_YEAR, 1 + AMETE_MIHRET_DELTA); |
| 167 | if (uprv_add32_overflow(year, -AMETE_MIHRET_DELTA, &year)) { |
| 168 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 169 | return 0; |
| 170 | } |
| 171 | return year; |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | EthiopicAmeteAlemCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) |
| 176 | { |
| 177 | int32_t eyear, month, day; |
| 178 | jdToCE(julianDay, getJDEpochOffset(), eyear, month, day, status); |
| 179 | if (U_FAILURE(status)) return; |
| 180 | |
| 181 | internalSet(UCAL_EXTENDED_YEAR, eyear); |
| 182 | internalSet(UCAL_ERA, AMETE_ALEM); |
| 183 | internalSet(UCAL_YEAR, eyear + AMETE_MIHRET_DELTA); |
| 184 | internalSet(UCAL_MONTH, month); |
| 185 | internalSet(UCAL_ORDINAL_MONTH, month); |
| 186 | internalSet(UCAL_DATE, day); |
| 187 | internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day); |
| 188 | } |
| 189 | |
| 190 | int32_t |
| 191 | EthiopicAmeteAlemCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const |
| 192 | { |
| 193 | if (field == UCAL_ERA) { |
| 194 | return 0; // Only one era in this mode, era is always 0 |
| 195 | } |
| 196 | return EthiopicCalendar::handleGetLimit(field, limitType); |
| 197 | } |
| 198 | |
| 199 | constexpr uint32_t kEthiopicAmeteAlemRelatedYearDiff = -5492; |
| 200 | |
| 201 | int32_t EthiopicAmeteAlemCalendar::getRelatedYear(UErrorCode &status) const |
| 202 | { |
| 203 | int32_t year = get(UCAL_EXTENDED_YEAR, status); |
| 204 | if (U_FAILURE(status)) { |
| 205 | return 0; |
| 206 | } |
| 207 | return year + kEthiopicAmeteAlemRelatedYearDiff; |
| 208 | } |
| 209 | |
| 210 | void EthiopicAmeteAlemCalendar::setRelatedYear(int32_t year) |
| 211 | { |
| 212 | // set extended year |
| 213 | set(UCAL_EXTENDED_YEAR, year - kEthiopicAmeteAlemRelatedYearDiff); |
| 214 | } |
| 215 | |
| 216 | int32_t |
| 217 | EthiopicAmeteAlemCalendar::defaultCenturyStartYear() const |
| 218 | { |
| 219 | return EthiopicCalendar::defaultCenturyStartYear() + AMETE_MIHRET_DELTA; |
| 220 | } |
| 221 | |
| 222 | U_NAMESPACE_END |
| 223 | |
| 224 | #endif |