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