//------------------------------------------------------------------------- // Rolling and adding functions overridden from Calendar // // These methods call through to the default implementation in IBMCalendar // for most of the fields and only handle the unusual ones themselves. //-------------------------------------------------------------------------
/** *Addasignedamounttoaspecifiedfield,usingthiscalendar'srules. *Forexample,toaddthreedaystothecurrentdate,youcancall *<code>add(Calendar.DATE,3)</code>. *<p> *Whenaddingtocertainfields,thevaluesofotherfieldsmayconflictand *needtobechanged.Forexample,whenaddingonetothe{@link#MONTHMONTH}field *forthedate"30Av5758",the{@link#DAY_OF_MONTHDAY_OF_MONTH}field *mustbeadjustedsothattheresultis"29Elul5758"ratherthantheinvalid *"30Elul5758". *<p> *Thismethodisabletoaddto *allfieldsexceptfor{@link#ERAERA},{@link#DST_OFFSETDST_OFFSET}, *and{@link#ZONE_OFFSETZONE_OFFSET}. *<p> *<b>Note:</b>Youshouldalwaysuse{@link#rollroll}andaddrather *thanattemptingtoperformarithmeticoperationsdirectlyonthefields *ofa<tt>HebrewCalendar</tt>.Sincethe{@link#MONTHMONTH}fieldbehaves *discontinuouslyinnon-leapyears,simplearithmeticcangiveinvalidresults. *<p> *@paramfieldthetimefield. *@paramamounttheamounttoaddtothefield. * *@exceptionIllegalArgumentExceptionifthefieldisinvalidorrefers *toafieldthatcannotbehandledbythismethod. *@internal
*/ void HebrewCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status)
{ if(U_FAILURE(status)) { return;
} switch (field) { case UCAL_MONTH: case UCAL_ORDINAL_MONTH:
{ // We can't just do a set(MONTH, get(MONTH) + amount). The // reason is ADAR_1. Suppose amount is +2 and we land in // ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But // if amount is -2 and we land in ADAR_1, then we have to // bump the other way -- down to SHEVAT. - Alan 11/00
int64_t month = get(UCAL_MONTH, status);
int32_t year = get(UCAL_YEAR, status);
UBool acrossAdar1; if (amount > 0) {
acrossAdar1 = (month < ADAR_1); // started before ADAR_1?
month += amount; // We know there are total 235 months in every 19 years. To speed // up the iteration, we first fast forward in the multiple of 235 // months for 19 years before the iteration which check the leap year. if (month >= MONTHS_IN_CYCLE) { if (uprv_add32_overflow(year, (month / MONTHS_IN_CYCLE) * YEARS_IN_CYCLE, &year)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
month %= MONTHS_IN_CYCLE;
}
for (;;) { if (acrossAdar1 && month>=ADAR_1 && !isLeapYear(year)) {
++month;
} if (month <= ELUL) { break;
}
month -= ELUL+1;
++year;
acrossAdar1 = true;
}
} else {
acrossAdar1 = (month > ADAR_1); // started after ADAR_1?
month += amount; // We know there are total 235 months in every 19 years. To speed // up the iteration, we first fast forward in the multiple of 235 // months for 19 years before the iteration which check the leap year. if (month <= -MONTHS_IN_CYCLE) { if (uprv_add32_overflow(year, (month / MONTHS_IN_CYCLE) * YEARS_IN_CYCLE, &year)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
month %= MONTHS_IN_CYCLE;
} for (;;) { if (acrossAdar1 && month<=ADAR_1 && !isLeapYear(year)) {
--month;
} if (month >= 0) { break;
}
month += ELUL+1;
--year;
acrossAdar1 = true;
}
}
set(UCAL_MONTH, month);
set(UCAL_YEAR, year);
pinField(UCAL_DAY_OF_MONTH, status); break;
}
UBool leapYear = isLeapYear(year);
int32_t yearLength = monthsInYear(year);
int32_t newMonth = month + (amount % yearLength); // // If it's not a leap year and we're rolling past the missing month // of ADAR_1, we need to roll an extra month to make up for it. // if (!leapYear) { if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) {
newMonth++;
} elseif (amount < 0 && month > ADAR_1 && newMonth <= ADAR_1) {
newMonth--;
}
}
set(UCAL_MONTH, (newMonth + 13) % 13);
pinField(UCAL_DAY_OF_MONTH, status); return;
} default:
Calendar::roll(field, amount, status);
}
}
//------------------------------------------------------------------------- // Support methods //-------------------------------------------------------------------------
// Hebrew date calculations are performed in terms of days, hours, and // "parts" (or halakim), which are 1/1080 of an hour, or 3 1/3 seconds. staticconst int32_t HOUR_PARTS = 1080; staticconst int32_t DAY_PARTS = 24*HOUR_PARTS;
// An approximate value for the length of a lunar month. // It is used to calculate the approximate year and month of a given // absolute date. staticconst int32_t MONTH_DAYS = 29; staticconst int32_t MONTH_FRACT = 12*HOUR_PARTS + 793; staticconst int32_t MONTH_PARTS = MONTH_DAYS*DAY_PARTS + MONTH_FRACT;
// The time of the new moon (in parts) on 1 Tishri, year 1 (the epoch) // counting from noon on the day before. BAHARAD is an abbreviation of // Bet (Monday), Hey (5 hours from sunset), Resh-Daled (204). staticconst int32_t BAHARAD = 11*HOUR_PARTS + 204;
if (day == 0) { // # of months before year
int64_t months = ClockMath::floorDivideInt64(
(235LL * static_cast<int64_t>(year) - 234LL), 19LL);
int64_t frac = months * MONTH_FRACT + BAHARAD; // Fractional part of day #
day = months * 29LL + frac / DAY_PARTS; // Whole # part of calculation
frac = frac % DAY_PARTS; // Time of day
int32_t wd = (day % 7); // Day of week (0 == Monday)
if (wd == 2 || wd == 4 || wd == 6) { // If the 1st is on Sun, Wed, or Fri, postpone to the next day
day += 1;
wd = (day % 7);
} elseif (wd == 1 && frac > 15*HOUR_PARTS+204 && !HebrewCalendar::isLeapYear(year) ) { // If the new moon falls after 3:11:20am (15h204p from the previous noon) // on a Tuesday and it is not a leap year, postpone by 2 days. // This prevents 356-day years.
day += 2;
} elseif (wd == 0 && frac > 21*HOUR_PARTS+589 && HebrewCalendar::isLeapYear(year-1) ) { // If the new moon falls after 9:32:43 1/3am (21h589p from yesterday noon) // on a Monday and *last* year was a leap year, postpone by 1 day. // Prevents 382-day years.
day += 1;
} if (day > INT32_MAX || day < INT32_MIN) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
CalendarCache::put(&gCache, year, static_cast<int32_t>(day), status);
} // Out of range value is alread rejected before putting into cache.
U_ASSERT(INT32_MIN <= day && day <= INT32_MAX); return day;
}
/** *Returnsthelengthofthegivenmonthinthegivenyear *@internal
*/
int32_t HebrewCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month, UErrorCode& status) const { if(U_FAILURE(status)) { return0;
} // Resolve out-of-range months. This is necessary in order to // obtain the correct year. We correct to // a 12- or 13-month year (add/subtract 12 or 13, depending // on the year) but since we _always_ number from 0..12, and // the leap year determines whether or not month 5 (Adar 1) // is present, we allow 0..12 in any given year. while (month < 0) {
month += monthsInYear(--extendedYear);
} // Careful: allow 0..12 in all years while (month > 12) {
month -= monthsInYear(extendedYear++);
}
switch (month) { case HESHVAN: case KISLEV:
{ // These two month lengths can vary
int32_t type = yearType(extendedYear, status); if(U_FAILURE(status)) { return0;
} return MONTH_LENGTH[month][type];
}
default: // The rest are a fixed length return MONTH_LENGTH[month][0];
}
}
void HebrewCalendar::validateField(UCalendarDateFields field, UErrorCode &status) { if ((field == UCAL_MONTH || field == UCAL_ORDINAL_MONTH)
&& !isLeapYear(handleGetExtendedYear(status)) && internalGetMonth(status) == ADAR_1) { if (U_FAILURE(status)) { return;
}
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
Calendar::validateField(field, status);
} //------------------------------------------------------------------------- // Functions for converting from milliseconds to field values //-------------------------------------------------------------------------
/** *Subclassesmayoverridethismethodtocomputeseveralfields *specifictoeachcalendarsystem.Theseare: * *<ul><li>ERA *<li>YEAR *<li>MONTH *<li>DAY_OF_MONTH *<li>DAY_OF_YEAR *<li>EXTENDED_YEAR</ul> * *SubclassescanrefertotheDAY_OF_WEEKandDOW_LOCALfields, *whichwillbesetwhenthismethodiscalled.Subclassescan *alsocallthegetGregorianXxx()methodstoobtainGregorian *calendarequivalentsforthegivenJulianday. * *<p>Inaddition,subclassesshouldcomputeanysubclass-specific *fields,thatis,fieldsfromBASE_FIELD_COUNTto *getFieldCount()-1. *@internal
*/ void HebrewCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) { if (U_FAILURE(status)) { return;
}
int32_t d = julianDay - 347997; double m = ClockMath::floorDivide((d * static_cast<double>(DAY_PARTS)), static_cast<double>(MONTH_PARTS)); // Months (approx)
int32_t year = static_cast<int32_t>(ClockMath::floorDivide((19. * m + 234.), 235.) + 1.); // Years (approx)
int32_t ys = startOfYear(year, status); // 1st day of year if (U_FAILURE(status)) { return;
}
int32_t dayOfYear = (d - ys);
// Because of the postponement rules, it's possible to guess wrong. Fix it. while (dayOfYear < 1) {
year--;
ys = startOfYear(year, status); if (U_FAILURE(status)) { return;
}
dayOfYear = (d - ys);
}
// Now figure out which month we're in, and the date within that month
int32_t type = yearType(year, status); if (U_FAILURE(status)) { return;
}
UBool isLeap = isLeapYear(year);
int32_t month = 0;
int32_t momax = UPRV_LENGTHOF(MONTH_START); while (month < momax &&
dayOfYear > ( isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type] ) ) {
month++;
} if (month >= momax || month<=0) { // TODO: I found dayOfYear could be out of range when // a large value is set to julianDay. I patched startOfYear // to reduce the chace, but it could be still reproduced either // by startOfYear or other places. For now, we check // the month is in valid range to avoid out of array index // access problem here. However, we need to carefully review // the calendar implementation to check the extreme limit of // each calendar field and the code works well for any values // in the valid value range. -yoshito
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
month--; int dayOfMonth = dayOfYear - (isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type]);
internalSet(UCAL_ERA, 0); // Check out of bound year
int32_t min_year = handleGetLimit(UCAL_EXTENDED_YEAR, UCAL_LIMIT_MINIMUM); if (year < min_year) { if (!isLenient()) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
year = min_year;
}
int32_t max_year = handleGetLimit(UCAL_EXTENDED_YEAR, UCAL_LIMIT_MAXIMUM); if (max_year < year) { if (!isLenient()) {
status = U_ILLEGAL_ARGUMENT_ERROR; return;
}
year = max_year;
}
internalSet(UCAL_YEAR, year);
internalSet(UCAL_EXTENDED_YEAR, year);
int32_t ordinal_month = month; if (!isLeap && ordinal_month > ADAR_1) {
ordinal_month--;
}
internalSet(UCAL_ORDINAL_MONTH, ordinal_month);
internalSet(UCAL_MONTH, month);
internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
}
//------------------------------------------------------------------------- // Functions for converting from field values to milliseconds //-------------------------------------------------------------------------
/** *@internal
*/
int32_t HebrewCalendar::handleGetExtendedYear(UErrorCode& status ) { if (U_FAILURE(status)) { return0;
} if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
} return internalGet(UCAL_YEAR, 1); // Default to year 1
}
/** *ReturnJDofstartofgivenmonth/year. *@internal
*/
int64_t HebrewCalendar::handleComputeMonthStart(
int32_t eyear, int32_t month, UBool /*useMonth*/, UErrorCode& status) const { if (U_FAILURE(status)) { return0;
} // Resolve out-of-range months. This is necessary in order to // obtain the correct year. We correct to // a 12- or 13-month year (add/subtract 12 or 13, depending // on the year) but since we _always_ number from 0..12, and // the leap year determines whether or not month 5 (Adar 1) // is present, we allow 0..12 in any given year.
// The month could be in large value, we first roll 235 months to 19 years // before the while loop. if (month <= -MONTHS_IN_CYCLE || month >= MONTHS_IN_CYCLE) { if (uprv_add32_overflow(eyear, (month / MONTHS_IN_CYCLE) * YEARS_IN_CYCLE, &eyear)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
month %= MONTHS_IN_CYCLE;
} while (month < 0) { if (uprv_add32_overflow(eyear, -1, &eyear) ||
uprv_add32_overflow(month, monthsInYear(eyear), &month)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
} // Careful: allow 0..12 in all years while (month > 12) { if (uprv_add32_overflow(month, -monthsInYear(eyear), &month) ||
uprv_add32_overflow(eyear, 1, &eyear)) {
status = U_ILLEGAL_ARGUMENT_ERROR; return0;
}
}
int64_t day = startOfYear(eyear, status);
if(U_FAILURE(status)) { return0;
}
if (month != 0) {
int32_t type = yearType(eyear, status); if (U_FAILURE(status)) { return0;
} if (isLeapYear(eyear)) {
day += LEAP_MONTH_START[month][type];
} else {
day += MONTH_START[month][type];
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.