| 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 - 1; |
| 110 | |
| 111 | int64_t julian = 365LL * y + |
| 112 | ClockMath::floorDivideInt64(y, 4LL) + (JULIAN_1_CE - 3) + // Julian cal |
| 113 | ClockMath::floorDivideInt64(y, 400LL) - |
| 114 | ClockMath::floorDivideInt64(y, 100LL) + 2 + // => Gregorian cal |
| 115 | DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom |
| 116 | |
| 117 | return julian - JULIAN_1970_CE; // JD => epoch day |
| 118 | } |
| 119 | |
| 120 | void Grego::dayToFields(int32_t day, int32_t& year, int8_t& month, |
| 121 | int8_t& dom, int8_t& dow, int16_t& doy, UErrorCode& status) { |
| 122 | year = dayToYear(day, doy, status); // one-based doy |
| 123 | if (U_FAILURE(status)) return; |
| 124 | |
| 125 | // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar) |
| 126 | if (uprv_add32_overflow(day, JULIAN_1970_CE - JULIAN_1_CE, &day)) { |
| 127 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // Gregorian day zero is a Monday. |
| 132 | dow = (day + 1) % 7; |
| 133 | dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY; |
| 134 | |
| 135 | // Common Julian/Gregorian calculation |
| 136 | int32_t correction = 0; |
| 137 | bool isLeap = isLeapYear(year); |
| 138 | int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1 |
| 139 | if (doy > march1) { |
| 140 | correction = isLeap ? 1 : 2; |
| 141 | } |
| 142 | month = (12 * (doy - 1 + correction) + 6) / 367; // zero-based month |
| 143 | dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)]; // one-based DOM |
| 144 | } |
| 145 | |
| 146 | int32_t Grego::dayToYear(int32_t day, UErrorCode& status) { |
| 147 | int16_t unusedDOY; |
| 148 | return dayToYear(day, unusedDOY, status); |
| 149 | } |
| 150 | |
| 151 | int32_t Grego::dayToYear(int32_t day, int16_t& doy, UErrorCode& status) { |
| 152 | if (U_FAILURE(status)) return 0; |
| 153 | // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar) |
| 154 | if (uprv_add32_overflow(day, JULIAN_1970_CE - JULIAN_1_CE, &day)) { |
| 155 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | // Convert from the day number to the multiple radix |
| 160 | // representation. We use 400-year, 100-year, and 4-year cycles. |
| 161 | // For example, the 4-year cycle has 4 years + 1 leap day; giving |
| 162 | // 1461 == 365*4 + 1 days. |
| 163 | int32_t doy32; |
| 164 | int32_t n400 = ClockMath::floorDivide(day, 146097, &doy32); // 400-year cycle length |
| 165 | int32_t n100 = ClockMath::floorDivide(doy32, 36524, &doy32); // 100-year cycle length |
| 166 | int32_t n4 = ClockMath::floorDivide(doy32, 1461, &doy32); // 4-year cycle length |
| 167 | int32_t n1 = ClockMath::floorDivide(doy32, 365, &doy32); |
| 168 | int32_t year = 400*n400 + 100*n100 + 4*n4 + n1; |
| 169 | if (n100 == 4 || n1 == 4) { |
| 170 | doy = 365; // Dec 31 at end of 4- or 400-year cycle |
| 171 | } else { |
| 172 | doy = doy32; |
| 173 | ++year; |
| 174 | } |
| 175 | doy++; // one-based doy |
| 176 | return year; |
| 177 | } |
| 178 | |
| 179 | void Grego::dayToFields(int32_t day, int32_t& year, int8_t& month, |
| 180 | int8_t& dom, int8_t& dow, UErrorCode& status) { |
| 181 | int16_t unusedDOY; |
| 182 | dayToFields(day, year, month, dom, dow, unusedDOY, status); |
| 183 | } |
| 184 | |
| 185 | void Grego::dayToFields(int32_t day, int32_t& year, int8_t& month, |
| 186 | int8_t& dom, int16_t& doy, UErrorCode& status) { |
| 187 | int8_t unusedDOW; |
| 188 | dayToFields(day, year, month, dom, unusedDOW, doy, status); |
| 189 | } |
| 190 | |
| 191 | void Grego::timeToFields(UDate time, int32_t& year, int8_t& month, |
| 192 | int8_t& dom, int32_t& mid, UErrorCode& status) { |
| 193 | int8_t unusedDOW; |
| 194 | timeToFields(time, year, month, dom, unusedDOW, mid, status); |
| 195 | } |
| 196 | |
| 197 | void Grego::timeToFields(UDate time, int32_t& year, int8_t& month, |
| 198 | int8_t& dom, int8_t& dow, int32_t& mid, UErrorCode& status) { |
| 199 | int16_t unusedDOY; |
| 200 | timeToFields(time, year, month, dom, dow, unusedDOY, mid, status); |
| 201 | } |
| 202 | |
| 203 | void Grego::timeToFields(UDate time, int32_t& year, int8_t& month, |
| 204 | int8_t& dom, int8_t& dow, int16_t& doy, int32_t& mid, UErrorCode& status) { |
| 205 | if (U_FAILURE(status)) return; |
| 206 | double day = ClockMath::floorDivide(time, U_MILLIS_PER_DAY, &mid); |
| 207 | if (day > INT32_MAX || day < INT32_MIN) { |
| 208 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 209 | return; |
| 210 | } |
| 211 | dayToFields(day, year, month, dom, dow, doy, status); |
| 212 | } |
| 213 | |
| 214 | int32_t Grego::timeToYear(UDate time, UErrorCode& status) { |
| 215 | if (U_FAILURE(status)) return 0; |
| 216 | double day = ClockMath::floorDivide(time, double(U_MILLIS_PER_DAY)); |
| 217 | if (day > INT32_MAX || day < INT32_MIN) { |
| 218 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 219 | return 0; |
| 220 | } |
| 221 | return Grego::dayToYear(day, status); |
| 222 | } |
| 223 | |
| 224 | int32_t Grego::dayOfWeek(int32_t day) { |
| 225 | int32_t dow; |
| 226 | ClockMath::floorDivide(day + int{UCAL_THURSDAY}, 7, &dow); |
| 227 | return (dow == 0) ? UCAL_SATURDAY : dow; |
| 228 | } |
| 229 | |
| 230 | int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) { |
| 231 | int32_t weekInMonth = (dom + 6)/7; |
| 232 | if (weekInMonth == 4) { |
| 233 | if (dom + 7 > monthLength(year, month)) { |
| 234 | weekInMonth = -1; |
| 235 | } |
| 236 | } else if (weekInMonth == 5) { |
| 237 | weekInMonth = -1; |
| 238 | } |
| 239 | return weekInMonth; |
| 240 | } |
| 241 | |
| 242 | U_NAMESPACE_END |
| 243 | |
| 244 | #endif |
| 245 | //eof |