X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/46f4442e9a5a4f3b98b7c1083586332f6a8a99a4..a01113dcd0f39d5da295ef82785beff9ed86fe38:/icuSources/i18n/gregoimp.cpp diff --git a/icuSources/i18n/gregoimp.cpp b/icuSources/i18n/gregoimp.cpp index 22c22ca0..537aa19d 100644 --- a/icuSources/i18n/gregoimp.cpp +++ b/icuSources/i18n/gregoimp.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** - * Copyright (c) 2003-2007, International Business Machines + * Copyright (c) 2003-2008, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -18,18 +20,19 @@ #include "cstring.h" #include "uassert.h" -#if defined(U_DEBUG_CALDATA) -#include -#endif - U_NAMESPACE_BEGIN -int32_t Math::floorDivide(int32_t numerator, int32_t denominator) { +int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator) { return (numerator >= 0) ? numerator / denominator : ((numerator + 1) / denominator) - 1; } -int32_t Math::floorDivide(double numerator, int32_t denominator, +int64_t ClockMath::floorDivide(int64_t numerator, int64_t denominator) { + return (numerator >= 0) ? + numerator / denominator : ((numerator + 1) / denominator) - 1; +} + +int32_t ClockMath::floorDivide(double numerator, int32_t denominator, int32_t& remainder) { double quotient; quotient = uprv_floor(numerator / denominator); @@ -37,7 +40,7 @@ int32_t Math::floorDivide(double numerator, int32_t denominator, return (int32_t) quotient; } -double Math::floorDivide(double dividend, double divisor, +double ClockMath::floorDivide(double dividend, double divisor, double& remainder) { // Only designed to work for positive divisors U_ASSERT(divisor > 0); @@ -86,8 +89,8 @@ double Grego::fieldsToDay(int32_t year, int32_t month, int32_t dom) { int32_t y = year - 1; - double julian = 365 * y + Math::floorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal - Math::floorDivide(y, 400) - Math::floorDivide(y, 100) + 2 + // => Gregorian cal + double julian = 365 * y + ClockMath::floorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal + ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2 + // => Gregorian cal DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom return julian - JULIAN_1970_CE; // JD => epoch day @@ -103,10 +106,10 @@ void Grego::dayToFields(double day, int32_t& year, int32_t& month, // representation. We use 400-year, 100-year, and 4-year cycles. // For example, the 4-year cycle has 4 years + 1 leap day; giving // 1461 == 365*4 + 1 days. - int32_t n400 = Math::floorDivide(day, 146097, doy); // 400-year cycle length - int32_t n100 = Math::floorDivide(doy, 36524, doy); // 100-year cycle length - int32_t n4 = Math::floorDivide(doy, 1461, doy); // 4-year cycle length - int32_t n1 = Math::floorDivide(doy, 365, doy); + int32_t n400 = ClockMath::floorDivide(day, 146097, doy); // 400-year cycle length + int32_t n100 = ClockMath::floorDivide(doy, 36524, doy); // 100-year cycle length + int32_t n4 = ClockMath::floorDivide(doy, 1461, doy); // 4-year cycle length + int32_t n1 = ClockMath::floorDivide(doy, 365, doy); year = 400*n400 + 100*n100 + 4*n4 + n1; if (n100 == 4 || n1 == 4) { doy = 365; // Dec 31 at end of 4- or 400-year cycle @@ -134,14 +137,14 @@ void Grego::dayToFields(double day, int32_t& year, int32_t& month, void Grego::timeToFields(UDate time, int32_t& year, int32_t& month, int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid) { double millisInDay; - double day = Math::floorDivide((double)time, (double)U_MILLIS_PER_DAY, millisInDay); + double day = ClockMath::floorDivide((double)time, (double)U_MILLIS_PER_DAY, millisInDay); mid = (int32_t)millisInDay; dayToFields(day, year, month, dom, dow, doy); } int32_t Grego::dayOfWeek(double day) { int32_t dow; - Math::floorDivide(day + UCAL_THURSDAY, 7, dow); + ClockMath::floorDivide(day + UCAL_THURSDAY, 7, dow); return (dow == 0) ? UCAL_SATURDAY : dow; } @@ -157,171 +160,6 @@ int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) { return weekInMonth; } -/* ---- CalendarData ------ */ - -#define U_CALENDAR_KEY "calendar" -#define U_GREGORIAN_KEY "gregorian" -#define U_FORMAT_KEY "format" -#define U_DEFAULT_KEY "default" -#define U_CALENDAR_DATA ((char*)0) - - -// CalendarData::CalendarData(const Locale& loc, UErrorCode& status) -// : fFillin(NULL), fBundle(NULL), fFallback(NULL) { -// initData(loc.getBaseName(), (char*) "???", status); -// } - -CalendarData::CalendarData(const Locale& loc, const char *type, UErrorCode& status) - : fFillin(NULL), fOtherFillin(NULL), fBundle(NULL), fFallback(NULL) { - initData(loc.getBaseName(), type, status); -} - -void CalendarData::initData(const char *locale, const char *type, UErrorCode& status) { - fOtherFillin = ures_open(U_CALENDAR_DATA, locale, &status); - fFillin = ures_getByKey(fOtherFillin, U_CALENDAR_KEY, fFillin, &status); - - if((type != NULL) && - (*type != '\0') && - (uprv_strcmp(type, U_GREGORIAN_KEY))) - { - fBundle = ures_getByKeyWithFallback(fFillin, type, NULL, &status); - fFallback = ures_getByKeyWithFallback(fFillin, U_GREGORIAN_KEY, NULL, &status); - -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: CalendarData(%s, %s, %s) -> main(%p, %s)=%s, fallback(%p, %s)=%s\n", - this, locale, type, u_errorName(status), fBundle, type, fBundle?ures_getLocale(fBundle, &status):"", - fFallback, U_GREGORIAN_KEY, fFallback?ures_getLocale(fFallback, &status):""); -#endif - - } else { - fBundle = ures_getByKeyWithFallback(fFillin, U_GREGORIAN_KEY, NULL, &status); -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: CalendarData(%s, %s, %s) -> main(%p, %s)=%s, fallback = NULL\n", - this, locale, type, u_errorName(status), fBundle, U_GREGORIAN_KEY, fBundle?ures_getLocale(fBundle, &status):"" ); -#endif - } -} - -CalendarData::~CalendarData() { - ures_close(fFillin); - ures_close(fBundle); - ures_close(fFallback); - ures_close(fOtherFillin); -} - -UResourceBundle* -CalendarData::getByKey(const char *key, UErrorCode& status) { - if(U_FAILURE(status)) { - return NULL; - } - - if(fBundle) { - fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status); -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: get %s -> %s - from MAIN %s\n",this, key, u_errorName(status), ures_getLocale(fFillin, &status)); -#endif - } - if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) { - status = U_ZERO_ERROR; // retry with fallback (gregorian) - fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status); -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: get %s -> %s - from FALLBACK %s\n",this, key, u_errorName(status), ures_getLocale(fFillin, &status)); -#endif - } - return fFillin; -} - -UResourceBundle* CalendarData::getByKey2(const char *key, const char *subKey, UErrorCode& status) { - if(U_FAILURE(status)) { - return NULL; - } - - if(fBundle) { -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: //\n"); -#endif - fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status); - fOtherFillin = ures_getByKeyWithFallback(fFillin, U_FORMAT_KEY, fOtherFillin, &status); - fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: get %s/format/%s -> %s - from MAIN %s\n", this, key, subKey, u_errorName(status), ures_getLocale(fFillin, &status)); -#endif - } - if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) { - status = U_ZERO_ERROR; // retry with fallback (gregorian) - fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status); - fOtherFillin = ures_getByKeyWithFallback(fFillin, U_FORMAT_KEY, fOtherFillin, &status); - fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: get %s/format/%s -> %s - from FALLBACK %s\n",this, key, subKey, u_errorName(status), ures_getLocale(fFillin,&status)); -#endif - } - -//// handling of 'default' keyword on failure: Commented out for 3.0. -// if((status == U_MISSING_RESOURCE_ERROR) && -// uprv_strcmp(subKey,U_DEFAULT_KEY)) { // avoid recursion -// #if defined (U_DEBUG_CALDATA) -// fprintf(stderr, "%p: - attempting fallback -\n", this); -// fflush(stderr); -// #endif -// UErrorCode subStatus = U_ZERO_ERROR; -// int32_t len; -// char kwBuf[128] = ""; -// const UChar *kw; -// /* fFillin = */ getByKey2(key, U_DEFAULT_KEY, subStatus); -// kw = ures_getString(fFillin, &len, &subStatus); -// if(len>126) { // too big -// len = 0; -// } -// if(U_SUCCESS(subStatus) && (len>0)) { -// u_UCharsToChars(kw, kwBuf, len+1); -// if(*kwBuf && uprv_strcmp(kwBuf,subKey)) { -// #if defined (U_DEBUG_CALDATA) -// fprintf(stderr, "%p: trying %s/format/default -> \"%s\"\n",this, key, kwBuf); -// #endif -// // now try again with the default -// status = U_ZERO_ERROR; -// /* fFillin = */ getByKey2(key, kwBuf, status); -// } -// #if defined (U_DEBUG_CALDATA) -// } else { -// fprintf(stderr, "%p: could not load %s/format/default - fail out (%s)\n",this, key, kwBuf, u_errorName(status)); -// #endif -// } -// } - - return fFillin; -} - -UResourceBundle* CalendarData::getByKey3(const char *key, const char *contextKey, const char *subKey, UErrorCode& status) { - if(U_FAILURE(status)) { - return NULL; - } - - if(fBundle) { -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: //\n"); -#endif - fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status); - fOtherFillin = ures_getByKeyWithFallback(fFillin, contextKey, fOtherFillin, &status); - fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: get %s/%s/%s -> %s - from MAIN %s\n", this, key, contextKey, subKey, u_errorName(status), ures_getLocale(fFillin, &status)); -#endif - } - if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) { - status = U_ZERO_ERROR; // retry with fallback (gregorian) - fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status); - fOtherFillin = ures_getByKeyWithFallback(fFillin, contextKey, fOtherFillin, &status); - fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status); -#if defined (U_DEBUG_CALDATA) - fprintf(stderr, "%p: get %s/%s/%s -> %s - from FALLBACK %s\n",this, key, contextKey, subKey, u_errorName(status), ures_getLocale(fFillin,&status)); -#endif - } - - return fFillin; -} - U_NAMESPACE_END #endif