• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2003 - 2013, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 */
9 
10 #include "unicode/utypes.h"
11 
12 #if !UCONFIG_NO_FORMATTING
13 
14 #include "umutex.h"
15 #include "coptccal.h"
16 #include "cecal.h"
17 #include <float.h>
18 
19 U_NAMESPACE_BEGIN
20 
21 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CopticCalendar)
22 
23 static const int32_t COPTIC_JD_EPOCH_OFFSET  = 1824665;
24 
25 //-------------------------------------------------------------------------
26 // Constructors...
27 //-------------------------------------------------------------------------
28 
CopticCalendar(const Locale & aLocale,UErrorCode & success)29 CopticCalendar::CopticCalendar(const Locale& aLocale, UErrorCode& success)
30 : CECalendar(aLocale, success)
31 {
32 }
33 
CopticCalendar(const CopticCalendar & other)34 CopticCalendar::CopticCalendar (const CopticCalendar& other)
35 : CECalendar(other)
36 {
37 }
38 
~CopticCalendar()39 CopticCalendar::~CopticCalendar()
40 {
41 }
42 
43 CopticCalendar*
clone() const44 CopticCalendar::clone() const
45 {
46     return new CopticCalendar(*this);
47 }
48 
49 const char*
getType() const50 CopticCalendar::getType() const
51 {
52     return "coptic";
53 }
54 
55 //-------------------------------------------------------------------------
56 // Calendar framework
57 //-------------------------------------------------------------------------
58 
59 int32_t
handleGetExtendedYear()60 CopticCalendar::handleGetExtendedYear()
61 {
62     int32_t eyear;
63     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
64         eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
65     } else {
66         // The year defaults to the epoch start, the era to CE
67         int32_t era = internalGet(UCAL_ERA, CE);
68         if (era == BCE) {
69             eyear = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year
70         } else {
71             eyear = internalGet(UCAL_YEAR, 1); // Default to year 1
72         }
73     }
74     return eyear;
75 }
76 
77 void
handleComputeFields(int32_t julianDay,UErrorCode &)78 CopticCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)
79 {
80     int32_t eyear, month, day, era, year;
81     jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);
82 
83     if (eyear <= 0) {
84         era = BCE;
85         year = 1 - eyear;
86     } else {
87         era = CE;
88         year = eyear;
89     }
90 
91     internalSet(UCAL_EXTENDED_YEAR, eyear);
92     internalSet(UCAL_ERA, era);
93     internalSet(UCAL_YEAR, year);
94     internalSet(UCAL_MONTH, month);
95     internalSet(UCAL_ORDINAL_MONTH, month);
96     internalSet(UCAL_DATE, day);
97     internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);
98 }
99 
100 constexpr uint32_t kCopticRelatedYearDiff = 284;
101 
getRelatedYear(UErrorCode & status) const102 int32_t CopticCalendar::getRelatedYear(UErrorCode &status) const
103 {
104     int32_t year = get(UCAL_EXTENDED_YEAR, status);
105     if (U_FAILURE(status)) {
106         return 0;
107     }
108     return year + kCopticRelatedYearDiff;
109 }
110 
setRelatedYear(int32_t year)111 void CopticCalendar::setRelatedYear(int32_t year)
112 {
113     // set extended year
114     set(UCAL_EXTENDED_YEAR, year - kCopticRelatedYearDiff);
115 }
116 
117 /**
118  * The system maintains a static default century start date and Year.  They are
119  * initialized the first time they are used.  Once the system default century date
120  * and year are set, they do not change.
121  */
122 static UDate           gSystemDefaultCenturyStart       = DBL_MIN;
123 static int32_t         gSystemDefaultCenturyStartYear   = -1;
124 static icu::UInitOnce  gSystemDefaultCenturyInit        {};
125 
126 
initializeSystemDefaultCentury()127 static void U_CALLCONV initializeSystemDefaultCentury() {
128     UErrorCode status = U_ZERO_ERROR;
129     CopticCalendar calendar(Locale("@calendar=coptic"), status);
130     if (U_SUCCESS(status)) {
131         calendar.setTime(Calendar::getNow(), status);
132         calendar.add(UCAL_YEAR, -80, status);
133         gSystemDefaultCenturyStart = calendar.getTime(status);
134         gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
135     }
136     // We have no recourse upon failure unless we want to propagate the failure
137     // out.
138 }
139 
140 UDate
defaultCenturyStart() const141 CopticCalendar::defaultCenturyStart() const
142 {
143     // lazy-evaluate systemDefaultCenturyStart
144     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
145     return gSystemDefaultCenturyStart;
146 }
147 
148 int32_t
defaultCenturyStartYear() const149 CopticCalendar::defaultCenturyStartYear() const
150 {
151     // lazy-evaluate systemDefaultCenturyStart
152     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
153     return gSystemDefaultCenturyStartYear;
154 }
155 
156 
157 int32_t
getJDEpochOffset() const158 CopticCalendar::getJDEpochOffset() const
159 {
160     return COPTIC_JD_EPOCH_OFFSET;
161 }
162 
163 
164 #if 0
165 // We do not want to introduce this API in ICU4C.
166 // It was accidentally introduced in ICU4J as a public API.
167 
168 //-------------------------------------------------------------------------
169 // Calendar system Conversion methods...
170 //-------------------------------------------------------------------------
171 
172 int32_t
173 CopticCalendar::copticToJD(int32_t year, int32_t month, int32_t day)
174 {
175     return CECalendar::ceToJD(year, month, day, COPTIC_JD_EPOCH_OFFSET);
176 }
177 #endif
178 
179 U_NAMESPACE_END
180 
181 #endif /* #if !UCONFIG_NO_FORMATTING */
182 //eof
183