hebrwcal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (C) 2003-2013, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. ******************************************************************************
  8. *
  9. * File HEBRWCAL.H
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 05/13/2003 srl copied from gregocal.h
  15. * 11/26/2003 srl copied from buddhcal.h
  16. ******************************************************************************
  17. */
  18. #ifndef HEBRWCAL_H
  19. #define HEBRWCAL_H
  20. #include "unicode/utypes.h"
  21. #if !UCONFIG_NO_FORMATTING
  22. #include "unicode/calendar.h"
  23. #include "unicode/gregocal.h"
  24. U_NAMESPACE_BEGIN
  25. /**
  26. * <code>HebrewCalendar</code> is a subclass of <code>Calendar</code>
  27. * that that implements the traditional Hebrew calendar.
  28. * This is the civil calendar in Israel and the liturgical calendar
  29. * of the Jewish faith worldwide.
  30. * <p>
  31. * The Hebrew calendar is lunisolar and thus has a number of interesting
  32. * properties that distinguish it from the Gregorian. Months start
  33. * on the day of (an arithmetic approximation of) each new moon. Since the
  34. * solar year (approximately 365.24 days) is not an even multiple of
  35. * the lunar month (approximately 29.53 days) an extra "leap month" is
  36. * inserted in 7 out of every 19 years. To make matters even more
  37. * interesting, the start of a year can be delayed by up to three days
  38. * in order to prevent certain holidays from falling on the Sabbath and
  39. * to prevent certain illegal year lengths. Finally, the lengths of certain
  40. * months can vary depending on the number of days in the year.
  41. * <p>
  42. * The leap month is known as "Adar 1" and is inserted between the
  43. * months of Shevat and Adar in leap years. Since the leap month does
  44. * not come at the end of the year, calculations involving
  45. * month numbers are particularly complex. Users of this class should
  46. * make sure to use the {@link #roll roll} and {@link #add add} methods
  47. * rather than attempting to perform date arithmetic by manipulating
  48. * the fields directly.
  49. * <p>
  50. * <b>Note:</b> In the traditional Hebrew calendar, days start at sunset.
  51. * However, in order to keep the time fields in this class
  52. * synchronized with those of the other calendars and with local clock time,
  53. * we treat days and months as beginning at midnight,
  54. * roughly 6 hours after the corresponding sunset.
  55. * <p>
  56. * If you are interested in more information on the rules behind the Hebrew
  57. * calendar, see one of the following references:
  58. * <ul>
  59. * <li>"<a href="http://www.amazon.com/exec/obidos/ASIN/0521564743">Calendrical Calculations</a>",
  60. * by Nachum Dershowitz & Edward Reingold, Cambridge University Press, 1997, pages 85-91.
  61. *
  62. * <li>Hebrew Calendar Science and Myths,
  63. * <a href="http://www.geocities.com/Athens/1584/">
  64. * http://www.geocities.com/Athens/1584/</a>
  65. *
  66. * <li>The Calendar FAQ,
  67. * <a href="http://www.faqs.org/faqs/calendars/faq/">
  68. * http://www.faqs.org/faqs/calendars/faq/</a>
  69. * </ul>
  70. * <p>
  71. * @see com.ibm.icu.util.GregorianCalendar
  72. *
  73. * @author Laura Werner
  74. * @author Alan Liu
  75. * @author Steven R. Loomis
  76. * <p>
  77. * @internal
  78. */
  79. class U_I18N_API HebrewCalendar : public Calendar {
  80. public:
  81. /**
  82. * Useful constants for HebrewCalendar.
  83. * @internal
  84. */
  85. enum Month {
  86. /**
  87. * Constant for Tishri, the 1st month of the Hebrew year.
  88. */
  89. TISHRI,
  90. /**
  91. * Constant for Heshvan, the 2nd month of the Hebrew year.
  92. */
  93. HESHVAN,
  94. /**
  95. * Constant for Kislev, the 3rd month of the Hebrew year.
  96. */
  97. KISLEV,
  98. /**
  99. * Constant for Tevet, the 4th month of the Hebrew year.
  100. */
  101. TEVET,
  102. /**
  103. * Constant for Shevat, the 5th month of the Hebrew year.
  104. */
  105. SHEVAT,
  106. /**
  107. * Constant for Adar I, the 6th month of the Hebrew year
  108. * (present in leap years only). In non-leap years, the calendar
  109. * jumps from Shevat (5th month) to Adar (7th month).
  110. */
  111. ADAR_1,
  112. /**
  113. * Constant for the Adar, the 7th month of the Hebrew year.
  114. */
  115. ADAR,
  116. /**
  117. * Constant for Nisan, the 8th month of the Hebrew year.
  118. */
  119. NISAN,
  120. /**
  121. * Constant for Iyar, the 9th month of the Hebrew year.
  122. */
  123. IYAR,
  124. /**
  125. * Constant for Sivan, the 10th month of the Hebrew year.
  126. */
  127. SIVAN,
  128. /**
  129. * Constant for Tammuz, the 11th month of the Hebrew year.
  130. */
  131. TAMUZ,
  132. /**
  133. * Constant for Av, the 12th month of the Hebrew year.
  134. */
  135. AV,
  136. /**
  137. * Constant for Elul, the 13th month of the Hebrew year.
  138. */
  139. ELUL
  140. };
  141. /**
  142. * Constructs a HebrewCalendar based on the current time in the default time zone
  143. * with the given locale.
  144. *
  145. * @param aLocale The given locale.
  146. * @param success Indicates the status of HebrewCalendar object construction.
  147. * Returns U_ZERO_ERROR if constructed successfully.
  148. * @internal
  149. */
  150. HebrewCalendar(const Locale& aLocale, UErrorCode& success);
  151. /**
  152. * Destructor
  153. * @internal
  154. */
  155. virtual ~HebrewCalendar();
  156. /**
  157. * Copy constructor
  158. * @param source the object to be copied.
  159. * @internal
  160. */
  161. HebrewCalendar(const HebrewCalendar& source);
  162. /**
  163. * Create and return a polymorphic copy of this calendar.
  164. * @return return a polymorphic copy of this calendar.
  165. * @internal
  166. */
  167. virtual HebrewCalendar* clone() const override;
  168. public:
  169. /**
  170. * Override Calendar Returns a unique class ID POLYMORPHICALLY. Pure virtual
  171. * override. This method is to implement a simple version of RTTI, since not all C++
  172. * compilers support genuine RTTI. Polymorphic operator==() and clone() methods call
  173. * this method.
  174. *
  175. * @return The class ID for this object. All objects of a given class have the
  176. * same class ID. Objects of other classes have different class IDs.
  177. * @internal
  178. */
  179. virtual UClassID getDynamicClassID() const override;
  180. /**
  181. * Return the class ID for this class. This is useful only for comparing to a return
  182. * value from getDynamicClassID(). For example:
  183. *
  184. * Base* polymorphic_pointer = createPolymorphicObject();
  185. * if (polymorphic_pointer->getDynamicClassID() ==
  186. * Derived::getStaticClassID()) ...
  187. *
  188. * @return The class ID for all objects of this class.
  189. * @internal
  190. */
  191. static UClassID U_EXPORT2 getStaticClassID();
  192. /**
  193. * return the calendar type, "hebrew".
  194. *
  195. * @return calendar type
  196. * @internal
  197. */
  198. virtual const char * getType() const override;
  199. // Calendar API
  200. public:
  201. /**
  202. * (Overrides Calendar) UDate Arithmetic function. Adds the specified (signed) amount
  203. * of time to the given time field, based on the calendar's rules. For more
  204. * information, see the documentation for Calendar::add().
  205. *
  206. * @param field The time field.
  207. * @param amount The amount of date or time to be added to the field.
  208. * @param status Output param set to success/failure code on exit. If any value
  209. * previously set in the time field is invalid, this will be set to
  210. * an error status.
  211. */
  212. virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status) override;
  213. /**
  214. * @deprecated ICU 2.6 use UCalendarDateFields instead of EDateFields
  215. */
  216. virtual void add(EDateFields field, int32_t amount, UErrorCode& status) override;
  217. /**
  218. * (Overrides Calendar) Rolls up or down by the given amount in the specified field.
  219. * For more information, see the documentation for Calendar::roll().
  220. *
  221. * @param field The time field.
  222. * @param amount Indicates amount to roll.
  223. * @param status Output param set to success/failure code on exit. If any value
  224. * previously set in the time field is invalid, this will be set to
  225. * an error status.
  226. * @internal
  227. */
  228. virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) override;
  229. /**
  230. * (Overrides Calendar) Rolls up or down by the given amount in the specified field.
  231. * For more information, see the documentation for Calendar::roll().
  232. *
  233. * @param field The time field.
  234. * @param amount Indicates amount to roll.
  235. * @param status Output param set to success/failure code on exit. If any value
  236. * previously set in the time field is invalid, this will be set to
  237. * an error status.
  238. * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
  239. ` */
  240. virtual void roll(EDateFields field, int32_t amount, UErrorCode& status) override;
  241. /**
  242. * @internal
  243. */
  244. static UBool isLeapYear(int32_t year) ;
  245. /**
  246. * @return The related Gregorian year; will be obtained by modifying the value
  247. * obtained by get from UCAL_EXTENDED_YEAR field
  248. * @internal
  249. */
  250. virtual int32_t getRelatedYear(UErrorCode &status) const override;
  251. /**
  252. * @param year The related Gregorian year to set; will be modified as necessary then
  253. * set in UCAL_EXTENDED_YEAR field
  254. * @internal
  255. */
  256. virtual void setRelatedYear(int32_t year) override;
  257. protected:
  258. /**
  259. * Subclass API for defining limits of different types.
  260. * Subclasses must implement this method to return limits for the
  261. * following fields:
  262. *
  263. * <pre>UCAL_ERA
  264. * UCAL_YEAR
  265. * UCAL_MONTH
  266. * UCAL_WEEK_OF_YEAR
  267. * UCAL_WEEK_OF_MONTH
  268. * UCAL_DATE (DAY_OF_MONTH on Java)
  269. * UCAL_DAY_OF_YEAR
  270. * UCAL_DAY_OF_WEEK_IN_MONTH
  271. * UCAL_YEAR_WOY
  272. * UCAL_EXTENDED_YEAR</pre>
  273. *
  274. * @param field one of the above field numbers
  275. * @param limitType one of <code>MINIMUM</code>, <code>GREATEST_MINIMUM</code>,
  276. * <code>LEAST_MAXIMUM</code>, or <code>MAXIMUM</code>
  277. * @internal
  278. */
  279. virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const override;
  280. /**
  281. * Return the number of days in the given month of the given extended
  282. * year of this calendar system. Subclasses should override this
  283. * method if they can provide a more correct or more efficient
  284. * implementation than the default implementation in Calendar.
  285. * @internal
  286. */
  287. virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const override;
  288. /**
  289. * Return the number of days in the given extended year of this
  290. * calendar system. Subclasses should override this method if they can
  291. * provide a more correct or more efficient implementation than the
  292. * default implementation in Calendar.
  293. * @stable ICU 2.0
  294. */
  295. virtual int32_t handleGetYearLength(int32_t eyear) const override;
  296. /**
  297. * Subclasses may override this method to compute several fields
  298. * specific to each calendar system. These are:
  299. *
  300. * <ul><li>ERA
  301. * <li>YEAR
  302. * <li>MONTH
  303. * <li>DAY_OF_MONTH
  304. * <li>DAY_OF_YEAR
  305. * <li>EXTENDED_YEAR</ul>
  306. *
  307. * <p>The GregorianCalendar implementation implements
  308. * a calendar with the specified Julian/Gregorian cutover date.
  309. * @internal
  310. */
  311. virtual void handleComputeFields(int32_t julianDay, UErrorCode &status) override;
  312. /**
  313. * Return the extended year defined by the current fields. This will
  314. * use the UCAL_EXTENDED_YEAR field or the UCAL_YEAR and supra-year fields (such
  315. * as UCAL_ERA) specific to the calendar system, depending on which set of
  316. * fields is newer.
  317. * @return the extended year
  318. * @internal
  319. */
  320. virtual int32_t handleGetExtendedYear() override;
  321. /**
  322. * Return the Julian day number of day before the first day of the
  323. * given month in the given extended year. Subclasses should override
  324. * this method to implement their calendar system.
  325. * @param eyear the extended year
  326. * @param month the zero-based month, or 0 if useMonth is false
  327. * @param useMonth if false, compute the day before the first day of
  328. * the given year, otherwise, compute the day before the first day of
  329. * the given month
  330. * @param return the Julian day number of the day before the first
  331. * day of the given month and year
  332. * @internal
  333. */
  334. virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
  335. UBool useMonth) const override;
  336. /**
  337. * Validate a single field of this calendar.
  338. * Overrides Calendar::validateField(int) to provide
  339. * special handling for month validation for Hebrew calendar.
  340. * @internal
  341. */
  342. virtual void validateField(UCalendarDateFields field, UErrorCode &status) override;
  343. protected:
  344. /**
  345. * Returns true because the Hebrew Calendar does have a default century
  346. * @internal
  347. */
  348. virtual UBool haveDefaultCentury() const override;
  349. /**
  350. * Returns the date of the start of the default century
  351. * @return start of century - in milliseconds since epoch, 1970
  352. * @internal
  353. */
  354. virtual UDate defaultCenturyStart() const override;
  355. /**
  356. * Returns the year in which the default century begins
  357. * @internal
  358. */
  359. virtual int32_t defaultCenturyStartYear() const override;
  360. public:
  361. /**
  362. * Returns true if the date is in a leap year.
  363. *
  364. * @param status ICU Error Code
  365. * @return True if the date in the fields is in a Temporal proposal
  366. * defined leap year. False otherwise.
  367. */
  368. virtual bool inTemporalLeapYear(UErrorCode& status) const override;
  369. /**
  370. * Gets The Temporal monthCode value corresponding to the month for the date.
  371. * The value is a string identifier that starts with the literal grapheme
  372. * "M" followed by two graphemes representing the zero-padded month number
  373. * of the current month in a normal (non-leap) year and suffixed by an
  374. * optional literal grapheme "L" if this is a leap month in a lunisolar
  375. * calendar. For the Hebrew calendar, the values are "M01" .. "M12" for
  376. * non-leap year, and "M01" .. "M05", "M05L", "M06" .. "M12" for leap year.
  377. *
  378. * @param status ICU Error Code
  379. * @return One of 13 possible strings in {"M01".. "M05", "M05L",
  380. * "M06" .. "M12"}.
  381. * @draft ICU 73
  382. */
  383. virtual const char* getTemporalMonthCode(UErrorCode& status) const override;
  384. /**
  385. * Sets The Temporal monthCode which is a string identifier that starts
  386. * with the literal grapheme "M" followed by two graphemes representing
  387. * the zero-padded month number of the current month in a normal
  388. * (non-leap) year and suffixed by an optional literal grapheme "L" if this
  389. * is a leap month in a lunisolar calendar. For Hebrew calendar, the values
  390. * are "M01" .. "M12" for non-leap years, and "M01" .. "M05", "M05L", "M06"
  391. * .. "M12" for leap year.
  392. *
  393. * @param temporalMonth The value to be set for temporal monthCode.
  394. * @param status ICU Error Code
  395. *
  396. * @draft ICU 73
  397. */
  398. virtual void setTemporalMonthCode(const char* code, UErrorCode& status ) override;
  399. protected:
  400. virtual int32_t internalGetMonth() const override;
  401. private: // Calendar-specific implementation
  402. /**
  403. * Finds the day # of the first day in the given Hebrew year.
  404. * To do this, we want to calculate the time of the Tishri 1 new moon
  405. * in that year.
  406. * <p>
  407. * The algorithm here is similar to ones described in a number of
  408. * references, including:
  409. * <ul>
  410. * <li>"Calendrical Calculations", by Nachum Dershowitz & Edward Reingold,
  411. * Cambridge University Press, 1997, pages 85-91.
  412. *
  413. * <li>Hebrew Calendar Science and Myths,
  414. * <a href="http://www.geocities.com/Athens/1584/">
  415. * http://www.geocities.com/Athens/1584/</a>
  416. *
  417. * <li>The Calendar FAQ,
  418. * <a href="http://www.faqs.org/faqs/calendars/faq/">
  419. * http://www.faqs.org/faqs/calendars/faq/</a>
  420. * </ul>
  421. * @param year extended year
  422. * @return day number (JD)
  423. * @internal
  424. */
  425. static int32_t startOfYear(int32_t year, UErrorCode& status);
  426. static int32_t absoluteDayToDayOfWeek(int32_t day) ;
  427. /**
  428. * @internal
  429. */
  430. int32_t yearType(int32_t year) const;
  431. /**
  432. * @internal
  433. */
  434. static int32_t monthsInYear(int32_t year) ;
  435. };
  436. U_NAMESPACE_END
  437. #endif /* #if !UCONFIG_NO_FORMATTING */
  438. #endif
  439. //eof