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 int32_t
handleComputeMonthStart(int32_t eyear,int32_t emonth,UBool) const80 CECalendar::handleComputeMonthStart(int32_t eyear,int32_t emonth, UBool /*useMonth*/) const
81 {
82 return ceToJD(eyear, emonth, 0, getJDEpochOffset());
83 }
84
85 int32_t
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const86 CECalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
87 {
88 return LIMITS[field][limitType];
89 }
90
91 UBool
haveDefaultCentury() const92 CECalendar::haveDefaultCentury() const
93 {
94 return true;
95 }
96
97 //-------------------------------------------------------------------------
98 // Calendar system Conversion methods...
99 //-------------------------------------------------------------------------
100 int32_t
ceToJD(int32_t year,int32_t month,int32_t date,int32_t jdEpochOffset)101 CECalendar::ceToJD(int32_t year, int32_t month, int32_t date, int32_t jdEpochOffset)
102 {
103 // handle month > 12, < 0 (e.g. from add/set)
104 if ( month >= 0 ) {
105 year += month/13;
106 month %= 13;
107 } else {
108 ++month;
109 year += month/13 - 1;
110 month = month%13 + 12;
111 }
112 return (int32_t) (
113 jdEpochOffset // difference from Julian epoch to 1,1,1
114 + 365 * year // number of days from years
115 + ClockMath::floorDivide(year, 4) // extra day of leap year
116 + 30 * month // number of days from months (months are 0-based)
117 + date - 1 // number of days for present month (1 based)
118 );
119 }
120
121 void
jdToCE(int32_t julianDay,int32_t jdEpochOffset,int32_t & year,int32_t & month,int32_t & day)122 CECalendar::jdToCE(int32_t julianDay, int32_t jdEpochOffset, int32_t& year, int32_t& month, int32_t& day)
123 {
124 int32_t c4; // number of 4 year cycle (1461 days)
125 int32_t r4; // remainder of 4 year cycle, always positive
126
127 c4 = ClockMath::floorDivide(julianDay - jdEpochOffset, 1461, &r4);
128
129 year = 4 * c4 + (r4/365 - r4/1460); // 4 * <number of 4year cycle> + <years within the last cycle>
130
131 int32_t doy = (r4 == 1460) ? 365 : (r4 % 365); // days in present year
132
133 month = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month
134 day = (doy % 30) + 1; // 1-based days in a month
135 }
136
137 static const char* kMonthCode13 = "M13";
138
getTemporalMonthCode(UErrorCode & status) const139 const char* CECalendar::getTemporalMonthCode(UErrorCode& status) const {
140 if (get(UCAL_MONTH, status) == 12) return kMonthCode13;
141 return Calendar::getTemporalMonthCode(status);
142 }
143
144 void
setTemporalMonthCode(const char * code,UErrorCode & status)145 CECalendar::setTemporalMonthCode(const char* code, UErrorCode& status) {
146 if (U_FAILURE(status)) return;
147 if (uprv_strcmp(code, kMonthCode13) == 0) {
148 set(UCAL_MONTH, 12);
149 set(UCAL_IS_LEAP_MONTH, 0);
150 return;
151 }
152 Calendar::setTemporalMonthCode(code, status);
153 }
154
155 U_NAMESPACE_END
156
157 #endif /* #if !UCONFIG_NO_FORMATTING */
158 //eof
159