xref: /aosp_15_r20/external/icu/icu4c/source/i18n/gregoimp.cpp (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
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 
floorDivideInt64(int64_t numerator,int64_t denominator)30 int64_t ClockMath::floorDivideInt64(int64_t numerator, int64_t denominator) {
31     return (numerator >= 0) ?
32         numerator / denominator : ((numerator + 1) / denominator) - 1;
33 }
34 
floorDivide(int32_t numerator,int32_t denominator,int32_t * remainder)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 
floorDivide(double numerator,int32_t denominator,int32_t * remainder)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 = (int32_t) (uprv_floor(numerator) - (quotient * denominator));
55     }
56     return quotient;
57 }
58 
floorDivide(double dividend,double divisor,double * remainder)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 
fieldsToDay(int32_t year,int32_t month,int32_t dom)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 
dayToFields(int32_t day,int32_t & year,int32_t & month,int32_t & dom,int32_t & dow,int32_t & doy)120 void Grego::dayToFields(int32_t day, int32_t& year, int32_t& month,
121                         int32_t& dom, int32_t& dow, int32_t& doy) {
122 
123     // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar)
124     day += JULIAN_1970_CE - JULIAN_1_CE;
125 
126     // Convert from the day number to the multiple radix
127     // representation.  We use 400-year, 100-year, and 4-year cycles.
128     // For example, the 4-year cycle has 4 years + 1 leap day; giving
129     // 1461 == 365*4 + 1 days.
130     int32_t n400 = ClockMath::floorDivide(day, 146097, &doy); // 400-year cycle length
131     int32_t n100 = ClockMath::floorDivide(doy, 36524, &doy); // 100-year cycle length
132     int32_t n4   = ClockMath::floorDivide(doy, 1461, &doy); // 4-year cycle length
133     int32_t n1   = ClockMath::floorDivide(doy, 365, &doy);
134     year = 400*n400 + 100*n100 + 4*n4 + n1;
135     if (n100 == 4 || n1 == 4) {
136         doy = 365; // Dec 31 at end of 4- or 400-year cycle
137     } else {
138         ++year;
139     }
140 
141     UBool isLeap = isLeapYear(year);
142 
143     // Gregorian day zero is a Monday.
144     dow = (day + 1) % 7;
145     dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY;
146 
147     // Common Julian/Gregorian calculation
148     int32_t correction = 0;
149     int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1
150     if (doy >= march1) {
151         correction = isLeap ? 1 : 2;
152     }
153     month = (12 * (doy + correction) + 6) / 367; // zero-based month
154     dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)] + 1; // one-based DOM
155     doy++; // one-based doy
156 }
157 
timeToFields(UDate time,int32_t & year,int32_t & month,int32_t & dom,int32_t & dow,int32_t & doy,int32_t & mid)158 void Grego::timeToFields(UDate time, int32_t& year, int32_t& month,
159                         int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid) {
160     double millisInDay;
161     double day = ClockMath::floorDivide((double)time, (double)U_MILLIS_PER_DAY, &millisInDay);
162     mid = (int32_t)millisInDay;
163     dayToFields(day, year, month, dom, dow, doy);
164 }
165 
dayOfWeek(int32_t day)166 int32_t Grego::dayOfWeek(int32_t day) {
167     int32_t dow;
168     ClockMath::floorDivide(day + int{UCAL_THURSDAY}, 7, &dow);
169     return (dow == 0) ? UCAL_SATURDAY : dow;
170 }
171 
dayOfWeekInMonth(int32_t year,int32_t month,int32_t dom)172 int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) {
173     int32_t weekInMonth = (dom + 6)/7;
174     if (weekInMonth == 4) {
175         if (dom + 7 > monthLength(year, month)) {
176             weekInMonth = -1;
177         }
178     } else if (weekInMonth == 5) {
179         weekInMonth = -1;
180     }
181     return weekInMonth;
182 }
183 
184 U_NAMESPACE_END
185 
186 #endif
187 //eof
188