xref: /aosp_15_r20/external/cronet/third_party/icu/source/i18n/gregoimp.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
floorDivide(int32_t numerator,int32_t denominator)25 int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator) {
26     return (numerator >= 0) ?
27         numerator / denominator : ((numerator + 1) / denominator) - 1;
28 }
29 
floorDivide(int64_t numerator,int64_t denominator)30 int64_t ClockMath::floorDivide(int64_t numerator, int64_t denominator) {
31     return (numerator >= 0) ?
32         numerator / denominator : ((numerator + 1) / denominator) - 1;
33 }
34 
floorDivide(double numerator,int32_t denominator,int32_t * remainder)35 int32_t ClockMath::floorDivide(double numerator, int32_t denominator,
36                           int32_t* remainder) {
37     // For an integer n and representable ⌊x/n⌋, ⌊RN(x/n)⌋=⌊x/n⌋, where RN is
38     // rounding to nearest.
39     double quotient = uprv_floor(numerator / denominator);
40     if (remainder != nullptr) {
41       // For doubles x and n, where n is an integer and ⌊x+n⌋ < 2³¹, the
42       // expression `(int32_t) (x + n)` evaluated with rounding to nearest
43       // differs from ⌊x+n⌋ if 0 < ⌈x⌉−x ≪ x+n, as `x + n` is rounded up to
44       // n+⌈x⌉ = ⌊x+n⌋ + 1.  Rewriting it as ⌊x⌋+n makes the addition exact.
45       *remainder = (int32_t) (uprv_floor(numerator) - (quotient * denominator));
46     }
47     return (int32_t) quotient;
48 }
49 
floorDivide(double dividend,double divisor,double * remainder)50 double ClockMath::floorDivide(double dividend, double divisor,
51                          double* remainder) {
52     // Only designed to work for positive divisors
53     U_ASSERT(divisor > 0);
54     double quotient = floorDivide(dividend, divisor);
55     double r = dividend - (quotient * divisor);
56     // N.B. For certain large dividends, on certain platforms, there
57     // is a bug such that the quotient is off by one.  If you doubt
58     // this to be true, set a breakpoint below and run cintltst.
59     if (r < 0 || r >= divisor) {
60         // E.g. 6.7317038241449352e+022 / 86400000.0 is wrong on my
61         // machine (too high by one).  4.1792057231752762e+024 /
62         // 86400000.0 is wrong the other way (too low).
63         double q = quotient;
64         quotient += (r < 0) ? -1 : +1;
65         if (q == quotient) {
66             // For quotients > ~2^53, we won't be able to add or
67             // subtract one, since the LSB of the mantissa will be >
68             // 2^0; that is, the exponent (base 2) will be larger than
69             // the length, in bits, of the mantissa.  In that case, we
70             // can't give a correct answer, so we set the remainder to
71             // zero.  This has the desired effect of making extreme
72             // values give back an approximate answer rather than
73             // crashing.  For example, UDate values above a ~10^25
74             // might all have a time of midnight.
75             r = 0;
76         } else {
77             r = dividend - (quotient * divisor);
78         }
79     }
80     U_ASSERT(0 <= r && r < divisor);
81     if (remainder != nullptr) {
82         *remainder = r;
83     }
84     return quotient;
85 }
86 
87 const int32_t JULIAN_1_CE    = 1721426; // January 1, 1 CE Gregorian
88 const int32_t JULIAN_1970_CE = 2440588; // January 1, 1970 CE Gregorian
89 
90 const int16_t Grego::DAYS_BEFORE[24] =
91     {0,31,59,90,120,151,181,212,243,273,304,334,
92      0,31,60,91,121,152,182,213,244,274,305,335};
93 
94 const int8_t Grego::MONTH_LENGTH[24] =
95     {31,28,31,30,31,30,31,31,30,31,30,31,
96      31,29,31,30,31,30,31,31,30,31,30,31};
97 
fieldsToDay(int32_t year,int32_t month,int32_t dom)98 double Grego::fieldsToDay(int32_t year, int32_t month, int32_t dom) {
99 
100     int32_t y = year - 1;
101 
102     double julian = 365 * y + ClockMath::floorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal
103         ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2 + // => Gregorian cal
104         DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom
105 
106     return julian - JULIAN_1970_CE; // JD => epoch day
107 }
108 
dayToFields(double day,int32_t & year,int32_t & month,int32_t & dom,int32_t & dow,int32_t & doy)109 void Grego::dayToFields(double day, int32_t& year, int32_t& month,
110                         int32_t& dom, int32_t& dow, int32_t& doy) {
111 
112     // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar)
113     day += JULIAN_1970_CE - JULIAN_1_CE;
114 
115     // Convert from the day number to the multiple radix
116     // representation.  We use 400-year, 100-year, and 4-year cycles.
117     // For example, the 4-year cycle has 4 years + 1 leap day; giving
118     // 1461 == 365*4 + 1 days.
119     int32_t n400 = ClockMath::floorDivide(day, 146097, &doy); // 400-year cycle length
120     int32_t n100 = ClockMath::floorDivide(doy, 36524, &doy); // 100-year cycle length
121     int32_t n4   = ClockMath::floorDivide(doy, 1461, &doy); // 4-year cycle length
122     int32_t n1   = ClockMath::floorDivide(doy, 365, &doy);
123     year = 400*n400 + 100*n100 + 4*n4 + n1;
124     if (n100 == 4 || n1 == 4) {
125         doy = 365; // Dec 31 at end of 4- or 400-year cycle
126     } else {
127         ++year;
128     }
129 
130     UBool isLeap = isLeapYear(year);
131 
132     // Gregorian day zero is a Monday.
133     dow = (int32_t) uprv_fmod(day + 1, 7);
134     dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY;
135 
136     // Common Julian/Gregorian calculation
137     int32_t correction = 0;
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 + correction) + 6) / 367; // zero-based month
143     dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)] + 1; // one-based DOM
144     doy++; // one-based doy
145 }
146 
timeToFields(UDate time,int32_t & year,int32_t & month,int32_t & dom,int32_t & dow,int32_t & doy,int32_t & mid)147 void Grego::timeToFields(UDate time, int32_t& year, int32_t& month,
148                         int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid) {
149     double millisInDay;
150     double day = ClockMath::floorDivide((double)time, (double)U_MILLIS_PER_DAY, &millisInDay);
151     mid = (int32_t)millisInDay;
152     dayToFields(day, year, month, dom, dow, doy);
153 }
154 
dayOfWeek(double day)155 int32_t Grego::dayOfWeek(double day) {
156     int32_t dow;
157     ClockMath::floorDivide(day + int{UCAL_THURSDAY}, 7, &dow);
158     return (dow == 0) ? UCAL_SATURDAY : dow;
159 }
160 
dayOfWeekInMonth(int32_t year,int32_t month,int32_t dom)161 int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) {
162     int32_t weekInMonth = (dom + 6)/7;
163     if (weekInMonth == 4) {
164         if (dom + 7 > monthLength(year, month)) {
165             weekInMonth = -1;
166         }
167     } else if (weekInMonth == 5) {
168         weekInMonth = -1;
169     }
170     return weekInMonth;
171 }
172 
173 U_NAMESPACE_END
174 
175 #endif
176 //eof
177