1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2003-2008, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * Created: September 2 2003
11 **********************************************************************
16 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/ures.h"
20 #include "unicode/locid.h"
26 * A utility class providing mathematical functions used by time zone
27 * and calendar code. Do not instantiate. Formerly just named 'Math'.
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
41 static int32_t floorDivide(int32_t numerator, int32_t denominator);
44 * Divide two numbers, 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
52 static inline double floorDivide(double numerator, double denominator);
55 * Divide two numbers, returning the floor of the quotient and
56 * the modulus remainder. Unlike the built-in division, this is
57 * mathematically well-behaved. E.g., <code>-1/4</code> => 0 and
58 * <code>-1%4</code> => -1, but <code>floorDivide(-1,4)</code> =>
59 * -1 with <code>remainder</code> => 3. NOTE: If numerator is
60 * too large, the returned quotient may overflow.
61 * @param numerator the numerator
62 * @param denominator a divisor which must be != 0
63 * @param remainder output parameter to receive the
64 * remainder. Unlike <code>numerator % denominator</code>, this
65 * will always be non-negative, in the half-open range <code>[0,
66 * |denominator|)</code>.
67 * @return the floor of the quotient
69 static int32_t floorDivide(double numerator, int32_t denominator,
73 * For a positive divisor, return the quotient and remainder
74 * such that dividend = quotient*divisor + remainder and
75 * 0 <= remainder < divisor.
77 * Works around edge-case bugs. Handles pathological input
78 * (divident >> divisor) reasonably.
80 * Calling with a divisor <= 0 is disallowed.
82 static double floorDivide(double dividend, double divisor,
86 // Useful millisecond constants
87 #define kOneDay (1.0 * U_MILLIS_PER_DAY) // 86,400,000
88 #define kOneHour (60*60*1000)
89 #define kOneMinute 60000
90 #define kOneSecond 1000
91 #define kOneMillisecond 1
92 #define kOneWeek (7.0 * kOneDay) // 604,800,000
95 #define kJan1_1JulianDay 1721426 // January 1, year 1 (Gregorian)
97 #define kEpochStartAsJulianDay 2440588 // January 1, 1970 (Gregorian)
99 #define kEpochYear 1970
102 #define kEarliestViableMillis -185331720384000000.0 // minimum representable by julian day -1e17
104 #define kLatestViableMillis 185753453990400000.0 // max representable by julian day +1e17
107 * The minimum supported Julian day. This value is equivalent to
110 #define MIN_JULIAN (-0x7F000000)
113 * The minimum supported epoch milliseconds. This value is equivalent
116 #define MIN_MILLIS ((MIN_JULIAN - kEpochStartAsJulianDay) * kOneDay)
119 * The maximum supported Julian day. This value is equivalent to
122 #define MAX_JULIAN (+0x7F000000)
125 * The maximum supported epoch milliseconds. This value is equivalent
128 #define MAX_MILLIS ((MAX_JULIAN - kEpochStartAsJulianDay) * kOneDay)
131 * A utility class providing proleptic Gregorian calendar functions
132 * used by time zone and calendar code. Do not instantiate.
134 * Note: Unlike GregorianCalendar, all computations performed by this
135 * class occur in the pure proleptic GregorianCalendar.
140 * Return TRUE if the given year is a leap year.
141 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
142 * @return TRUE if the year is a leap year
144 static inline UBool isLeapYear(int32_t year);
147 * Return the number of days in the given month.
148 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
149 * @param month 0-based month, with 0==Jan
150 * @return the number of days in the given month
152 static inline int8_t monthLength(int32_t year, int32_t month);
155 * Return the length of a previous month of the Gregorian calendar.
156 * @param y the extended year
157 * @param m the 0-based month number
158 * @return the number of days in the month previous to the given month
160 static inline int8_t previousMonthLength(int y, int m);
163 * Convert a year, month, and day-of-month, given in the proleptic
164 * Gregorian calendar, to 1970 epoch days.
165 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
166 * @param month 0-based month, with 0==Jan
167 * @param dom 1-based day of month
168 * @return the day number, with day 0 == Jan 1 1970
170 static double fieldsToDay(int32_t year, int32_t month, int32_t dom);
173 * Convert a 1970-epoch day number to proleptic Gregorian year,
174 * month, day-of-month, and day-of-week.
175 * @param day 1970-epoch day (integral value)
176 * @param year output parameter to receive year
177 * @param month output parameter to receive month (0-based, 0==Jan)
178 * @param dom output parameter to receive day-of-month (1-based)
179 * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
180 * @param doy output parameter to receive day-of-year (1-based)
182 static void dayToFields(double day, int32_t& year, int32_t& month,
183 int32_t& dom, int32_t& dow, int32_t& doy);
186 * Convert a 1970-epoch day number to proleptic Gregorian year,
187 * month, day-of-month, and day-of-week.
188 * @param day 1970-epoch day (integral value)
189 * @param year output parameter to receive year
190 * @param month output parameter to receive month (0-based, 0==Jan)
191 * @param dom output parameter to receive day-of-month (1-based)
192 * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
194 static inline void dayToFields(double day, int32_t& year, int32_t& month,
195 int32_t& dom, int32_t& dow);
198 * Convert a 1970-epoch milliseconds to proleptic Gregorian year,
199 * month, day-of-month, and day-of-week, day of year and millis-in-day.
200 * @param time 1970-epoch milliseconds
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 doy output parameter to receive day-of-year (1-based)
206 * @param mid output parameter to recieve millis-in-day
208 static void timeToFields(UDate time, int32_t& year, int32_t& month,
209 int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid);
212 * Return the day of week on the 1970-epoch day
213 * @param day the 1970-epoch day (integral value)
214 * @return the day of week
216 static int32_t dayOfWeek(double day);
219 * Returns the ordinal number for the specified day of week within the month.
220 * The valid return value is 1, 2, 3, 4 or -1.
221 * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
222 * @param month 0-based month, with 0==Jan
223 * @param dom 1-based day of month
224 * @return The ordinal number for the specified day of week within the month
226 static int32_t dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom);
229 * Converts Julian day to time as milliseconds.
230 * @param julian the given Julian day number.
231 * @return time as milliseconds.
234 static inline double julianDayToMillis(int32_t julian);
237 * Converts time as milliseconds to Julian day.
238 * @param millis the given milliseconds.
239 * @return the Julian day number.
242 static inline int32_t millisToJulianDay(double millis);
245 * Calculates the Gregorian day shift value for an extended year.
246 * @param eyear Extended year
247 * @returns number of days to ADD to Julian in order to convert from J->G
249 static inline int32_t gregorianShift(int32_t eyear);
252 static const int16_t DAYS_BEFORE[24];
253 static const int8_t MONTH_LENGTH[24];
256 inline double ClockMath::floorDivide(double numerator, double denominator) {
257 return uprv_floor(numerator / denominator);
260 inline UBool Grego::isLeapYear(int32_t year) {
261 // year&0x3 == year%4
262 return ((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0));
266 Grego::monthLength(int32_t year, int32_t month) {
267 return MONTH_LENGTH[month + (isLeapYear(year) ? 12 : 0)];
271 Grego::previousMonthLength(int y, int m) {
272 return (m > 0) ? monthLength(y, m-1) : 31;
275 inline void Grego::dayToFields(double day, int32_t& year, int32_t& month,
276 int32_t& dom, int32_t& dow) {
278 dayToFields(day,year,month,dom,dow,doy_unused);
281 inline double Grego::julianDayToMillis(int32_t julian)
283 return (julian - kEpochStartAsJulianDay) * kOneDay;
286 inline int32_t Grego::millisToJulianDay(double millis) {
287 return (int32_t) (kEpochStartAsJulianDay + ClockMath::floorDivide(millis, (double)kOneDay));
290 inline int32_t Grego::gregorianShift(int32_t eyear) {
292 int32_t gregShift = ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2;
298 #endif // !UCONFIG_NO_FORMATTING