1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2003 - 2013, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
21 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CopticCalendar)
23 static const int32_t COPTIC_JD_EPOCH_OFFSET = 1824665;
25 //-------------------------------------------------------------------------
27 //-------------------------------------------------------------------------
29 CopticCalendar::CopticCalendar(const Locale& aLocale, UErrorCode& success)
30 : CECalendar(aLocale, success)
34 CopticCalendar::CopticCalendar (const CopticCalendar& other)
39 CopticCalendar::~CopticCalendar()
44 CopticCalendar::clone() const
46 return new CopticCalendar(*this);
50 CopticCalendar::getType() const
55 //-------------------------------------------------------------------------
57 //-------------------------------------------------------------------------
60 CopticCalendar::handleGetExtendedYear()
63 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
64 eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
66 // The year defaults to the epoch start, the era to CE
67 int32_t era = internalGet(UCAL_ERA, CE);
69 eyear = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year
71 eyear = internalGet(UCAL_YEAR, 1); // Default to year 1
78 CopticCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)
80 int32_t eyear, month, day, era, year;
81 jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);
91 internalSet(UCAL_EXTENDED_YEAR, eyear);
92 internalSet(UCAL_ERA, era);
93 internalSet(UCAL_YEAR, year);
94 internalSet(UCAL_MONTH, month);
95 internalSet(UCAL_DATE, day);
96 internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);
100 * The system maintains a static default century start date and Year. They are
101 * initialized the first time they are used. Once the system default century date
102 * and year are set, they do not change.
104 static UDate gSystemDefaultCenturyStart = DBL_MIN;
105 static int32_t gSystemDefaultCenturyStartYear = -1;
106 static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER;
109 static void U_CALLCONV initializeSystemDefaultCentury() {
110 UErrorCode status = U_ZERO_ERROR;
111 CopticCalendar calendar(Locale("@calendar=coptic"), status);
112 if (U_SUCCESS(status)) {
113 calendar.setTime(Calendar::getNow(), status);
114 calendar.add(UCAL_YEAR, -80, status);
115 gSystemDefaultCenturyStart = calendar.getTime(status);
116 gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
118 // We have no recourse upon failure unless we want to propagate the failure
123 CopticCalendar::defaultCenturyStart() const
125 // lazy-evaluate systemDefaultCenturyStart
126 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
127 return gSystemDefaultCenturyStart;
131 CopticCalendar::defaultCenturyStartYear() const
133 // lazy-evaluate systemDefaultCenturyStart
134 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
135 return gSystemDefaultCenturyStartYear;
140 CopticCalendar::getJDEpochOffset() const
142 return COPTIC_JD_EPOCH_OFFSET;
147 // We do not want to introduce this API in ICU4C.
148 // It was accidentally introduced in ICU4J as a public API.
150 //-------------------------------------------------------------------------
151 // Calendar system Conversion methods...
152 //-------------------------------------------------------------------------
155 CopticCalendar::copticToJD(int32_t year, int32_t month, int32_t day)
157 return CECalendar::ceToJD(year, month, day, COPTIC_JD_EPOCH_OFFSET);
163 #endif /* #if !UCONFIG_NO_FORMATTING */