michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (c) 2003-2008, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * Author: Alan Liu michael@0: * Created: September 2 2003 michael@0: * Since: ICU 2.8 michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #include "gregoimp.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ucal.h" michael@0: #include "uresimp.h" michael@0: #include "cstring.h" michael@0: #include "uassert.h" michael@0: michael@0: #if defined(U_DEBUG_CALDATA) michael@0: #include michael@0: #endif michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator) { michael@0: return (numerator >= 0) ? michael@0: numerator / denominator : ((numerator + 1) / denominator) - 1; michael@0: } michael@0: michael@0: int32_t ClockMath::floorDivide(double numerator, int32_t denominator, michael@0: int32_t& remainder) { michael@0: double quotient; michael@0: quotient = uprv_floor(numerator / denominator); michael@0: remainder = (int32_t) (numerator - (quotient * denominator)); michael@0: return (int32_t) quotient; michael@0: } michael@0: michael@0: double ClockMath::floorDivide(double dividend, double divisor, michael@0: double& remainder) { michael@0: // Only designed to work for positive divisors michael@0: U_ASSERT(divisor > 0); michael@0: double quotient = floorDivide(dividend, divisor); michael@0: remainder = dividend - (quotient * divisor); michael@0: // N.B. For certain large dividends, on certain platforms, there michael@0: // is a bug such that the quotient is off by one. If you doubt michael@0: // this to be true, set a breakpoint below and run cintltst. michael@0: if (remainder < 0 || remainder >= divisor) { michael@0: // E.g. 6.7317038241449352e+022 / 86400000.0 is wrong on my michael@0: // machine (too high by one). 4.1792057231752762e+024 / michael@0: // 86400000.0 is wrong the other way (too low). michael@0: double q = quotient; michael@0: quotient += (remainder < 0) ? -1 : +1; michael@0: if (q == quotient) { michael@0: // For quotients > ~2^53, we won't be able to add or michael@0: // subtract one, since the LSB of the mantissa will be > michael@0: // 2^0; that is, the exponent (base 2) will be larger than michael@0: // the length, in bits, of the mantissa. In that case, we michael@0: // can't give a correct answer, so we set the remainder to michael@0: // zero. This has the desired effect of making extreme michael@0: // values give back an approximate answer rather than michael@0: // crashing. For example, UDate values above a ~10^25 michael@0: // might all have a time of midnight. michael@0: remainder = 0; michael@0: } else { michael@0: remainder = dividend - (quotient * divisor); michael@0: } michael@0: } michael@0: U_ASSERT(0 <= remainder && remainder < divisor); michael@0: return quotient; michael@0: } michael@0: michael@0: const int32_t JULIAN_1_CE = 1721426; // January 1, 1 CE Gregorian michael@0: const int32_t JULIAN_1970_CE = 2440588; // January 1, 1970 CE Gregorian michael@0: michael@0: const int16_t Grego::DAYS_BEFORE[24] = michael@0: {0,31,59,90,120,151,181,212,243,273,304,334, michael@0: 0,31,60,91,121,152,182,213,244,274,305,335}; michael@0: michael@0: const int8_t Grego::MONTH_LENGTH[24] = michael@0: {31,28,31,30,31,30,31,31,30,31,30,31, michael@0: 31,29,31,30,31,30,31,31,30,31,30,31}; michael@0: michael@0: double Grego::fieldsToDay(int32_t year, int32_t month, int32_t dom) { michael@0: michael@0: int32_t y = year - 1; michael@0: michael@0: double julian = 365 * y + ClockMath::floorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal michael@0: ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2 + // => Gregorian cal michael@0: DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom michael@0: michael@0: return julian - JULIAN_1970_CE; // JD => epoch day michael@0: } michael@0: michael@0: void Grego::dayToFields(double day, int32_t& year, int32_t& month, michael@0: int32_t& dom, int32_t& dow, int32_t& doy) { michael@0: michael@0: // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar) michael@0: day += JULIAN_1970_CE - JULIAN_1_CE; michael@0: michael@0: // Convert from the day number to the multiple radix michael@0: // representation. We use 400-year, 100-year, and 4-year cycles. michael@0: // For example, the 4-year cycle has 4 years + 1 leap day; giving michael@0: // 1461 == 365*4 + 1 days. michael@0: int32_t n400 = ClockMath::floorDivide(day, 146097, doy); // 400-year cycle length michael@0: int32_t n100 = ClockMath::floorDivide(doy, 36524, doy); // 100-year cycle length michael@0: int32_t n4 = ClockMath::floorDivide(doy, 1461, doy); // 4-year cycle length michael@0: int32_t n1 = ClockMath::floorDivide(doy, 365, doy); michael@0: year = 400*n400 + 100*n100 + 4*n4 + n1; michael@0: if (n100 == 4 || n1 == 4) { michael@0: doy = 365; // Dec 31 at end of 4- or 400-year cycle michael@0: } else { michael@0: ++year; michael@0: } michael@0: michael@0: UBool isLeap = isLeapYear(year); michael@0: michael@0: // Gregorian day zero is a Monday. michael@0: dow = (int32_t) uprv_fmod(day + 1, 7); michael@0: dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY; michael@0: michael@0: // Common Julian/Gregorian calculation michael@0: int32_t correction = 0; michael@0: int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1 michael@0: if (doy >= march1) { michael@0: correction = isLeap ? 1 : 2; michael@0: } michael@0: month = (12 * (doy + correction) + 6) / 367; // zero-based month michael@0: dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)] + 1; // one-based DOM michael@0: doy++; // one-based doy michael@0: } michael@0: michael@0: void Grego::timeToFields(UDate time, int32_t& year, int32_t& month, michael@0: int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid) { michael@0: double millisInDay; michael@0: double day = ClockMath::floorDivide((double)time, (double)U_MILLIS_PER_DAY, millisInDay); michael@0: mid = (int32_t)millisInDay; michael@0: dayToFields(day, year, month, dom, dow, doy); michael@0: } michael@0: michael@0: int32_t Grego::dayOfWeek(double day) { michael@0: int32_t dow; michael@0: ClockMath::floorDivide(day + UCAL_THURSDAY, 7, dow); michael@0: return (dow == 0) ? UCAL_SATURDAY : dow; michael@0: } michael@0: michael@0: int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) { michael@0: int32_t weekInMonth = (dom + 6)/7; michael@0: if (weekInMonth == 4) { michael@0: if (dom + 7 > monthLength(year, month)) { michael@0: weekInMonth = -1; michael@0: } michael@0: } else if (weekInMonth == 5) { michael@0: weekInMonth = -1; michael@0: } michael@0: return weekInMonth; michael@0: } michael@0: michael@0: /* ---- CalendarData ------ */ michael@0: michael@0: #define U_CALENDAR_KEY "calendar" michael@0: #define U_GREGORIAN_KEY "gregorian" michael@0: #define U_FORMAT_KEY "format" michael@0: #define U_DEFAULT_KEY "default" michael@0: #define U_CALENDAR_DATA ((char*)0) michael@0: michael@0: michael@0: // CalendarData::CalendarData(const Locale& loc, UErrorCode& status) michael@0: // : fFillin(NULL), fBundle(NULL), fFallback(NULL) { michael@0: // initData(loc.getBaseName(), (char*) "???", status); michael@0: // } michael@0: michael@0: CalendarData::CalendarData(const Locale& loc, const char *type, UErrorCode& status) michael@0: : fFillin(NULL), fOtherFillin(NULL), fBundle(NULL), fFallback(NULL) { michael@0: initData(loc.getBaseName(), type, status); michael@0: } michael@0: michael@0: void CalendarData::initData(const char *locale, const char *type, UErrorCode& status) { michael@0: fOtherFillin = ures_open(U_CALENDAR_DATA, locale, &status); michael@0: fFillin = ures_getByKey(fOtherFillin, U_CALENDAR_KEY, fFillin, &status); michael@0: michael@0: if((type != NULL) && michael@0: (*type != '\0') && michael@0: (uprv_strcmp(type, U_GREGORIAN_KEY))) michael@0: { michael@0: fBundle = ures_getByKeyWithFallback(fFillin, type, NULL, &status); michael@0: fFallback = ures_getByKeyWithFallback(fFillin, U_GREGORIAN_KEY, NULL, &status); michael@0: michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: CalendarData(%s, %s, %s) -> main(%p, %s)=%s, fallback(%p, %s)=%s\n", michael@0: this, locale, type, u_errorName(status), fBundle, type, fBundle?ures_getLocale(fBundle, &status):"", michael@0: fFallback, U_GREGORIAN_KEY, fFallback?ures_getLocale(fFallback, &status):""); michael@0: #endif michael@0: michael@0: } else { michael@0: fBundle = ures_getByKeyWithFallback(fFillin, U_GREGORIAN_KEY, NULL, &status); michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: CalendarData(%s, %s, %s) -> main(%p, %s)=%s, fallback = NULL\n", michael@0: this, locale, type, u_errorName(status), fBundle, U_GREGORIAN_KEY, fBundle?ures_getLocale(fBundle, &status):"" ); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: CalendarData::~CalendarData() { michael@0: ures_close(fFillin); michael@0: ures_close(fBundle); michael@0: ures_close(fFallback); michael@0: ures_close(fOtherFillin); michael@0: } michael@0: michael@0: UResourceBundle* michael@0: CalendarData::getByKey(const char *key, UErrorCode& status) { michael@0: if(U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if(fBundle) { michael@0: fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status); michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: get %s -> %s - from MAIN %s\n",this, key, u_errorName(status), ures_getLocale(fFillin, &status)); michael@0: #endif michael@0: } michael@0: if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) { michael@0: status = U_ZERO_ERROR; // retry with fallback (gregorian) michael@0: fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status); michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: get %s -> %s - from FALLBACK %s\n",this, key, u_errorName(status), ures_getLocale(fFillin, &status)); michael@0: #endif michael@0: } michael@0: return fFillin; michael@0: } michael@0: michael@0: UResourceBundle* CalendarData::getByKey2(const char *key, const char *subKey, UErrorCode& status) { michael@0: if(U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if(fBundle) { michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: //\n"); michael@0: #endif michael@0: fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status); michael@0: fOtherFillin = ures_getByKeyWithFallback(fFillin, U_FORMAT_KEY, fOtherFillin, &status); michael@0: fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: get %s/format/%s -> %s - from MAIN %s\n", this, key, subKey, u_errorName(status), ures_getLocale(fFillin, &status)); michael@0: #endif michael@0: } michael@0: if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) { michael@0: status = U_ZERO_ERROR; // retry with fallback (gregorian) michael@0: fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status); michael@0: fOtherFillin = ures_getByKeyWithFallback(fFillin, U_FORMAT_KEY, fOtherFillin, &status); michael@0: fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: get %s/format/%s -> %s - from FALLBACK %s\n",this, key, subKey, u_errorName(status), ures_getLocale(fFillin,&status)); michael@0: #endif michael@0: } michael@0: michael@0: //// handling of 'default' keyword on failure: Commented out for 3.0. michael@0: // if((status == U_MISSING_RESOURCE_ERROR) && michael@0: // uprv_strcmp(subKey,U_DEFAULT_KEY)) { // avoid recursion michael@0: // #if defined (U_DEBUG_CALDATA) michael@0: // fprintf(stderr, "%p: - attempting fallback -\n", this); michael@0: // fflush(stderr); michael@0: // #endif michael@0: // UErrorCode subStatus = U_ZERO_ERROR; michael@0: // int32_t len; michael@0: // char kwBuf[128] = ""; michael@0: // const UChar *kw; michael@0: // /* fFillin = */ getByKey2(key, U_DEFAULT_KEY, subStatus); michael@0: // kw = ures_getString(fFillin, &len, &subStatus); michael@0: // if(len>126) { // too big michael@0: // len = 0; michael@0: // } michael@0: // if(U_SUCCESS(subStatus) && (len>0)) { michael@0: // u_UCharsToChars(kw, kwBuf, len+1); michael@0: // if(*kwBuf && uprv_strcmp(kwBuf,subKey)) { michael@0: // #if defined (U_DEBUG_CALDATA) michael@0: // fprintf(stderr, "%p: trying %s/format/default -> \"%s\"\n",this, key, kwBuf); michael@0: // #endif michael@0: // // now try again with the default michael@0: // status = U_ZERO_ERROR; michael@0: // /* fFillin = */ getByKey2(key, kwBuf, status); michael@0: // } michael@0: // #if defined (U_DEBUG_CALDATA) michael@0: // } else { michael@0: // fprintf(stderr, "%p: could not load %s/format/default - fail out (%s)\n",this, key, kwBuf, u_errorName(status)); michael@0: // #endif michael@0: // } michael@0: // } michael@0: michael@0: return fFillin; michael@0: } michael@0: michael@0: UResourceBundle* CalendarData::getByKey3(const char *key, const char *contextKey, const char *subKey, UErrorCode& status) { michael@0: if(U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if(fBundle) { michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: //\n"); michael@0: #endif michael@0: fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status); michael@0: fOtherFillin = ures_getByKeyWithFallback(fFillin, contextKey, fOtherFillin, &status); michael@0: fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: get %s/%s/%s -> %s - from MAIN %s\n", this, key, contextKey, subKey, u_errorName(status), ures_getLocale(fFillin, &status)); michael@0: #endif michael@0: } michael@0: if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) { michael@0: status = U_ZERO_ERROR; // retry with fallback (gregorian) michael@0: fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status); michael@0: fOtherFillin = ures_getByKeyWithFallback(fFillin, contextKey, fOtherFillin, &status); michael@0: fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); michael@0: #if defined (U_DEBUG_CALDATA) michael@0: fprintf(stderr, "%p: get %s/%s/%s -> %s - from FALLBACK %s\n",this, key, contextKey, subKey, u_errorName(status), ures_getLocale(fFillin,&status)); michael@0: #endif michael@0: } michael@0: michael@0: return fFillin; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: //eof