From f8339df7181b4deab941bec103b11bd2b2a8199c Mon Sep 17 00:00:00 2001 From: Andrew J Macheret Date: Thu, 25 Oct 2007 20:26:34 +0000 Subject: [PATCH] ICU-4081 Ported Chinese calendar from icu4j. Mostly working. X-SVN-Rev: 22853 --- icu4c/source/i18n/astro.cpp | 8 +- icu4c/source/i18n/astro.h | 4 +- icu4c/source/i18n/calendar.cpp | 10 +- icu4c/source/i18n/chnsecal.cpp | 851 +++++++++++++++++++++++- icu4c/source/i18n/chnsecal.h | 315 ++++++++- icu4c/source/i18n/gregocal.cpp | 3 +- icu4c/source/i18n/hebrwcal.cpp | 1 + icu4c/source/i18n/i18n.vcproj | 30 +- icu4c/source/i18n/indiancal.cpp | 3 +- icu4c/source/i18n/islamcal.cpp | 5 +- icu4c/source/i18n/persncal.cpp | 5 +- icu4c/source/i18n/ucln_in.h | 1 + icu4c/source/i18n/unicode/calendar.h | 9 + icu4c/source/i18n/unicode/ucal.h | 6 + icu4c/source/tools/ctestfw/udbgutil.cpp | 1 + 15 files changed, 1216 insertions(+), 36 deletions(-) diff --git a/icu4c/source/i18n/astro.cpp b/icu4c/source/i18n/astro.cpp index 566d498d904..32ae5d853ab 100644 --- a/icu4c/source/i18n/astro.cpp +++ b/icu4c/source/i18n/astro.cpp @@ -650,9 +650,9 @@ double CalendarAstronomer::SUMMER_SOLSTICE() { * @internal * @deprecated ICU 2.4. This class may be removed or modified. */ -/*double CalendarAstronomer::WINTER_SOLSTICE() { +double CalendarAstronomer::WINTER_SOLSTICE() { return ((CalendarAstronomer::PI*3)/2); -}*/ +} CalendarAstronomer::AngleFunc::~AngleFunc() {} @@ -1128,9 +1128,9 @@ double CalendarAstronomer::getMoonPhase() { * @internal * @deprecated ICU 2.4. This class may be removed or modified. */ -/*const CalendarAstronomer::MoonAge CalendarAstronomer::NEW_MOON() { +const CalendarAstronomer::MoonAge CalendarAstronomer::NEW_MOON() { return CalendarAstronomer::MoonAge(0); -}*/ +} /** * Constant representing the moon's first quarter. diff --git a/icu4c/source/i18n/astro.h b/icu4c/source/i18n/astro.h index 6c28f2e3e0e..efb5acc0da1 100644 --- a/icu4c/source/i18n/astro.h +++ b/icu4c/source/i18n/astro.h @@ -521,7 +521,7 @@ public: * Note: In this case, "winter" refers to the northern hemisphere's seasons. * @internal */ -// static double WINTER_SOLSTICE(); + static double WINTER_SOLSTICE(); /** * Find the next time at which the sun's ecliptic longitude will have @@ -596,7 +596,7 @@ public: * For use with {@link #getMoonTime getMoonTime} * @internal */ -// static const MoonAge NEW_MOON(); + static const MoonAge NEW_MOON(); /** * Constant representing the moon's first quarter. diff --git a/icu4c/source/i18n/calendar.cpp b/icu4c/source/i18n/calendar.cpp index 6a932f68e68..976309ac677 100644 --- a/icu4c/source/i18n/calendar.cpp +++ b/icu4c/source/i18n/calendar.cpp @@ -37,7 +37,7 @@ #include "hebrwcal.h" #include "persncal.h" #include "indiancal.h" -//#include "chnsecal.h" +#include "chnsecal.h" #include "unicode/calendar.h" #include "cpputils.h" #include "servloc.h" @@ -196,8 +196,8 @@ static Calendar *createStandardCalendar(char *calType, const Locale &canLoc, UEr return new HebrewCalendar(canLoc, status); } else if(!uprv_strcmp(calType, "persian")) { return new PersianCalendar(canLoc, status); - //} else if(!uprv_strcmp(calType, "chinese")) { - //return new ChineseCalendar(canLoc, status); + } else if(!uprv_strcmp(calType, "chinese")) { + return new ChineseCalendar(canLoc, status); } else if(!uprv_strcmp(calType, "indian")) { return new IndianCalendar(canLoc, status); } else { @@ -472,7 +472,8 @@ static const int32_t kCalendarLimits[UCAL_FIELD_COUNT][4] = { { 1, 1, 7, 7 }, // DOW_LOCAL {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // EXTENDED_YEAR { -0x7F000000, -0x7F000000, 0x7F000000, 0x7F000000 }, // JULIAN_DAY - { 0, 0, 24*kOneHour-1, 24*kOneHour-1 } // MILLISECONDS_IN_DAY + { 0, 0, 24*kOneHour-1, 24*kOneHour-1 }, // MILLISECONDS_IN_DAY + { 0, 0, 1, 1 }, // IS_LEAP_MONTH }; // Resource bundle tags read by this class @@ -2086,6 +2087,7 @@ int32_t Calendar::getLimit(UCalendarDateFields field, ELimitType limitType) cons case UCAL_DOW_LOCAL: case UCAL_JULIAN_DAY: case UCAL_MILLISECONDS_IN_DAY: + case UCAL_IS_LEAP_MONTH: return kCalendarLimits[field][limitType]; default: return handleGetLimit(field, limitType); diff --git a/icu4c/source/i18n/chnsecal.cpp b/icu4c/source/i18n/chnsecal.cpp index fda2aaca16e..868481c1c4a 100644 --- a/icu4c/source/i18n/chnsecal.cpp +++ b/icu4c/source/i18n/chnsecal.cpp @@ -1,13 +1,856 @@ /* ****************************************************************************** - * Copyright (C) 2003-2004, International Business Machines Corporation and * - * others. All Rights Reserved. * + * Copyright (C) 2007, International Business Machines Corporation + * and others. All Rights Reserved. ****************************************************************************** * + * File CHNSECAL.CPP * - ****************************************************************************** + * Modification History: + * + * Date Name Description + * 9/18/2007 ajmacher ported from java ChineseCalendar + ***************************************************************************** */ + #include "chnsecal.h" -// Placeholder for now until the implementation can be finished. +#if !UCONFIG_NO_FORMATTING + +#include "umutex.h" +#include +#include "gregoimp.h" // Math +#include "astro.h" // CalendarAstronomer +#include "uhash.h" +#include "ucln_in.h" + +// Debugging +#ifdef U_DEBUG_CHNSECAL +# include +# include +static void debug_chnsecal_loc(const char *f, int32_t l) +{ + fprintf(stderr, "%s:%d: ", f, l); +} + +static void debug_chnsecal_msg(const char *pat, ...) +{ + va_list ap; + va_start(ap, pat); + vfprintf(stderr, pat, ap); + fflush(stderr); +} +// must use double parens, i.e.: U_DEBUG_CHNSECAL_MSG(("four is: %d",4)); +#define U_DEBUG_CHNSECAL_MSG(x) {debug_chnsecal_loc(__FILE__,__LINE__);debug_chnsecal_msg x;} +#else +#define U_DEBUG_CHNSECAL_MSG(x) +#endif + + +// --- The cache -- +static UMTX astroLock = 0; // pod bay door lock +static U_NAMESPACE_QUALIFIER CalendarAstronomer *gChineseCalendarAstro = NULL; +static U_NAMESPACE_QUALIFIER CalendarCache *gChineseCalendarWinterSolsticeCache = NULL; +static U_NAMESPACE_QUALIFIER CalendarCache *gChineseCalendarNewYearCache = NULL; + + +U_CDECL_BEGIN +static UBool calendar_chinese_cleanup(void) { + if (gChineseCalendarAstro) { + delete gChineseCalendarAstro; + gChineseCalendarAstro = NULL; + } + if (gChineseCalendarWinterSolsticeCache) { + delete gChineseCalendarWinterSolsticeCache; + gChineseCalendarWinterSolsticeCache = NULL; + } + if (gChineseCalendarNewYearCache) { + delete gChineseCalendarNewYearCache; + gChineseCalendarNewYearCache = NULL; + } + umtx_destroy(&astroLock); + return TRUE; +} +U_CDECL_END + +U_NAMESPACE_BEGIN + + +// Implementation of the ChineseCalendar class + + +//------------------------------------------------------------------------- +// Constructors... +//------------------------------------------------------------------------- + + +Calendar* ChineseCalendar::clone() const { + return new ChineseCalendar(*this); +} + +ChineseCalendar::ChineseCalendar(const Locale& aLocale, UErrorCode& success) +: Calendar(TimeZone::createDefault(), aLocale, success) +{ + setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly. +} + +ChineseCalendar::ChineseCalendar(const ChineseCalendar& other) : Calendar(other) { +} + +ChineseCalendar::~ChineseCalendar() +{ +} + +const char *ChineseCalendar::getType() const { + return "chinese"; +} + +//------------------------------------------------------------------------- +// Minimum / Maximum access functions +//------------------------------------------------------------------------- + + +static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { + // Minimum Greatest Least Maximum + // Minimum Maximum + { 1, 1, 83333, 83333}, // ERA + { 1, 1, 70, 70}, // YEAR + { 0, 0, 11, 11}, // MONTH + { 1, 1, 50, 55}, // WEEK_OF_YEAR + { 1, 1, 4, 6}, // WEEK_OF_MONTH + { 1, 1, 29, 30}, // DAY_OF_MONTH + { 1, 1, 353, 385}, // DAY_OF_YEAR + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK + { -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET + { -5000001, -5000001, 5000001, 5000001}, // YEAR_WOY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL + { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH +}; + + +/** +* @draft ICU 2.4 +*/ +int32_t ChineseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { + return LIMITS[field][limitType]; +} + + +//---------------------------------------------------------------------- +// Calendar framework +//---------------------------------------------------------------------- + +/** + * Implement abstract Calendar method to return the extended year + * defined by the current fields. This will use either the ERA and + * YEAR field as the cycle and year-of-cycle, or the EXTENDED_YEAR + * field as the continuous year count, depending on which is newer. + * @stable ICU 2.8 + */ +int32_t ChineseCalendar::handleGetExtendedYear() { + int32_t year; + if (newestStamp(UCAL_ERA, UCAL_YEAR, kUnset) <= fStamp[UCAL_EXTENDED_YEAR]) { + year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 + } else { + int32_t cycle = internalGet(UCAL_ERA, 1) - 1; // 0-based cycle + year = cycle * 60 + internalGet(UCAL_YEAR, 1); + } + return year; +} + +/** + * Override Calendar method to return the number of days in the given + * extended year and month. + * + *

Note: This method also reads the IS_LEAP_MONTH field to determine + * whether or not the given month is a leap month. + * @stable ICU 2.8 + */ +int32_t ChineseCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) { + int32_t thisStart = handleComputeMonthStart(extendedYear, month, TRUE) - + kEpochStartAsJulianDay + 1; // Julian day -> local days + int32_t nextStart = newMoonNear(thisStart + SYNODIC_GAP, TRUE); + return nextStart - thisStart; +} + +/** + * Override Calendar to compute several fields specific to the Chinese + * calendar system. These are: + * + *

  • ERA + *
  • YEAR + *
  • MONTH + *
  • DAY_OF_MONTH + *
  • DAY_OF_YEAR + *
  • EXTENDED_YEAR
+ * + * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this + * method is called. The getGregorianXxx() methods return Gregorian + * calendar equivalents for the given Julian day. + * + *

Compute the ChineseCalendar-specific field IS_LEAP_MONTH. + * @stable ICU 2.8 + */ +void ChineseCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) { + + computeChineseFields(julianDay - kEpochStartAsJulianDay, // local days + getGregorianYear(), getGregorianMonth(), + TRUE); // set all fields +} + +/** + * Field resolution table that incorporates IS_LEAP_MONTH. + */ +const UFieldResolutionTable ChineseCalendar::CHINESE_DATE_PRECEDENCE[] = +{ + { + { UCAL_DAY_OF_MONTH, kResolveSTOP }, + { UCAL_WEEK_OF_YEAR, UCAL_DAY_OF_WEEK, kResolveSTOP }, + { UCAL_WEEK_OF_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, + { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, + { UCAL_WEEK_OF_YEAR, UCAL_DOW_LOCAL, kResolveSTOP }, + { UCAL_WEEK_OF_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, + { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, + { UCAL_DAY_OF_YEAR, kResolveSTOP }, + { kResolveRemap | UCAL_DAY_OF_MONTH, UCAL_IS_LEAP_MONTH, kResolveSTOP }, + { kResolveSTOP } + }, + { + { UCAL_WEEK_OF_YEAR, kResolveSTOP }, + { UCAL_WEEK_OF_MONTH, kResolveSTOP }, + { UCAL_DAY_OF_WEEK_IN_MONTH, kResolveSTOP }, + { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, + { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, + { kResolveSTOP } + }, + {{kResolveSTOP}} +}; + +/** + * Override Calendar to add IS_LEAP_MONTH to the field resolution + * table. + * @stable ICU 2.8 + */ +const UFieldResolutionTable* ChineseCalendar::getFieldResolutionTable() const { + return CHINESE_DATE_PRECEDENCE; +} + +/** + * Return the Julian day number of day before the first day of the + * given month in the given extended year. + * + *

Note: This method reads the IS_LEAP_MONTH field to determine + * whether the given month is a leap month. + * @param eyear the extended year + * @param month the zero-based month. The month is also determined + * by reading the IS_LEAP_MONTH field. + * @return the Julian day number of the day before the first + * day of the given month and year + * @stable ICU 2.8 + */ +int32_t ChineseCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const { + + ChineseCalendar *nonConstThis = (ChineseCalendar*)this; // cast away const + + // If the month is out of range, adjust it into range, and + // modify the extended year value accordingly. + if (month < 0 || month > 11) { + double m = month; + eyear += (int32_t)Math::floorDivide(m, 12.0, m); + month = (int32_t)m; + } + + int32_t gyear = eyear + CHINESE_EPOCH_YEAR - 1; // Gregorian year + int32_t theNewYear = newYear(gyear); + int32_t newMoon = newMoonNear(theNewYear + month * 29, TRUE); + + int32_t julianDay = newMoon + kEpochStartAsJulianDay; + + // Save fields for later restoration + int32_t saveMonth = internalGet(UCAL_MONTH); + int32_t saveIsLeapMonth = internalGet(UCAL_IS_LEAP_MONTH); + + // Ignore IS_LEAP_MONTH field if useMonth is false + int32_t isLeapMonth = useMonth ? saveIsLeapMonth : 0; + + UErrorCode status = U_ZERO_ERROR; + nonConstThis->computeGregorianFields(julianDay, status); + if (U_FAILURE(status)) + return 0; + + // This will modify the MONTH and IS_LEAP_MONTH fields (only) + nonConstThis->computeChineseFields(newMoon, getGregorianYear(), + getGregorianMonth(), FALSE); + + if (month != internalGet(UCAL_MONTH) || + isLeapMonth != internalGet(UCAL_IS_LEAP_MONTH)) { + newMoon = newMoonNear(newMoon + SYNODIC_GAP, TRUE); + julianDay = newMoon + kEpochStartAsJulianDay; + } + + nonConstThis->internalSet(UCAL_MONTH, saveMonth); + nonConstThis->internalSet(UCAL_IS_LEAP_MONTH, saveIsLeapMonth); + + return julianDay - 1; +} + + +/** + * Override Calendar to handle leap months properly. + * @stable ICU 2.8 + */ +void ChineseCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status) { + switch (field) { + case UCAL_MONTH: + if (amount != 0) { + int32_t dom = get(UCAL_DAY_OF_MONTH, status); + if (U_FAILURE(status)) break; + int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day + if (U_FAILURE(status)) break; + int32_t moon = day - dom + 1; // New moon + offsetMonth(moon, dom, amount); + } + break; + default: + Calendar::add(field, amount, status); + break; + } +} + +/** + * Override Calendar to handle leap months properly. + * @stable ICU 2.8 + */ +void ChineseCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) { + switch (field) { + case UCAL_MONTH: + if (amount != 0) { + int32_t dom = get(UCAL_DAY_OF_MONTH, status); + if (U_FAILURE(status)) break; + int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day + if (U_FAILURE(status)) break; + int32_t moon = day - dom + 1; // New moon (start of this month) + + // Note throughout the following: Months 12 and 1 are never + // followed by a leap month (D&R p. 185). + + // Compute the adjusted month number m. This is zero-based + // value from 0..11 in a non-leap year, and from 0..12 in a + // leap year. + int32_t m = get(UCAL_MONTH, status); // 0-based month + if (U_FAILURE(status)) break; + if (isLeapYear) { // (member variable) + if (get(UCAL_IS_LEAP_MONTH, status) == 1) { + ++m; + } else { + // Check for a prior leap month. (In the + // following, month 0 is the first month of the + // year.) Month 0 is never followed by a leap + // month, and we know month m is not a leap month. + // moon1 will be the start of month 0 if there is + // no leap month between month 0 and month m; + // otherwise it will be the start of month 1. + int moon1 = moon - + (int) (CalendarAstronomer::SYNODIC_MONTH * (m - 0.5)); + moon1 = newMoonNear(moon1, TRUE); + if (isLeapMonthBetween(moon1, moon)) { + ++m; + } + } + if (U_FAILURE(status)) break; + } + + // Now do the standard roll computation on m, with the + // allowed range of 0..n-1, where n is 12 or 13. + int32_t n = isLeapYear ? 13 : 12; // Months in this year + int32_t newM = (m + amount) % n; + if (newM < 0) { + newM += n; + } + + if (newM != m) { + offsetMonth(moon, dom, newM - m); + } + } + break; + default: + Calendar::roll(field, amount, status); + break; + } +} + + +//------------------------------------------------------------------ +// Support methods and constants +//------------------------------------------------------------------ + +/** + * The start year of the Chinese calendar, the 61st year of the reign + * of Huang Di. Some sources use the first year of his reign, + * resulting in EXTENDED_YEAR values 60 years greater and ERA (cycle) + * values one greater. + */ +const int32_t ChineseCalendar::CHINESE_EPOCH_YEAR = -2636; // Gregorian year + +/** + * The offset from GMT in milliseconds at which we perform astronomical + * computations. Some sources use a different historically accurate + * offset of GMT+7:45:40 for years before 1929; we do not do this. + */ +const double ChineseCalendar::CHINA_OFFSET = 8 * kOneHour; + +/** + * Value to be added or subtracted from the local days of a new moon to + * get close to the next or prior new moon, but not cross it. Must be + * >= 1 and < CalendarAstronomer.SYNODIC_MONTH. + */ +const int32_t ChineseCalendar::SYNODIC_GAP = 25; + +/** + * Convert local days to UTC epoch milliseconds. + * @param days days after January 1, 1970 0:00 Asia/Shanghai + * @return milliseconds after January 1, 1970 0:00 GMT + */ +double ChineseCalendar::daysToMillis(double days) { + return (days * kOneDay) - CHINA_OFFSET; +} + +/** + * Convert UTC epoch milliseconds to local days. + * @param millis milliseconds after January 1, 1970 0:00 GMT + * @return days after January 1, 1970 0:00 Asia/Shanghai + */ +double ChineseCalendar::millisToDays(double millis) { + return Math::floorDivide(millis + CHINA_OFFSET, kOneDay); +} + +//------------------------------------------------------------------ +// Astronomical computations +//------------------------------------------------------------------ + + +/** + * Return the major solar term on or after December 15 of the given + * Gregorian year, that is, the winter solstice of the given year. + * Computations are relative to Asia/Shanghai time zone. + * @param gyear a Gregorian year + * @return days after January 1, 1970 0:00 Asia/Shanghai of the + * winter solstice of the given year + */ +int32_t ChineseCalendar::winterSolstice(int32_t gyear) const { + + UErrorCode status = U_ZERO_ERROR; + int32_t cacheValue = CalendarCache::get(&gChineseCalendarWinterSolsticeCache, gyear, status); + + if (cacheValue == 0) { + // In books December 15 is used, but it fails for some years + // using our algorithms, e.g.: 1298 1391 1492 1553 1560. That + // is, winterSolstice(1298) starts search at Dec 14 08:00:00 + // PST 1298 with a final result of Dec 14 10:31:59 PST 1299. + double ms = daysToMillis(Grego::fieldsToDay(gyear, UCAL_DECEMBER, 1)); + + umtx_lock(&astroLock); + if(gChineseCalendarAstro == NULL) { + gChineseCalendarAstro = new CalendarAstronomer(); + ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup); + } + gChineseCalendarAstro->setTime(ms); + UDate solarLong = gChineseCalendarAstro->getSunTime(CalendarAstronomer::WINTER_SOLSTICE(), TRUE); + umtx_unlock(&astroLock); + + // Winter solstice is 270 degrees solar longitude aka Dongzhi + cacheValue = (int32_t)millisToDays(solarLong); + CalendarCache::put(&gChineseCalendarWinterSolsticeCache, gyear, cacheValue, status); + } + if(U_FAILURE(status)) { + cacheValue = 0; + } + return cacheValue; +} + +/** + * Return the closest new moon to the given date, searching either + * forward or backward in time. + * @param days days after January 1, 1970 0:00 Asia/Shanghai + * @param after if true, search for a new moon on or after the given + * date; otherwise, search for a new moon before it + * @return days after January 1, 1970 0:00 Asia/Shanghai of the nearest + * new moon after or before days + */ +int32_t ChineseCalendar::newMoonNear(double days, UBool after) const { + + umtx_lock(&astroLock); + if(gChineseCalendarAstro == NULL) { + gChineseCalendarAstro = new CalendarAstronomer(); + ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup); + } + gChineseCalendarAstro->setTime(daysToMillis(days)); + UDate newMoon = gChineseCalendarAstro->getMoonTime(CalendarAstronomer::NEW_MOON(), after); + umtx_unlock(&astroLock); + + return millisToDays(newMoon); +} + +/** + * Return the nearest integer number of synodic months between + * two dates. + * @param day1 days after January 1, 1970 0:00 Asia/Shanghai + * @param day2 days after January 1, 1970 0:00 Asia/Shanghai + * @return the nearest integer number of months between day1 and day2 + */ +int32_t ChineseCalendar::synodicMonthsBetween(int32_t day1, int32_t day2) const { + return (int32_t) (((day2 - day1) / CalendarAstronomer::SYNODIC_MONTH) + .5); +} + +/** + * Return the major solar term on or before a given date. This + * will be an integer from 1..12, with 1 corresponding to 330 degrees, + * 2 to 0 degrees, 3 to 30 degrees,..., and 12 to 300 degrees. + * @param days days after January 1, 1970 0:00 Asia/Shanghai + */ +int32_t ChineseCalendar::majorSolarTerm(int32_t days) const { + + umtx_lock(&astroLock); + if(gChineseCalendarAstro == NULL) { + gChineseCalendarAstro = new CalendarAstronomer(); + ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup); + } + gChineseCalendarAstro->setTime(daysToMillis(days)); + UDate solarLongitude = gChineseCalendarAstro->getSunLongitude(); + umtx_unlock(&astroLock); + + // Compute (floor(solarLongitude / (pi/6)) + 2) % 12 + int32_t term = ( ((int32_t)(6 * solarLongitude / CalendarAstronomer::PI)) + 2 ) % 12; + if (term < 1) { + term += 12; + } + return term; +} + +/** + * Return true if the given month lacks a major solar term. + * @param newMoon days after January 1, 1970 0:00 Asia/Shanghai of a new + * moon + */ +UBool ChineseCalendar::hasNoMajorSolarTerm(int32_t newMoon) const { + return majorSolarTerm(newMoon) == + majorSolarTerm(newMoonNear(newMoon + SYNODIC_GAP, TRUE)); +} + + +//------------------------------------------------------------------ +// Time to fields +//------------------------------------------------------------------ + +/** + * Return true if there is a leap month on or after month newMoon1 and + * at or before month newMoon2. + * @param newMoon1 days after January 1, 1970 0:00 Asia/Shanghai of a + * new moon + * @param newMoon2 days after January 1, 1970 0:00 Asia/Shanghai of a + * new moon + */ +UBool ChineseCalendar::isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const { + +#ifdef U_DEBUG_CHNSECAL + // This is only needed to debug the timeOfAngle divergence bug. + // Remove this later. Liu 11/9/00 + if (synodicMonthsBetween(newMoon1, newMoon2) >= 50) { + U_DEBUG_CHNSECAL_MSG(( + "isLeapMonthBetween(%d, %d): Invalid parameters", newMoon1, newMoon2 + )); + } +#endif + + return (newMoon2 >= newMoon1) && + (isLeapMonthBetween(newMoon1, newMoonNear(newMoon2 - SYNODIC_GAP, FALSE)) || + hasNoMajorSolarTerm(newMoon2)); +} + +/** + * Compute fields for the Chinese calendar system. This method can + * either set all relevant fields, as required by + * handleComputeFields(), or it can just set the MONTH and + * IS_LEAP_MONTH fields, as required by + * handleComputeMonthStart(). + * + *

As a side effect, this method sets {@link #isLeapYear}. + * @param days days after January 1, 1970 0:00 Asia/Shanghai of the + * date to compute fields for + * @param gyear the Gregorian year of the given date + * @param gmonth the Gregorian month of the given date + * @param setAllFields if true, set the EXTENDED_YEAR, ERA, YEAR, + * DAY_OF_MONTH, and DAY_OF_YEAR fields. In either case set the MONTH + * and IS_LEAP_MONTH fields. + */ +void ChineseCalendar::computeChineseFields(int32_t days, int32_t gyear, int32_t gmonth, + UBool setAllFields) { + + // Find the winter solstices before and after the target date. + // These define the boundaries of this Chinese year, specifically, + // the position of month 11, which always contains the solstice. + // We want solsticeBefore <= date < solsticeAfter. + int32_t solsticeBefore; + int32_t solsticeAfter = winterSolstice(gyear); + if (days < solsticeAfter) { + solsticeBefore = winterSolstice(gyear - 1); + } else { + solsticeBefore = solsticeAfter; + solsticeAfter = winterSolstice(gyear + 1); + } + + // Find the start of the month after month 11. This will be either + // the prior month 12 or leap month 11 (very rare). Also find the + // start of the following month 11. + int32_t firstMoon = newMoonNear(solsticeBefore + 1, TRUE); + int32_t lastMoon = newMoonNear(solsticeAfter + 1, FALSE); + int32_t thisMoon = newMoonNear(days + 1, FALSE); // Start of this month + // Note: isLeapYear is a member variable + isLeapYear = synodicMonthsBetween(firstMoon, lastMoon) == 12; + + int32_t month = synodicMonthsBetween(firstMoon, thisMoon); + if (isLeapYear && isLeapMonthBetween(firstMoon, thisMoon)) { + month--; + } + if (month < 1) { + month += 12; + } + + UBool isLeapMonth = isLeapYear && + hasNoMajorSolarTerm(thisMoon) && + !isLeapMonthBetween(firstMoon, newMoonNear(thisMoon - SYNODIC_GAP, FALSE)); + + internalSet(UCAL_MONTH, month-1); // Convert from 1-based to 0-based + internalSet(UCAL_IS_LEAP_MONTH, isLeapMonth?1:0); + + if (setAllFields) { + + int32_t year = gyear - CHINESE_EPOCH_YEAR; + if (month < 11 || + gmonth >= UCAL_JULY) { + year++; + } + int32_t dayOfMonth = days - thisMoon + 1; + + internalSet(UCAL_EXTENDED_YEAR, year); + + // 0->0,60 1->1,1 60->1,60 61->2,1 etc. + int32_t yearOfCycle; + int32_t cycle = Math::floorDivide(year - 1, 60, yearOfCycle); + internalSet(UCAL_ERA, cycle + 1); + internalSet(UCAL_YEAR, yearOfCycle + 1); + + internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); + + // Days will be before the first new year we compute if this + // date is in month 11, leap 11, 12. There is never a leap 12. + // New year computations are cached so this should be cheap in + // the long run. + int32_t theNewYear = newYear(gyear); + if (days < theNewYear) { + theNewYear = newYear(gyear-1); + } + internalSet(UCAL_DAY_OF_YEAR, days - theNewYear + 1); + } +} + + +//------------------------------------------------------------------ +// Fields to time +//------------------------------------------------------------------ + +/** + * Return the Chinese new year of the given Gregorian year. + * @param gyear a Gregorian year + * @return days after January 1, 1970 0:00 Asia/Shanghai of the + * Chinese new year of the given year (this will be a new moon) + */ +int32_t ChineseCalendar::newYear(int32_t gyear) const { + UErrorCode status = U_ZERO_ERROR; + int32_t cacheValue = CalendarCache::get(&gChineseCalendarNewYearCache, gyear, status); + + if (cacheValue == 0) { + + int32_t solsticeBefore= winterSolstice(gyear - 1); + int32_t solsticeAfter = winterSolstice(gyear); + int32_t newMoon1 = newMoonNear(solsticeBefore + 1, TRUE); + int32_t newMoon2 = newMoonNear(newMoon1 + SYNODIC_GAP, TRUE); + int32_t newMoon11 = newMoonNear(solsticeAfter + 1, FALSE); + + if (synodicMonthsBetween(newMoon1, newMoon11) == 12 && + (hasNoMajorSolarTerm(newMoon1) || hasNoMajorSolarTerm(newMoon2))) { + cacheValue = newMoonNear(newMoon2 + SYNODIC_GAP, TRUE); + } else { + cacheValue = newMoon2; + } + + CalendarCache::put(&gChineseCalendarNewYearCache, gyear, cacheValue, status); + } + if(U_FAILURE(status)) { + cacheValue = 0; + } + return cacheValue; +} + +/** + * Adjust this calendar to be delta months before or after a given + * start position, pinning the day of month if necessary. The start + * position is given as a local days number for the start of the month + * and a day-of-month. Used by add() and roll(). + * @param newMoon the local days of the first day of the month of the + * start position (days after January 1, 1970 0:00 Asia/Shanghai) + * @param dom the 1-based day-of-month of the start position + * @param delta the number of months to move forward or backward from + * the start position + */ +void ChineseCalendar::offsetMonth(int32_t newMoon, int32_t dom, int32_t delta) { + UErrorCode status = U_ZERO_ERROR; + + // Move to the middle of the month before our target month. + newMoon += (int32_t) (CalendarAstronomer::SYNODIC_MONTH * (delta - 0.5)); + + // Search forward to the target month's new moon + newMoon = newMoonNear(newMoon, TRUE); + + // Find the target dom + int32_t jd = newMoon + kEpochStartAsJulianDay - 1 + dom; + + // Pin the dom. In this calendar all months are 29 or 30 days + // so pinning just means handling dom 30. + if (dom > 29) { + set(UCAL_JULIAN_DAY, jd-1, status); + if (U_FAILURE(status)) return; + // TODO Fix this. We really shouldn't ever have to + // explicitly call complete(). This is either a bug in + // this method, in ChineseCalendar, or in + // Calendar.getActualMaximum(). I suspect the last. + complete(status); + if (U_FAILURE(status)) return; + if (getActualMaximum(UCAL_DAY_OF_MONTH, status) >= dom) { + if (U_FAILURE(status)) return; + set(UCAL_JULIAN_DAY, jd, status); + if (U_FAILURE(status)) return; + } + } else { + set(UCAL_JULIAN_DAY, jd, status); + if (U_FAILURE(status)) return; + } +} + + +UBool +ChineseCalendar::inDaylightTime(UErrorCode& status) const +{ + // copied from GregorianCalendar + if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) + return FALSE; + + // Force an update of the state of the Calendar. + ((ChineseCalendar*)this)->complete(status); // cast away const + + return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE); +} + +// default century +const UDate ChineseCalendar::fgSystemDefaultCentury = DBL_MIN; +const int32_t ChineseCalendar::fgSystemDefaultCenturyYear = -1; + +UDate ChineseCalendar::fgSystemDefaultCenturyStart = DBL_MIN; +int32_t ChineseCalendar::fgSystemDefaultCenturyStartYear = -1; + + +UBool ChineseCalendar::haveDefaultCentury() const +{ + return TRUE; +} + +UDate ChineseCalendar::defaultCenturyStart() const +{ + return internalGetDefaultCenturyStart(); +} + +int32_t ChineseCalendar::defaultCenturyStartYear() const +{ + return internalGetDefaultCenturyStartYear(); +} + +UDate +ChineseCalendar::internalGetDefaultCenturyStart() const +{ + // lazy-evaluate systemDefaultCenturyStart + UBool needsUpdate; + UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate); + + if (needsUpdate) { + initializeSystemDefaultCentury(); + } + + // use defaultCenturyStart unless it's the flag value; + // then use systemDefaultCenturyStart + + return fgSystemDefaultCenturyStart; +} + +int32_t +ChineseCalendar::internalGetDefaultCenturyStartYear() const +{ + // lazy-evaluate systemDefaultCenturyStartYear + UBool needsUpdate; + UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate); + + if (needsUpdate) { + initializeSystemDefaultCentury(); + } + + // use defaultCenturyStart unless it's the flag value; + // then use systemDefaultCenturyStartYear + + return fgSystemDefaultCenturyStartYear; +} + +void +ChineseCalendar::initializeSystemDefaultCentury() +{ + // initialize systemDefaultCentury and systemDefaultCenturyYear based + // on the current time. They'll be set to 80 years before + // the current time. + // No point in locking as it should be idempotent. + if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) + { + UErrorCode status = U_ZERO_ERROR; + ChineseCalendar calendar(Locale("@calendar=chinese"),status); + if (U_SUCCESS(status)) + { + calendar.setTime(Calendar::getNow(), status); + calendar.add(UCAL_YEAR, -80, status); + UDate newStart = calendar.getTime(status); + int32_t newYear = calendar.get(UCAL_YEAR, status); + { + umtx_lock(NULL); + fgSystemDefaultCenturyStart = newStart; + fgSystemDefaultCenturyStartYear = newYear; + umtx_unlock(NULL); + } + } + // We have no recourse upon failure unless we want to propagate the failure + // out. + } +} + +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ChineseCalendar) + +U_NAMESPACE_END + +#endif diff --git a/icu4c/source/i18n/chnsecal.h b/icu4c/source/i18n/chnsecal.h index 9d5a9f7709e..b606ee90e36 100644 --- a/icu4c/source/i18n/chnsecal.h +++ b/icu4c/source/i18n/chnsecal.h @@ -1,11 +1,316 @@ /* - ****************************************************************************** - * Copyright (C) 1996-2003, International Business Machines Corporation and * - * others. All Rights Reserved. * + ******************************************************************************** + * Copyright (C) 2007, International Business Machines Corporation + * and others. All Rights Reserved. ****************************************************************************** * + * File CHNSECAL.H * - ************************************************************************** + * Modification History: + * + * Date Name Description + * 9/18/2007 ajmacher ported from java ChineseCalendar + ***************************************************************************** */ -// placeholder +#ifndef CHNSECAL_H +#define CHNSECAL_H + +#include "unicode/utypes.h" + +#if !UCONFIG_NO_FORMATTING + +#include "unicode/calendar.h" + +U_NAMESPACE_BEGIN + +/** + * ChineseCalendar is a concrete subclass of {@link Calendar} + * that implements a traditional Chinese calendar. The traditional Chinese + * calendar is a lunisolar calendar: Each month starts on a new moon, and + * the months are numbered according to solar events, specifically, to + * guarantee that month 11 always contains the winter solstice. In order + * to accomplish this, leap months are inserted in certain years. Leap + * months are numbered the same as the month they follow. The decision of + * which month is a leap month depends on the relative movements of the sun + * and moon. + * + *

This class defines one addition field beyond those defined by + * Calendar: The IS_LEAP_MONTH field takes the + * value of 0 for normal months, or 1 for leap months. + * + *

All astronomical computations are performed with respect to a time + * zone of GMT+8:00 and a longitude of 120 degrees east. Although some + * calendars implement a historically more accurate convention of using + * Beijing's local longitude (116 degrees 25 minutes east) and time zone + * (GMT+7:45:40) for dates before 1929, we do not implement this here. + * + *

Years are counted in two different ways in the Chinese calendar. The + * first method is by sequential numbering from the 61st year of the reign + * of Huang Di, 2637 BCE, which is designated year 1 on the Chinese + * calendar. The second method uses 60-year cycles from the same starting + * point, which is designated year 1 of cycle 1. In this class, the + * EXTENDED_YEAR field contains the sequential year count. + * The ERA field contains the cycle number, and the + * YEAR field contains the year of the cycle, a value between + * 1 and 60. + * + *

There is some variation in what is considered the starting point of + * the calendar, with some sources starting in the first year of the reign + * of Huang Di, rather than the 61st. This gives continuous year numbers + * 60 years greater and cycle numbers one greater than what this class + * implements. + * + *

Because ChineseCalendar defines an additional field and + * redefines the way the ERA field is used, it requires a new + * format class, ChineseDateFormat. As always, use the + * methods DateFormat.getXxxInstance(Calendar cal,...) to + * obtain a formatter for this calendar. + * + *

References:

+ * + *

+ * This class should not be subclassed.

+ *

+ * ChineseCalendar usually should be instantiated using + * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a ULocale + * with the tag "@calendar=chinese".

+ * + * @see com.ibm.icu.text.ChineseDateFormat + * @see com.ibm.icu.util.Calendar + * @author Alan Liu + * @stable ICU 2.8 + */ +class ChineseCalendar : public Calendar { + public: + //------------------------------------------------------------------------- + // Constructors... + //------------------------------------------------------------------------- + + /** + * Constructs an ChineseCalendar based on the current time in the default time zone + * with the given locale. + * + * @param aLocale The given locale. + * @param success Indicates the status of ChineseCalendar object construction. + * Returns U_ZERO_ERROR if constructed successfully. + * @internal + */ + ChineseCalendar(const Locale& aLocale, UErrorCode &success); + + /** + * Copy Constructor + * @internal + */ + ChineseCalendar(const ChineseCalendar& other); + + /** + * Destructor. + * @internal + */ + virtual ~ChineseCalendar(); + + // clone + virtual Calendar* clone() const; + + private: + + //------------------------------------------------------------------------- + // Internal data.... + //------------------------------------------------------------------------- + + UBool isLeapYear; + + //---------------------------------------------------------------------- + // Calendar framework + //---------------------------------------------------------------------- + + protected: + virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const; + virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month); + //virtual int32_t handleGetYearLength(int32_t extendedYear) const; + virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const; + virtual int32_t handleGetExtendedYear(); + virtual void handleComputeFields(int32_t julianDay, UErrorCode &status); + virtual const UFieldResolutionTable* getFieldResolutionTable() const; + + + +public: + virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode &status); + virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode &status); + + + //---------------------------------------------------------------------- + // Internal methods & astronomical calculations + //---------------------------------------------------------------------- + +private: + + static const UFieldResolutionTable CHINESE_DATE_PRECEDENCE[]; + + /** + * The start year of the Chinese calendar, the 61st year of the reign + * of Huang Di. Some sources use the first year of his reign, + * resulting in EXTENDED_YEAR values 60 years greater and ERA (cycle) + * values one greater. + */ + static const int32_t CHINESE_EPOCH_YEAR; // Gregorian year + + /** + * The offset from GMT in milliseconds at which we perform astronomical + * computations. Some sources use a different historically accurate + * offset of GMT+7:45:40 for years before 1929; we do not do this. + */ + static const double CHINA_OFFSET; + + /** + * Value to be added or subtracted from the local days of a new moon to + * get close to the next or prior new moon, but not cross it. Must be + * >= 1 and < CalendarAstronomer.SYNODIC_MONTH. + */ + static const int32_t SYNODIC_GAP; + + + static double daysToMillis(double days); + static double millisToDays(double millis); + virtual int32_t winterSolstice(int32_t gyear) const; + virtual int32_t newMoonNear(double days, UBool after) const; + virtual int32_t synodicMonthsBetween(int32_t day1, int32_t day2) const; + virtual int32_t majorSolarTerm(int32_t days) const; + virtual UBool hasNoMajorSolarTerm(int32_t newMoon) const; + virtual UBool isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const; + virtual void computeChineseFields(int32_t days, int32_t gyear, + int32_t gmonth, UBool setAllFields); + virtual int32_t newYear(int32_t gyear) const; + virtual void offsetMonth(int32_t newMoon, int32_t dom, int32_t delta); + + + // UObject stuff + public: + /** + * @return The class ID for this object. All objects of a given class have the + * same class ID. Objects of other classes have different class IDs. + * @internal + */ + virtual UClassID getDynamicClassID(void) const; + + /** + * Return the class ID for this class. This is useful only for comparing to a return + * value from getDynamicClassID(). For example: + * + * Base* polymorphic_pointer = createPolymorphicObject(); + * if (polymorphic_pointer->getDynamicClassID() == + * Derived::getStaticClassID()) ... + * + * @return The class ID for all objects of this class. + * @internal + */ + U_I18N_API static UClassID U_EXPORT2 getStaticClassID(void); + + /** + * return the calendar type, "chinese". + * + * @return calendar type + * @internal + */ + virtual const char * getType() const; + + + protected: + /** + * (Overrides Calendar) Return true if the current date for this Calendar is in + * Daylight Savings Time. Recognizes DST_OFFSET, if it is set. + * + * @param status Fill-in parameter which receives the status of this operation. + * @return True if the current date for this Calendar is in Daylight Savings Time, + * false, otherwise. + * @internal + */ + virtual UBool inDaylightTime(UErrorCode& status) const; + + + /** + * Returns TRUE because the Islamic Calendar does have a default century + * @internal + */ + virtual UBool haveDefaultCentury() const; + + /** + * Returns the date of the start of the default century + * @return start of century - in milliseconds since epoch, 1970 + * @internal + */ + virtual UDate defaultCenturyStart() const; + + /** + * Returns the year in which the default century begins + * @internal + */ + virtual int32_t defaultCenturyStartYear() const; + + private: // default century stuff. + /** + * The system maintains a static default century start date. This is initialized + * the first time it is used. Before then, it is set to SYSTEM_DEFAULT_CENTURY to + * indicate an uninitialized state. Once the system default century date and year + * are set, they do not change. + */ + static UDate fgSystemDefaultCenturyStart; + + /** + * See documentation for systemDefaultCenturyStart. + */ + static int32_t fgSystemDefaultCenturyStartYear; + + /** + * Default value that indicates the defaultCenturyStartYear is unitialized + */ + static const int32_t fgSystemDefaultCenturyYear; + + /** + * start of default century, as a date + */ + static const UDate fgSystemDefaultCentury; + + /** + * Returns the beginning date of the 100-year window that dates + * with 2-digit years are considered to fall within. + */ + UDate internalGetDefaultCenturyStart(void) const; + + /** + * Returns the first year of the 100-year window that dates with + * 2-digit years are considered to fall within. + */ + int32_t internalGetDefaultCenturyStartYear(void) const; + + /** + * Initializes the 100-year window that dates with 2-digit years + * are considered to fall within so that its start date is 80 years + * before the current time. + */ + static void initializeSystemDefaultCentury(void); + + ChineseCalendar(); // default constructor not implemented +}; + +U_NAMESPACE_END + +#endif +#endif + + + diff --git a/icu4c/source/i18n/gregocal.cpp b/icu4c/source/i18n/gregocal.cpp index ba342505d5b..bfc582331ac 100644 --- a/icu4c/source/i18n/gregocal.cpp +++ b/icu4c/source/i18n/gregocal.cpp @@ -97,7 +97,8 @@ static const int32_t kGregorianCalendarLimits[UCAL_FIELD_COUNT][4] = { {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// DOW_LOCAL { -140742, -140742, 140742, 144683 }, // EXTENDED_YEAR {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// JULIAN_DAY - {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1} // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH }; /* diff --git a/icu4c/source/i18n/hebrwcal.cpp b/icu4c/source/i18n/hebrwcal.cpp index b3ac7bc7fe7..0eec9593d55 100644 --- a/icu4c/source/i18n/hebrwcal.cpp +++ b/icu4c/source/i18n/hebrwcal.cpp @@ -59,6 +59,7 @@ static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { { -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH }; /** diff --git a/icu4c/source/i18n/i18n.vcproj b/icu4c/source/i18n/i18n.vcproj index 3925cc59e72..997445c6c81 100644 --- a/icu4c/source/i18n/i18n.vcproj +++ b/icu4c/source/i18n/i18n.vcproj @@ -534,7 +534,7 @@ > @@ -543,7 +543,7 @@ > @@ -582,6 +582,14 @@ /> + + + + @@ -822,7 +830,7 @@ > @@ -831,7 +839,7 @@ > @@ -1308,7 +1316,7 @@ > @@ -1317,7 +1325,7 @@ > @@ -1334,7 +1342,7 @@ > @@ -1343,7 +1351,7 @@ > @@ -1550,7 +1558,7 @@ > @@ -1576,7 +1584,7 @@ > @@ -1585,7 +1593,7 @@ > diff --git a/icu4c/source/i18n/indiancal.cpp b/icu4c/source/i18n/indiancal.cpp index 0d948cb520e..bef664568e2 100644 --- a/icu4c/source/i18n/indiancal.cpp +++ b/icu4c/source/i18n/indiancal.cpp @@ -78,7 +78,8 @@ static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL { 1, 1, 5000000, 5000000}, // EXTENDED_YEAR {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY - {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1} // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH }; static const double JULIAN_EPOCH = 1721425.5; diff --git a/icu4c/source/i18n/islamcal.cpp b/icu4c/source/i18n/islamcal.cpp index a6d093a94eb..74de62a9c8a 100644 --- a/icu4c/source/i18n/islamcal.cpp +++ b/icu4c/source/i18n/islamcal.cpp @@ -162,7 +162,8 @@ static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL { 1, 1, 5000000, 5000000 }, // EXTENDED_YEAR {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY - {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1} // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH }; /** @@ -271,10 +272,10 @@ double IslamicCalendar::moonAge(UDate time) umtx_lock(&astroLock); if(gIslamicCalendarAstro == NULL) { gIslamicCalendarAstro = new CalendarAstronomer(); + ucln_i18n_registerCleanup(UCLN_I18N_ISLAMIC_CALENDAR, calendar_islamic_cleanup); } gIslamicCalendarAstro->setTime(time); age = gIslamicCalendarAstro->getMoonAge(); - ucln_i18n_registerCleanup(UCLN_I18N_ISLAMIC_CALENDAR, calendar_islamic_cleanup); umtx_unlock(&astroLock); // Convert to degrees and normalize... diff --git a/icu4c/source/i18n/persncal.cpp b/icu4c/source/i18n/persncal.cpp index 5660163d007..62105af88b5 100644 --- a/icu4c/source/i18n/persncal.cpp +++ b/icu4c/source/i18n/persncal.cpp @@ -222,8 +222,9 @@ static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { { -140742, -140742, 144683, 140742 }, // YEAR_WOY { 0, 0, 6, 6 }, // DOW_LOCAL { -2500, -2500, 2500, 2500 }, // EXTENDED_YEAR - {/* -1, -1, -1, -1 */}, // JULIAN_DAY - {/* -1, -1, -1, -1 */}, // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY + {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH }; static const int32_t MONTH_COUNT[12][4] = { //len len2 st st2 diff --git a/icu4c/source/i18n/ucln_in.h b/icu4c/source/i18n/ucln_in.h index 6feae009edd..5fdf4cbc408 100644 --- a/icu4c/source/i18n/ucln_in.h +++ b/icu4c/source/i18n/ucln_in.h @@ -28,6 +28,7 @@ typedef enum ECleanupI18NType { UCLN_I18N_TRANSLITERATOR, UCLN_I18N_REGEX, UCLN_I18N_ISLAMIC_CALENDAR, + UCLN_I18N_CHINESE_CALENDAR, UCLN_I18N_HEBREW_CALENDAR, UCLN_I18N_ASTRO_CALENDAR, UCLN_I18N_CALENDAR, diff --git a/icu4c/source/i18n/unicode/calendar.h b/icu4c/source/i18n/unicode/calendar.h index d048a8b8a8a..6da30b0960d 100644 --- a/icu4c/source/i18n/unicode/calendar.h +++ b/icu4c/source/i18n/unicode/calendar.h @@ -192,6 +192,11 @@ public: DST_OFFSET, // Example: 0 or U_MILLIS_PER_HOUR YEAR_WOY, // 'Y' Example: 1..big number - Year of Week of Year DOW_LOCAL, // 'e' Example: 1..7 - Day of Week / Localized + + EXTENDED_YEAR, + JULIAN_DAY, + MILLISECONDS_IN_DAY, + IS_LEAP_MONTH, FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields. #endif /* U_HIDE_DEPRECATED_API */ @@ -1938,6 +1943,8 @@ private: */ void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec); + protected: + /** * Compute the Gregorian calendar year, month, and day of month from the * Julian day. These values are not stored in fields, but in member @@ -1950,6 +1957,8 @@ private: */ void computeGregorianFields(int32_t julianDay, UErrorCode &ec); + private: + /** * Compute the fields WEEK_OF_YEAR, YEAR_WOY, WEEK_OF_MONTH, * DAY_OF_WEEK_IN_MONTH, and DOW_LOCAL from EXTENDED_YEAR, YEAR, diff --git a/icu4c/source/i18n/unicode/ucal.h b/icu4c/source/i18n/unicode/ucal.h index 43648f4c502..9c1d85a5553 100644 --- a/icu4c/source/i18n/unicode/ucal.h +++ b/icu4c/source/i18n/unicode/ucal.h @@ -397,6 +397,12 @@ enum UCalendarDateFields { * @stable ICU 2.8 */ UCAL_MILLISECONDS_IN_DAY, + + /** + * Whether or not the current month is a leap month (0 or 1). See the Chinese calendar for + * an example of this. + */ + UCAL_IS_LEAP_MONTH, /** * Field count diff --git a/icu4c/source/tools/ctestfw/udbgutil.cpp b/icu4c/source/tools/ctestfw/udbgutil.cpp index c4f55966b17..a7252aef7f9 100644 --- a/icu4c/source/tools/ctestfw/udbgutil.cpp +++ b/icu4c/source/tools/ctestfw/udbgutil.cpp @@ -56,6 +56,7 @@ static const Field names_UCalendarDateFields[] = FIELD_NAME_STR( LEN_UCAL, UCAL_EXTENDED_YEAR ), FIELD_NAME_STR( LEN_UCAL, UCAL_JULIAN_DAY ), FIELD_NAME_STR( LEN_UCAL, UCAL_MILLISECONDS_IN_DAY ), + FIELD_NAME_STR( LEN_UCAL, UCAL_IS_LEAP_MONTH ), };