michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (c) 2003-2008, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * Author: Alan Liu michael@0: * Created: September 2 2003 michael@0: * Since: ICU 2.8 michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #ifndef GREGOIMP_H michael@0: #define GREGOIMP_H michael@0: #include "unicode/utypes.h" michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ures.h" michael@0: #include "unicode/locid.h" michael@0: #include "putilimp.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * A utility class providing mathematical functions used by time zone michael@0: * and calendar code. Do not instantiate. Formerly just named 'Math'. michael@0: * @internal michael@0: */ michael@0: class ClockMath { michael@0: public: michael@0: /** michael@0: * Divide two integers, returning the floor of the quotient. michael@0: * Unlike the built-in division, this is mathematically michael@0: * well-behaved. E.g., -1/4 => 0 but michael@0: * floorDivide(-1,4) => -1. michael@0: * @param numerator the numerator michael@0: * @param denominator a divisor which must be != 0 michael@0: * @return the floor of the quotient michael@0: */ michael@0: static int32_t floorDivide(int32_t numerator, int32_t denominator); michael@0: michael@0: /** michael@0: * Divide two numbers, returning the floor of the quotient. michael@0: * Unlike the built-in division, this is mathematically michael@0: * well-behaved. E.g., -1/4 => 0 but michael@0: * floorDivide(-1,4) => -1. michael@0: * @param numerator the numerator michael@0: * @param denominator a divisor which must be != 0 michael@0: * @return the floor of the quotient michael@0: */ michael@0: static inline double floorDivide(double numerator, double denominator); michael@0: michael@0: /** michael@0: * Divide two numbers, returning the floor of the quotient and michael@0: * the modulus remainder. Unlike the built-in division, this is michael@0: * mathematically well-behaved. E.g., -1/4 => 0 and michael@0: * -1%4 => -1, but floorDivide(-1,4) => michael@0: * -1 with remainder => 3. NOTE: If numerator is michael@0: * too large, the returned quotient may overflow. michael@0: * @param numerator the numerator michael@0: * @param denominator a divisor which must be != 0 michael@0: * @param remainder output parameter to receive the michael@0: * remainder. Unlike numerator % denominator, this michael@0: * will always be non-negative, in the half-open range [0, michael@0: * |denominator|). michael@0: * @return the floor of the quotient michael@0: */ michael@0: static int32_t floorDivide(double numerator, int32_t denominator, michael@0: int32_t& remainder); michael@0: michael@0: /** michael@0: * For a positive divisor, return the quotient and remainder michael@0: * such that dividend = quotient*divisor + remainder and michael@0: * 0 <= remainder < divisor. michael@0: * michael@0: * Works around edge-case bugs. Handles pathological input michael@0: * (divident >> divisor) reasonably. michael@0: * michael@0: * Calling with a divisor <= 0 is disallowed. michael@0: */ michael@0: static double floorDivide(double dividend, double divisor, michael@0: double& remainder); michael@0: }; michael@0: michael@0: // Useful millisecond constants michael@0: #define kOneDay (1.0 * U_MILLIS_PER_DAY) // 86,400,000 michael@0: #define kOneHour (60*60*1000) michael@0: #define kOneMinute 60000 michael@0: #define kOneSecond 1000 michael@0: #define kOneMillisecond 1 michael@0: #define kOneWeek (7.0 * kOneDay) // 604,800,000 michael@0: michael@0: // Epoch constants michael@0: #define kJan1_1JulianDay 1721426 // January 1, year 1 (Gregorian) michael@0: michael@0: #define kEpochStartAsJulianDay 2440588 // January 1, 1970 (Gregorian) michael@0: michael@0: #define kEpochYear 1970 michael@0: michael@0: michael@0: #define kEarliestViableMillis -185331720384000000.0 // minimum representable by julian day -1e17 michael@0: michael@0: #define kLatestViableMillis 185753453990400000.0 // max representable by julian day +1e17 michael@0: michael@0: /** michael@0: * The minimum supported Julian day. This value is equivalent to michael@0: * MIN_MILLIS. michael@0: */ michael@0: #define MIN_JULIAN (-0x7F000000) michael@0: michael@0: /** michael@0: * The minimum supported epoch milliseconds. This value is equivalent michael@0: * to MIN_JULIAN. michael@0: */ michael@0: #define MIN_MILLIS ((MIN_JULIAN - kEpochStartAsJulianDay) * kOneDay) michael@0: michael@0: /** michael@0: * The maximum supported Julian day. This value is equivalent to michael@0: * MAX_MILLIS. michael@0: */ michael@0: #define MAX_JULIAN (+0x7F000000) michael@0: michael@0: /** michael@0: * The maximum supported epoch milliseconds. This value is equivalent michael@0: * to MAX_JULIAN. michael@0: */ michael@0: #define MAX_MILLIS ((MAX_JULIAN - kEpochStartAsJulianDay) * kOneDay) michael@0: michael@0: /** michael@0: * A utility class providing proleptic Gregorian calendar functions michael@0: * used by time zone and calendar code. Do not instantiate. michael@0: * michael@0: * Note: Unlike GregorianCalendar, all computations performed by this michael@0: * class occur in the pure proleptic GregorianCalendar. michael@0: */ michael@0: class Grego { michael@0: public: michael@0: /** michael@0: * Return TRUE if the given year is a leap year. michael@0: * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc. michael@0: * @return TRUE if the year is a leap year michael@0: */ michael@0: static inline UBool isLeapYear(int32_t year); michael@0: michael@0: /** michael@0: * Return the number of days in the given month. michael@0: * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc. michael@0: * @param month 0-based month, with 0==Jan michael@0: * @return the number of days in the given month michael@0: */ michael@0: static inline int8_t monthLength(int32_t year, int32_t month); michael@0: michael@0: /** michael@0: * Return the length of a previous month of the Gregorian calendar. michael@0: * @param y the extended year michael@0: * @param m the 0-based month number michael@0: * @return the number of days in the month previous to the given month michael@0: */ michael@0: static inline int8_t previousMonthLength(int y, int m); michael@0: michael@0: /** michael@0: * Convert a year, month, and day-of-month, given in the proleptic michael@0: * Gregorian calendar, to 1970 epoch days. michael@0: * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc. michael@0: * @param month 0-based month, with 0==Jan michael@0: * @param dom 1-based day of month michael@0: * @return the day number, with day 0 == Jan 1 1970 michael@0: */ michael@0: static double fieldsToDay(int32_t year, int32_t month, int32_t dom); michael@0: michael@0: /** michael@0: * Convert a 1970-epoch day number to proleptic Gregorian year, michael@0: * month, day-of-month, and day-of-week. michael@0: * @param day 1970-epoch day (integral value) michael@0: * @param year output parameter to receive year michael@0: * @param month output parameter to receive month (0-based, 0==Jan) michael@0: * @param dom output parameter to receive day-of-month (1-based) michael@0: * @param dow output parameter to receive day-of-week (1-based, 1==Sun) michael@0: * @param doy output parameter to receive day-of-year (1-based) michael@0: */ michael@0: static void dayToFields(double day, int32_t& year, int32_t& month, michael@0: int32_t& dom, int32_t& dow, int32_t& doy); michael@0: michael@0: /** michael@0: * Convert a 1970-epoch day number to proleptic Gregorian year, michael@0: * month, day-of-month, and day-of-week. michael@0: * @param day 1970-epoch day (integral value) michael@0: * @param year output parameter to receive year michael@0: * @param month output parameter to receive month (0-based, 0==Jan) michael@0: * @param dom output parameter to receive day-of-month (1-based) michael@0: * @param dow output parameter to receive day-of-week (1-based, 1==Sun) michael@0: */ michael@0: static inline void dayToFields(double day, int32_t& year, int32_t& month, michael@0: int32_t& dom, int32_t& dow); michael@0: michael@0: /** michael@0: * Convert a 1970-epoch milliseconds to proleptic Gregorian year, michael@0: * month, day-of-month, and day-of-week, day of year and millis-in-day. michael@0: * @param time 1970-epoch milliseconds michael@0: * @param year output parameter to receive year michael@0: * @param month output parameter to receive month (0-based, 0==Jan) michael@0: * @param dom output parameter to receive day-of-month (1-based) michael@0: * @param dow output parameter to receive day-of-week (1-based, 1==Sun) michael@0: * @param doy output parameter to receive day-of-year (1-based) michael@0: * @param mid output parameter to recieve millis-in-day michael@0: */ michael@0: static void timeToFields(UDate time, int32_t& year, int32_t& month, michael@0: int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid); michael@0: michael@0: /** michael@0: * Return the day of week on the 1970-epoch day michael@0: * @param day the 1970-epoch day (integral value) michael@0: * @return the day of week michael@0: */ michael@0: static int32_t dayOfWeek(double day); michael@0: michael@0: /** michael@0: * Returns the ordinal number for the specified day of week within the month. michael@0: * The valid return value is 1, 2, 3, 4 or -1. michael@0: * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc. michael@0: * @param month 0-based month, with 0==Jan michael@0: * @param dom 1-based day of month michael@0: * @return The ordinal number for the specified day of week within the month michael@0: */ michael@0: static int32_t dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom); michael@0: michael@0: /** michael@0: * Converts Julian day to time as milliseconds. michael@0: * @param julian the given Julian day number. michael@0: * @return time as milliseconds. michael@0: * @internal michael@0: */ michael@0: static inline double julianDayToMillis(int32_t julian); michael@0: michael@0: /** michael@0: * Converts time as milliseconds to Julian day. michael@0: * @param millis the given milliseconds. michael@0: * @return the Julian day number. michael@0: * @internal michael@0: */ michael@0: static inline int32_t millisToJulianDay(double millis); michael@0: michael@0: /** michael@0: * Calculates the Gregorian day shift value for an extended year. michael@0: * @param eyear Extended year michael@0: * @returns number of days to ADD to Julian in order to convert from J->G michael@0: */ michael@0: static inline int32_t gregorianShift(int32_t eyear); michael@0: michael@0: private: michael@0: static const int16_t DAYS_BEFORE[24]; michael@0: static const int8_t MONTH_LENGTH[24]; michael@0: }; michael@0: michael@0: inline double ClockMath::floorDivide(double numerator, double denominator) { michael@0: return uprv_floor(numerator / denominator); michael@0: } michael@0: michael@0: inline UBool Grego::isLeapYear(int32_t year) { michael@0: // year&0x3 == year%4 michael@0: return ((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0)); michael@0: } michael@0: michael@0: inline int8_t michael@0: Grego::monthLength(int32_t year, int32_t month) { michael@0: return MONTH_LENGTH[month + (isLeapYear(year) ? 12 : 0)]; michael@0: } michael@0: michael@0: inline int8_t michael@0: Grego::previousMonthLength(int y, int m) { michael@0: return (m > 0) ? monthLength(y, m-1) : 31; michael@0: } michael@0: michael@0: inline void Grego::dayToFields(double day, int32_t& year, int32_t& month, michael@0: int32_t& dom, int32_t& dow) { michael@0: int32_t doy_unused; michael@0: dayToFields(day,year,month,dom,dow,doy_unused); michael@0: } michael@0: michael@0: inline double Grego::julianDayToMillis(int32_t julian) michael@0: { michael@0: return (julian - kEpochStartAsJulianDay) * kOneDay; michael@0: } michael@0: michael@0: inline int32_t Grego::millisToJulianDay(double millis) { michael@0: return (int32_t) (kEpochStartAsJulianDay + ClockMath::floorDivide(millis, (double)kOneDay)); michael@0: } michael@0: michael@0: inline int32_t Grego::gregorianShift(int32_t eyear) { michael@0: int32_t y = eyear-1; michael@0: int32_t gregShift = ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2; michael@0: return gregShift; michael@0: } michael@0: michael@0: /** michael@0: * This utility class provides convenient access to the data needed for a calendar. michael@0: * @internal ICU 3.0 michael@0: */ michael@0: class CalendarData : public UMemory { michael@0: public: michael@0: /** michael@0: * Construct a CalendarData from the given locale. michael@0: * @param loc locale to use. The 'calendar' keyword will be ignored. michael@0: * @param type calendar type. NULL indicates the gregorian calendar. michael@0: * No default lookup is done. michael@0: * @param status error code michael@0: */ michael@0: CalendarData(const Locale& loc, const char *type, UErrorCode& status); michael@0: michael@0: /** michael@0: * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()! michael@0: * The ResourceBundle C++ API should NOT be used because it is too slow for a low level API. michael@0: * michael@0: * @param key Resource key to data michael@0: * @param status Error Status michael@0: * @internal michael@0: */ michael@0: UResourceBundle* getByKey(const char *key, UErrorCode& status); michael@0: michael@0: /** michael@0: * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()! michael@0: * There is an implicit key of 'format' michael@0: * data is located in: "calendar/key/format/subKey" michael@0: * for example, calendar/dayNames/format/abbreviated michael@0: * The ResourceBundle C++ API should NOT be used because it is too slow for a low level API. michael@0: * michael@0: * @param key Resource key to data michael@0: * @param subKey Resource key to data michael@0: * @param status Error Status michael@0: * @internal michael@0: */ michael@0: UResourceBundle* getByKey2(const char *key, const char *subKey, UErrorCode& status); michael@0: michael@0: /** michael@0: * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()! michael@0: * data is located in: "calendar/key/contextKey/subKey" michael@0: * for example, calendar/dayNames/standalone/narrow michael@0: * The ResourceBundle C++ API should NOT be used because it is too slow for a low level API. michael@0: * michael@0: * @param key Resource key to data michael@0: * @param contextKey Resource key to data michael@0: * @param subKey Resource key to data michael@0: * @param status Error Status michael@0: * @internal michael@0: */ michael@0: UResourceBundle* getByKey3(const char *key, const char *contextKey, const char *subKey, UErrorCode& status); michael@0: michael@0: ~CalendarData(); michael@0: michael@0: private: michael@0: void initData(const char *locale, const char *type, UErrorCode& status); michael@0: michael@0: UResourceBundle *fFillin; michael@0: UResourceBundle *fOtherFillin; michael@0: UResourceBundle *fBundle; michael@0: UResourceBundle *fFallback; michael@0: CalendarData(); // Not implemented. michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // !UCONFIG_NO_FORMATTING michael@0: #endif // GREGOIMP_H michael@0: michael@0: //eof