michael@0: /*
michael@0: *****************************************************************************
michael@0: * Copyright (C) 2007-2013, International Business Machines Corporation
michael@0: * and others. All Rights Reserved.
michael@0: *****************************************************************************
michael@0: *
michael@0: * File CHNSECAL.H
michael@0: *
michael@0: * Modification History:
michael@0: *
michael@0: * Date Name Description
michael@0: * 9/18/2007 ajmacher ported from java ChineseCalendar
michael@0: *****************************************************************************
michael@0: */
michael@0:
michael@0: #ifndef CHNSECAL_H
michael@0: #define CHNSECAL_H
michael@0:
michael@0: #include "unicode/utypes.h"
michael@0:
michael@0: #if !UCONFIG_NO_FORMATTING
michael@0:
michael@0: #include "unicode/calendar.h"
michael@0: #include "unicode/timezone.h"
michael@0:
michael@0: U_NAMESPACE_BEGIN
michael@0:
michael@0: /**
michael@0: * ChineseCalendar
is a concrete subclass of {@link Calendar}
michael@0: * that implements a traditional Chinese calendar. The traditional Chinese
michael@0: * calendar is a lunisolar calendar: Each month starts on a new moon, and
michael@0: * the months are numbered according to solar events, specifically, to
michael@0: * guarantee that month 11 always contains the winter solstice. In order
michael@0: * to accomplish this, leap months are inserted in certain years. Leap
michael@0: * months are numbered the same as the month they follow. The decision of
michael@0: * which month is a leap month depends on the relative movements of the sun
michael@0: * and moon.
michael@0: *
michael@0: *
This class defines one addition field beyond those defined by
michael@0: * Calendar
: The IS_LEAP_MONTH
field takes the
michael@0: * value of 0 for normal months, or 1 for leap months.
michael@0: *
michael@0: *
All astronomical computations are performed with respect to a time
michael@0: * zone of GMT+8:00 and a longitude of 120 degrees east. Although some
michael@0: * calendars implement a historically more accurate convention of using
michael@0: * Beijing's local longitude (116 degrees 25 minutes east) and time zone
michael@0: * (GMT+7:45:40) for dates before 1929, we do not implement this here.
michael@0: *
michael@0: *
Years are counted in two different ways in the Chinese calendar. The
michael@0: * first method is by sequential numbering from the 61st year of the reign
michael@0: * of Huang Di, 2637 BCE, which is designated year 1 on the Chinese
michael@0: * calendar. The second method uses 60-year cycles from the same starting
michael@0: * point, which is designated year 1 of cycle 1. In this class, the
michael@0: * EXTENDED_YEAR
field contains the sequential year count.
michael@0: * The ERA
field contains the cycle number, and the
michael@0: * YEAR
field contains the year of the cycle, a value between
michael@0: * 1 and 60.
michael@0: *
michael@0: *
There is some variation in what is considered the starting point of
michael@0: * the calendar, with some sources starting in the first year of the reign
michael@0: * of Huang Di, rather than the 61st. This gives continuous year numbers
michael@0: * 60 years greater and cycle numbers one greater than what this class
michael@0: * implements.
michael@0: *
michael@0: *
Because ChineseCalendar
defines an additional field and
michael@0: * redefines the way the ERA
field is used, it requires a new
michael@0: * format class, ChineseDateFormat
. As always, use the
michael@0: * methods DateFormat.getXxxInstance(Calendar cal,...)
to
michael@0: * obtain a formatter for this calendar.
michael@0: *
michael@0: *
References:
michael@0: *
michael@0: * - Dershowitz and Reingold, Calendrical Calculations,
michael@0: * Cambridge University Press, 1997
michael@0: *
michael@0: * - Helmer Aslaksen's
michael@0: *
michael@0: * Chinese Calendar page
michael@0: *
michael@0: * - The
michael@0: * Calendar FAQ
michael@0: *
michael@0: *
michael@0: *
michael@0: *
michael@0: * This class should only be subclassed to implement variants of the Chinese lunar calendar.
michael@0: *
michael@0: * ChineseCalendar usually should be instantiated using
michael@0: * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a ULocale
michael@0: * with the tag "@calendar=chinese"
.
michael@0: *
michael@0: * @see com.ibm.icu.text.ChineseDateFormat
michael@0: * @see com.ibm.icu.util.Calendar
michael@0: * @author Alan Liu
michael@0: * @internal
michael@0: */
michael@0: class U_I18N_API ChineseCalendar : public Calendar {
michael@0: public:
michael@0: //-------------------------------------------------------------------------
michael@0: // Constructors...
michael@0: //-------------------------------------------------------------------------
michael@0:
michael@0: /**
michael@0: * Constructs a ChineseCalendar based on the current time in the default time zone
michael@0: * with the given locale.
michael@0: *
michael@0: * @param aLocale The given locale.
michael@0: * @param success Indicates the status of ChineseCalendar object construction.
michael@0: * Returns U_ZERO_ERROR if constructed successfully.
michael@0: * @internal
michael@0: */
michael@0: ChineseCalendar(const Locale& aLocale, UErrorCode &success);
michael@0:
michael@0: protected:
michael@0:
michael@0: /**
michael@0: * Constructs a ChineseCalendar based on the current time in the default time zone
michael@0: * with the given locale, using the specified epoch year and time zone for
michael@0: * astronomical calculations.
michael@0: *
michael@0: * @param aLocale The given locale.
michael@0: * @param epochYear The epoch year to use for calculation.
michael@0: * @param zoneAstroCalc The TimeZone to use for astronomical calculations. If null,
michael@0: * will be set appropriately for Chinese calendar (UTC + 8:00).
michael@0: * @param success Indicates the status of ChineseCalendar object construction;
michael@0: * if successful, will not be changed to an error value.
michael@0: * @internal
michael@0: */
michael@0: ChineseCalendar(const Locale& aLocale, int32_t epochYear, const TimeZone* zoneAstroCalc, UErrorCode &success);
michael@0:
michael@0: public:
michael@0: /**
michael@0: * Copy Constructor
michael@0: * @internal
michael@0: */
michael@0: ChineseCalendar(const ChineseCalendar& other);
michael@0:
michael@0: /**
michael@0: * Destructor.
michael@0: * @internal
michael@0: */
michael@0: virtual ~ChineseCalendar();
michael@0:
michael@0: // clone
michael@0: virtual Calendar* clone() const;
michael@0:
michael@0: private:
michael@0:
michael@0: //-------------------------------------------------------------------------
michael@0: // Internal data....
michael@0: //-------------------------------------------------------------------------
michael@0:
michael@0: UBool isLeapYear;
michael@0: int32_t fEpochYear; // Start year of this Chinese calendar instance.
michael@0: const TimeZone* fZoneAstroCalc; // Zone used for the astronomical calculation
michael@0: // of this Chinese calendar instance.
michael@0:
michael@0: //----------------------------------------------------------------------
michael@0: // Calendar framework
michael@0: //----------------------------------------------------------------------
michael@0:
michael@0: protected:
michael@0: virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const;
michael@0: virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const;
michael@0: virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const;
michael@0: virtual int32_t handleGetExtendedYear();
michael@0: virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
michael@0: virtual const UFieldResolutionTable* getFieldResolutionTable() const;
michael@0:
michael@0: public:
michael@0: virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode &status);
michael@0: virtual void add(EDateFields field, int32_t amount, UErrorCode &status);
michael@0: virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode &status);
michael@0: virtual void roll(EDateFields field, int32_t amount, UErrorCode &status);
michael@0:
michael@0: //----------------------------------------------------------------------
michael@0: // Internal methods & astronomical calculations
michael@0: //----------------------------------------------------------------------
michael@0:
michael@0: private:
michael@0:
michael@0: static const UFieldResolutionTable CHINESE_DATE_PRECEDENCE[];
michael@0:
michael@0: double daysToMillis(double days) const;
michael@0: double millisToDays(double millis) const;
michael@0: virtual int32_t winterSolstice(int32_t gyear) const;
michael@0: virtual int32_t newMoonNear(double days, UBool after) const;
michael@0: virtual int32_t synodicMonthsBetween(int32_t day1, int32_t day2) const;
michael@0: virtual int32_t majorSolarTerm(int32_t days) const;
michael@0: virtual UBool hasNoMajorSolarTerm(int32_t newMoon) const;
michael@0: virtual UBool isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const;
michael@0: virtual void computeChineseFields(int32_t days, int32_t gyear,
michael@0: int32_t gmonth, UBool setAllFields);
michael@0: virtual int32_t newYear(int32_t gyear) const;
michael@0: virtual void offsetMonth(int32_t newMoon, int32_t dom, int32_t delta);
michael@0: const TimeZone* getChineseCalZoneAstroCalc(void) const;
michael@0:
michael@0: // UObject stuff
michael@0: public:
michael@0: /**
michael@0: * @return The class ID for this object. All objects of a given class have the
michael@0: * same class ID. Objects of other classes have different class IDs.
michael@0: * @internal
michael@0: */
michael@0: virtual UClassID getDynamicClassID(void) const;
michael@0:
michael@0: /**
michael@0: * Return the class ID for this class. This is useful only for comparing to a return
michael@0: * value from getDynamicClassID(). For example:
michael@0: *
michael@0: * Base* polymorphic_pointer = createPolymorphicObject();
michael@0: * if (polymorphic_pointer->getDynamicClassID() ==
michael@0: * Derived::getStaticClassID()) ...
michael@0: *
michael@0: * @return The class ID for all objects of this class.
michael@0: * @internal
michael@0: */
michael@0: static UClassID U_EXPORT2 getStaticClassID(void);
michael@0:
michael@0: /**
michael@0: * return the calendar type, "chinese".
michael@0: *
michael@0: * @return calendar type
michael@0: * @internal
michael@0: */
michael@0: virtual const char * getType() const;
michael@0:
michael@0:
michael@0: protected:
michael@0: /**
michael@0: * (Overrides Calendar) Return true if the current date for this Calendar is in
michael@0: * Daylight Savings Time. Recognizes DST_OFFSET, if it is set.
michael@0: *
michael@0: * @param status Fill-in parameter which receives the status of this operation.
michael@0: * @return True if the current date for this Calendar is in Daylight Savings Time,
michael@0: * false, otherwise.
michael@0: * @internal
michael@0: */
michael@0: virtual UBool inDaylightTime(UErrorCode& status) const;
michael@0:
michael@0:
michael@0: /**
michael@0: * Returns TRUE because the Islamic Calendar does have a default century
michael@0: * @internal
michael@0: */
michael@0: virtual UBool haveDefaultCentury() const;
michael@0:
michael@0: /**
michael@0: * Returns the date of the start of the default century
michael@0: * @return start of century - in milliseconds since epoch, 1970
michael@0: * @internal
michael@0: */
michael@0: virtual UDate defaultCenturyStart() const;
michael@0:
michael@0: /**
michael@0: * Returns the year in which the default century begins
michael@0: * @internal
michael@0: */
michael@0: virtual int32_t defaultCenturyStartYear() const;
michael@0:
michael@0: private: // default century stuff.
michael@0:
michael@0: /**
michael@0: * Returns the beginning date of the 100-year window that dates
michael@0: * with 2-digit years are considered to fall within.
michael@0: */
michael@0: UDate internalGetDefaultCenturyStart(void) const;
michael@0:
michael@0: /**
michael@0: * Returns the first year of the 100-year window that dates with
michael@0: * 2-digit years are considered to fall within.
michael@0: */
michael@0: int32_t internalGetDefaultCenturyStartYear(void) const;
michael@0:
michael@0: ChineseCalendar(); // default constructor not implemented
michael@0: };
michael@0:
michael@0: U_NAMESPACE_END
michael@0:
michael@0: #endif
michael@0: #endif