1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/ethpccal.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,205 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* Copyright (C) 2003 - 2013, International Business Machines Corporation and 1.7 +* others. All Rights Reserved. 1.8 +******************************************************************************* 1.9 +*/ 1.10 + 1.11 +#include "unicode/utypes.h" 1.12 + 1.13 +#if !UCONFIG_NO_FORMATTING 1.14 + 1.15 +#include "umutex.h" 1.16 +#include "ethpccal.h" 1.17 +#include "cecal.h" 1.18 +#include <float.h> 1.19 + 1.20 +U_NAMESPACE_BEGIN 1.21 + 1.22 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicCalendar) 1.23 + 1.24 +//static const int32_t JD_EPOCH_OFFSET_AMETE_ALEM = -285019; 1.25 +static const int32_t JD_EPOCH_OFFSET_AMETE_MIHRET = 1723856; 1.26 +static const int32_t AMETE_MIHRET_DELTA = 5500; // 5501 - 1 (Amete Alem 5501 = Amete Mihret 1) 1.27 + 1.28 +//------------------------------------------------------------------------- 1.29 +// Constructors... 1.30 +//------------------------------------------------------------------------- 1.31 + 1.32 +EthiopicCalendar::EthiopicCalendar(const Locale& aLocale, 1.33 + UErrorCode& success, 1.34 + EEraType type /*= AMETE_MIHRET_ERA*/) 1.35 +: CECalendar(aLocale, success), 1.36 + eraType(type) 1.37 +{ 1.38 +} 1.39 + 1.40 +EthiopicCalendar::EthiopicCalendar(const EthiopicCalendar& other) 1.41 +: CECalendar(other), 1.42 + eraType(other.eraType) 1.43 +{ 1.44 +} 1.45 + 1.46 +EthiopicCalendar::~EthiopicCalendar() 1.47 +{ 1.48 +} 1.49 + 1.50 +Calendar* 1.51 +EthiopicCalendar::clone() const 1.52 +{ 1.53 + return new EthiopicCalendar(*this); 1.54 +} 1.55 + 1.56 +const char * 1.57 +EthiopicCalendar::getType() const 1.58 +{ 1.59 + if (isAmeteAlemEra()) { 1.60 + return "ethiopic-amete-alem"; 1.61 + } 1.62 + return "ethiopic"; 1.63 +} 1.64 + 1.65 +void 1.66 +EthiopicCalendar::setAmeteAlemEra(UBool onOff) 1.67 +{ 1.68 + eraType = onOff ? AMETE_ALEM_ERA : AMETE_MIHRET_ERA; 1.69 +} 1.70 + 1.71 +UBool 1.72 +EthiopicCalendar::isAmeteAlemEra() const 1.73 +{ 1.74 + return (eraType == AMETE_ALEM_ERA); 1.75 +} 1.76 + 1.77 +//------------------------------------------------------------------------- 1.78 +// Calendar framework 1.79 +//------------------------------------------------------------------------- 1.80 + 1.81 +int32_t 1.82 +EthiopicCalendar::handleGetExtendedYear() 1.83 +{ 1.84 + // Ethiopic calendar uses EXTENDED_YEAR aligned to 1.85 + // Amelete Hihret year always. 1.86 + int32_t eyear; 1.87 + if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { 1.88 + eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 1.89 + } else if (isAmeteAlemEra()) { 1.90 + eyear = internalGet(UCAL_YEAR, 1 + AMETE_MIHRET_DELTA) 1.91 + - AMETE_MIHRET_DELTA; // Default to year 1 of Amelete Mihret 1.92 + } else { 1.93 + // The year defaults to the epoch start, the era to AMETE_MIHRET 1.94 + int32_t era = internalGet(UCAL_ERA, AMETE_MIHRET); 1.95 + if (era == AMETE_MIHRET) { 1.96 + eyear = internalGet(UCAL_YEAR, 1); // Default to year 1 1.97 + } else { 1.98 + eyear = internalGet(UCAL_YEAR, 1) - AMETE_MIHRET_DELTA; 1.99 + } 1.100 + } 1.101 + return eyear; 1.102 +} 1.103 + 1.104 +void 1.105 +EthiopicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) 1.106 +{ 1.107 + int32_t eyear, month, day, era, year; 1.108 + jdToCE(julianDay, getJDEpochOffset(), eyear, month, day); 1.109 + 1.110 + if (isAmeteAlemEra()) { 1.111 + era = AMETE_ALEM; 1.112 + year = eyear + AMETE_MIHRET_DELTA; 1.113 + } else { 1.114 + if (eyear > 0) { 1.115 + era = AMETE_MIHRET; 1.116 + year = eyear; 1.117 + } else { 1.118 + era = AMETE_ALEM; 1.119 + year = eyear + AMETE_MIHRET_DELTA; 1.120 + } 1.121 + } 1.122 + 1.123 + internalSet(UCAL_EXTENDED_YEAR, eyear); 1.124 + internalSet(UCAL_ERA, era); 1.125 + internalSet(UCAL_YEAR, year); 1.126 + internalSet(UCAL_MONTH, month); 1.127 + internalSet(UCAL_DATE, day); 1.128 + internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day); 1.129 +} 1.130 + 1.131 +int32_t 1.132 +EthiopicCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const 1.133 +{ 1.134 + if (isAmeteAlemEra() && field == UCAL_ERA) { 1.135 + return 0; // Only one era in this mode, era is always 0 1.136 + } 1.137 + return CECalendar::handleGetLimit(field, limitType); 1.138 +} 1.139 + 1.140 +/** 1.141 + * The system maintains a static default century start date and Year. They are 1.142 + * initialized the first time they are used. Once the system default century date 1.143 + * and year are set, they do not change. 1.144 + */ 1.145 +static UDate gSystemDefaultCenturyStart = DBL_MIN; 1.146 +static int32_t gSystemDefaultCenturyStartYear = -1; 1.147 +static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; 1.148 + 1.149 +static void U_CALLCONV initializeSystemDefaultCentury() 1.150 +{ 1.151 + UErrorCode status = U_ZERO_ERROR; 1.152 + EthiopicCalendar calendar(Locale("@calendar=ethiopic"), status); 1.153 + if (U_SUCCESS(status)) { 1.154 + calendar.setTime(Calendar::getNow(), status); 1.155 + calendar.add(UCAL_YEAR, -80, status); 1.156 + 1.157 + gSystemDefaultCenturyStart = calendar.getTime(status); 1.158 + gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); 1.159 + } 1.160 + // We have no recourse upon failure unless we want to propagate the failure 1.161 + // out. 1.162 +} 1.163 + 1.164 +UDate 1.165 +EthiopicCalendar::defaultCenturyStart() const 1.166 +{ 1.167 + // lazy-evaluate systemDefaultCenturyStart 1.168 + umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); 1.169 + return gSystemDefaultCenturyStart; 1.170 +} 1.171 + 1.172 +int32_t 1.173 +EthiopicCalendar::defaultCenturyStartYear() const 1.174 +{ 1.175 + // lazy-evaluate systemDefaultCenturyStartYear 1.176 + umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); 1.177 + if (isAmeteAlemEra()) { 1.178 + return gSystemDefaultCenturyStartYear + AMETE_MIHRET_DELTA; 1.179 + } 1.180 + return gSystemDefaultCenturyStartYear; 1.181 +} 1.182 + 1.183 + 1.184 +int32_t 1.185 +EthiopicCalendar::getJDEpochOffset() const 1.186 +{ 1.187 + return JD_EPOCH_OFFSET_AMETE_MIHRET; 1.188 +} 1.189 + 1.190 + 1.191 +#if 0 1.192 +// We do not want to introduce this API in ICU4C. 1.193 +// It was accidentally introduced in ICU4J as a public API. 1.194 + 1.195 +//------------------------------------------------------------------------- 1.196 +// Calendar system Conversion methods... 1.197 +//------------------------------------------------------------------------- 1.198 + 1.199 +int32_t 1.200 +EthiopicCalendar::ethiopicToJD(int32_t year, int32_t month, int32_t date) 1.201 +{ 1.202 + return ceToJD(year, month, date, JD_EPOCH_OFFSET_AMETE_MIHRET); 1.203 +} 1.204 +#endif 1.205 + 1.206 +U_NAMESPACE_END 1.207 + 1.208 +#endif