// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*****************************************************************************4* Copyright (C) 2007-2013, International Business Machines Corporation5* and others. All Rights Reserved.6*****************************************************************************7*8* File CHNSECAL.H9*10* Modification History:11*12* Date Name Description13* 9/18/2007 ajmacher ported from java ChineseCalendar14*****************************************************************************15*/1617#ifndef CHNSECAL_H18#define CHNSECAL_H1920#include "unicode/utypes.h"2122#if !UCONFIG_NO_FORMATTING2324#include "unicode/calendar.h"25#include "unicode/timezone.h"2627U_NAMESPACE_BEGIN2829/**30* <code>ChineseCalendar</code> is a concrete subclass of {@link Calendar}31* that implements a traditional Chinese calendar. The traditional Chinese32* calendar is a lunisolar calendar: Each month starts on a new moon, and33* the months are numbered according to solar events, specifically, to34* guarantee that month 11 always contains the winter solstice. In order35* to accomplish this, leap months are inserted in certain years. Leap36* months are numbered the same as the month they follow. The decision of37* which month is a leap month depends on the relative movements of the sun38* and moon.39*40* <p>This class defines one addition field beyond those defined by41* <code>Calendar</code>: The <code>IS_LEAP_MONTH</code> field takes the42* value of 0 for normal months, or 1 for leap months.43*44* <p>All astronomical computations are performed with respect to a time45* zone of GMT+8:00 and a longitude of 120 degrees east. Although some46* calendars implement a historically more accurate convention of using47* Beijing's local longitude (116 degrees 25 minutes east) and time zone48* (GMT+7:45:40) for dates before 1929, we do not implement this here.49*50* <p>Years are counted in two different ways in the Chinese calendar. The51* first method is by sequential numbering from the 61st year of the reign52* of Huang Di, 2637 BCE, which is designated year 1 on the Chinese53* calendar. The second method uses 60-year cycles from the same starting54* point, which is designated year 1 of cycle 1. In this class, the55* <code>EXTENDED_YEAR</code> field contains the sequential year count.56* The <code>ERA</code> field contains the cycle number, and the57* <code>YEAR</code> field contains the year of the cycle, a value between58* 1 and 60.59*60* <p>There is some variation in what is considered the starting point of61* the calendar, with some sources starting in the first year of the reign62* of Huang Di, rather than the 61st. This gives continuous year numbers63* 60 years greater and cycle numbers one greater than what this class64* implements.65*66* <p>Because <code>ChineseCalendar</code> defines an additional field and67* redefines the way the <code>ERA</code> field is used, it requires a new68* format class, <code>ChineseDateFormat</code>. As always, use the69* methods <code>DateFormat.getXxxInstance(Calendar cal,...)</code> to70* obtain a formatter for this calendar.71*72* <p>References:<ul>73*74* <li>Dershowitz and Reingold, <i>Calendrical Calculations</i>,75* Cambridge University Press, 1997</li>76*77* <li>Helmer Aslaksen's78* <a href="http://www.math.nus.edu.sg/aslaksen/calendar/chinese.shtml">79* Chinese Calendar page</a></li>80*81* <li>The <a href="http://www.tondering.dk/claus/calendar.html">82* Calendar FAQ</a></li>83*84* </ul>85*86* <p>87* This class should only be subclassed to implement variants of the Chinese lunar calendar.</p>88* <p>89* ChineseCalendar usually should be instantiated using90* {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>91* with the tag <code>"@calendar=chinese"</code>.</p>92*93* @see com.ibm.icu.text.ChineseDateFormat94* @see com.ibm.icu.util.Calendar95* @author Alan Liu96* @internal97*/98class U_I18N_API ChineseCalendar : public Calendar {99public:100//-------------------------------------------------------------------------101// Constructors...102//-------------------------------------------------------------------------103104/**105* Constructs a ChineseCalendar based on the current time in the default time zone106* with the given locale.107*108* @param aLocale The given locale.109* @param success Indicates the status of ChineseCalendar object construction.110* Returns U_ZERO_ERROR if constructed successfully.111* @internal112*/113ChineseCalendar(const Locale& aLocale, UErrorCode &success);114115protected:116117/**118* Constructs a ChineseCalendar based on the current time in the default time zone119* with the given locale, using the specified epoch year and time zone for120* astronomical calculations.121*122* @param aLocale The given locale.123* @param epochYear The epoch year to use for calculation.124* @param zoneAstroCalc The TimeZone to use for astronomical calculations. If null,125* will be set appropriately for Chinese calendar (UTC + 8:00).126* @param success Indicates the status of ChineseCalendar object construction;127* if successful, will not be changed to an error value.128* @internal129*/130ChineseCalendar(const Locale& aLocale, int32_t epochYear, const TimeZone* zoneAstroCalc, UErrorCode &success);131132public:133/**134* Copy Constructor135* @internal136*/137ChineseCalendar(const ChineseCalendar& other);138139/**140* Destructor.141* @internal142*/143virtual ~ChineseCalendar();144145// clone146virtual ChineseCalendar* clone() const override;147148private:149150//-------------------------------------------------------------------------151// Internal data....152//-------------------------------------------------------------------------153154UBool isLeapYear;155int32_t fEpochYear; // Start year of this Chinese calendar instance.156const TimeZone* fZoneAstroCalc; // Zone used for the astronomical calculation157// of this Chinese calendar instance.158159//----------------------------------------------------------------------160// Calendar framework161//----------------------------------------------------------------------162163protected:164virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const override;165virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const override;166virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const override;167virtual int32_t handleGetExtendedYear() override;168virtual void handleComputeFields(int32_t julianDay, UErrorCode &status) override;169virtual const UFieldResolutionTable* getFieldResolutionTable() const override;170171public:172virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode &status) override;173virtual void add(EDateFields field, int32_t amount, UErrorCode &status) override;174virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode &status) override;175virtual void roll(EDateFields field, int32_t amount, UErrorCode &status) override;176177//----------------------------------------------------------------------178// Internal methods & astronomical calculations179//----------------------------------------------------------------------180181private:182183static const UFieldResolutionTable CHINESE_DATE_PRECEDENCE[];184185double daysToMillis(double days) const;186double millisToDays(double millis) const;187virtual int32_t winterSolstice(int32_t gyear) const;188virtual int32_t newMoonNear(double days, UBool after) const;189virtual int32_t synodicMonthsBetween(int32_t day1, int32_t day2) const;190virtual int32_t majorSolarTerm(int32_t days) const;191virtual UBool hasNoMajorSolarTerm(int32_t newMoon) const;192virtual UBool isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const;193virtual void computeChineseFields(int32_t days, int32_t gyear,194int32_t gmonth, UBool setAllFields);195virtual int32_t newYear(int32_t gyear) const;196virtual void offsetMonth(int32_t newMoon, int32_t dom, int32_t delta);197const TimeZone* getChineseCalZoneAstroCalc(void) const;198199// UObject stuff200public:201/**202* @return The class ID for this object. All objects of a given class have the203* same class ID. Objects of other classes have different class IDs.204* @internal205*/206virtual UClassID getDynamicClassID(void) const override;207208/**209* Return the class ID for this class. This is useful only for comparing to a return210* value from getDynamicClassID(). For example:211*212* Base* polymorphic_pointer = createPolymorphicObject();213* if (polymorphic_pointer->getDynamicClassID() ==214* Derived::getStaticClassID()) ...215*216* @return The class ID for all objects of this class.217* @internal218*/219static UClassID U_EXPORT2 getStaticClassID(void);220221/**222* return the calendar type, "chinese".223*224* @return calendar type225* @internal226*/227virtual const char * getType() const override;228229230protected:231/**232* (Overrides Calendar) Return true if the current date for this Calendar is in233* Daylight Savings Time. Recognizes DST_OFFSET, if it is set.234*235* @param status Fill-in parameter which receives the status of this operation.236* @return True if the current date for this Calendar is in Daylight Savings Time,237* false, otherwise.238* @internal239*/240virtual UBool inDaylightTime(UErrorCode& status) const override;241242243/**244* Returns true because the Islamic Calendar does have a default century245* @internal246*/247virtual UBool haveDefaultCentury() const override;248249/**250* Returns the date of the start of the default century251* @return start of century - in milliseconds since epoch, 1970252* @internal253*/254virtual UDate defaultCenturyStart() const override;255256/**257* Returns the year in which the default century begins258* @internal259*/260virtual int32_t defaultCenturyStartYear() const override;261262private: // default century stuff.263264/**265* Returns the beginning date of the 100-year window that dates266* with 2-digit years are considered to fall within.267*/268UDate internalGetDefaultCenturyStart(void) const;269270/**271* Returns the first year of the 100-year window that dates with272* 2-digit years are considered to fall within.273*/274int32_t internalGetDefaultCenturyStartYear(void) const;275276ChineseCalendar() = delete; // default constructor not implemented277};278279U_NAMESPACE_END280281#endif282#endif283284285