michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2003 - 2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "umutex.h" michael@0: #include "coptccal.h" michael@0: #include "cecal.h" michael@0: #include michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CopticCalendar) michael@0: michael@0: static const int32_t COPTIC_JD_EPOCH_OFFSET = 1824665; michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: CopticCalendar::CopticCalendar(const Locale& aLocale, UErrorCode& success) michael@0: : CECalendar(aLocale, success) michael@0: { michael@0: } michael@0: michael@0: CopticCalendar::CopticCalendar (const CopticCalendar& other) michael@0: : CECalendar(other) michael@0: { michael@0: } michael@0: michael@0: CopticCalendar::~CopticCalendar() michael@0: { michael@0: } michael@0: michael@0: Calendar* michael@0: CopticCalendar::clone() const michael@0: { michael@0: return new CopticCalendar(*this); michael@0: } michael@0: michael@0: const char* michael@0: CopticCalendar::getType() const michael@0: { michael@0: return "coptic"; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Calendar framework michael@0: //------------------------------------------------------------------------- michael@0: michael@0: int32_t michael@0: CopticCalendar::handleGetExtendedYear() michael@0: { michael@0: int32_t eyear; michael@0: if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { michael@0: eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 michael@0: } else { michael@0: // The year defaults to the epoch start, the era to CE michael@0: int32_t era = internalGet(UCAL_ERA, CE); michael@0: if (era == BCE) { michael@0: eyear = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year michael@0: } else { michael@0: eyear = internalGet(UCAL_YEAR, 1); // Default to year 1 michael@0: } michael@0: } michael@0: return eyear; michael@0: } michael@0: michael@0: void michael@0: CopticCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) michael@0: { michael@0: int32_t eyear, month, day, era, year; michael@0: jdToCE(julianDay, getJDEpochOffset(), eyear, month, day); michael@0: michael@0: if (eyear <= 0) { michael@0: era = BCE; michael@0: year = 1 - eyear; michael@0: } else { michael@0: era = CE; michael@0: year = eyear; michael@0: } michael@0: michael@0: internalSet(UCAL_EXTENDED_YEAR, eyear); michael@0: internalSet(UCAL_ERA, era); michael@0: internalSet(UCAL_YEAR, year); michael@0: internalSet(UCAL_MONTH, month); michael@0: internalSet(UCAL_DATE, day); michael@0: internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day); michael@0: } michael@0: michael@0: /** michael@0: * The system maintains a static default century start date and Year. They are michael@0: * initialized the first time they are used. Once the system default century date michael@0: * and year are set, they do not change. michael@0: */ michael@0: static UDate gSystemDefaultCenturyStart = DBL_MIN; michael@0: static int32_t gSystemDefaultCenturyStartYear = -1; michael@0: static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; michael@0: michael@0: michael@0: static void U_CALLCONV initializeSystemDefaultCentury() { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: CopticCalendar calendar(Locale("@calendar=coptic"), status); michael@0: if (U_SUCCESS(status)) { michael@0: calendar.setTime(Calendar::getNow(), status); michael@0: calendar.add(UCAL_YEAR, -80, status); michael@0: gSystemDefaultCenturyStart = calendar.getTime(status); michael@0: gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); michael@0: } michael@0: // We have no recourse upon failure unless we want to propagate the failure michael@0: // out. michael@0: } michael@0: michael@0: UDate michael@0: CopticCalendar::defaultCenturyStart() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t michael@0: CopticCalendar::defaultCenturyStartYear() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: michael@0: int32_t michael@0: CopticCalendar::getJDEpochOffset() const michael@0: { michael@0: return COPTIC_JD_EPOCH_OFFSET; michael@0: } michael@0: michael@0: michael@0: #if 0 michael@0: // We do not want to introduce this API in ICU4C. michael@0: // It was accidentally introduced in ICU4J as a public API. michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Calendar system Conversion methods... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: int32_t michael@0: CopticCalendar::copticToJD(int32_t year, int32_t month, int32_t day) michael@0: { michael@0: return CECalendar::ceToJD(year, month, day, COPTIC_JD_EPOCH_OFFSET); michael@0: } michael@0: #endif michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: //eof