gregoimp.cpp (8643B)
1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ********************************************************************** 5 * Copyright (c) 2003-2008, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ********************************************************************** 8 * Author: Alan Liu 9 * Created: September 2 2003 10 * Since: ICU 2.8 11 ********************************************************************** 12 */ 13 14 #include "gregoimp.h" 15 16 #if !UCONFIG_NO_FORMATTING 17 18 #include "unicode/ucal.h" 19 #include "uresimp.h" 20 #include "cstring.h" 21 #include "uassert.h" 22 23 U_NAMESPACE_BEGIN 24 25 int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator) { 26 return (numerator >= 0) ? 27 numerator / denominator : ((numerator + 1) / denominator) - 1; 28 } 29 30 int64_t ClockMath::floorDivideInt64(int64_t numerator, int64_t denominator) { 31 return (numerator >= 0) ? 32 numerator / denominator : ((numerator + 1) / denominator) - 1; 33 } 34 35 int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator, 36 int32_t* remainder) { 37 int64_t quotient = floorDivide(numerator, denominator); 38 if (remainder != nullptr) { 39 *remainder = numerator - (quotient * denominator); 40 } 41 return quotient; 42 } 43 44 double ClockMath::floorDivide(double numerator, int32_t denominator, 45 int32_t* remainder) { 46 // For an integer n and representable ⌊x/n⌋, ⌊RN(x/n)⌋=⌊x/n⌋, where RN is 47 // rounding to nearest. 48 double quotient = uprv_floor(numerator / denominator); 49 if (remainder != nullptr) { 50 // For doubles x and n, where n is an integer and ⌊x+n⌋ < 2³¹, the 51 // expression `(int32_t) (x + n)` evaluated with rounding to nearest 52 // differs from ⌊x+n⌋ if 0 < ⌈x⌉−x ≪ x+n, as `x + n` is rounded up to 53 // n+⌈x⌉ = ⌊x+n⌋ + 1. Rewriting it as ⌊x⌋+n makes the addition exact. 54 *remainder = static_cast<int32_t>(uprv_floor(numerator) - (quotient * denominator)); 55 } 56 return quotient; 57 } 58 59 double ClockMath::floorDivide(double dividend, double divisor, 60 double* remainder) { 61 // Only designed to work for positive divisors 62 U_ASSERT(divisor > 0); 63 double quotient = floorDivide(dividend, divisor); 64 double r = dividend - (quotient * divisor); 65 // N.B. For certain large dividends, on certain platforms, there 66 // is a bug such that the quotient is off by one. If you doubt 67 // this to be true, set a breakpoint below and run cintltst. 68 if (r < 0 || r >= divisor) { 69 // E.g. 6.7317038241449352e+022 / 86400000.0 is wrong on my 70 // machine (too high by one). 4.1792057231752762e+024 / 71 // 86400000.0 is wrong the other way (too low). 72 double q = quotient; 73 quotient += (r < 0) ? -1 : +1; 74 if (q == quotient) { 75 // For quotients > ~2^53, we won't be able to add or 76 // subtract one, since the LSB of the mantissa will be > 77 // 2^0; that is, the exponent (base 2) will be larger than 78 // the length, in bits, of the mantissa. In that case, we 79 // can't give a correct answer, so we set the remainder to 80 // zero. This has the desired effect of making extreme 81 // values give back an approximate answer rather than 82 // crashing. For example, UDate values above a ~10^25 83 // might all have a time of midnight. 84 r = 0; 85 } else { 86 r = dividend - (quotient * divisor); 87 } 88 } 89 U_ASSERT(0 <= r && r < divisor); 90 if (remainder != nullptr) { 91 *remainder = r; 92 } 93 return quotient; 94 } 95 96 const int32_t JULIAN_1_CE = 1721426; // January 1, 1 CE Gregorian 97 const int32_t JULIAN_1970_CE = 2440588; // January 1, 1970 CE Gregorian 98 99 const int16_t Grego::DAYS_BEFORE[24] = 100 {0,31,59,90,120,151,181,212,243,273,304,334, 101 0,31,60,91,121,152,182,213,244,274,305,335}; 102 103 const int8_t Grego::MONTH_LENGTH[24] = 104 {31,28,31,30,31,30,31,31,30,31,30,31, 105 31,29,31,30,31,30,31,31,30,31,30,31}; 106 107 int64_t Grego::fieldsToDay(int32_t year, int32_t month, int32_t dom) { 108 109 int64_t y = year; 110 y--; 111 112 int64_t julian = 365LL * y + 113 ClockMath::floorDivideInt64(y, 4LL) + (JULIAN_1_CE - 3) + // Julian cal 114 ClockMath::floorDivideInt64(y, 400LL) - 115 ClockMath::floorDivideInt64(y, 100LL) + 2 + // => Gregorian cal 116 DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom 117 118 return julian - JULIAN_1970_CE; // JD => epoch day 119 } 120 121 void Grego::dayToFields(int32_t day, int32_t& year, int8_t& month, 122 int8_t& dom, int8_t& dow, int16_t& doy, UErrorCode& status) { 123 year = dayToYear(day, doy, status); // one-based doy 124 if (U_FAILURE(status)) return; 125 126 // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar) 127 if (uprv_add32_overflow(day, JULIAN_1970_CE - JULIAN_1_CE, &day)) { 128 status = U_ILLEGAL_ARGUMENT_ERROR; 129 return; 130 } 131 132 // Gregorian day zero is a Monday. 133 dow = (day + 1) % 7; 134 dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY; 135 136 // Common Julian/Gregorian calculation 137 int32_t correction = 0; 138 bool isLeap = isLeapYear(year); 139 int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1 140 if (doy > march1) { 141 correction = isLeap ? 1 : 2; 142 } 143 month = (12 * (doy - 1 + correction) + 6) / 367; // zero-based month 144 dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)]; // one-based DOM 145 } 146 147 int32_t Grego::dayToYear(int32_t day, UErrorCode& status) { 148 int16_t unusedDOY; 149 return dayToYear(day, unusedDOY, status); 150 } 151 152 int32_t Grego::dayToYear(int32_t day, int16_t& doy, UErrorCode& status) { 153 if (U_FAILURE(status)) return 0; 154 // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar) 155 if (uprv_add32_overflow(day, JULIAN_1970_CE - JULIAN_1_CE, &day)) { 156 status = U_ILLEGAL_ARGUMENT_ERROR; 157 return 0; 158 } 159 160 // Convert from the day number to the multiple radix 161 // representation. We use 400-year, 100-year, and 4-year cycles. 162 // For example, the 4-year cycle has 4 years + 1 leap day; giving 163 // 1461 == 365*4 + 1 days. 164 int32_t doy32; 165 int32_t n400 = ClockMath::floorDivide(day, 146097, &doy32); // 400-year cycle length 166 int32_t n100 = ClockMath::floorDivide(doy32, 36524, &doy32); // 100-year cycle length 167 int32_t n4 = ClockMath::floorDivide(doy32, 1461, &doy32); // 4-year cycle length 168 int32_t n1 = ClockMath::floorDivide(doy32, 365, &doy32); 169 int32_t year = 400*n400 + 100*n100 + 4*n4 + n1; 170 if (n100 == 4 || n1 == 4) { 171 doy = 365; // Dec 31 at end of 4- or 400-year cycle 172 } else { 173 doy = doy32; 174 ++year; 175 } 176 doy++; // one-based doy 177 return year; 178 } 179 180 void Grego::timeToFields(UDate time, int32_t& year, int8_t& month, 181 int8_t& dom, int32_t& mid, UErrorCode& status) { 182 int8_t unusedDOW; 183 timeToFields(time, year, month, dom, unusedDOW, mid, status); 184 } 185 186 void Grego::timeToFields(UDate time, int32_t& year, int8_t& month, 187 int8_t& dom, int8_t& dow, int32_t& mid, UErrorCode& status) { 188 int16_t unusedDOY; 189 timeToFields(time, year, month, dom, dow, unusedDOY, mid, status); 190 } 191 192 void Grego::timeToFields(UDate time, int32_t& year, int8_t& month, 193 int8_t& dom, int8_t& dow, int16_t& doy, int32_t& mid, UErrorCode& status) { 194 if (U_FAILURE(status)) return; 195 double day = ClockMath::floorDivide(time, U_MILLIS_PER_DAY, &mid); 196 if (day > INT32_MAX || day < INT32_MIN) { 197 status = U_ILLEGAL_ARGUMENT_ERROR; 198 return; 199 } 200 dayToFields(day, year, month, dom, dow, doy, status); 201 } 202 203 int32_t Grego::timeToYear(UDate time, UErrorCode& status) { 204 if (U_FAILURE(status)) return 0; 205 double day = ClockMath::floorDivide(time, double(U_MILLIS_PER_DAY)); 206 if (day > INT32_MAX || day < INT32_MIN) { 207 status = U_ILLEGAL_ARGUMENT_ERROR; 208 return 0; 209 } 210 return Grego::dayToYear(day, status); 211 } 212 213 int32_t Grego::dayOfWeek(int32_t day) { 214 int32_t dow; 215 ClockMath::floorDivide(day + int{UCAL_THURSDAY}, 7, &dow); 216 return (dow == 0) ? UCAL_SATURDAY : dow; 217 } 218 219 int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) { 220 int32_t weekInMonth = (dom + 6)/7; 221 if (weekInMonth == 4) { 222 if (dom + 7 > monthLength(year, month)) { 223 weekInMonth = -1; 224 } 225 } else if (weekInMonth == 5) { 226 weekInMonth = -1; 227 } 228 return weekInMonth; 229 } 230 231 U_NAMESPACE_END 232 233 #endif 234 //eof