intl/icu/source/i18n/gregoimp.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (c) 2003-2008, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 * Author: Alan Liu
michael@0 7 * Created: September 2 2003
michael@0 8 * Since: ICU 2.8
michael@0 9 **********************************************************************
michael@0 10 */
michael@0 11
michael@0 12 #include "gregoimp.h"
michael@0 13
michael@0 14 #if !UCONFIG_NO_FORMATTING
michael@0 15
michael@0 16 #include "unicode/ucal.h"
michael@0 17 #include "uresimp.h"
michael@0 18 #include "cstring.h"
michael@0 19 #include "uassert.h"
michael@0 20
michael@0 21 #if defined(U_DEBUG_CALDATA)
michael@0 22 #include <stdio.h>
michael@0 23 #endif
michael@0 24
michael@0 25 U_NAMESPACE_BEGIN
michael@0 26
michael@0 27 int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator) {
michael@0 28 return (numerator >= 0) ?
michael@0 29 numerator / denominator : ((numerator + 1) / denominator) - 1;
michael@0 30 }
michael@0 31
michael@0 32 int32_t ClockMath::floorDivide(double numerator, int32_t denominator,
michael@0 33 int32_t& remainder) {
michael@0 34 double quotient;
michael@0 35 quotient = uprv_floor(numerator / denominator);
michael@0 36 remainder = (int32_t) (numerator - (quotient * denominator));
michael@0 37 return (int32_t) quotient;
michael@0 38 }
michael@0 39
michael@0 40 double ClockMath::floorDivide(double dividend, double divisor,
michael@0 41 double& remainder) {
michael@0 42 // Only designed to work for positive divisors
michael@0 43 U_ASSERT(divisor > 0);
michael@0 44 double quotient = floorDivide(dividend, divisor);
michael@0 45 remainder = dividend - (quotient * divisor);
michael@0 46 // N.B. For certain large dividends, on certain platforms, there
michael@0 47 // is a bug such that the quotient is off by one. If you doubt
michael@0 48 // this to be true, set a breakpoint below and run cintltst.
michael@0 49 if (remainder < 0 || remainder >= divisor) {
michael@0 50 // E.g. 6.7317038241449352e+022 / 86400000.0 is wrong on my
michael@0 51 // machine (too high by one). 4.1792057231752762e+024 /
michael@0 52 // 86400000.0 is wrong the other way (too low).
michael@0 53 double q = quotient;
michael@0 54 quotient += (remainder < 0) ? -1 : +1;
michael@0 55 if (q == quotient) {
michael@0 56 // For quotients > ~2^53, we won't be able to add or
michael@0 57 // subtract one, since the LSB of the mantissa will be >
michael@0 58 // 2^0; that is, the exponent (base 2) will be larger than
michael@0 59 // the length, in bits, of the mantissa. In that case, we
michael@0 60 // can't give a correct answer, so we set the remainder to
michael@0 61 // zero. This has the desired effect of making extreme
michael@0 62 // values give back an approximate answer rather than
michael@0 63 // crashing. For example, UDate values above a ~10^25
michael@0 64 // might all have a time of midnight.
michael@0 65 remainder = 0;
michael@0 66 } else {
michael@0 67 remainder = dividend - (quotient * divisor);
michael@0 68 }
michael@0 69 }
michael@0 70 U_ASSERT(0 <= remainder && remainder < divisor);
michael@0 71 return quotient;
michael@0 72 }
michael@0 73
michael@0 74 const int32_t JULIAN_1_CE = 1721426; // January 1, 1 CE Gregorian
michael@0 75 const int32_t JULIAN_1970_CE = 2440588; // January 1, 1970 CE Gregorian
michael@0 76
michael@0 77 const int16_t Grego::DAYS_BEFORE[24] =
michael@0 78 {0,31,59,90,120,151,181,212,243,273,304,334,
michael@0 79 0,31,60,91,121,152,182,213,244,274,305,335};
michael@0 80
michael@0 81 const int8_t Grego::MONTH_LENGTH[24] =
michael@0 82 {31,28,31,30,31,30,31,31,30,31,30,31,
michael@0 83 31,29,31,30,31,30,31,31,30,31,30,31};
michael@0 84
michael@0 85 double Grego::fieldsToDay(int32_t year, int32_t month, int32_t dom) {
michael@0 86
michael@0 87 int32_t y = year - 1;
michael@0 88
michael@0 89 double julian = 365 * y + ClockMath::floorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal
michael@0 90 ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2 + // => Gregorian cal
michael@0 91 DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom
michael@0 92
michael@0 93 return julian - JULIAN_1970_CE; // JD => epoch day
michael@0 94 }
michael@0 95
michael@0 96 void Grego::dayToFields(double day, int32_t& year, int32_t& month,
michael@0 97 int32_t& dom, int32_t& dow, int32_t& doy) {
michael@0 98
michael@0 99 // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar)
michael@0 100 day += JULIAN_1970_CE - JULIAN_1_CE;
michael@0 101
michael@0 102 // Convert from the day number to the multiple radix
michael@0 103 // representation. We use 400-year, 100-year, and 4-year cycles.
michael@0 104 // For example, the 4-year cycle has 4 years + 1 leap day; giving
michael@0 105 // 1461 == 365*4 + 1 days.
michael@0 106 int32_t n400 = ClockMath::floorDivide(day, 146097, doy); // 400-year cycle length
michael@0 107 int32_t n100 = ClockMath::floorDivide(doy, 36524, doy); // 100-year cycle length
michael@0 108 int32_t n4 = ClockMath::floorDivide(doy, 1461, doy); // 4-year cycle length
michael@0 109 int32_t n1 = ClockMath::floorDivide(doy, 365, doy);
michael@0 110 year = 400*n400 + 100*n100 + 4*n4 + n1;
michael@0 111 if (n100 == 4 || n1 == 4) {
michael@0 112 doy = 365; // Dec 31 at end of 4- or 400-year cycle
michael@0 113 } else {
michael@0 114 ++year;
michael@0 115 }
michael@0 116
michael@0 117 UBool isLeap = isLeapYear(year);
michael@0 118
michael@0 119 // Gregorian day zero is a Monday.
michael@0 120 dow = (int32_t) uprv_fmod(day + 1, 7);
michael@0 121 dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY;
michael@0 122
michael@0 123 // Common Julian/Gregorian calculation
michael@0 124 int32_t correction = 0;
michael@0 125 int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1
michael@0 126 if (doy >= march1) {
michael@0 127 correction = isLeap ? 1 : 2;
michael@0 128 }
michael@0 129 month = (12 * (doy + correction) + 6) / 367; // zero-based month
michael@0 130 dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)] + 1; // one-based DOM
michael@0 131 doy++; // one-based doy
michael@0 132 }
michael@0 133
michael@0 134 void Grego::timeToFields(UDate time, int32_t& year, int32_t& month,
michael@0 135 int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid) {
michael@0 136 double millisInDay;
michael@0 137 double day = ClockMath::floorDivide((double)time, (double)U_MILLIS_PER_DAY, millisInDay);
michael@0 138 mid = (int32_t)millisInDay;
michael@0 139 dayToFields(day, year, month, dom, dow, doy);
michael@0 140 }
michael@0 141
michael@0 142 int32_t Grego::dayOfWeek(double day) {
michael@0 143 int32_t dow;
michael@0 144 ClockMath::floorDivide(day + UCAL_THURSDAY, 7, dow);
michael@0 145 return (dow == 0) ? UCAL_SATURDAY : dow;
michael@0 146 }
michael@0 147
michael@0 148 int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) {
michael@0 149 int32_t weekInMonth = (dom + 6)/7;
michael@0 150 if (weekInMonth == 4) {
michael@0 151 if (dom + 7 > monthLength(year, month)) {
michael@0 152 weekInMonth = -1;
michael@0 153 }
michael@0 154 } else if (weekInMonth == 5) {
michael@0 155 weekInMonth = -1;
michael@0 156 }
michael@0 157 return weekInMonth;
michael@0 158 }
michael@0 159
michael@0 160 /* ---- CalendarData ------ */
michael@0 161
michael@0 162 #define U_CALENDAR_KEY "calendar"
michael@0 163 #define U_GREGORIAN_KEY "gregorian"
michael@0 164 #define U_FORMAT_KEY "format"
michael@0 165 #define U_DEFAULT_KEY "default"
michael@0 166 #define U_CALENDAR_DATA ((char*)0)
michael@0 167
michael@0 168
michael@0 169 // CalendarData::CalendarData(const Locale& loc, UErrorCode& status)
michael@0 170 // : fFillin(NULL), fBundle(NULL), fFallback(NULL) {
michael@0 171 // initData(loc.getBaseName(), (char*) "???", status);
michael@0 172 // }
michael@0 173
michael@0 174 CalendarData::CalendarData(const Locale& loc, const char *type, UErrorCode& status)
michael@0 175 : fFillin(NULL), fOtherFillin(NULL), fBundle(NULL), fFallback(NULL) {
michael@0 176 initData(loc.getBaseName(), type, status);
michael@0 177 }
michael@0 178
michael@0 179 void CalendarData::initData(const char *locale, const char *type, UErrorCode& status) {
michael@0 180 fOtherFillin = ures_open(U_CALENDAR_DATA, locale, &status);
michael@0 181 fFillin = ures_getByKey(fOtherFillin, U_CALENDAR_KEY, fFillin, &status);
michael@0 182
michael@0 183 if((type != NULL) &&
michael@0 184 (*type != '\0') &&
michael@0 185 (uprv_strcmp(type, U_GREGORIAN_KEY)))
michael@0 186 {
michael@0 187 fBundle = ures_getByKeyWithFallback(fFillin, type, NULL, &status);
michael@0 188 fFallback = ures_getByKeyWithFallback(fFillin, U_GREGORIAN_KEY, NULL, &status);
michael@0 189
michael@0 190 #if defined (U_DEBUG_CALDATA)
michael@0 191 fprintf(stderr, "%p: CalendarData(%s, %s, %s) -> main(%p, %s)=%s, fallback(%p, %s)=%s\n",
michael@0 192 this, locale, type, u_errorName(status), fBundle, type, fBundle?ures_getLocale(fBundle, &status):"",
michael@0 193 fFallback, U_GREGORIAN_KEY, fFallback?ures_getLocale(fFallback, &status):"");
michael@0 194 #endif
michael@0 195
michael@0 196 } else {
michael@0 197 fBundle = ures_getByKeyWithFallback(fFillin, U_GREGORIAN_KEY, NULL, &status);
michael@0 198 #if defined (U_DEBUG_CALDATA)
michael@0 199 fprintf(stderr, "%p: CalendarData(%s, %s, %s) -> main(%p, %s)=%s, fallback = NULL\n",
michael@0 200 this, locale, type, u_errorName(status), fBundle, U_GREGORIAN_KEY, fBundle?ures_getLocale(fBundle, &status):"" );
michael@0 201 #endif
michael@0 202 }
michael@0 203 }
michael@0 204
michael@0 205 CalendarData::~CalendarData() {
michael@0 206 ures_close(fFillin);
michael@0 207 ures_close(fBundle);
michael@0 208 ures_close(fFallback);
michael@0 209 ures_close(fOtherFillin);
michael@0 210 }
michael@0 211
michael@0 212 UResourceBundle*
michael@0 213 CalendarData::getByKey(const char *key, UErrorCode& status) {
michael@0 214 if(U_FAILURE(status)) {
michael@0 215 return NULL;
michael@0 216 }
michael@0 217
michael@0 218 if(fBundle) {
michael@0 219 fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status);
michael@0 220 #if defined (U_DEBUG_CALDATA)
michael@0 221 fprintf(stderr, "%p: get %s -> %s - from MAIN %s\n",this, key, u_errorName(status), ures_getLocale(fFillin, &status));
michael@0 222 #endif
michael@0 223 }
michael@0 224 if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) {
michael@0 225 status = U_ZERO_ERROR; // retry with fallback (gregorian)
michael@0 226 fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status);
michael@0 227 #if defined (U_DEBUG_CALDATA)
michael@0 228 fprintf(stderr, "%p: get %s -> %s - from FALLBACK %s\n",this, key, u_errorName(status), ures_getLocale(fFillin, &status));
michael@0 229 #endif
michael@0 230 }
michael@0 231 return fFillin;
michael@0 232 }
michael@0 233
michael@0 234 UResourceBundle* CalendarData::getByKey2(const char *key, const char *subKey, UErrorCode& status) {
michael@0 235 if(U_FAILURE(status)) {
michael@0 236 return NULL;
michael@0 237 }
michael@0 238
michael@0 239 if(fBundle) {
michael@0 240 #if defined (U_DEBUG_CALDATA)
michael@0 241 fprintf(stderr, "%p: //\n");
michael@0 242 #endif
michael@0 243 fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status);
michael@0 244 fOtherFillin = ures_getByKeyWithFallback(fFillin, U_FORMAT_KEY, fOtherFillin, &status);
michael@0 245 fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status);
michael@0 246 #if defined (U_DEBUG_CALDATA)
michael@0 247 fprintf(stderr, "%p: get %s/format/%s -> %s - from MAIN %s\n", this, key, subKey, u_errorName(status), ures_getLocale(fFillin, &status));
michael@0 248 #endif
michael@0 249 }
michael@0 250 if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) {
michael@0 251 status = U_ZERO_ERROR; // retry with fallback (gregorian)
michael@0 252 fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status);
michael@0 253 fOtherFillin = ures_getByKeyWithFallback(fFillin, U_FORMAT_KEY, fOtherFillin, &status);
michael@0 254 fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status);
michael@0 255 #if defined (U_DEBUG_CALDATA)
michael@0 256 fprintf(stderr, "%p: get %s/format/%s -> %s - from FALLBACK %s\n",this, key, subKey, u_errorName(status), ures_getLocale(fFillin,&status));
michael@0 257 #endif
michael@0 258 }
michael@0 259
michael@0 260 //// handling of 'default' keyword on failure: Commented out for 3.0.
michael@0 261 // if((status == U_MISSING_RESOURCE_ERROR) &&
michael@0 262 // uprv_strcmp(subKey,U_DEFAULT_KEY)) { // avoid recursion
michael@0 263 // #if defined (U_DEBUG_CALDATA)
michael@0 264 // fprintf(stderr, "%p: - attempting fallback -\n", this);
michael@0 265 // fflush(stderr);
michael@0 266 // #endif
michael@0 267 // UErrorCode subStatus = U_ZERO_ERROR;
michael@0 268 // int32_t len;
michael@0 269 // char kwBuf[128] = "";
michael@0 270 // const UChar *kw;
michael@0 271 // /* fFillin = */ getByKey2(key, U_DEFAULT_KEY, subStatus);
michael@0 272 // kw = ures_getString(fFillin, &len, &subStatus);
michael@0 273 // if(len>126) { // too big
michael@0 274 // len = 0;
michael@0 275 // }
michael@0 276 // if(U_SUCCESS(subStatus) && (len>0)) {
michael@0 277 // u_UCharsToChars(kw, kwBuf, len+1);
michael@0 278 // if(*kwBuf && uprv_strcmp(kwBuf,subKey)) {
michael@0 279 // #if defined (U_DEBUG_CALDATA)
michael@0 280 // fprintf(stderr, "%p: trying %s/format/default -> \"%s\"\n",this, key, kwBuf);
michael@0 281 // #endif
michael@0 282 // // now try again with the default
michael@0 283 // status = U_ZERO_ERROR;
michael@0 284 // /* fFillin = */ getByKey2(key, kwBuf, status);
michael@0 285 // }
michael@0 286 // #if defined (U_DEBUG_CALDATA)
michael@0 287 // } else {
michael@0 288 // fprintf(stderr, "%p: could not load %s/format/default - fail out (%s)\n",this, key, kwBuf, u_errorName(status));
michael@0 289 // #endif
michael@0 290 // }
michael@0 291 // }
michael@0 292
michael@0 293 return fFillin;
michael@0 294 }
michael@0 295
michael@0 296 UResourceBundle* CalendarData::getByKey3(const char *key, const char *contextKey, const char *subKey, UErrorCode& status) {
michael@0 297 if(U_FAILURE(status)) {
michael@0 298 return NULL;
michael@0 299 }
michael@0 300
michael@0 301 if(fBundle) {
michael@0 302 #if defined (U_DEBUG_CALDATA)
michael@0 303 fprintf(stderr, "%p: //\n");
michael@0 304 #endif
michael@0 305 fFillin = ures_getByKeyWithFallback(fBundle, key, fFillin, &status);
michael@0 306 fOtherFillin = ures_getByKeyWithFallback(fFillin, contextKey, fOtherFillin, &status);
michael@0 307 fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status);
michael@0 308 #if defined (U_DEBUG_CALDATA)
michael@0 309 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 310 #endif
michael@0 311 }
michael@0 312 if(fFallback && (status == U_MISSING_RESOURCE_ERROR)) {
michael@0 313 status = U_ZERO_ERROR; // retry with fallback (gregorian)
michael@0 314 fFillin = ures_getByKeyWithFallback(fFallback, key, fFillin, &status);
michael@0 315 fOtherFillin = ures_getByKeyWithFallback(fFillin, contextKey, fOtherFillin, &status);
michael@0 316 fFillin = ures_getByKeyWithFallback(fOtherFillin, subKey, fFillin, &status);
michael@0 317 #if defined (U_DEBUG_CALDATA)
michael@0 318 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 319 #endif
michael@0 320 }
michael@0 321
michael@0 322 return fFillin;
michael@0 323 }
michael@0 324
michael@0 325 U_NAMESPACE_END
michael@0 326
michael@0 327 #endif
michael@0 328 //eof

mercurial