• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  **********************************************************************
3  * Copyright (c) 2003-2007, International Business Machines
4  * Corporation and others.  All Rights Reserved.
5  **********************************************************************
6  * Author: Alan Liu
7  * Created: September 2 2003
8  * Since: ICU 2.8
9  **********************************************************************
10  */
11 
12 #ifndef GREGOIMP_H
13 #define GREGOIMP_H
14 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_FORMATTING
16 
17 #include "unicode/ures.h"
18 #include "unicode/locid.h"
19 #include "putilimp.h"
20 
21 U_NAMESPACE_BEGIN
22 
23 /**
24  * A utility class providing mathematical functions used by time zone
25  * and calendar code.  Do not instantiate.
26  */
27 class Math {
28  public:
29     /**
30      * Divide two integers, returning the floor of the quotient.
31      * Unlike the built-in division, this is mathematically
32      * well-behaved.  E.g., <code>-1/4</code> => 0 but
33      * <code>floorDivide(-1,4)</code> => -1.
34      * @param numerator the numerator
35      * @param denominator a divisor which must be != 0
36      * @return the floor of the quotient
37      */
38     static int32_t floorDivide(int32_t numerator, int32_t denominator);
39 
40     /**
41      * Divide two numbers, returning the floor of the quotient.
42      * Unlike the built-in division, this is mathematically
43      * well-behaved.  E.g., <code>-1/4</code> => 0 but
44      * <code>floorDivide(-1,4)</code> => -1.
45      * @param numerator the numerator
46      * @param denominator a divisor which must be != 0
47      * @return the floor of the quotient
48      */
49     static inline double floorDivide(double numerator, double denominator);
50 
51     /**
52      * Divide two numbers, returning the floor of the quotient and
53      * the modulus remainder.  Unlike the built-in division, this is
54      * mathematically well-behaved.  E.g., <code>-1/4</code> => 0 and
55      * <code>-1%4</code> => -1, but <code>floorDivide(-1,4)</code> =>
56      * -1 with <code>remainder</code> => 3.  NOTE: If numerator is
57      * too large, the returned quotient may overflow.
58      * @param numerator the numerator
59      * @param denominator a divisor which must be != 0
60      * @param remainder output parameter to receive the
61      * remainder. Unlike <code>numerator % denominator</code>, this
62      * will always be non-negative, in the half-open range <code>[0,
63      * |denominator|)</code>.
64      * @return the floor of the quotient
65      */
66     static int32_t floorDivide(double numerator, int32_t denominator,
67                                int32_t& remainder);
68 
69     /**
70      * For a positive divisor, return the quotient and remainder
71      * such that dividend = quotient*divisor + remainder and
72      * 0 <= remainder < divisor.
73      *
74      * Works around edge-case bugs.  Handles pathological input
75      * (divident >> divisor) reasonably.
76      *
77      * Calling with a divisor <= 0 is disallowed.
78      */
79     static double floorDivide(double dividend, double divisor,
80                               double& remainder);
81 };
82 
83 // Useful millisecond constants
84 #define kOneDay    (1.0 * U_MILLIS_PER_DAY)       //  86,400,000
85 #define kOneHour   (60*60*1000)
86 #define kOneMinute 60000
87 #define kOneSecond 1000
88 #define kOneMillisecond  1
89 #define kOneWeek   (7.0 * kOneDay) // 604,800,000
90 
91 // Epoch constants
92 #define kJan1_1JulianDay  1721426 // January 1, year 1 (Gregorian)
93 
94 #define kEpochStartAsJulianDay  2440588 // January 1, 1970 (Gregorian)
95 
96 #define kEpochYear              1970
97 
98 
99 #define kEarliestViableMillis  -185331720384000000.0  // minimum representable by julian day  -1e17
100 
101 #define kLatestViableMillis     185753453990400000.0  // max representable by julian day      +1e17
102 
103 /**
104  * The minimum supported Julian day.  This value is equivalent to
105  * MIN_MILLIS.
106  */
107 #define MIN_JULIAN (-0x7F000000)
108 
109 /**
110  * The minimum supported epoch milliseconds.  This value is equivalent
111  * to MIN_JULIAN.
112  */
113 #define MIN_MILLIS ((MIN_JULIAN - kEpochStartAsJulianDay) * kOneDay)
114 
115 /**
116  * The maximum supported Julian day.  This value is equivalent to
117  * MAX_MILLIS.
118  */
119 #define MAX_JULIAN (+0x7F000000)
120 
121 /**
122  * The maximum supported epoch milliseconds.  This value is equivalent
123  * to MAX_JULIAN.
124  */
125 #define MAX_MILLIS ((MAX_JULIAN - kEpochStartAsJulianDay) * kOneDay)
126 
127 /**
128  * A utility class providing proleptic Gregorian calendar functions
129  * used by time zone and calendar code.  Do not instantiate.
130  *
131  * Note:  Unlike GregorianCalendar, all computations performed by this
132  * class occur in the pure proleptic GregorianCalendar.
133  */
134 class Grego {
135  public:
136     /**
137      * Return TRUE if the given year is a leap year.
138      * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
139      * @return TRUE if the year is a leap year
140      */
141     static inline UBool isLeapYear(int32_t year);
142 
143     /**
144      * Return the number of days in the given month.
145      * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
146      * @param month 0-based month, with 0==Jan
147      * @return the number of days in the given month
148      */
149     static inline int8_t monthLength(int32_t year, int32_t month);
150 
151     /**
152      * Return the length of a previous month of the Gregorian calendar.
153      * @param y the extended year
154      * @param m the 0-based month number
155      * @return the number of days in the month previous to the given month
156      */
157     static inline int8_t previousMonthLength(int y, int m);
158 
159     /**
160      * Convert a year, month, and day-of-month, given in the proleptic
161      * Gregorian calendar, to 1970 epoch days.
162      * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
163      * @param month 0-based month, with 0==Jan
164      * @param dom 1-based day of month
165      * @return the day number, with day 0 == Jan 1 1970
166      */
167     static double fieldsToDay(int32_t year, int32_t month, int32_t dom);
168 
169     /**
170      * Convert a 1970-epoch day number to proleptic Gregorian year,
171      * month, day-of-month, and day-of-week.
172      * @param day 1970-epoch day (integral value)
173      * @param year output parameter to receive year
174      * @param month output parameter to receive month (0-based, 0==Jan)
175      * @param dom output parameter to receive day-of-month (1-based)
176      * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
177      * @param doy output parameter to receive day-of-year (1-based)
178      */
179     static void dayToFields(double day, int32_t& year, int32_t& month,
180                             int32_t& dom, int32_t& dow, int32_t& doy);
181 
182     /**
183      * Convert a 1970-epoch day number to proleptic Gregorian year,
184      * month, day-of-month, and day-of-week.
185      * @param day 1970-epoch day (integral value)
186      * @param year output parameter to receive year
187      * @param month output parameter to receive month (0-based, 0==Jan)
188      * @param dom output parameter to receive day-of-month (1-based)
189      * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
190      */
191     static inline void dayToFields(double day, int32_t& year, int32_t& month,
192                                    int32_t& dom, int32_t& dow);
193 
194     /**
195      * Convert a 1970-epoch milliseconds to proleptic Gregorian year,
196      * month, day-of-month, and day-of-week, day of year and millis-in-day.
197      * @param time 1970-epoch milliseconds
198      * @param year output parameter to receive year
199      * @param month output parameter to receive month (0-based, 0==Jan)
200      * @param dom output parameter to receive day-of-month (1-based)
201      * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
202      * @param doy output parameter to receive day-of-year (1-based)
203      * @param mid output parameter to recieve millis-in-day
204      */
205     static void timeToFields(UDate time, int32_t& year, int32_t& month,
206                             int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid);
207 
208     /**
209      * Return the day of week on the 1970-epoch day
210      * @param day the 1970-epoch day (integral value)
211      * @return the day of week
212      */
213     static int32_t dayOfWeek(double day);
214 
215     /**
216      * Returns the ordinal number for the specified day of week within the month.
217      * The valid return value is 1, 2, 3, 4 or -1.
218      * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
219      * @param month 0-based month, with 0==Jan
220      * @param dom 1-based day of month
221      * @return The ordinal number for the specified day of week within the month
222      */
223     static int32_t dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom);
224 
225     /**
226      * Converts Julian day to time as milliseconds.
227      * @param julian the given Julian day number.
228      * @return time as milliseconds.
229      * @internal
230      */
231     static inline double julianDayToMillis(int32_t julian);
232 
233     /**
234      * Converts time as milliseconds to Julian day.
235      * @param millis the given milliseconds.
236      * @return the Julian day number.
237      * @internal
238      */
239     static inline int32_t millisToJulianDay(double millis);
240 
241     /**
242      * Calculates the Gregorian day shift value for an extended year.
243      * @param eyear Extended year
244      * @returns number of days to ADD to Julian in order to convert from J->G
245      */
246     static inline int32_t gregorianShift(int32_t eyear);
247 
248  private:
249     static const int16_t DAYS_BEFORE[24];
250     static const int8_t MONTH_LENGTH[24];
251 };
252 
floorDivide(double numerator,double denominator)253 inline double Math::floorDivide(double numerator, double denominator) {
254     return uprv_floor(numerator / denominator);
255 }
256 
isLeapYear(int32_t year)257 inline UBool Grego::isLeapYear(int32_t year) {
258     // year&0x3 == year%4
259     return ((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0));
260 }
261 
262 inline int8_t
monthLength(int32_t year,int32_t month)263 Grego::monthLength(int32_t year, int32_t month) {
264     return MONTH_LENGTH[month + (isLeapYear(year) ? 12 : 0)];
265 }
266 
267 inline int8_t
previousMonthLength(int y,int m)268 Grego::previousMonthLength(int y, int m) {
269   return (m > 0) ? monthLength(y, m-1) : 31;
270 }
271 
dayToFields(double day,int32_t & year,int32_t & month,int32_t & dom,int32_t & dow)272 inline void Grego::dayToFields(double day, int32_t& year, int32_t& month,
273                                int32_t& dom, int32_t& dow) {
274   int32_t doy_unused;
275   dayToFields(day,year,month,dom,dow,doy_unused);
276 }
277 
julianDayToMillis(int32_t julian)278 inline double Grego::julianDayToMillis(int32_t julian)
279 {
280   return (julian - kEpochStartAsJulianDay) * kOneDay;
281 }
282 
millisToJulianDay(double millis)283 inline int32_t Grego::millisToJulianDay(double millis) {
284   return (int32_t) (kEpochStartAsJulianDay + Math::floorDivide(millis, (double)kOneDay));
285 }
286 
gregorianShift(int32_t eyear)287 inline int32_t Grego::gregorianShift(int32_t eyear) {
288   int32_t y = eyear-1;
289   int32_t gregShift = Math::floorDivide(y, 400) - Math::floorDivide(y, 100) + 2;
290   return gregShift;
291 }
292 
293 /**
294  * This utility class provides convenient access to the data needed for a calendar.
295  * @internal ICU 3.0
296  */
297 class CalendarData : public UMemory {
298 public:
299     /**
300      * Construct a CalendarData from the given locale.
301      * @param loc locale to use. The 'calendar' keyword will be ignored.
302      * @param type calendar type. NULL indicates the gregorian calendar.
303      * No default lookup is done.
304      * @param status error code
305      */
306     CalendarData(const Locale& loc, const char *type, UErrorCode& status);
307 
308     /**
309      * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!
310      * The ResourceBundle C++ API should NOT be used because it is too slow for a low level API.
311      *
312      * @param key Resource key to data
313      * @param status Error Status
314      * @internal
315      */
316     UResourceBundle* getByKey(const char *key, UErrorCode& status);
317 
318     /**
319      * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!
320      * There is an implicit key of 'format'
321      * data is located in:   "calendar/key/format/subKey"
322      * for example,  calendar/dayNames/format/abbreviated
323      * The ResourceBundle C++ API should NOT be used because it is too slow for a low level API.
324      *
325      * @param key Resource key to data
326      * @param subKey Resource key to data
327      * @param status Error Status
328      * @internal
329      */
330     UResourceBundle* getByKey2(const char *key, const char *subKey, UErrorCode& status);
331 
332     /**
333      * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!
334      * data is located in:   "calendar/key/contextKey/subKey"
335      * for example,  calendar/dayNames/standalone/narrow
336      * The ResourceBundle C++ API should NOT be used because it is too slow for a low level API.
337      *
338      * @param key Resource key to data
339      * @param contextKey Resource key to data
340      * @param subKey Resource key to data
341      * @param status Error Status
342      * @internal
343      */
344     UResourceBundle* getByKey3(const char *key, const char *contextKey, const char *subKey, UErrorCode& status);
345 
346     ~CalendarData();
347 
348 private:
349     void initData(const char *locale, const char *type, UErrorCode& status);
350 
351     UResourceBundle *fFillin;
352     UResourceBundle *fOtherFillin;
353     UResourceBundle *fBundle;
354     UResourceBundle *fFallback;
355     CalendarData(); // Not implemented.
356 };
357 
358 U_NAMESPACE_END
359 
360 #endif // !UCONFIG_NO_FORMATTING
361 #endif // GREGOIMP_H
362 
363 //eof
364