chnsecal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************
  5. * Copyright (C) 2007-2013, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. *****************************************************************************
  8. *
  9. * File CHNSECAL.H
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 9/18/2007 ajmacher ported from java ChineseCalendar
  15. *****************************************************************************
  16. */
  17. #ifndef CHNSECAL_H
  18. #define CHNSECAL_H
  19. #include "unicode/utypes.h"
  20. #if !UCONFIG_NO_FORMATTING
  21. #include "unicode/calendar.h"
  22. #include "unicode/timezone.h"
  23. U_NAMESPACE_BEGIN
  24. /**
  25. * <code>ChineseCalendar</code> is a concrete subclass of {@link Calendar}
  26. * that implements a traditional Chinese calendar. The traditional Chinese
  27. * calendar is a lunisolar calendar: Each month starts on a new moon, and
  28. * the months are numbered according to solar events, specifically, to
  29. * guarantee that month 11 always contains the winter solstice. In order
  30. * to accomplish this, leap months are inserted in certain years. Leap
  31. * months are numbered the same as the month they follow. The decision of
  32. * which month is a leap month depends on the relative movements of the sun
  33. * and moon.
  34. *
  35. * <p>This class defines one addition field beyond those defined by
  36. * <code>Calendar</code>: The <code>IS_LEAP_MONTH</code> field takes the
  37. * value of 0 for normal months, or 1 for leap months.
  38. *
  39. * <p>All astronomical computations are performed with respect to a time
  40. * zone of GMT+8:00 and a longitude of 120 degrees east. Although some
  41. * calendars implement a historically more accurate convention of using
  42. * Beijing's local longitude (116 degrees 25 minutes east) and time zone
  43. * (GMT+7:45:40) for dates before 1929, we do not implement this here.
  44. *
  45. * <p>Years are counted in two different ways in the Chinese calendar. The
  46. * first method is by sequential numbering from the 61st year of the reign
  47. * of Huang Di, 2637 BCE, which is designated year 1 on the Chinese
  48. * calendar. The second method uses 60-year cycles from the same starting
  49. * point, which is designated year 1 of cycle 1. In this class, the
  50. * <code>EXTENDED_YEAR</code> field contains the sequential year count.
  51. * The <code>ERA</code> field contains the cycle number, and the
  52. * <code>YEAR</code> field contains the year of the cycle, a value between
  53. * 1 and 60.
  54. *
  55. * <p>There is some variation in what is considered the starting point of
  56. * the calendar, with some sources starting in the first year of the reign
  57. * of Huang Di, rather than the 61st. This gives continuous year numbers
  58. * 60 years greater and cycle numbers one greater than what this class
  59. * implements.
  60. *
  61. * <p>Because <code>ChineseCalendar</code> defines an additional field and
  62. * redefines the way the <code>ERA</code> field is used, it requires a new
  63. * format class, <code>ChineseDateFormat</code>. As always, use the
  64. * methods <code>DateFormat.getXxxInstance(Calendar cal,...)</code> to
  65. * obtain a formatter for this calendar.
  66. *
  67. * <p>References:<ul>
  68. *
  69. * <li>Dershowitz and Reingold, <i>Calendrical Calculations</i>,
  70. * Cambridge University Press, 1997</li>
  71. *
  72. * <li>Helmer Aslaksen's
  73. * <a href="http://www.math.nus.edu.sg/aslaksen/calendar/chinese.shtml">
  74. * Chinese Calendar page</a></li>
  75. *
  76. * <li>The <a href="http://www.tondering.dk/claus/calendar.html">
  77. * Calendar FAQ</a></li>
  78. *
  79. * </ul>
  80. *
  81. * <p>
  82. * This class should only be subclassed to implement variants of the Chinese lunar calendar.</p>
  83. * <p>
  84. * ChineseCalendar usually should be instantiated using
  85. * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
  86. * with the tag <code>"@calendar=chinese"</code>.</p>
  87. *
  88. * @see com.ibm.icu.text.ChineseDateFormat
  89. * @see com.ibm.icu.util.Calendar
  90. * @author Alan Liu
  91. * @internal
  92. */
  93. class U_I18N_API ChineseCalendar : public Calendar {
  94. public:
  95. //-------------------------------------------------------------------------
  96. // Constructors...
  97. //-------------------------------------------------------------------------
  98. /**
  99. * Constructs a ChineseCalendar based on the current time in the default time zone
  100. * with the given locale.
  101. *
  102. * @param aLocale The given locale.
  103. * @param success Indicates the status of ChineseCalendar object construction.
  104. * Returns U_ZERO_ERROR if constructed successfully.
  105. * @internal
  106. */
  107. ChineseCalendar(const Locale& aLocale, UErrorCode &success);
  108. protected:
  109. /**
  110. * Constructs a ChineseCalendar based on the current time in the default time zone
  111. * with the given locale, using the specified epoch year and time zone for
  112. * astronomical calculations.
  113. *
  114. * @param aLocale The given locale.
  115. * @param epochYear The epoch year to use for calculation.
  116. * @param zoneAstroCalc The TimeZone to use for astronomical calculations. If null,
  117. * will be set appropriately for Chinese calendar (UTC + 8:00).
  118. * @param success Indicates the status of ChineseCalendar object construction;
  119. * if successful, will not be changed to an error value.
  120. * @internal
  121. */
  122. ChineseCalendar(const Locale& aLocale, int32_t epochYear, const TimeZone* zoneAstroCalc, UErrorCode &success);
  123. public:
  124. /**
  125. * Copy Constructor
  126. * @internal
  127. */
  128. ChineseCalendar(const ChineseCalendar& other);
  129. /**
  130. * Destructor.
  131. * @internal
  132. */
  133. virtual ~ChineseCalendar();
  134. // clone
  135. virtual ChineseCalendar* clone() const;
  136. private:
  137. //-------------------------------------------------------------------------
  138. // Internal data....
  139. //-------------------------------------------------------------------------
  140. UBool isLeapYear;
  141. int32_t fEpochYear; // Start year of this Chinese calendar instance.
  142. const TimeZone* fZoneAstroCalc; // Zone used for the astronomical calculation
  143. // of this Chinese calendar instance.
  144. //----------------------------------------------------------------------
  145. // Calendar framework
  146. //----------------------------------------------------------------------
  147. protected:
  148. virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const;
  149. virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const;
  150. virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const;
  151. virtual int32_t handleGetExtendedYear();
  152. virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
  153. virtual const UFieldResolutionTable* getFieldResolutionTable() const;
  154. public:
  155. virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode &status);
  156. virtual void add(EDateFields field, int32_t amount, UErrorCode &status);
  157. virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode &status);
  158. virtual void roll(EDateFields field, int32_t amount, UErrorCode &status);
  159. //----------------------------------------------------------------------
  160. // Internal methods & astronomical calculations
  161. //----------------------------------------------------------------------
  162. private:
  163. static const UFieldResolutionTable CHINESE_DATE_PRECEDENCE[];
  164. double daysToMillis(double days) const;
  165. double millisToDays(double millis) const;
  166. virtual int32_t winterSolstice(int32_t gyear) const;
  167. virtual int32_t newMoonNear(double days, UBool after) const;
  168. virtual int32_t synodicMonthsBetween(int32_t day1, int32_t day2) const;
  169. virtual int32_t majorSolarTerm(int32_t days) const;
  170. virtual UBool hasNoMajorSolarTerm(int32_t newMoon) const;
  171. virtual UBool isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const;
  172. virtual void computeChineseFields(int32_t days, int32_t gyear,
  173. int32_t gmonth, UBool setAllFields);
  174. virtual int32_t newYear(int32_t gyear) const;
  175. virtual void offsetMonth(int32_t newMoon, int32_t dom, int32_t delta);
  176. const TimeZone* getChineseCalZoneAstroCalc(void) const;
  177. // UObject stuff
  178. public:
  179. /**
  180. * @return The class ID for this object. All objects of a given class have the
  181. * same class ID. Objects of other classes have different class IDs.
  182. * @internal
  183. */
  184. virtual UClassID getDynamicClassID(void) const;
  185. /**
  186. * Return the class ID for this class. This is useful only for comparing to a return
  187. * value from getDynamicClassID(). For example:
  188. *
  189. * Base* polymorphic_pointer = createPolymorphicObject();
  190. * if (polymorphic_pointer->getDynamicClassID() ==
  191. * Derived::getStaticClassID()) ...
  192. *
  193. * @return The class ID for all objects of this class.
  194. * @internal
  195. */
  196. static UClassID U_EXPORT2 getStaticClassID(void);
  197. /**
  198. * return the calendar type, "chinese".
  199. *
  200. * @return calendar type
  201. * @internal
  202. */
  203. virtual const char * getType() const;
  204. protected:
  205. /**
  206. * (Overrides Calendar) Return true if the current date for this Calendar is in
  207. * Daylight Savings Time. Recognizes DST_OFFSET, if it is set.
  208. *
  209. * @param status Fill-in parameter which receives the status of this operation.
  210. * @return True if the current date for this Calendar is in Daylight Savings Time,
  211. * false, otherwise.
  212. * @internal
  213. */
  214. virtual UBool inDaylightTime(UErrorCode& status) const;
  215. /**
  216. * Returns TRUE because the Islamic Calendar does have a default century
  217. * @internal
  218. */
  219. virtual UBool haveDefaultCentury() const;
  220. /**
  221. * Returns the date of the start of the default century
  222. * @return start of century - in milliseconds since epoch, 1970
  223. * @internal
  224. */
  225. virtual UDate defaultCenturyStart() const;
  226. /**
  227. * Returns the year in which the default century begins
  228. * @internal
  229. */
  230. virtual int32_t defaultCenturyStartYear() const;
  231. private: // default century stuff.
  232. /**
  233. * Returns the beginning date of the 100-year window that dates
  234. * with 2-digit years are considered to fall within.
  235. */
  236. UDate internalGetDefaultCenturyStart(void) const;
  237. /**
  238. * Returns the first year of the 100-year window that dates with
  239. * 2-digit years are considered to fall within.
  240. */
  241. int32_t internalGetDefaultCenturyStartYear(void) const;
  242. ChineseCalendar(); // default constructor not implemented
  243. };
  244. U_NAMESPACE_END
  245. #endif
  246. #endif