gregocal.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. * Copyright (C) 1997-2013, International Business Machines Corporation and others.
  5. * All Rights Reserved.
  6. ********************************************************************************
  7. *
  8. * File GREGOCAL.H
  9. *
  10. * Modification History:
  11. *
  12. * Date Name Description
  13. * 04/22/97 aliu Overhauled header.
  14. * 07/28/98 stephen Sync with JDK 1.2
  15. * 09/04/98 stephen Re-sync with JDK 8/31 putback
  16. * 09/14/98 stephen Changed type of kOneDay, kOneWeek to double.
  17. * Fixed bug in roll()
  18. * 10/15/99 aliu Fixed j31, incorrect WEEK_OF_YEAR computation.
  19. * Added documentation of WEEK_OF_YEAR computation.
  20. * 10/15/99 aliu Fixed j32, cannot set date to Feb 29 2000 AD.
  21. * {JDK bug 4210209 4209272}
  22. * 11/07/2003 srl Update, clean up documentation.
  23. ********************************************************************************
  24. */
  25. #ifndef GREGOCAL_H
  26. #define GREGOCAL_H
  27. #include "utypes.h"
  28. #if !UCONFIG_NO_FORMATTING
  29. #include "calendar.h"
  30. /**
  31. * \file
  32. * \brief C++ API: Concrete class which provides the standard calendar.
  33. */
  34. U_NAMESPACE_BEGIN
  35. /**
  36. * Concrete class which provides the standard calendar used by most of the world.
  37. * <P>
  38. * The standard (Gregorian) calendar has 2 eras, BC and AD.
  39. * <P>
  40. * This implementation handles a single discontinuity, which corresponds by default to
  41. * the date the Gregorian calendar was originally instituted (October 15, 1582). Not all
  42. * countries adopted the Gregorian calendar then, so this cutover date may be changed by
  43. * the caller.
  44. * <P>
  45. * Prior to the institution of the Gregorian Calendar, New Year's Day was March 25. To
  46. * avoid confusion, this Calendar always uses January 1. A manual adjustment may be made
  47. * if desired for dates that are prior to the Gregorian changeover and which fall
  48. * between January 1 and March 24.
  49. *
  50. * <p>Values calculated for the <code>WEEK_OF_YEAR</code> field range from 1 to
  51. * 53. Week 1 for a year is the first week that contains at least
  52. * <code>getMinimalDaysInFirstWeek()</code> days from that year. It thus
  53. * depends on the values of <code>getMinimalDaysInFirstWeek()</code>,
  54. * <code>getFirstDayOfWeek()</code>, and the day of the week of January 1.
  55. * Weeks between week 1 of one year and week 1 of the following year are
  56. * numbered sequentially from 2 to 52 or 53 (as needed).
  57. *
  58. * <p>For example, January 1, 1998 was a Thursday. If
  59. * <code>getFirstDayOfWeek()</code> is <code>MONDAY</code> and
  60. * <code>getMinimalDaysInFirstWeek()</code> is 4 (these are the values
  61. * reflecting ISO 8601 and many national standards), then week 1 of 1998 starts
  62. * on December 29, 1997, and ends on January 4, 1998. If, however,
  63. * <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>, then week 1 of 1998
  64. * starts on January 4, 1998, and ends on January 10, 1998; the first three days
  65. * of 1998 then are part of week 53 of 1997.
  66. *
  67. * <p>Example for using GregorianCalendar:
  68. * <pre>
  69. * \code
  70. * // get the supported ids for GMT-08:00 (Pacific Standard Time)
  71. * UErrorCode success = U_ZERO_ERROR;
  72. * const StringEnumeration *ids = TimeZone::createEnumeration(-8 * 60 * 60 * 1000);
  73. * // if no ids were returned, something is wrong. get out.
  74. * if (ids == 0 || ids->count(success) == 0) {
  75. * return;
  76. * }
  77. *
  78. * // begin output
  79. * cout << "Current Time" << endl;
  80. *
  81. * // create a Pacific Standard Time time zone
  82. * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids->unext(NULL, success)));
  83. *
  84. * // set up rules for daylight savings time
  85. * pdt->setStartRule(UCAL_MARCH, 1, UCAL_SUNDAY, 2 * 60 * 60 * 1000);
  86. * pdt->setEndRule(UCAL_NOVEMBER, 2, UCAL_SUNDAY, 2 * 60 * 60 * 1000);
  87. *
  88. * // create a GregorianCalendar with the Pacific Daylight time zone
  89. * // and the current date and time
  90. * Calendar* calendar = new GregorianCalendar( pdt, success );
  91. *
  92. * // print out a bunch of interesting things
  93. * cout << "ERA: " << calendar->get( UCAL_ERA, success ) << endl;
  94. * cout << "YEAR: " << calendar->get( UCAL_YEAR, success ) << endl;
  95. * cout << "MONTH: " << calendar->get( UCAL_MONTH, success ) << endl;
  96. * cout << "WEEK_OF_YEAR: " << calendar->get( UCAL_WEEK_OF_YEAR, success ) << endl;
  97. * cout << "WEEK_OF_MONTH: " << calendar->get( UCAL_WEEK_OF_MONTH, success ) << endl;
  98. * cout << "DATE: " << calendar->get( UCAL_DATE, success ) << endl;
  99. * cout << "DAY_OF_MONTH: " << calendar->get( UCAL_DAY_OF_MONTH, success ) << endl;
  100. * cout << "DAY_OF_YEAR: " << calendar->get( UCAL_DAY_OF_YEAR, success ) << endl;
  101. * cout << "DAY_OF_WEEK: " << calendar->get( UCAL_DAY_OF_WEEK, success ) << endl;
  102. * cout << "DAY_OF_WEEK_IN_MONTH: " << calendar->get( UCAL_DAY_OF_WEEK_IN_MONTH, success ) << endl;
  103. * cout << "AM_PM: " << calendar->get( UCAL_AM_PM, success ) << endl;
  104. * cout << "HOUR: " << calendar->get( UCAL_HOUR, success ) << endl;
  105. * cout << "HOUR_OF_DAY: " << calendar->get( UCAL_HOUR_OF_DAY, success ) << endl;
  106. * cout << "MINUTE: " << calendar->get( UCAL_MINUTE, success ) << endl;
  107. * cout << "SECOND: " << calendar->get( UCAL_SECOND, success ) << endl;
  108. * cout << "MILLISECOND: " << calendar->get( UCAL_MILLISECOND, success ) << endl;
  109. * cout << "ZONE_OFFSET: " << (calendar->get( UCAL_ZONE_OFFSET, success )/(60*60*1000)) << endl;
  110. * cout << "DST_OFFSET: " << (calendar->get( UCAL_DST_OFFSET, success )/(60*60*1000)) << endl;
  111. *
  112. * cout << "Current Time, with hour reset to 3" << endl;
  113. * calendar->clear(UCAL_HOUR_OF_DAY); // so doesn't override
  114. * calendar->set(UCAL_HOUR, 3);
  115. * cout << "ERA: " << calendar->get( UCAL_ERA, success ) << endl;
  116. * cout << "YEAR: " << calendar->get( UCAL_YEAR, success ) << endl;
  117. * cout << "MONTH: " << calendar->get( UCAL_MONTH, success ) << endl;
  118. * cout << "WEEK_OF_YEAR: " << calendar->get( UCAL_WEEK_OF_YEAR, success ) << endl;
  119. * cout << "WEEK_OF_MONTH: " << calendar->get( UCAL_WEEK_OF_MONTH, success ) << endl;
  120. * cout << "DATE: " << calendar->get( UCAL_DATE, success ) << endl;
  121. * cout << "DAY_OF_MONTH: " << calendar->get( UCAL_DAY_OF_MONTH, success ) << endl;
  122. * cout << "DAY_OF_YEAR: " << calendar->get( UCAL_DAY_OF_YEAR, success ) << endl;
  123. * cout << "DAY_OF_WEEK: " << calendar->get( UCAL_DAY_OF_WEEK, success ) << endl;
  124. * cout << "DAY_OF_WEEK_IN_MONTH: " << calendar->get( UCAL_DAY_OF_WEEK_IN_MONTH, success ) << endl;
  125. * cout << "AM_PM: " << calendar->get( UCAL_AM_PM, success ) << endl;
  126. * cout << "HOUR: " << calendar->get( UCAL_HOUR, success ) << endl;
  127. * cout << "HOUR_OF_DAY: " << calendar->get( UCAL_HOUR_OF_DAY, success ) << endl;
  128. * cout << "MINUTE: " << calendar->get( UCAL_MINUTE, success ) << endl;
  129. * cout << "SECOND: " << calendar->get( UCAL_SECOND, success ) << endl;
  130. * cout << "MILLISECOND: " << calendar->get( UCAL_MILLISECOND, success ) << endl;
  131. * cout << "ZONE_OFFSET: " << (calendar->get( UCAL_ZONE_OFFSET, success )/(60*60*1000)) << endl; // in hours
  132. * cout << "DST_OFFSET: " << (calendar->get( UCAL_DST_OFFSET, success )/(60*60*1000)) << endl; // in hours
  133. *
  134. * if (U_FAILURE(success)) {
  135. * cout << "An error occured. success=" << u_errorName(success) << endl;
  136. * }
  137. *
  138. * delete ids;
  139. * delete calendar; // also deletes pdt
  140. * \endcode
  141. * </pre>
  142. * @stable ICU 2.0
  143. */
  144. class U_I18N_API GregorianCalendar: public Calendar {
  145. public:
  146. /**
  147. * Useful constants for GregorianCalendar and TimeZone.
  148. * @stable ICU 2.0
  149. */
  150. enum EEras {
  151. BC,
  152. AD
  153. };
  154. /**
  155. * Constructs a default GregorianCalendar using the current time in the default time
  156. * zone with the default locale.
  157. *
  158. * @param success Indicates the status of GregorianCalendar object construction.
  159. * Returns U_ZERO_ERROR if constructed successfully.
  160. * @stable ICU 2.0
  161. */
  162. GregorianCalendar(UErrorCode& success);
  163. /**
  164. * Constructs a GregorianCalendar based on the current time in the given time zone
  165. * with the default locale. Clients are no longer responsible for deleting the given
  166. * time zone object after it's adopted.
  167. *
  168. * @param zoneToAdopt The given timezone.
  169. * @param success Indicates the status of GregorianCalendar object construction.
  170. * Returns U_ZERO_ERROR if constructed successfully.
  171. * @stable ICU 2.0
  172. */
  173. GregorianCalendar(TimeZone* zoneToAdopt, UErrorCode& success);
  174. /**
  175. * Constructs a GregorianCalendar based on the current time in the given time zone
  176. * with the default locale.
  177. *
  178. * @param zone The given timezone.
  179. * @param success Indicates the status of GregorianCalendar object construction.
  180. * Returns U_ZERO_ERROR if constructed successfully.
  181. * @stable ICU 2.0
  182. */
  183. GregorianCalendar(const TimeZone& zone, UErrorCode& success);
  184. /**
  185. * Constructs a GregorianCalendar based on the current time in the default time zone
  186. * with the given locale.
  187. *
  188. * @param aLocale The given locale.
  189. * @param success Indicates the status of GregorianCalendar object construction.
  190. * Returns U_ZERO_ERROR if constructed successfully.
  191. * @stable ICU 2.0
  192. */
  193. GregorianCalendar(const Locale& aLocale, UErrorCode& success);
  194. /**
  195. * Constructs a GregorianCalendar based on the current time in the given time zone
  196. * with the given locale. Clients are no longer responsible for deleting the given
  197. * time zone object after it's adopted.
  198. *
  199. * @param zoneToAdopt The given timezone.
  200. * @param aLocale The given locale.
  201. * @param success Indicates the status of GregorianCalendar object construction.
  202. * Returns U_ZERO_ERROR if constructed successfully.
  203. * @stable ICU 2.0
  204. */
  205. GregorianCalendar(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
  206. /**
  207. * Constructs a GregorianCalendar based on the current time in the given time zone
  208. * with the given locale.
  209. *
  210. * @param zone The given timezone.
  211. * @param aLocale The given locale.
  212. * @param success Indicates the status of GregorianCalendar object construction.
  213. * Returns U_ZERO_ERROR if constructed successfully.
  214. * @stable ICU 2.0
  215. */
  216. GregorianCalendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
  217. /**
  218. * Constructs a GregorianCalendar with the given AD date set in the default time
  219. * zone with the default locale.
  220. *
  221. * @param year The value used to set the YEAR time field in the calendar.
  222. * @param month The value used to set the MONTH time field in the calendar. Month
  223. * value is 0-based. e.g., 0 for January.
  224. * @param date The value used to set the DATE time field in the calendar.
  225. * @param success Indicates the status of GregorianCalendar object construction.
  226. * Returns U_ZERO_ERROR if constructed successfully.
  227. * @stable ICU 2.0
  228. */
  229. GregorianCalendar(int32_t year, int32_t month, int32_t date, UErrorCode& success);
  230. /**
  231. * Constructs a GregorianCalendar with the given AD date and time set for the
  232. * default time zone with the default locale.
  233. *
  234. * @param year The value used to set the YEAR time field in the calendar.
  235. * @param month The value used to set the MONTH time field in the calendar. Month
  236. * value is 0-based. e.g., 0 for January.
  237. * @param date The value used to set the DATE time field in the calendar.
  238. * @param hour The value used to set the HOUR_OF_DAY time field in the calendar.
  239. * @param minute The value used to set the MINUTE time field in the calendar.
  240. * @param success Indicates the status of GregorianCalendar object construction.
  241. * Returns U_ZERO_ERROR if constructed successfully.
  242. * @stable ICU 2.0
  243. */
  244. GregorianCalendar(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, UErrorCode& success);
  245. /**
  246. * Constructs a GregorianCalendar with the given AD date and time set for the
  247. * default time zone with the default locale.
  248. *
  249. * @param year The value used to set the YEAR time field in the calendar.
  250. * @param month The value used to set the MONTH time field in the calendar. Month
  251. * value is 0-based. e.g., 0 for January.
  252. * @param date The value used to set the DATE time field in the calendar.
  253. * @param hour The value used to set the HOUR_OF_DAY time field in the calendar.
  254. * @param minute The value used to set the MINUTE time field in the calendar.
  255. * @param second The value used to set the SECOND time field in the calendar.
  256. * @param success Indicates the status of GregorianCalendar object construction.
  257. * Returns U_ZERO_ERROR if constructed successfully.
  258. * @stable ICU 2.0
  259. */
  260. GregorianCalendar(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second, UErrorCode& success);
  261. /**
  262. * Destructor
  263. * @stable ICU 2.0
  264. */
  265. virtual ~GregorianCalendar();
  266. /**
  267. * Copy constructor
  268. * @param source the object to be copied.
  269. * @stable ICU 2.0
  270. */
  271. GregorianCalendar(const GregorianCalendar& source);
  272. /**
  273. * Default assignment operator
  274. * @param right the object to be copied.
  275. * @stable ICU 2.0
  276. */
  277. GregorianCalendar& operator=(const GregorianCalendar& right);
  278. /**
  279. * Create and return a polymorphic copy of this calendar.
  280. * @return return a polymorphic copy of this calendar.
  281. * @stable ICU 2.0
  282. */
  283. virtual Calendar* clone(void) const;
  284. /**
  285. * Sets the GregorianCalendar change date. This is the point when the switch from
  286. * Julian dates to Gregorian dates occurred. Default is 00:00:00 local time, October
  287. * 15, 1582. Previous to this time and date will be Julian dates.
  288. *
  289. * @param date The given Gregorian cutover date.
  290. * @param success Output param set to success/failure code on exit.
  291. * @stable ICU 2.0
  292. */
  293. void setGregorianChange(UDate date, UErrorCode& success);
  294. /**
  295. * Gets the Gregorian Calendar change date. This is the point when the switch from
  296. * Julian dates to Gregorian dates occurred. Default is 00:00:00 local time, October
  297. * 15, 1582. Previous to this time and date will be Julian dates.
  298. *
  299. * @return The Gregorian cutover time for this calendar.
  300. * @stable ICU 2.0
  301. */
  302. UDate getGregorianChange(void) const;
  303. /**
  304. * Return true if the given year is a leap year. Determination of whether a year is
  305. * a leap year is actually very complicated. We do something crude and mostly
  306. * correct here, but for a real determination you need a lot of contextual
  307. * information. For example, in Sweden, the change from Julian to Gregorian happened
  308. * in a complex way resulting in missed leap years and double leap years between
  309. * 1700 and 1753. Another example is that after the start of the Julian calendar in
  310. * 45 B.C., the leap years did not regularize until 8 A.D. This method ignores these
  311. * quirks, and pays attention only to the Julian onset date and the Gregorian
  312. * cutover (which can be changed).
  313. *
  314. * @param year The given year.
  315. * @return True if the given year is a leap year; false otherwise.
  316. * @stable ICU 2.0
  317. */
  318. UBool isLeapYear(int32_t year) const;
  319. /**
  320. * Returns TRUE if the given Calendar object is equivalent to this
  321. * one. Calendar override.
  322. *
  323. * @param other the Calendar to be compared with this Calendar
  324. * @stable ICU 2.4
  325. */
  326. virtual UBool isEquivalentTo(const Calendar& other) const;
  327. /**
  328. * (Overrides Calendar) Rolls up or down by the given amount in the specified field.
  329. * For more information, see the documentation for Calendar::roll().
  330. *
  331. * @param field The time field.
  332. * @param amount Indicates amount to roll.
  333. * @param status Output param set to success/failure code on exit. If any value
  334. * previously set in the time field is invalid, this will be set to
  335. * an error status.
  336. * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
  337. */
  338. virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
  339. /**
  340. * (Overrides Calendar) Rolls up or down by the given amount in the specified field.
  341. * For more information, see the documentation for Calendar::roll().
  342. *
  343. * @param field The time field.
  344. * @param amount Indicates amount to roll.
  345. * @param status Output param set to success/failure code on exit. If any value
  346. * previously set in the time field is invalid, this will be set to
  347. * an error status.
  348. * @stable ICU 2.6.
  349. */
  350. virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
  351. #ifndef U_HIDE_DEPRECATED_API
  352. /**
  353. * Return the minimum value that this field could have, given the current date.
  354. * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
  355. * @param field the time field.
  356. * @return the minimum value that this field could have, given the current date.
  357. * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field) instead.
  358. */
  359. int32_t getActualMinimum(EDateFields field) const;
  360. /**
  361. * Return the minimum value that this field could have, given the current date.
  362. * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
  363. * @param field the time field.
  364. * @param status
  365. * @return the minimum value that this field could have, given the current date.
  366. * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field) instead. (Added to ICU 3.0 for signature consistency)
  367. */
  368. int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
  369. #endif /* U_HIDE_DEPRECATED_API */
  370. /**
  371. * Return the minimum value that this field could have, given the current date.
  372. * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
  373. * @param field the time field.
  374. * @param status error result.
  375. * @return the minimum value that this field could have, given the current date.
  376. * @stable ICU 3.0
  377. */
  378. int32_t getActualMinimum(UCalendarDateFields field, UErrorCode &status) const;
  379. #ifndef U_HIDE_DEPRECATED_API
  380. /**
  381. * Return the maximum value that this field could have, given the current date.
  382. * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
  383. * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
  384. * for some years the actual maximum for MONTH is 12, and for others 13.
  385. * @param field the time field.
  386. * @return the maximum value that this field could have, given the current date.
  387. * @deprecated ICU 2.6. Use getActualMaximum(UCalendarDateFields field) instead.
  388. */
  389. int32_t getActualMaximum(EDateFields field) const;
  390. #endif /* U_HIDE_DEPRECATED_API */
  391. /**
  392. * Return the maximum value that this field could have, given the current date.
  393. * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
  394. * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
  395. * for some years the actual maximum for MONTH is 12, and for others 13.
  396. * @param field the time field.
  397. * @param status returns any errors that may result from this function call.
  398. * @return the maximum value that this field could have, given the current date.
  399. * @stable ICU 2.6
  400. */
  401. virtual int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
  402. /**
  403. * (Overrides Calendar) Return true if the current date for this Calendar is in
  404. * Daylight Savings Time. Recognizes DST_OFFSET, if it is set.
  405. *
  406. * @param status Fill-in parameter which receives the status of this operation.
  407. * @return True if the current date for this Calendar is in Daylight Savings Time,
  408. * false, otherwise.
  409. * @stable ICU 2.0
  410. */
  411. virtual UBool inDaylightTime(UErrorCode& status) const;
  412. public:
  413. /**
  414. * Override Calendar Returns a unique class ID POLYMORPHICALLY. Pure virtual
  415. * override. This method is to implement a simple version of RTTI, since not all C++
  416. * compilers support genuine RTTI. Polymorphic operator==() and clone() methods call
  417. * this method.
  418. *
  419. * @return The class ID for this object. All objects of a given class have the
  420. * same class ID. Objects of other classes have different class IDs.
  421. * @stable ICU 2.0
  422. */
  423. virtual UClassID getDynamicClassID(void) const;
  424. /**
  425. * Return the class ID for this class. This is useful only for comparing to a return
  426. * value from getDynamicClassID(). For example:
  427. *
  428. * Base* polymorphic_pointer = createPolymorphicObject();
  429. * if (polymorphic_pointer->getDynamicClassID() ==
  430. * Derived::getStaticClassID()) ...
  431. *
  432. * @return The class ID for all objects of this class.
  433. * @stable ICU 2.0
  434. */
  435. static UClassID U_EXPORT2 getStaticClassID(void);
  436. /**
  437. * Returns the calendar type name string for this Calendar object.
  438. * The returned string is the legacy ICU calendar attribute value,
  439. * for example, "gregorian" or "japanese".
  440. *
  441. * For more details see the Calendar::getType() documentation.
  442. *
  443. * @return legacy calendar type name string
  444. * @stable ICU 49
  445. */
  446. virtual const char * getType() const;
  447. private:
  448. GregorianCalendar(); // default constructor not implemented
  449. protected:
  450. /**
  451. * Return the ERA. We need a special method for this because the
  452. * default ERA is AD, but a zero (unset) ERA is BC.
  453. * @return the ERA.
  454. * @internal
  455. */
  456. virtual int32_t internalGetEra() const;
  457. /**
  458. * Return the Julian day number of day before the first day of the
  459. * given month in the given extended year. Subclasses should override
  460. * this method to implement their calendar system.
  461. * @param eyear the extended year
  462. * @param month the zero-based month, or 0 if useMonth is false
  463. * @param useMonth if false, compute the day before the first day of
  464. * the given year, otherwise, compute the day before the first day of
  465. * the given month
  466. * @return the Julian day number of the day before the first
  467. * day of the given month and year
  468. * @internal
  469. */
  470. virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
  471. UBool useMonth) const;
  472. /**
  473. * Subclasses may override this. This method calls
  474. * handleGetMonthLength() to obtain the calendar-specific month
  475. * length.
  476. * @param bestField which field to use to calculate the date
  477. * @return julian day specified by calendar fields.
  478. * @internal
  479. */
  480. virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField) ;
  481. /**
  482. * Return the number of days in the given month of the given extended
  483. * year of this calendar system. Subclasses should override this
  484. * method if they can provide a more correct or more efficient
  485. * implementation than the default implementation in Calendar.
  486. * @internal
  487. */
  488. virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const;
  489. /**
  490. * Return the number of days in the given extended year of this
  491. * calendar system. Subclasses should override this method if they can
  492. * provide a more correct or more efficient implementation than the
  493. * default implementation in Calendar.
  494. * @stable ICU 2.0
  495. */
  496. virtual int32_t handleGetYearLength(int32_t eyear) const;
  497. /**
  498. * return the length of the given month.
  499. * @param month the given month.
  500. * @return the length of the given month.
  501. * @internal
  502. */
  503. virtual int32_t monthLength(int32_t month) const;
  504. /**
  505. * return the length of the month according to the given year.
  506. * @param month the given month.
  507. * @param year the given year.
  508. * @return the length of the month
  509. * @internal
  510. */
  511. virtual int32_t monthLength(int32_t month, int32_t year) const;
  512. #ifndef U_HIDE_INTERNAL_API
  513. /**
  514. * return the length of the given year.
  515. * @param year the given year.
  516. * @return the length of the given year.
  517. * @internal
  518. */
  519. int32_t yearLength(int32_t year) const;
  520. /**
  521. * return the length of the year field.
  522. * @return the length of the year field
  523. * @internal
  524. */
  525. int32_t yearLength(void) const;
  526. /**
  527. * After adjustments such as add(MONTH), add(YEAR), we don't want the
  528. * month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar
  529. * 3, we want it to go to Feb 28. Adjustments which might run into this
  530. * problem call this method to retain the proper month.
  531. * @internal
  532. */
  533. void pinDayOfMonth(void);
  534. #endif /* U_HIDE_INTERNAL_API */
  535. /**
  536. * Return the day number with respect to the epoch. January 1, 1970 (Gregorian)
  537. * is day zero.
  538. * @param status Fill-in parameter which receives the status of this operation.
  539. * @return the day number with respect to the epoch.
  540. * @internal
  541. */
  542. virtual UDate getEpochDay(UErrorCode& status);
  543. /**
  544. * Subclass API for defining limits of different types.
  545. * Subclasses must implement this method to return limits for the
  546. * following fields:
  547. *
  548. * <pre>UCAL_ERA
  549. * UCAL_YEAR
  550. * UCAL_MONTH
  551. * UCAL_WEEK_OF_YEAR
  552. * UCAL_WEEK_OF_MONTH
  553. * UCAL_DATE (DAY_OF_MONTH on Java)
  554. * UCAL_DAY_OF_YEAR
  555. * UCAL_DAY_OF_WEEK_IN_MONTH
  556. * UCAL_YEAR_WOY
  557. * UCAL_EXTENDED_YEAR</pre>
  558. *
  559. * @param field one of the above field numbers
  560. * @param limitType one of <code>MINIMUM</code>, <code>GREATEST_MINIMUM</code>,
  561. * <code>LEAST_MAXIMUM</code>, or <code>MAXIMUM</code>
  562. * @internal
  563. */
  564. virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const;
  565. /**
  566. * Return the extended year defined by the current fields. This will
  567. * use the UCAL_EXTENDED_YEAR field or the UCAL_YEAR and supra-year fields (such
  568. * as UCAL_ERA) specific to the calendar system, depending on which set of
  569. * fields is newer.
  570. * @return the extended year
  571. * @internal
  572. */
  573. virtual int32_t handleGetExtendedYear();
  574. /**
  575. * Subclasses may override this to convert from week fields
  576. * (YEAR_WOY and WEEK_OF_YEAR) to an extended year in the case
  577. * where YEAR, EXTENDED_YEAR are not set.
  578. * The Gregorian implementation assumes a yearWoy in gregorian format, according to the current era.
  579. * @return the extended year, UCAL_EXTENDED_YEAR
  580. * @internal
  581. */
  582. virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
  583. /**
  584. * Subclasses may override this method to compute several fields
  585. * specific to each calendar system. These are:
  586. *
  587. * <ul><li>ERA
  588. * <li>YEAR
  589. * <li>MONTH
  590. * <li>DAY_OF_MONTH
  591. * <li>DAY_OF_YEAR
  592. * <li>EXTENDED_YEAR</ul>
  593. *
  594. * <p>The GregorianCalendar implementation implements
  595. * a calendar with the specified Julian/Gregorian cutover date.
  596. * @internal
  597. */
  598. virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
  599. private:
  600. /**
  601. * Compute the julian day number of the given year.
  602. * @param isGregorian if true, using Gregorian calendar, otherwise using Julian calendar
  603. * @param year the given year.
  604. * @param isLeap true if the year is a leap year.
  605. * @return
  606. */
  607. static double computeJulianDayOfYear(UBool isGregorian, int32_t year,
  608. UBool& isLeap);
  609. /**
  610. * Validates the values of the set time fields. True if they're all valid.
  611. * @return True if the set time fields are all valid.
  612. */
  613. UBool validateFields(void) const;
  614. /**
  615. * Validates the value of the given time field. True if it's valid.
  616. */
  617. UBool boundsCheck(int32_t value, UCalendarDateFields field) const;
  618. /**
  619. * Return the pseudo-time-stamp for two fields, given their
  620. * individual pseudo-time-stamps. If either of the fields
  621. * is unset, then the aggregate is unset. Otherwise, the
  622. * aggregate is the later of the two stamps.
  623. * @param stamp_a One given field.
  624. * @param stamp_b Another given field.
  625. * @return the pseudo-time-stamp for two fields
  626. */
  627. int32_t aggregateStamp(int32_t stamp_a, int32_t stamp_b);
  628. /**
  629. * The point at which the Gregorian calendar rules are used, measured in
  630. * milliseconds from the standard epoch. Default is October 15, 1582
  631. * (Gregorian) 00:00:00 UTC, that is, October 4, 1582 (Julian) is followed
  632. * by October 15, 1582 (Gregorian). This corresponds to Julian day number
  633. * 2299161. This is measured from the standard epoch, not in Julian Days.
  634. */
  635. UDate fGregorianCutover;
  636. /**
  637. * Julian day number of the Gregorian cutover
  638. */
  639. int32_t fCutoverJulianDay;
  640. /**
  641. * Midnight, local time (using this Calendar's TimeZone) at or before the
  642. * gregorianCutover. This is a pure date value with no time of day or
  643. * timezone component.
  644. */
  645. UDate fNormalizedGregorianCutover;// = gregorianCutover;
  646. /**
  647. * The year of the gregorianCutover, with 0 representing
  648. * 1 BC, -1 representing 2 BC, etc.
  649. */
  650. int32_t fGregorianCutoverYear;// = 1582;
  651. /**
  652. * The year of the gregorianCutover, with 0 representing
  653. * 1 BC, -1 representing 2 BC, etc.
  654. */
  655. int32_t fGregorianCutoverJulianDay;// = 2299161;
  656. /**
  657. * Converts time as milliseconds to Julian date. The Julian date used here is not a
  658. * true Julian date, since it is measured from midnight, not noon.
  659. *
  660. * @param millis The given milliseconds.
  661. * @return The Julian date number.
  662. */
  663. static double millisToJulianDay(UDate millis);
  664. /**
  665. * Converts Julian date to time as milliseconds. The Julian date used here is not a
  666. * true Julian date, since it is measured from midnight, not noon.
  667. *
  668. * @param julian The given Julian date number.
  669. * @return Time as milliseconds.
  670. */
  671. static UDate julianDayToMillis(double julian);
  672. /**
  673. * Used by handleComputeJulianDay() and handleComputeMonthStart().
  674. * Temporary field indicating whether the calendar is currently Gregorian as opposed to Julian.
  675. */
  676. UBool fIsGregorian;
  677. /**
  678. * Used by handleComputeJulianDay() and handleComputeMonthStart().
  679. * Temporary field indicating that the sense of the gregorian cutover should be inverted
  680. * to handle certain calculations on and around the cutover date.
  681. */
  682. UBool fInvertGregorian;
  683. public: // internal implementation
  684. /**
  685. * @return TRUE if this calendar has the notion of a default century
  686. * @internal
  687. */
  688. virtual UBool haveDefaultCentury() const;
  689. /**
  690. * @return the start of the default century
  691. * @internal
  692. */
  693. virtual UDate defaultCenturyStart() const;
  694. /**
  695. * @return the beginning year of the default century
  696. * @internal
  697. */
  698. virtual int32_t defaultCenturyStartYear() const;
  699. };
  700. U_NAMESPACE_END
  701. #endif /* #if !UCONFIG_NO_FORMATTING */
  702. #endif // _GREGOCAL
  703. //eof