| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#include "mozilla/intl/calendar/ICU4XChineseBasedCalendar.h" |
| 6 |
|
| 7 |
namespace mozilla::intl::calendar { |
| 8 |
|
| 9 |
ICU4XChineseBasedCalendar::ICU4XChineseBasedCalendar( |
| 10 |
capi::ICU4XAnyCalendarKind kind, const icu::Locale& locale, |
| 11 |
UErrorCode& success) |
| 12 |
: ICU4XCalendar(kind, locale, success) {} |
| 13 |
|
| 14 |
ICU4XChineseBasedCalendar::ICU4XChineseBasedCalendar( |
| 15 |
capi::ICU4XAnyCalendarKind kind, const icu::TimeZone& timeZone, |
| 16 |
const icu::Locale& locale, UErrorCode& success) |
| 17 |
: ICU4XCalendar(kind, timeZone, locale, success) {} |
| 18 |
|
| 19 |
ICU4XChineseBasedCalendar::ICU4XChineseBasedCalendar( |
| 20 |
const ICU4XChineseBasedCalendar& other) |
| 21 |
: ICU4XCalendar(other) {} |
| 22 |
|
| 23 |
ICU4XChineseBasedCalendar::~ICU4XChineseBasedCalendar() = default; |
| 24 |
|
| 25 |
//////////////////////////////////////////// |
| 26 |
// ICU4XCalendar implementation overrides // |
| 27 |
//////////////////////////////////////////// |
| 28 |
|
| 29 |
bool ICU4XChineseBasedCalendar::hasLeapMonths() const { return true; } |
| 30 |
|
| 31 |
bool ICU4XChineseBasedCalendar::hasMonthCode(MonthCode monthCode) const { |
| 32 |
return monthCode.ordinal() <= 12; |
| 33 |
} |
| 34 |
|
| 35 |
bool ICU4XChineseBasedCalendar::requiresFallbackForExtendedYear( |
| 36 |
int32_t year) const { |
| 37 |
// Same limits as in js/src/builtin/temporal/Calendar.cpp. |
| 38 |
return std::abs(year) > 10'000; |
| 39 |
} |
| 40 |
|
| 41 |
bool ICU4XChineseBasedCalendar::requiresFallbackForGregorianYear( |
| 42 |
int32_t year) const { |
| 43 |
// Same limits as in js/src/builtin/temporal/Calendar.cpp. |
| 44 |
return std::abs(year) > 10'000; |
| 45 |
} |
| 46 |
|
| 47 |
//////////////////////////////////////////// |
| 48 |
// icu::Calendar implementation overrides // |
| 49 |
//////////////////////////////////////////// |
| 50 |
|
| 51 |
bool ICU4XChineseBasedCalendar::inTemporalLeapYear(UErrorCode& status) const { |
| 52 |
int32_t days = getActualMaximum(UCAL_DAY_OF_YEAR, status); |
| 53 |
if (U_FAILURE(status)) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
constexpr int32_t maxDaysInMonth = 30; |
| 58 |
constexpr int32_t monthsInNonLeapYear = 12; |
| 59 |
return days > (monthsInNonLeapYear * maxDaysInMonth); |
| 60 |
} |
| 61 |
|
| 62 |
int32_t ICU4XChineseBasedCalendar::getRelatedYear(UErrorCode& status) const { |
| 63 |
int32_t year = get(UCAL_EXTENDED_YEAR, status); |
| 64 |
if (U_FAILURE(status)) { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
return year + relatedYearDifference(); |
| 68 |
} |
| 69 |
|
| 70 |
void ICU4XChineseBasedCalendar::setRelatedYear(int32_t year) { |
| 71 |
set(UCAL_EXTENDED_YEAR, year - relatedYearDifference()); |
| 72 |
} |
| 73 |
|
| 74 |
void ICU4XChineseBasedCalendar::handleComputeFields(int32_t julianDay, |
| 75 |
UErrorCode& status) { |
| 76 |
int32_t gyear = getGregorianYear(); |
| 77 |
|
| 78 |
// Use the fallback calendar for years outside the range supported by ICU4X. |
| 79 |
if (requiresFallbackForGregorianYear(gyear)) { |
| 80 |
handleComputeFieldsFromFallback(julianDay, status); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
int32_t gmonth = getGregorianMonth() + 1; |
| 85 |
int32_t gday = getGregorianDayOfMonth(); |
| 86 |
|
| 87 |
MOZ_ASSERT(1 <= gmonth && gmonth <= 12); |
| 88 |
MOZ_ASSERT(1 <= gday && gday <= 31); |
| 89 |
|
| 90 |
auto date = createICU4XDate(ISODate{gyear, gmonth, gday}, status); |
| 91 |
if (U_FAILURE(status)) { |
| 92 |
return; |
| 93 |
} |
| 94 |
MOZ_ASSERT(date); |
| 95 |
|
| 96 |
MonthCode monthCode = monthCodeFrom(date.get(), status); |
| 97 |
if (U_FAILURE(status)) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
int32_t extendedYear = capi::ICU4XDate_year_in_era(date.get()); |
| 102 |
int32_t month = capi::ICU4XDate_ordinal_month(date.get()); |
| 103 |
int32_t dayOfMonth = capi::ICU4XDate_day_of_month(date.get()); |
| 104 |
int32_t dayOfYear = capi::ICU4XDate_day_of_year(date.get()); |
| 105 |
|
| 106 |
MOZ_ASSERT(1 <= month && month <= 13); |
| 107 |
MOZ_ASSERT(1 <= dayOfMonth && dayOfMonth <= 30); |
| 108 |
MOZ_ASSERT(1 <= dayOfYear && dayOfYear <= (13 * 30)); |
| 109 |
|
| 110 |
// Compute the cycle and year of cycle relative to the Chinese calendar, even |
| 111 |
// when this is the Dangi calendar. |
| 112 |
int32_t chineseExtendedYear = |
| 113 |
extendedYear + relatedYearDifference() - chineseRelatedYearDiff; |
| 114 |
int32_t cycle_year = chineseExtendedYear - 1; |
| 115 |
int32_t cycle = FloorDiv(cycle_year, 60); |
| 116 |
int32_t yearOfCycle = cycle_year - (cycle * 60); |
| 117 |
|
| 118 |
internalSet(UCAL_ERA, cycle + 1); |
| 119 |
internalSet(UCAL_YEAR, yearOfCycle + 1); |
| 120 |
internalSet(UCAL_EXTENDED_YEAR, extendedYear); |
| 121 |
internalSet(UCAL_MONTH, monthCode.ordinal() - 1); |
| 122 |
internalSet(UCAL_ORDINAL_MONTH, month - 1); |
| 123 |
internalSet(UCAL_IS_LEAP_MONTH, monthCode.isLeapMonth() ? 1 : 0); |
| 124 |
internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); |
| 125 |
internalSet(UCAL_DAY_OF_YEAR, dayOfYear); |
| 126 |
} |
| 127 |
|
| 128 |
// Limits table copied from i18n/chnsecal.cpp. Licensed under: |
| 129 |
// |
| 130 |
// © 2016 and later: Unicode, Inc. and others. |
| 131 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 132 |
static const int32_t CHINESE_CALENDAR_LIMITS[UCAL_FIELD_COUNT][4] = { |
| 133 |
// clang-format off |
| 134 |
// Minimum Greatest Least Maximum |
| 135 |
// Minimum Maximum |
| 136 |
{ 1, 1, 83333, 83333}, // ERA |
| 137 |
{ 1, 1, 60, 60}, // YEAR |
| 138 |
{ 0, 0, 11, 11}, // MONTH |
| 139 |
{ 1, 1, 50, 55}, // WEEK_OF_YEAR |
| 140 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH |
| 141 |
{ 1, 1, 29, 30}, // DAY_OF_MONTH |
| 142 |
{ 1, 1, 353, 385}, // DAY_OF_YEAR |
| 143 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK |
| 144 |
{ -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH |
| 145 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM |
| 146 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR |
| 147 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY |
| 148 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE |
| 149 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND |
| 150 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND |
| 151 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET |
| 152 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET |
| 153 |
{ -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY |
| 154 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL |
| 155 |
{ -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR |
| 156 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY |
| 157 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY |
| 158 |
{ 0, 0, 1, 1}, // IS_LEAP_MONTH |
| 159 |
{ 0, 0, 11, 12}, // ORDINAL_MONTH |
| 160 |
// clang-format on |
| 161 |
}; |
| 162 |
|
| 163 |
int32_t ICU4XChineseBasedCalendar::handleGetLimit(UCalendarDateFields field, |
| 164 |
ELimitType limitType) const { |
| 165 |
return CHINESE_CALENDAR_LIMITS[field][limitType]; |
| 166 |
} |
| 167 |
|
| 168 |
// Field resolution table copied from i18n/chnsecal.cpp. Licensed under: |
| 169 |
// |
| 170 |
// © 2016 and later: Unicode, Inc. and others. |
| 171 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 172 |
const icu::UFieldResolutionTable |
| 173 |
ICU4XChineseBasedCalendar::CHINESE_DATE_PRECEDENCE[] = { |
| 174 |
// clang-format off |
| 175 |
{ |
| 176 |
{ UCAL_DAY_OF_MONTH, kResolveSTOP }, |
| 177 |
{ UCAL_WEEK_OF_YEAR, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 178 |
{ UCAL_WEEK_OF_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 179 |
{ UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 180 |
{ UCAL_WEEK_OF_YEAR, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 181 |
{ UCAL_WEEK_OF_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 182 |
{ UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 183 |
{ UCAL_DAY_OF_YEAR, kResolveSTOP }, |
| 184 |
{ kResolveRemap | UCAL_DAY_OF_MONTH, UCAL_IS_LEAP_MONTH, kResolveSTOP }, |
| 185 |
{ kResolveSTOP } |
| 186 |
}, |
| 187 |
{ |
| 188 |
{ UCAL_WEEK_OF_YEAR, kResolveSTOP }, |
| 189 |
{ UCAL_WEEK_OF_MONTH, kResolveSTOP }, |
| 190 |
{ UCAL_DAY_OF_WEEK_IN_MONTH, kResolveSTOP }, |
| 191 |
{ kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 192 |
{ kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 193 |
{ kResolveSTOP } |
| 194 |
}, |
| 195 |
{{kResolveSTOP}} |
| 196 |
// clang-format on |
| 197 |
}; |
| 198 |
|
| 199 |
const icu::UFieldResolutionTable* |
| 200 |
ICU4XChineseBasedCalendar::getFieldResolutionTable() const { |
| 201 |
return CHINESE_DATE_PRECEDENCE; |
| 202 |
} |
| 203 |
|
| 204 |
} // namespace mozilla::intl::calendar |