11cb0ef41Sopenharmony_ci// © 2016 and later: Unicode, Inc. and others. 21cb0ef41Sopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html 31cb0ef41Sopenharmony_ci/* 41cb0ef41Sopenharmony_ci ********************************************************************** 51cb0ef41Sopenharmony_ci * Copyright (c) 2003-2008, International Business Machines 61cb0ef41Sopenharmony_ci * Corporation and others. All Rights Reserved. 71cb0ef41Sopenharmony_ci ********************************************************************** 81cb0ef41Sopenharmony_ci * Author: Alan Liu 91cb0ef41Sopenharmony_ci * Created: September 2 2003 101cb0ef41Sopenharmony_ci * Since: ICU 2.8 111cb0ef41Sopenharmony_ci ********************************************************************** 121cb0ef41Sopenharmony_ci */ 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci#include "gregoimp.h" 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci#if !UCONFIG_NO_FORMATTING 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci#include "unicode/ucal.h" 191cb0ef41Sopenharmony_ci#include "uresimp.h" 201cb0ef41Sopenharmony_ci#include "cstring.h" 211cb0ef41Sopenharmony_ci#include "uassert.h" 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciU_NAMESPACE_BEGIN 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciint32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator) { 261cb0ef41Sopenharmony_ci return (numerator >= 0) ? 271cb0ef41Sopenharmony_ci numerator / denominator : ((numerator + 1) / denominator) - 1; 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciint64_t ClockMath::floorDivide(int64_t numerator, int64_t denominator) { 311cb0ef41Sopenharmony_ci return (numerator >= 0) ? 321cb0ef41Sopenharmony_ci numerator / denominator : ((numerator + 1) / denominator) - 1; 331cb0ef41Sopenharmony_ci} 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciint32_t ClockMath::floorDivide(double numerator, int32_t denominator, 361cb0ef41Sopenharmony_ci int32_t* remainder) { 371cb0ef41Sopenharmony_ci // For an integer n and representable ⌊x/n⌋, ⌊RN(x/n)⌋=⌊x/n⌋, where RN is 381cb0ef41Sopenharmony_ci // rounding to nearest. 391cb0ef41Sopenharmony_ci double quotient = uprv_floor(numerator / denominator); 401cb0ef41Sopenharmony_ci if (remainder != nullptr) { 411cb0ef41Sopenharmony_ci // For doubles x and n, where n is an integer and ⌊x+n⌋ < 2³¹, the 421cb0ef41Sopenharmony_ci // expression `(int32_t) (x + n)` evaluated with rounding to nearest 431cb0ef41Sopenharmony_ci // differs from ⌊x+n⌋ if 0 < ⌈x⌉−x ≪ x+n, as `x + n` is rounded up to 441cb0ef41Sopenharmony_ci // n+⌈x⌉ = ⌊x+n⌋ + 1. Rewriting it as ⌊x⌋+n makes the addition exact. 451cb0ef41Sopenharmony_ci *remainder = (int32_t) (uprv_floor(numerator) - (quotient * denominator)); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci return (int32_t) quotient; 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cidouble ClockMath::floorDivide(double dividend, double divisor, 511cb0ef41Sopenharmony_ci double* remainder) { 521cb0ef41Sopenharmony_ci // Only designed to work for positive divisors 531cb0ef41Sopenharmony_ci U_ASSERT(divisor > 0); 541cb0ef41Sopenharmony_ci double quotient = floorDivide(dividend, divisor); 551cb0ef41Sopenharmony_ci double r = dividend - (quotient * divisor); 561cb0ef41Sopenharmony_ci // N.B. For certain large dividends, on certain platforms, there 571cb0ef41Sopenharmony_ci // is a bug such that the quotient is off by one. If you doubt 581cb0ef41Sopenharmony_ci // this to be true, set a breakpoint below and run cintltst. 591cb0ef41Sopenharmony_ci if (r < 0 || r >= divisor) { 601cb0ef41Sopenharmony_ci // E.g. 6.7317038241449352e+022 / 86400000.0 is wrong on my 611cb0ef41Sopenharmony_ci // machine (too high by one). 4.1792057231752762e+024 / 621cb0ef41Sopenharmony_ci // 86400000.0 is wrong the other way (too low). 631cb0ef41Sopenharmony_ci double q = quotient; 641cb0ef41Sopenharmony_ci quotient += (r < 0) ? -1 : +1; 651cb0ef41Sopenharmony_ci if (q == quotient) { 661cb0ef41Sopenharmony_ci // For quotients > ~2^53, we won't be able to add or 671cb0ef41Sopenharmony_ci // subtract one, since the LSB of the mantissa will be > 681cb0ef41Sopenharmony_ci // 2^0; that is, the exponent (base 2) will be larger than 691cb0ef41Sopenharmony_ci // the length, in bits, of the mantissa. In that case, we 701cb0ef41Sopenharmony_ci // can't give a correct answer, so we set the remainder to 711cb0ef41Sopenharmony_ci // zero. This has the desired effect of making extreme 721cb0ef41Sopenharmony_ci // values give back an approximate answer rather than 731cb0ef41Sopenharmony_ci // crashing. For example, UDate values above a ~10^25 741cb0ef41Sopenharmony_ci // might all have a time of midnight. 751cb0ef41Sopenharmony_ci r = 0; 761cb0ef41Sopenharmony_ci } else { 771cb0ef41Sopenharmony_ci r = dividend - (quotient * divisor); 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci U_ASSERT(0 <= r && r < divisor); 811cb0ef41Sopenharmony_ci if (remainder != nullptr) { 821cb0ef41Sopenharmony_ci *remainder = r; 831cb0ef41Sopenharmony_ci } 841cb0ef41Sopenharmony_ci return quotient; 851cb0ef41Sopenharmony_ci} 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ciconst int32_t JULIAN_1_CE = 1721426; // January 1, 1 CE Gregorian 881cb0ef41Sopenharmony_ciconst int32_t JULIAN_1970_CE = 2440588; // January 1, 1970 CE Gregorian 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ciconst int16_t Grego::DAYS_BEFORE[24] = 911cb0ef41Sopenharmony_ci {0,31,59,90,120,151,181,212,243,273,304,334, 921cb0ef41Sopenharmony_ci 0,31,60,91,121,152,182,213,244,274,305,335}; 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciconst int8_t Grego::MONTH_LENGTH[24] = 951cb0ef41Sopenharmony_ci {31,28,31,30,31,30,31,31,30,31,30,31, 961cb0ef41Sopenharmony_ci 31,29,31,30,31,30,31,31,30,31,30,31}; 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_cidouble Grego::fieldsToDay(int32_t year, int32_t month, int32_t dom) { 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci int32_t y = year - 1; 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci double julian = 365 * y + ClockMath::floorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal 1031cb0ef41Sopenharmony_ci ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2 + // => Gregorian cal 1041cb0ef41Sopenharmony_ci DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci return julian - JULIAN_1970_CE; // JD => epoch day 1071cb0ef41Sopenharmony_ci} 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_civoid Grego::dayToFields(double day, int32_t& year, int32_t& month, 1101cb0ef41Sopenharmony_ci int32_t& dom, int32_t& dow, int32_t& doy) { 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar) 1131cb0ef41Sopenharmony_ci day += JULIAN_1970_CE - JULIAN_1_CE; 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci // Convert from the day number to the multiple radix 1161cb0ef41Sopenharmony_ci // representation. We use 400-year, 100-year, and 4-year cycles. 1171cb0ef41Sopenharmony_ci // For example, the 4-year cycle has 4 years + 1 leap day; giving 1181cb0ef41Sopenharmony_ci // 1461 == 365*4 + 1 days. 1191cb0ef41Sopenharmony_ci int32_t n400 = ClockMath::floorDivide(day, 146097, &doy); // 400-year cycle length 1201cb0ef41Sopenharmony_ci int32_t n100 = ClockMath::floorDivide(doy, 36524, &doy); // 100-year cycle length 1211cb0ef41Sopenharmony_ci int32_t n4 = ClockMath::floorDivide(doy, 1461, &doy); // 4-year cycle length 1221cb0ef41Sopenharmony_ci int32_t n1 = ClockMath::floorDivide(doy, 365, &doy); 1231cb0ef41Sopenharmony_ci year = 400*n400 + 100*n100 + 4*n4 + n1; 1241cb0ef41Sopenharmony_ci if (n100 == 4 || n1 == 4) { 1251cb0ef41Sopenharmony_ci doy = 365; // Dec 31 at end of 4- or 400-year cycle 1261cb0ef41Sopenharmony_ci } else { 1271cb0ef41Sopenharmony_ci ++year; 1281cb0ef41Sopenharmony_ci } 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci UBool isLeap = isLeapYear(year); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci // Gregorian day zero is a Monday. 1331cb0ef41Sopenharmony_ci dow = (int32_t) uprv_fmod(day + 1, 7); 1341cb0ef41Sopenharmony_ci dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY; 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci // Common Julian/Gregorian calculation 1371cb0ef41Sopenharmony_ci int32_t correction = 0; 1381cb0ef41Sopenharmony_ci int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1 1391cb0ef41Sopenharmony_ci if (doy >= march1) { 1401cb0ef41Sopenharmony_ci correction = isLeap ? 1 : 2; 1411cb0ef41Sopenharmony_ci } 1421cb0ef41Sopenharmony_ci month = (12 * (doy + correction) + 6) / 367; // zero-based month 1431cb0ef41Sopenharmony_ci dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)] + 1; // one-based DOM 1441cb0ef41Sopenharmony_ci doy++; // one-based doy 1451cb0ef41Sopenharmony_ci} 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_civoid Grego::timeToFields(UDate time, int32_t& year, int32_t& month, 1481cb0ef41Sopenharmony_ci int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid) { 1491cb0ef41Sopenharmony_ci double millisInDay; 1501cb0ef41Sopenharmony_ci double day = ClockMath::floorDivide((double)time, (double)U_MILLIS_PER_DAY, &millisInDay); 1511cb0ef41Sopenharmony_ci mid = (int32_t)millisInDay; 1521cb0ef41Sopenharmony_ci dayToFields(day, year, month, dom, dow, doy); 1531cb0ef41Sopenharmony_ci} 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ciint32_t Grego::dayOfWeek(double day) { 1561cb0ef41Sopenharmony_ci int32_t dow; 1571cb0ef41Sopenharmony_ci ClockMath::floorDivide(day + int{UCAL_THURSDAY}, 7, &dow); 1581cb0ef41Sopenharmony_ci return (dow == 0) ? UCAL_SATURDAY : dow; 1591cb0ef41Sopenharmony_ci} 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ciint32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) { 1621cb0ef41Sopenharmony_ci int32_t weekInMonth = (dom + 6)/7; 1631cb0ef41Sopenharmony_ci if (weekInMonth == 4) { 1641cb0ef41Sopenharmony_ci if (dom + 7 > monthLength(year, month)) { 1651cb0ef41Sopenharmony_ci weekInMonth = -1; 1661cb0ef41Sopenharmony_ci } 1671cb0ef41Sopenharmony_ci } else if (weekInMonth == 5) { 1681cb0ef41Sopenharmony_ci weekInMonth = -1; 1691cb0ef41Sopenharmony_ci } 1701cb0ef41Sopenharmony_ci return weekInMonth; 1711cb0ef41Sopenharmony_ci} 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ciU_NAMESPACE_END 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci#endif 1761cb0ef41Sopenharmony_ci//eof 177