/src/icu/source/i18n/coptccal.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
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  |  |  | 
29  |  | CopticCalendar::CopticCalendar(const Locale& aLocale, UErrorCode& success)  | 
30  | 0  | : CECalendar(aLocale, success)  | 
31  | 0  | { | 
32  | 0  | }  | 
33  |  |  | 
34  |  | CopticCalendar::CopticCalendar (const CopticCalendar& other)   | 
35  | 0  | : CECalendar(other)  | 
36  | 0  | { | 
37  | 0  | }  | 
38  |  |  | 
39  |  | CopticCalendar::~CopticCalendar()  | 
40  | 0  | { | 
41  | 0  | }  | 
42  |  |  | 
43  |  | CopticCalendar*  | 
44  |  | CopticCalendar::clone() const  | 
45  | 0  | { | 
46  | 0  |     return new CopticCalendar(*this);  | 
47  | 0  | }  | 
48  |  |  | 
49  |  | const char*  | 
50  |  | CopticCalendar::getType() const  | 
51  | 0  | { | 
52  | 0  |     return "coptic";  | 
53  | 0  | }  | 
54  |  |  | 
55  |  | //-------------------------------------------------------------------------  | 
56  |  | // Calendar framework  | 
57  |  | //-------------------------------------------------------------------------  | 
58  |  |  | 
59  |  | int32_t  | 
60  |  | CopticCalendar::handleGetExtendedYear()  | 
61  | 0  | { | 
62  | 0  |     int32_t eyear;  | 
63  | 0  |     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { | 
64  | 0  |         eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1  | 
65  | 0  |     } else { | 
66  |  |         // The year defaults to the epoch start, the era to CE  | 
67  | 0  |         int32_t era = internalGet(UCAL_ERA, CE);  | 
68  | 0  |         if (era == BCE) { | 
69  | 0  |             eyear = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year  | 
70  | 0  |         } else { | 
71  | 0  |             eyear = internalGet(UCAL_YEAR, 1); // Default to year 1  | 
72  | 0  |         }  | 
73  | 0  |     }  | 
74  | 0  |     return eyear;  | 
75  | 0  | }  | 
76  |  |  | 
77  |  | void  | 
78  |  | CopticCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)  | 
79  | 0  | { | 
80  | 0  |     int32_t eyear, month, day, era, year;  | 
81  | 0  |     jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);  | 
82  |  | 
  | 
83  | 0  |     if (eyear <= 0) { | 
84  | 0  |         era = BCE;  | 
85  | 0  |         year = 1 - eyear;  | 
86  | 0  |     } else { | 
87  | 0  |         era = CE;  | 
88  | 0  |         year = eyear;  | 
89  | 0  |     }  | 
90  |  | 
  | 
91  | 0  |     internalSet(UCAL_EXTENDED_YEAR, eyear);  | 
92  | 0  |     internalSet(UCAL_ERA, era);  | 
93  | 0  |     internalSet(UCAL_YEAR, year);  | 
94  | 0  |     internalSet(UCAL_MONTH, month);  | 
95  | 0  |     internalSet(UCAL_DATE, day);  | 
96  | 0  |     internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);  | 
97  | 0  | }  | 
98  |  |  | 
99  |  | /**  | 
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.  | 
103  |  |  */  | 
104  |  | static UDate           gSystemDefaultCenturyStart       = DBL_MIN;  | 
105  |  | static int32_t         gSystemDefaultCenturyStartYear   = -1;  | 
106  |  | static icu::UInitOnce  gSystemDefaultCenturyInit        = U_INITONCE_INITIALIZER;  | 
107  |  |  | 
108  |  |  | 
109  | 0  | static void U_CALLCONV initializeSystemDefaultCentury() { | 
110  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
111  | 0  |     CopticCalendar calendar(Locale("@calendar=coptic"), status); | 
112  | 0  |     if (U_SUCCESS(status)) { | 
113  | 0  |         calendar.setTime(Calendar::getNow(), status);  | 
114  | 0  |         calendar.add(UCAL_YEAR, -80, status);  | 
115  | 0  |         gSystemDefaultCenturyStart = calendar.getTime(status);  | 
116  | 0  |         gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);  | 
117  | 0  |     }  | 
118  |  |     // We have no recourse upon failure unless we want to propagate the failure  | 
119  |  |     // out.  | 
120  | 0  | }  | 
121  |  |  | 
122  |  | UDate  | 
123  |  | CopticCalendar::defaultCenturyStart() const  | 
124  | 0  | { | 
125  |  |     // lazy-evaluate systemDefaultCenturyStart  | 
126  | 0  |     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);  | 
127  | 0  |     return gSystemDefaultCenturyStart;  | 
128  | 0  | }  | 
129  |  |  | 
130  |  | int32_t  | 
131  |  | CopticCalendar::defaultCenturyStartYear() const  | 
132  | 0  | { | 
133  |  |     // lazy-evaluate systemDefaultCenturyStart  | 
134  | 0  |     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);  | 
135  | 0  |     return gSystemDefaultCenturyStartYear;  | 
136  | 0  | }  | 
137  |  |  | 
138  |  |  | 
139  |  | int32_t  | 
140  |  | CopticCalendar::getJDEpochOffset() const  | 
141  | 0  | { | 
142  | 0  |     return COPTIC_JD_EPOCH_OFFSET;  | 
143  | 0  | }  | 
144  |  |  | 
145  |  |  | 
146  |  | #if 0  | 
147  |  | // We do not want to introduce this API in ICU4C.  | 
148  |  | // It was accidentally introduced in ICU4J as a public API.  | 
149  |  |  | 
150  |  | //-------------------------------------------------------------------------  | 
151  |  | // Calendar system Conversion methods...  | 
152  |  | //-------------------------------------------------------------------------  | 
153  |  |  | 
154  |  | int32_t  | 
155  |  | CopticCalendar::copticToJD(int32_t year, int32_t month, int32_t day)  | 
156  |  | { | 
157  |  |     return CECalendar::ceToJD(year, month, day, COPTIC_JD_EPOCH_OFFSET);  | 
158  |  | }  | 
159  |  | #endif  | 
160  |  |  | 
161  |  | U_NAMESPACE_END  | 
162  |  |  | 
163  |  | #endif /* #if !UCONFIG_NO_FORMATTING */  | 
164  |  | //eof  |