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 #ifndef GREGOIMP_H
15 #define GREGOIMP_H
16 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_FORMATTING
18
19 #include "unicode/ures.h"
20 #include "unicode/locid.h"
21 #include "putilimp.h"
22
23 U_NAMESPACE_BEGIN
24
25 /**
26 * A utility class providing mathematical functions used by time zone
27 * and calendar code. Do not instantiate. Formerly just named 'Math'.
28 * @internal
29 */
30 class ClockMath {
31 public:
32 /**
33 * Divide two integers, returning the floor of the quotient.
34 * Unlike the built-in division, this is mathematically
35 * well-behaved. E.g., <code>-1/4</code> => 0 but
36 * <code>floorDivide(-1,4)</code> => -1.
37 * @param numerator the numerator
38 * @param denominator a divisor which must be != 0
39 * @return the floor of the quotient
40 */
41 static int32_t floorDivide(int32_t numerator, int32_t denominator);
42
43 /**
44 * Divide two integers, returning the floor of the quotient.
45 * Unlike the built-in division, this is mathematically
46 * well-behaved. E.g., <code>-1/4</code> => 0 but
47 * <code>floorDivide(-1,4)</code> => -1.
48 * @param numerator the numerator
49 * @param denominator a divisor which must be != 0
50 * @return the floor of the quotient
51 */
52 static int64_t floorDivide(int64_t numerator, int64_t denominator);
53
54 /**
55 * Divide two numbers, returning the floor of the quotient.
56 * Unlike the built-in division, this is mathematically
57 * well-behaved. E.g., <code>-1/4</code> => 0 but
58 * <code>floorDivide(-1,4)</code> => -1.
59 * @param numerator the numerator
60 * @param denominator a divisor which must be != 0
61 * @return the floor of the quotient
62 */
63 static inline double floorDivide(double numerator, double denominator);
64
65 /**
66 * Divide two numbers, returning the floor of the quotient and
67 * the modulus remainder. Unlike the built-in division, this is
68 * mathematically well-behaved. E.g., <code>-1/4</code> => 0 and
69 * <code>-1%4</code> => -1, but <code>floorDivide(-1,4)</code> =>
70 * -1 with <code>remainder</code> => 3. NOTE: If numerator is
71 * too large, the returned quotient may overflow.
72 * @param numerator the numerator
73 * @param denominator a divisor which must be != 0
74 * @param remainder output parameter to receive the
75 * remainder. Unlike <code>numerator % denominator</code>, this
76 * will always be non-negative, in the half-open range <code>[0,
77 * |denominator|)</code>.
78 * @return the floor of the quotient
79 */
80 static int32_t floorDivide(double numerator, int32_t denominator,
81 int32_t* remainder);
82
83 /**
84 * For a positive divisor, return the quotient and remainder
85 * such that dividend = quotient*divisor + remainder and
86 * 0 <= remainder < divisor.
87 *
88 * Works around edge-case bugs. Handles pathological input
89 * (dividend >> divisor) reasonably.
90 *
91 * Calling with a divisor <= 0 is disallowed.
92 */
93 static double floorDivide(double dividend, double divisor,
94 double* remainder);
95 };
96
97 // Useful millisecond constants
98 #define kOneDay (1.0 * U_MILLIS_PER_DAY) // 86,400,000
99 #define kOneHour (60*60*1000)
100 #define kOneMinute 60000
101 #define kOneSecond 1000
102 #define kOneMillisecond 1
103 #define kOneWeek (7.0 * kOneDay) // 604,800,000
104
105 // Epoch constants
106 #define kJan1_1JulianDay 1721426 // January 1, year 1 (Gregorian)
107
108 #define kEpochStartAsJulianDay 2440588 // January 1, 1970 (Gregorian)
109
110 #define kEpochYear 1970
111
112
113 #define kEarliestViableMillis -185331720384000000.0 // minimum representable by julian day -1e17
114
115 #define kLatestViableMillis 185753453990400000.0 // max representable by julian day +1e17
116
117 /**
118 * The minimum supported Julian day. This value is equivalent to
119 * MIN_MILLIS.
120 */
121 #define MIN_JULIAN (-0x7F000000)
122
123 /**
124 * The minimum supported epoch milliseconds. This value is equivalent
125 * to MIN_JULIAN.
126 */
127 #define MIN_MILLIS ((MIN_JULIAN - kEpochStartAsJulianDay) * kOneDay)
128
129 /**
130 * The maximum supported Julian day. This value is equivalent to
131 * MAX_MILLIS.
132 */
133 #define MAX_JULIAN (+0x7F000000)
134
135 /**
136 * The maximum supported epoch milliseconds. This value is equivalent
137 * to MAX_JULIAN.
138 */
139 #define MAX_MILLIS ((MAX_JULIAN - kEpochStartAsJulianDay) * kOneDay)
140
141 /**
142 * A utility class providing proleptic Gregorian calendar functions
143 * used by time zone and calendar code. Do not instantiate.
144 *
145 * Note: Unlike GregorianCalendar, all computations performed by this
146 * class occur in the pure proleptic GregorianCalendar.
147 */
148 class Grego {
149 public:
150 /**
151 * Return true if the given year is a leap year.
152 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
153 * @return true if the year is a leap year
154 */
155 static inline UBool isLeapYear(int32_t year);
156
157 /**
158 * Return the number of days in the given month.
159 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
160 * @param month 0-based month, with 0==Jan
161 * @return the number of days in the given month
162 */
163 static inline int8_t monthLength(int32_t year, int32_t month);
164
165 /**
166 * Return the length of a previous month of the Gregorian calendar.
167 * @param y the extended year
168 * @param m the 0-based month number
169 * @return the number of days in the month previous to the given month
170 */
171 static inline int8_t previousMonthLength(int y, int m);
172
173 /**
174 * Convert a year, month, and day-of-month, given in the proleptic
175 * Gregorian calendar, to 1970 epoch days.
176 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
177 * @param month 0-based month, with 0==Jan
178 * @param dom 1-based day of month
179 * @return the day number, with day 0 == Jan 1 1970
180 */
181 static double fieldsToDay(int32_t year, int32_t month, int32_t dom);
182
183 /**
184 * Convert a 1970-epoch day number to proleptic Gregorian year,
185 * month, day-of-month, and day-of-week.
186 * @param day 1970-epoch day (integral value)
187 * @param year output parameter to receive year
188 * @param month output parameter to receive month (0-based, 0==Jan)
189 * @param dom output parameter to receive day-of-month (1-based)
190 * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
191 * @param doy output parameter to receive day-of-year (1-based)
192 * @param status error code.
193 */
194 static void dayToFields(double day, int32_t& year, int32_t& month,
195 int32_t& dom, int32_t& dow, int32_t& doy, UErrorCode& status);
196
197 /**
198 * Convert a 1970-epoch day number to proleptic Gregorian year,
199 * month, day-of-month, and day-of-week.
200 * @param day 1970-epoch day (integral value)
201 * @param year output parameter to receive year
202 * @param month output parameter to receive month (0-based, 0==Jan)
203 * @param dom output parameter to receive day-of-month (1-based)
204 * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
205 * @param status error code.
206 */
207 static inline void dayToFields(double day, int32_t& year, int32_t& month,
208 int32_t& dom, int32_t& dow, UErrorCode& status);
209
210 /**
211 * Convert a 1970-epoch milliseconds to proleptic Gregorian year,
212 * month, day-of-month, and day-of-week, day of year and millis-in-day.
213 * @param time 1970-epoch milliseconds
214 * @param year output parameter to receive year
215 * @param month output parameter to receive month (0-based, 0==Jan)
216 * @param dom output parameter to receive day-of-month (1-based)
217 * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
218 * @param doy output parameter to receive day-of-year (1-based)
219 * @param mid output parameter to receive millis-in-day
220 * @param status error code.
221 */
222 static void timeToFields(UDate time, int32_t& year, int32_t& month,
223 int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid, UErrorCode& status);
224
225 /**
226 * Return the day of week on the 1970-epoch day
227 * @param day the 1970-epoch day (integral value)
228 * @return the day of week
229 */
230 static int32_t dayOfWeek(double day);
231
232 /**
233 * Returns the ordinal number for the specified day of week within the month.
234 * The valid return value is 1, 2, 3, 4 or -1.
235 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
236 * @param month 0-based month, with 0==Jan
237 * @param dom 1-based day of month
238 * @return The ordinal number for the specified day of week within the month
239 */
240 static int32_t dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom);
241
242 /**
243 * Converts Julian day to time as milliseconds.
244 * @param julian the given Julian day number.
245 * @return time as milliseconds.
246 * @internal
247 */
248 static inline double julianDayToMillis(int32_t julian);
249
250 /**
251 * Converts time as milliseconds to Julian day.
252 * @param millis the given milliseconds.
253 * @return the Julian day number.
254 * @internal
255 */
256 static inline int32_t millisToJulianDay(double millis);
257
258 /**
259 * Calculates the Gregorian day shift value for an extended year.
260 * @param eyear Extended year
261 * @returns number of days to ADD to Julian in order to convert from J->G
262 */
263 static inline int32_t gregorianShift(int32_t eyear);
264
265 private:
266 static const int16_t DAYS_BEFORE[24];
267 static const int8_t MONTH_LENGTH[24];
268 };
269
floorDivide(double numerator,double denominator)270 inline double ClockMath::floorDivide(double numerator, double denominator) {
271 return uprv_floor(numerator / denominator);
272 }
273
isLeapYear(int32_t year)274 inline UBool Grego::isLeapYear(int32_t year) {
275 // year&0x3 == year%4
276 return ((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0));
277 }
278
279 inline int8_t
monthLength(int32_t year,int32_t month)280 Grego::monthLength(int32_t year, int32_t month) {
281 return MONTH_LENGTH[month + (isLeapYear(year) ? 12 : 0)];
282 }
283
284 inline int8_t
previousMonthLength(int y,int m)285 Grego::previousMonthLength(int y, int m) {
286 return (m > 0) ? monthLength(y, m-1) : 31;
287 }
288
dayToFields(double day,int32_t & year,int32_t & month,int32_t & dom,int32_t & dow,UErrorCode & status)289 inline void Grego::dayToFields(double day, int32_t& year, int32_t& month,
290 int32_t& dom, int32_t& dow, UErrorCode& status) {
291 int32_t doy_unused;
292 dayToFields(day,year,month,dom,dow,doy_unused, status);
293 }
294
julianDayToMillis(int32_t julian)295 inline double Grego::julianDayToMillis(int32_t julian)
296 {
297 return (julian - kEpochStartAsJulianDay) * kOneDay;
298 }
299
millisToJulianDay(double millis)300 inline int32_t Grego::millisToJulianDay(double millis) {
301 return (int32_t) (kEpochStartAsJulianDay + ClockMath::floorDivide(millis, (double)kOneDay));
302 }
303
gregorianShift(int32_t eyear)304 inline int32_t Grego::gregorianShift(int32_t eyear) {
305 int64_t y = (int64_t)eyear-1;
306 int32_t gregShift = static_cast<int32_t>(ClockMath::floorDivide(y, (int64_t)400) - ClockMath::floorDivide(y, (int64_t)100) + 2);
307 return gregShift;
308 }
309
310 U_NAMESPACE_END
311
312 #endif // !UCONFIG_NO_FORMATTING
313 #endif // GREGOIMP_H
314
315 //eof
316