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 - 2009, 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 "cecal.h"
15 #include "gregoimp.h" //Math
16 #include "cstring.h"
17
18 U_NAMESPACE_BEGIN
19
20 static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
21 // Minimum Greatest Least Maximum
22 // Minimum Maximum
23 { 0, 0, 1, 1}, // ERA
24 { 1, 1, 5000000, 5000000}, // YEAR
25 { 0, 0, 12, 12}, // MONTH
26 { 1, 1, 52, 53}, // WEEK_OF_YEAR
27 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
28 { 1, 1, 5, 30}, // DAY_OF_MONTH
29 { 1, 1, 365, 366}, // DAY_OF_YEAR
30 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
31 { -1, -1, 1, 5}, // DAY_OF_WEEK_IN_MONTH
32 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
33 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
34 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
35 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
36 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
37 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
38 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
39 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
40 { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY
41 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
42 { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR
43 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
44 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
45 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
46 { 0, 0, 12, 12}, // ORDINAL_MONTH
47 };
48
49 //-------------------------------------------------------------------------
50 // Constructors...
51 //-------------------------------------------------------------------------
52
CECalendar(const Locale & aLocale,UErrorCode & success)53 CECalendar::CECalendar(const Locale& aLocale, UErrorCode& success)
54 : Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)
55 {
56 setTimeInMillis(getNow(), success);
57 }
58
CECalendar(const CECalendar & other)59 CECalendar::CECalendar (const CECalendar& other)
60 : Calendar(other)
61 {
62 }
63
~CECalendar()64 CECalendar::~CECalendar()
65 {
66 }
67
68 CECalendar&
operator =(const CECalendar & right)69 CECalendar::operator=(const CECalendar& right)
70 {
71 Calendar::operator=(right);
72 return *this;
73 }
74
75 //-------------------------------------------------------------------------
76 // Calendar framework
77 //-------------------------------------------------------------------------
78
79 int64_t
handleComputeMonthStart(int32_t eyear,int32_t emonth,UBool,UErrorCode &) const80 CECalendar::handleComputeMonthStart(int32_t eyear,int32_t emonth, UBool /*useMonth*/, UErrorCode& /*status*/) const
81 {
82 int64_t year64 = eyear;
83 // handle month > 12, < 0 (e.g. from add/set)
84 if ( emonth >= 0 ) {
85 year64 += emonth/13;
86 emonth %= 13;
87 } else {
88 ++emonth;
89 year64 += emonth/13 - 1;
90 emonth = emonth%13 + 12;
91 }
92
93 return (
94 getJDEpochOffset() // difference from Julian epoch to 1,1,1
95 + 365LL * year64 // number of days from years
96 + ClockMath::floorDivideInt64(year64, 4LL) // extra day of leap year
97 + 30 * emonth // number of days from months (months are 0-based)
98 - 1 // number of days for present month (1 based)
99 );
100 }
101
102 int32_t
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const103 CECalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
104 {
105 return LIMITS[field][limitType];
106 }
107
108 //-------------------------------------------------------------------------
109 // Calendar system Conversion methods...
110 //-------------------------------------------------------------------------
111
112 void
jdToCE(int32_t julianDay,int32_t jdEpochOffset,int32_t & year,int32_t & month,int32_t & day,UErrorCode & status)113 CECalendar::jdToCE(int32_t julianDay, int32_t jdEpochOffset, int32_t& year, int32_t& month, int32_t& day, UErrorCode& status)
114 {
115 int32_t c4; // number of 4 year cycle (1461 days)
116 int32_t r4; // remainder of 4 year cycle, always positive
117
118 if (uprv_add32_overflow(julianDay, -jdEpochOffset, &julianDay)) {
119 status = U_ILLEGAL_ARGUMENT_ERROR;
120 return;
121 }
122 c4 = ClockMath::floorDivide(julianDay, 1461, &r4);
123
124 year = 4 * c4 + (r4/365 - r4/1460); // 4 * <number of 4year cycle> + <years within the last cycle>
125
126 int32_t doy = (r4 == 1460) ? 365 : (r4 % 365); // days in present year
127
128 month = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month
129 day = (doy % 30) + 1; // 1-based days in a month
130 }
131
132 static const char* kMonthCode13 = "M13";
133
getTemporalMonthCode(UErrorCode & status) const134 const char* CECalendar::getTemporalMonthCode(UErrorCode& status) const {
135 if (get(UCAL_MONTH, status) == 12) {
136 return kMonthCode13;
137 }
138 return Calendar::getTemporalMonthCode(status);
139 }
140
141 void
setTemporalMonthCode(const char * code,UErrorCode & status)142 CECalendar::setTemporalMonthCode(const char* code, UErrorCode& status) {
143 if (U_FAILURE(status)) {
144 return;
145 }
146 if (uprv_strcmp(code, kMonthCode13) == 0) {
147 set(UCAL_MONTH, 12);
148 set(UCAL_IS_LEAP_MONTH, 0);
149 return;
150 }
151 Calendar::setTemporalMonthCode(code, status);
152 }
153
154 U_NAMESPACE_END
155
156 #endif /* #if !UCONFIG_NO_FORMATTING */
157 //eof
158