1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/coptccal.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 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 "coptccal.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(CopticCalendar) 1.23 + 1.24 +static const int32_t COPTIC_JD_EPOCH_OFFSET = 1824665; 1.25 + 1.26 +//------------------------------------------------------------------------- 1.27 +// Constructors... 1.28 +//------------------------------------------------------------------------- 1.29 + 1.30 +CopticCalendar::CopticCalendar(const Locale& aLocale, UErrorCode& success) 1.31 +: CECalendar(aLocale, success) 1.32 +{ 1.33 +} 1.34 + 1.35 +CopticCalendar::CopticCalendar (const CopticCalendar& other) 1.36 +: CECalendar(other) 1.37 +{ 1.38 +} 1.39 + 1.40 +CopticCalendar::~CopticCalendar() 1.41 +{ 1.42 +} 1.43 + 1.44 +Calendar* 1.45 +CopticCalendar::clone() const 1.46 +{ 1.47 + return new CopticCalendar(*this); 1.48 +} 1.49 + 1.50 +const char* 1.51 +CopticCalendar::getType() const 1.52 +{ 1.53 + return "coptic"; 1.54 +} 1.55 + 1.56 +//------------------------------------------------------------------------- 1.57 +// Calendar framework 1.58 +//------------------------------------------------------------------------- 1.59 + 1.60 +int32_t 1.61 +CopticCalendar::handleGetExtendedYear() 1.62 +{ 1.63 + int32_t eyear; 1.64 + if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { 1.65 + eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 1.66 + } else { 1.67 + // The year defaults to the epoch start, the era to CE 1.68 + int32_t era = internalGet(UCAL_ERA, CE); 1.69 + if (era == BCE) { 1.70 + eyear = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year 1.71 + } else { 1.72 + eyear = internalGet(UCAL_YEAR, 1); // Default to year 1 1.73 + } 1.74 + } 1.75 + return eyear; 1.76 +} 1.77 + 1.78 +void 1.79 +CopticCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) 1.80 +{ 1.81 + int32_t eyear, month, day, era, year; 1.82 + jdToCE(julianDay, getJDEpochOffset(), eyear, month, day); 1.83 + 1.84 + if (eyear <= 0) { 1.85 + era = BCE; 1.86 + year = 1 - eyear; 1.87 + } else { 1.88 + era = CE; 1.89 + year = eyear; 1.90 + } 1.91 + 1.92 + internalSet(UCAL_EXTENDED_YEAR, eyear); 1.93 + internalSet(UCAL_ERA, era); 1.94 + internalSet(UCAL_YEAR, year); 1.95 + internalSet(UCAL_MONTH, month); 1.96 + internalSet(UCAL_DATE, day); 1.97 + internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day); 1.98 +} 1.99 + 1.100 +/** 1.101 + * The system maintains a static default century start date and Year. They are 1.102 + * initialized the first time they are used. Once the system default century date 1.103 + * and year are set, they do not change. 1.104 + */ 1.105 +static UDate gSystemDefaultCenturyStart = DBL_MIN; 1.106 +static int32_t gSystemDefaultCenturyStartYear = -1; 1.107 +static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; 1.108 + 1.109 + 1.110 +static void U_CALLCONV initializeSystemDefaultCentury() { 1.111 + UErrorCode status = U_ZERO_ERROR; 1.112 + CopticCalendar calendar(Locale("@calendar=coptic"), status); 1.113 + if (U_SUCCESS(status)) { 1.114 + calendar.setTime(Calendar::getNow(), status); 1.115 + calendar.add(UCAL_YEAR, -80, status); 1.116 + gSystemDefaultCenturyStart = calendar.getTime(status); 1.117 + gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); 1.118 + } 1.119 + // We have no recourse upon failure unless we want to propagate the failure 1.120 + // out. 1.121 +} 1.122 + 1.123 +UDate 1.124 +CopticCalendar::defaultCenturyStart() const 1.125 +{ 1.126 + // lazy-evaluate systemDefaultCenturyStart 1.127 + umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); 1.128 + return gSystemDefaultCenturyStart; 1.129 +} 1.130 + 1.131 +int32_t 1.132 +CopticCalendar::defaultCenturyStartYear() const 1.133 +{ 1.134 + // lazy-evaluate systemDefaultCenturyStart 1.135 + umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); 1.136 + return gSystemDefaultCenturyStartYear; 1.137 +} 1.138 + 1.139 + 1.140 +int32_t 1.141 +CopticCalendar::getJDEpochOffset() const 1.142 +{ 1.143 + return COPTIC_JD_EPOCH_OFFSET; 1.144 +} 1.145 + 1.146 + 1.147 +#if 0 1.148 +// We do not want to introduce this API in ICU4C. 1.149 +// It was accidentally introduced in ICU4J as a public API. 1.150 + 1.151 +//------------------------------------------------------------------------- 1.152 +// Calendar system Conversion methods... 1.153 +//------------------------------------------------------------------------- 1.154 + 1.155 +int32_t 1.156 +CopticCalendar::copticToJD(int32_t year, int32_t month, int32_t day) 1.157 +{ 1.158 + return CECalendar::ceToJD(year, month, day, COPTIC_JD_EPOCH_OFFSET); 1.159 +} 1.160 +#endif 1.161 + 1.162 +U_NAMESPACE_END 1.163 + 1.164 +#endif /* #if !UCONFIG_NO_FORMATTING */ 1.165 +//eof