intl/icu/source/i18n/ethpccal.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 * Copyright (C) 2003 - 2013, International Business Machines Corporation and
michael@0 4 * others. All Rights Reserved.
michael@0 5 *******************************************************************************
michael@0 6 */
michael@0 7
michael@0 8 #include "unicode/utypes.h"
michael@0 9
michael@0 10 #if !UCONFIG_NO_FORMATTING
michael@0 11
michael@0 12 #include "umutex.h"
michael@0 13 #include "ethpccal.h"
michael@0 14 #include "cecal.h"
michael@0 15 #include <float.h>
michael@0 16
michael@0 17 U_NAMESPACE_BEGIN
michael@0 18
michael@0 19 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicCalendar)
michael@0 20
michael@0 21 //static const int32_t JD_EPOCH_OFFSET_AMETE_ALEM = -285019;
michael@0 22 static const int32_t JD_EPOCH_OFFSET_AMETE_MIHRET = 1723856;
michael@0 23 static const int32_t AMETE_MIHRET_DELTA = 5500; // 5501 - 1 (Amete Alem 5501 = Amete Mihret 1)
michael@0 24
michael@0 25 //-------------------------------------------------------------------------
michael@0 26 // Constructors...
michael@0 27 //-------------------------------------------------------------------------
michael@0 28
michael@0 29 EthiopicCalendar::EthiopicCalendar(const Locale& aLocale,
michael@0 30 UErrorCode& success,
michael@0 31 EEraType type /*= AMETE_MIHRET_ERA*/)
michael@0 32 : CECalendar(aLocale, success),
michael@0 33 eraType(type)
michael@0 34 {
michael@0 35 }
michael@0 36
michael@0 37 EthiopicCalendar::EthiopicCalendar(const EthiopicCalendar& other)
michael@0 38 : CECalendar(other),
michael@0 39 eraType(other.eraType)
michael@0 40 {
michael@0 41 }
michael@0 42
michael@0 43 EthiopicCalendar::~EthiopicCalendar()
michael@0 44 {
michael@0 45 }
michael@0 46
michael@0 47 Calendar*
michael@0 48 EthiopicCalendar::clone() const
michael@0 49 {
michael@0 50 return new EthiopicCalendar(*this);
michael@0 51 }
michael@0 52
michael@0 53 const char *
michael@0 54 EthiopicCalendar::getType() const
michael@0 55 {
michael@0 56 if (isAmeteAlemEra()) {
michael@0 57 return "ethiopic-amete-alem";
michael@0 58 }
michael@0 59 return "ethiopic";
michael@0 60 }
michael@0 61
michael@0 62 void
michael@0 63 EthiopicCalendar::setAmeteAlemEra(UBool onOff)
michael@0 64 {
michael@0 65 eraType = onOff ? AMETE_ALEM_ERA : AMETE_MIHRET_ERA;
michael@0 66 }
michael@0 67
michael@0 68 UBool
michael@0 69 EthiopicCalendar::isAmeteAlemEra() const
michael@0 70 {
michael@0 71 return (eraType == AMETE_ALEM_ERA);
michael@0 72 }
michael@0 73
michael@0 74 //-------------------------------------------------------------------------
michael@0 75 // Calendar framework
michael@0 76 //-------------------------------------------------------------------------
michael@0 77
michael@0 78 int32_t
michael@0 79 EthiopicCalendar::handleGetExtendedYear()
michael@0 80 {
michael@0 81 // Ethiopic calendar uses EXTENDED_YEAR aligned to
michael@0 82 // Amelete Hihret year always.
michael@0 83 int32_t eyear;
michael@0 84 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
michael@0 85 eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
michael@0 86 } else if (isAmeteAlemEra()) {
michael@0 87 eyear = internalGet(UCAL_YEAR, 1 + AMETE_MIHRET_DELTA)
michael@0 88 - AMETE_MIHRET_DELTA; // Default to year 1 of Amelete Mihret
michael@0 89 } else {
michael@0 90 // The year defaults to the epoch start, the era to AMETE_MIHRET
michael@0 91 int32_t era = internalGet(UCAL_ERA, AMETE_MIHRET);
michael@0 92 if (era == AMETE_MIHRET) {
michael@0 93 eyear = internalGet(UCAL_YEAR, 1); // Default to year 1
michael@0 94 } else {
michael@0 95 eyear = internalGet(UCAL_YEAR, 1) - AMETE_MIHRET_DELTA;
michael@0 96 }
michael@0 97 }
michael@0 98 return eyear;
michael@0 99 }
michael@0 100
michael@0 101 void
michael@0 102 EthiopicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)
michael@0 103 {
michael@0 104 int32_t eyear, month, day, era, year;
michael@0 105 jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);
michael@0 106
michael@0 107 if (isAmeteAlemEra()) {
michael@0 108 era = AMETE_ALEM;
michael@0 109 year = eyear + AMETE_MIHRET_DELTA;
michael@0 110 } else {
michael@0 111 if (eyear > 0) {
michael@0 112 era = AMETE_MIHRET;
michael@0 113 year = eyear;
michael@0 114 } else {
michael@0 115 era = AMETE_ALEM;
michael@0 116 year = eyear + AMETE_MIHRET_DELTA;
michael@0 117 }
michael@0 118 }
michael@0 119
michael@0 120 internalSet(UCAL_EXTENDED_YEAR, eyear);
michael@0 121 internalSet(UCAL_ERA, era);
michael@0 122 internalSet(UCAL_YEAR, year);
michael@0 123 internalSet(UCAL_MONTH, month);
michael@0 124 internalSet(UCAL_DATE, day);
michael@0 125 internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);
michael@0 126 }
michael@0 127
michael@0 128 int32_t
michael@0 129 EthiopicCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
michael@0 130 {
michael@0 131 if (isAmeteAlemEra() && field == UCAL_ERA) {
michael@0 132 return 0; // Only one era in this mode, era is always 0
michael@0 133 }
michael@0 134 return CECalendar::handleGetLimit(field, limitType);
michael@0 135 }
michael@0 136
michael@0 137 /**
michael@0 138 * The system maintains a static default century start date and Year. They are
michael@0 139 * initialized the first time they are used. Once the system default century date
michael@0 140 * and year are set, they do not change.
michael@0 141 */
michael@0 142 static UDate gSystemDefaultCenturyStart = DBL_MIN;
michael@0 143 static int32_t gSystemDefaultCenturyStartYear = -1;
michael@0 144 static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER;
michael@0 145
michael@0 146 static void U_CALLCONV initializeSystemDefaultCentury()
michael@0 147 {
michael@0 148 UErrorCode status = U_ZERO_ERROR;
michael@0 149 EthiopicCalendar calendar(Locale("@calendar=ethiopic"), status);
michael@0 150 if (U_SUCCESS(status)) {
michael@0 151 calendar.setTime(Calendar::getNow(), status);
michael@0 152 calendar.add(UCAL_YEAR, -80, status);
michael@0 153
michael@0 154 gSystemDefaultCenturyStart = calendar.getTime(status);
michael@0 155 gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
michael@0 156 }
michael@0 157 // We have no recourse upon failure unless we want to propagate the failure
michael@0 158 // out.
michael@0 159 }
michael@0 160
michael@0 161 UDate
michael@0 162 EthiopicCalendar::defaultCenturyStart() const
michael@0 163 {
michael@0 164 // lazy-evaluate systemDefaultCenturyStart
michael@0 165 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
michael@0 166 return gSystemDefaultCenturyStart;
michael@0 167 }
michael@0 168
michael@0 169 int32_t
michael@0 170 EthiopicCalendar::defaultCenturyStartYear() const
michael@0 171 {
michael@0 172 // lazy-evaluate systemDefaultCenturyStartYear
michael@0 173 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
michael@0 174 if (isAmeteAlemEra()) {
michael@0 175 return gSystemDefaultCenturyStartYear + AMETE_MIHRET_DELTA;
michael@0 176 }
michael@0 177 return gSystemDefaultCenturyStartYear;
michael@0 178 }
michael@0 179
michael@0 180
michael@0 181 int32_t
michael@0 182 EthiopicCalendar::getJDEpochOffset() const
michael@0 183 {
michael@0 184 return JD_EPOCH_OFFSET_AMETE_MIHRET;
michael@0 185 }
michael@0 186
michael@0 187
michael@0 188 #if 0
michael@0 189 // We do not want to introduce this API in ICU4C.
michael@0 190 // It was accidentally introduced in ICU4J as a public API.
michael@0 191
michael@0 192 //-------------------------------------------------------------------------
michael@0 193 // Calendar system Conversion methods...
michael@0 194 //-------------------------------------------------------------------------
michael@0 195
michael@0 196 int32_t
michael@0 197 EthiopicCalendar::ethiopicToJD(int32_t year, int32_t month, int32_t date)
michael@0 198 {
michael@0 199 return ceToJD(year, month, date, JD_EPOCH_OFFSET_AMETE_MIHRET);
michael@0 200 }
michael@0 201 #endif
michael@0 202
michael@0 203 U_NAMESPACE_END
michael@0 204
michael@0 205 #endif

mercurial