calendar.h 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ********************************************************************************
  5. * Copyright (C) 1997-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. ********************************************************************************
  8. *
  9. * File CALENDAR.H
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 04/22/97 aliu Expanded and corrected comments and other header
  15. * contents.
  16. * 05/01/97 aliu Made equals(), before(), after() arguments const.
  17. * 05/20/97 aliu Replaced fAreFieldsSet with fAreFieldsInSync and
  18. * fAreAllFieldsSet.
  19. * 07/27/98 stephen Sync up with JDK 1.2
  20. * 11/15/99 weiv added YEAR_WOY and DOW_LOCAL
  21. * to EDateFields
  22. * 8/19/2002 srl Removed Javaisms
  23. * 11/07/2003 srl Update, clean up documentation.
  24. ********************************************************************************
  25. */
  26. #ifndef CALENDAR_H
  27. #define CALENDAR_H
  28. #include "unicode/utypes.h"
  29. #if U_SHOW_CPLUSPLUS_API
  30. /**
  31. * \file
  32. * \brief C++ API: Calendar object
  33. */
  34. #if !UCONFIG_NO_FORMATTING
  35. #include "unicode/uobject.h"
  36. #include "unicode/locid.h"
  37. #include "unicode/timezone.h"
  38. #include "unicode/ucal.h"
  39. #include "unicode/umisc.h"
  40. U_NAMESPACE_BEGIN
  41. class ICUServiceFactory;
  42. // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API,
  43. // it is a return type for a virtual method (@internal)
  44. /**
  45. * @internal
  46. */
  47. typedef int32_t UFieldResolutionTable[12][8];
  48. class BasicTimeZone;
  49. /**
  50. * `Calendar` is an abstract base class for converting between
  51. * a `UDate` object and a set of integer fields such as
  52. * `YEAR`, `MONTH`, `DAY`, `HOUR`, and so on.
  53. * (A `UDate` object represents a specific instant in
  54. * time with millisecond precision. See UDate
  55. * for information about the `UDate` class.)
  56. *
  57. * Subclasses of `Calendar` interpret a `UDate`
  58. * according to the rules of a specific calendar system.
  59. * The most commonly used subclass of `Calendar` is
  60. * `GregorianCalendar`. Other subclasses could represent
  61. * the various types of lunar calendars in use in many parts of the world.
  62. *
  63. * **NOTE**: (ICU 2.6) The subclass interface should be considered unstable -
  64. * it WILL change.
  65. *
  66. * Like other locale-sensitive classes, `Calendar` provides a
  67. * static method, `createInstance`, for getting a generally useful
  68. * object of this type. `Calendar`'s `createInstance` method
  69. * returns the appropriate `Calendar` subclass whose
  70. * time fields have been initialized with the current date and time:
  71. *
  72. * Calendar *rightNow = Calendar::createInstance(errCode);
  73. *
  74. * A `Calendar` object can produce all the time field values
  75. * needed to implement the date-time formatting for a particular language
  76. * and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
  77. *
  78. * When computing a `UDate` from time fields, some special circumstances
  79. * may arise: there may be insufficient information to compute the
  80. * `UDate` (such as only year and month but no day in the month),
  81. * there may be inconsistent information (such as "Tuesday, July 15, 1996"
  82. * -- July 15, 1996 is actually a Monday), or the input time might be ambiguous
  83. * because of time zone transition.
  84. *
  85. * **Insufficient information.** The calendar will use default
  86. * information to specify the missing fields. This may vary by calendar; for
  87. * the Gregorian calendar, the default for a field is the same as that of the
  88. * start of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc.
  89. *
  90. * **Inconsistent information.** If fields conflict, the calendar
  91. * will give preference to fields set more recently. For example, when
  92. * determining the day, the calendar will look for one of the following
  93. * combinations of fields. The most recent combination, as determined by the
  94. * most recently set single field, will be used.
  95. *
  96. * MONTH + DAY_OF_MONTH
  97. * MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
  98. * MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
  99. * DAY_OF_YEAR
  100. * DAY_OF_WEEK + WEEK_OF_YEAR
  101. *
  102. * For the time of day:
  103. *
  104. * HOUR_OF_DAY
  105. * AM_PM + HOUR
  106. *
  107. * **Ambiguous Wall Clock Time.** When time offset from UTC has
  108. * changed, it produces an ambiguous time slot around the transition. For example,
  109. * many US locations observe daylight saving time. On the date switching to daylight
  110. * saving time in US, wall clock time jumps from 12:59 AM (standard) to 2:00 AM
  111. * (daylight). Therefore, wall clock time from 1:00 AM to 1:59 AM do not exist on
  112. * the date. When the input wall time fall into this missing time slot, the ICU
  113. * Calendar resolves the time using the UTC offset before the transition by default.
  114. * In this example, 1:30 AM is interpreted as 1:30 AM standard time (non-exist),
  115. * so the final result will be 2:30 AM daylight time.
  116. *
  117. * On the date switching back to standard time, wall clock time is moved back one
  118. * hour at 2:00 AM. So wall clock time from 1:00 AM to 1:59 AM occur twice. In this
  119. * case, the ICU Calendar resolves the time using the UTC offset after the transition
  120. * by default. For example, 1:30 AM on the date is resolved as 1:30 AM standard time.
  121. *
  122. * Ambiguous wall clock time resolution behaviors can be customized by Calendar APIs
  123. * {@link #setRepeatedWallTimeOption} and {@link #setSkippedWallTimeOption}.
  124. * These methods are available in ICU 49 or later versions.
  125. *
  126. * **Note:** for some non-Gregorian calendars, different
  127. * fields may be necessary for complete disambiguation. For example, a full
  128. * specification of the historical Arabic astronomical calendar requires year,
  129. * month, day-of-month *and* day-of-week in some cases.
  130. *
  131. * **Note:** There are certain possible ambiguities in
  132. * interpretation of certain singular times, which are resolved in the
  133. * following ways:
  134. *
  135. * 1. 24:00:00 "belongs" to the following day. That is,
  136. * 23:59 on Dec 31, 1969 < 24:00 on Jan 1, 1970 < 24:01:00 on Jan 1, 1970
  137. * 2. Although historically not precise, midnight also belongs to "am",
  138. * and noon belongs to "pm", so on the same day,
  139. * 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm
  140. *
  141. * The date or time format strings are not part of the definition of a
  142. * calendar, as those must be modifiable or overridable by the user at
  143. * runtime. Use `DateFormat` to format dates.
  144. *
  145. * `Calendar` provides an API for field "rolling", where fields
  146. * can be incremented or decremented, but wrap around. For example, rolling the
  147. * month up in the date December 12, **1996** results in
  148. * January 12, **1996**.
  149. *
  150. * `Calendar` also provides a date arithmetic function for
  151. * adding the specified (signed) amount of time to a particular time field.
  152. * For example, subtracting 5 days from the date `September 12, 1996`
  153. * results in `September 7, 1996`.
  154. *
  155. * ***Supported range***
  156. *
  157. * The allowable range of `Calendar` has been narrowed. `GregorianCalendar` used
  158. * to attempt to support the range of dates with millisecond values from
  159. * `Long.MIN_VALUE` to `Long.MAX_VALUE`. The new `Calendar` protocol specifies the
  160. * maximum range of supportable dates as those having Julian day numbers
  161. * of `-0x7F000000` to `+0x7F000000`. This corresponds to years from ~5,800,000 BCE
  162. * to ~5,800,000 CE. Programmers should use the protected constants in `Calendar` to
  163. * specify an extremely early or extremely late date.
  164. *
  165. * <p>
  166. * The Japanese calendar uses a combination of era name and year number.
  167. * When an emperor of Japan abdicates and a new emperor ascends the throne,
  168. * a new era is declared and year number is reset to 1. Even if the date of
  169. * abdication is scheduled ahead of time, the new era name might not be
  170. * announced until just before the date. In such case, ICU4C may include
  171. * a start date of future era without actual era name, but not enabled
  172. * by default. ICU4C users who want to test the behavior of the future era
  173. * can enable the tentative era by:
  174. * <ul>
  175. * <li>Environment variable <code>ICU_ENABLE_TENTATIVE_ERA=true</code>.</li>
  176. * </ul>
  177. *
  178. * @stable ICU 2.0
  179. */
  180. class U_I18N_API Calendar : public UObject {
  181. public:
  182. #ifndef U_FORCE_HIDE_DEPRECATED_API
  183. /**
  184. * Field IDs for date and time. Used to specify date/time fields. ERA is calendar
  185. * specific. Example ranges given are for illustration only; see specific Calendar
  186. * subclasses for actual ranges.
  187. * @deprecated ICU 2.6. Use C enum UCalendarDateFields defined in ucal.h
  188. */
  189. enum EDateFields {
  190. #ifndef U_HIDE_DEPRECATED_API
  191. /*
  192. * ERA may be defined on other platforms. To avoid any potential problems undefined it here.
  193. */
  194. #ifdef ERA
  195. #undef ERA
  196. #endif
  197. ERA, // Example: 0..1
  198. YEAR, // Example: 1..big number
  199. MONTH, // Example: 0..11
  200. WEEK_OF_YEAR, // Example: 1..53
  201. WEEK_OF_MONTH, // Example: 1..4
  202. DATE, // Example: 1..31
  203. DAY_OF_YEAR, // Example: 1..365
  204. DAY_OF_WEEK, // Example: 1..7
  205. DAY_OF_WEEK_IN_MONTH, // Example: 1..4, may be specified as -1
  206. AM_PM, // Example: 0..1
  207. HOUR, // Example: 0..11
  208. HOUR_OF_DAY, // Example: 0..23
  209. MINUTE, // Example: 0..59
  210. SECOND, // Example: 0..59
  211. MILLISECOND, // Example: 0..999
  212. ZONE_OFFSET, // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR
  213. DST_OFFSET, // Example: 0 or U_MILLIS_PER_HOUR
  214. YEAR_WOY, // 'Y' Example: 1..big number - Year of Week of Year
  215. DOW_LOCAL, // 'e' Example: 1..7 - Day of Week / Localized
  216. EXTENDED_YEAR,
  217. JULIAN_DAY,
  218. MILLISECONDS_IN_DAY,
  219. IS_LEAP_MONTH,
  220. FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields.
  221. #endif /* U_HIDE_DEPRECATED_API */
  222. };
  223. #endif // U_FORCE_HIDE_DEPRECATED_API
  224. #ifndef U_HIDE_DEPRECATED_API
  225. /**
  226. * Useful constant for days of week. Note: Calendar day-of-week is 1-based. Clients
  227. * who create locale resources for the field of first-day-of-week should be aware of
  228. * this. For instance, in US locale, first-day-of-week is set to 1, i.e., SUNDAY.
  229. * @deprecated ICU 2.6. Use C enum UCalendarDaysOfWeek defined in ucal.h
  230. */
  231. enum EDaysOfWeek {
  232. SUNDAY = 1,
  233. MONDAY,
  234. TUESDAY,
  235. WEDNESDAY,
  236. THURSDAY,
  237. FRIDAY,
  238. SATURDAY
  239. };
  240. /**
  241. * Useful constants for month. Note: Calendar month is 0-based.
  242. * @deprecated ICU 2.6. Use C enum UCalendarMonths defined in ucal.h
  243. */
  244. enum EMonths {
  245. JANUARY,
  246. FEBRUARY,
  247. MARCH,
  248. APRIL,
  249. MAY,
  250. JUNE,
  251. JULY,
  252. AUGUST,
  253. SEPTEMBER,
  254. OCTOBER,
  255. NOVEMBER,
  256. DECEMBER,
  257. UNDECIMBER
  258. };
  259. /**
  260. * Useful constants for hour in 12-hour clock. Used in GregorianCalendar.
  261. * @deprecated ICU 2.6. Use C enum UCalendarAMPMs defined in ucal.h
  262. */
  263. enum EAmpm {
  264. AM,
  265. PM
  266. };
  267. #endif /* U_HIDE_DEPRECATED_API */
  268. /**
  269. * destructor
  270. * @stable ICU 2.0
  271. */
  272. virtual ~Calendar();
  273. /**
  274. * Create and return a polymorphic copy of this calendar.
  275. *
  276. * @return a polymorphic copy of this calendar.
  277. * @stable ICU 2.0
  278. */
  279. virtual Calendar* clone() const = 0;
  280. /**
  281. * Creates a Calendar using the default timezone and locale. Clients are responsible
  282. * for deleting the object returned.
  283. *
  284. * @param success Indicates the success/failure of Calendar creation. Filled in
  285. * with U_ZERO_ERROR if created successfully, set to a failure result
  286. * otherwise. U_MISSING_RESOURCE_ERROR will be returned if the resource data
  287. * requests a calendar type which has not been installed.
  288. * @return A Calendar if created successfully. nullptr otherwise.
  289. * @stable ICU 2.0
  290. */
  291. static Calendar* U_EXPORT2 createInstance(UErrorCode& success);
  292. /**
  293. * Creates a Calendar using the given timezone and the default locale.
  294. * The Calendar takes ownership of zoneToAdopt; the
  295. * client must not delete it.
  296. *
  297. * @param zoneToAdopt The given timezone to be adopted.
  298. * @param success Indicates the success/failure of Calendar creation. Filled in
  299. * with U_ZERO_ERROR if created successfully, set to a failure result
  300. * otherwise.
  301. * @return A Calendar if created successfully. nullptr otherwise.
  302. * @stable ICU 2.0
  303. */
  304. static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, UErrorCode& success);
  305. /**
  306. * Creates a Calendar using the given timezone and the default locale. The TimeZone
  307. * is _not_ adopted; the client is still responsible for deleting it.
  308. *
  309. * @param zone The timezone.
  310. * @param success Indicates the success/failure of Calendar creation. Filled in
  311. * with U_ZERO_ERROR if created successfully, set to a failure result
  312. * otherwise.
  313. * @return A Calendar if created successfully. nullptr otherwise.
  314. * @stable ICU 2.0
  315. */
  316. static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, UErrorCode& success);
  317. /**
  318. * Creates a Calendar using the default timezone and the given locale.
  319. *
  320. * @param aLocale The given locale.
  321. * @param success Indicates the success/failure of Calendar creation. Filled in
  322. * with U_ZERO_ERROR if created successfully, set to a failure result
  323. * otherwise.
  324. * @return A Calendar if created successfully. nullptr otherwise.
  325. * @stable ICU 2.0
  326. */
  327. static Calendar* U_EXPORT2 createInstance(const Locale& aLocale, UErrorCode& success);
  328. /**
  329. * Creates a Calendar using the given timezone and given locale.
  330. * The Calendar takes ownership of zoneToAdopt; the
  331. * client must not delete it.
  332. *
  333. * @param zoneToAdopt The given timezone to be adopted.
  334. * @param aLocale The given locale.
  335. * @param success Indicates the success/failure of Calendar creation. Filled in
  336. * with U_ZERO_ERROR if created successfully, set to a failure result
  337. * otherwise.
  338. * @return A Calendar if created successfully. nullptr otherwise.
  339. * @stable ICU 2.0
  340. */
  341. static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
  342. /**
  343. * Gets a Calendar using the given timezone and given locale. The TimeZone
  344. * is _not_ adopted; the client is still responsible for deleting it.
  345. *
  346. * @param zone The given timezone.
  347. * @param aLocale The given locale.
  348. * @param success Indicates the success/failure of Calendar creation. Filled in
  349. * with U_ZERO_ERROR if created successfully, set to a failure result
  350. * otherwise.
  351. * @return A Calendar if created successfully. nullptr otherwise.
  352. * @stable ICU 2.0
  353. */
  354. static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
  355. /**
  356. * Returns a list of the locales for which Calendars are installed.
  357. *
  358. * @param count Number of locales returned.
  359. * @return An array of Locale objects representing the set of locales for which
  360. * Calendars are installed. The system retains ownership of this list;
  361. * the caller must NOT delete it. Does not include user-registered Calendars.
  362. * @stable ICU 2.0
  363. */
  364. static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
  365. /**
  366. * Given a key and a locale, returns an array of string values in a preferred
  367. * order that would make a difference. These are all and only those values where
  368. * the open (creation) of the service with the locale formed from the input locale
  369. * plus input keyword and that value has different behavior than creation with the
  370. * input locale alone.
  371. * @param key one of the keys supported by this service. For now, only
  372. * "calendar" is supported.
  373. * @param locale the locale
  374. * @param commonlyUsed if set to true it will return only commonly used values
  375. * with the given locale in preferred order. Otherwise,
  376. * it will return all the available values for the locale.
  377. * @param status ICU Error Code
  378. * @return a string enumeration over keyword values for the given key and the locale.
  379. * @stable ICU 4.2
  380. */
  381. static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* key,
  382. const Locale& locale, UBool commonlyUsed, UErrorCode& status);
  383. /**
  384. * Returns the current UTC (GMT) time measured in milliseconds since 0:00:00 on 1/1/70
  385. * (derived from the system time).
  386. *
  387. * @return The current UTC time in milliseconds.
  388. * @stable ICU 2.0
  389. */
  390. static UDate U_EXPORT2 getNow();
  391. /**
  392. * Gets this Calendar's time as milliseconds. May involve recalculation of time due
  393. * to previous calls to set time field values. The time specified is non-local UTC
  394. * (GMT) time. Although this method is const, this object may actually be changed
  395. * (semantically const).
  396. *
  397. * @param status Output param set to success/failure code on exit. If any value
  398. * previously set in the time field is invalid or restricted by
  399. * leniency, this will be set to an error status.
  400. * @return The current time in UTC (GMT) time, or zero if the operation
  401. * failed.
  402. * @stable ICU 2.0
  403. */
  404. inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); }
  405. /**
  406. * Sets this Calendar's current time with the given UDate. The time specified should
  407. * be in non-local UTC (GMT) time.
  408. *
  409. * @param date The given UDate in UTC (GMT) time.
  410. * @param status Output param set to success/failure code on exit. If any value
  411. * set in the time field is invalid or restricted by
  412. * leniency, this will be set to an error status.
  413. * @stable ICU 2.0
  414. */
  415. inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); }
  416. /**
  417. * Compares the equality of two Calendar objects. Objects of different subclasses
  418. * are considered unequal. This comparison is very exacting; two Calendar objects
  419. * must be in exactly the same state to be considered equal. To compare based on the
  420. * represented time, use equals() instead.
  421. *
  422. * @param that The Calendar object to be compared with.
  423. * @return true if the given Calendar is the same as this Calendar; false
  424. * otherwise.
  425. * @stable ICU 2.0
  426. */
  427. virtual bool operator==(const Calendar& that) const;
  428. /**
  429. * Compares the inequality of two Calendar objects.
  430. *
  431. * @param that The Calendar object to be compared with.
  432. * @return true if the given Calendar is not the same as this Calendar; false
  433. * otherwise.
  434. * @stable ICU 2.0
  435. */
  436. bool operator!=(const Calendar& that) const {return !operator==(that);}
  437. /**
  438. * Returns true if the given Calendar object is equivalent to this
  439. * one. An equivalent Calendar will behave exactly as this one
  440. * does, but it may be set to a different time. By contrast, for
  441. * the operator==() method to return true, the other Calendar must
  442. * be set to the same time.
  443. *
  444. * @param other the Calendar to be compared with this Calendar
  445. * @stable ICU 2.4
  446. */
  447. virtual UBool isEquivalentTo(const Calendar& other) const;
  448. /**
  449. * Compares the Calendar time, whereas Calendar::operator== compares the equality of
  450. * Calendar objects.
  451. *
  452. * @param when The Calendar to be compared with this Calendar. Although this is a
  453. * const parameter, the object may be modified physically
  454. * (semantically const).
  455. * @param status Output param set to success/failure code on exit. If any value
  456. * previously set in the time field is invalid or restricted by
  457. * leniency, this will be set to an error status.
  458. * @return True if the current time of this Calendar is equal to the time of
  459. * Calendar when; false otherwise.
  460. * @stable ICU 2.0
  461. */
  462. UBool equals(const Calendar& when, UErrorCode& status) const;
  463. /**
  464. * Returns true if this Calendar's current time is before "when"'s current time.
  465. *
  466. * @param when The Calendar to be compared with this Calendar. Although this is a
  467. * const parameter, the object may be modified physically
  468. * (semantically const).
  469. * @param status Output param set to success/failure code on exit. If any value
  470. * previously set in the time field is invalid or restricted by
  471. * leniency, this will be set to an error status.
  472. * @return True if the current time of this Calendar is before the time of
  473. * Calendar when; false otherwise.
  474. * @stable ICU 2.0
  475. */
  476. UBool before(const Calendar& when, UErrorCode& status) const;
  477. /**
  478. * Returns true if this Calendar's current time is after "when"'s current time.
  479. *
  480. * @param when The Calendar to be compared with this Calendar. Although this is a
  481. * const parameter, the object may be modified physically
  482. * (semantically const).
  483. * @param status Output param set to success/failure code on exit. If any value
  484. * previously set in the time field is invalid or restricted by
  485. * leniency, this will be set to an error status.
  486. * @return True if the current time of this Calendar is after the time of
  487. * Calendar when; false otherwise.
  488. * @stable ICU 2.0
  489. */
  490. UBool after(const Calendar& when, UErrorCode& status) const;
  491. #ifndef U_FORCE_HIDE_DEPRECATED_API
  492. /**
  493. * UDate Arithmetic function. Adds the specified (signed) amount of time to the given
  494. * time field, based on the calendar's rules. For example, to subtract 5 days from
  495. * the current time of the calendar, call add(Calendar::DATE, -5). When adding on
  496. * the month or Calendar::MONTH field, other fields like date might conflict and
  497. * need to be changed. For instance, adding 1 month on the date 01/31/96 will result
  498. * in 02/29/96.
  499. * Adding a positive value always means moving forward in time, so for the Gregorian calendar,
  500. * starting with 100 BC and adding +1 to year results in 99 BC (even though this actually reduces
  501. * the numeric value of the field itself).
  502. *
  503. * @param field Specifies which date field to modify.
  504. * @param amount The amount of time to be added to the field, in the natural unit
  505. * for that field (e.g., days for the day fields, hours for the hour
  506. * field.)
  507. * @param status Output param set to success/failure code on exit. If any value
  508. * previously set in the time field is invalid or restricted by
  509. * leniency, this will be set to an error status.
  510. * @deprecated ICU 2.6. use add(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
  511. */
  512. virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
  513. #endif // U_FORCE_HIDE_DEPRECATED_API
  514. /**
  515. * UDate Arithmetic function. Adds the specified (signed) amount of time to the given
  516. * time field, based on the calendar's rules. For example, to subtract 5 days from
  517. * the current time of the calendar, call add(Calendar::DATE, -5). When adding on
  518. * the month or Calendar::MONTH field, other fields like date might conflict and
  519. * need to be changed. For instance, adding 1 month on the date 01/31/96 will result
  520. * in 02/29/96.
  521. * Adding a positive value always means moving forward in time, so for the Gregorian calendar,
  522. * starting with 100 BC and adding +1 to year results in 99 BC (even though this actually reduces
  523. * the numeric value of the field itself).
  524. *
  525. * @param field Specifies which date field to modify.
  526. * @param amount The amount of time to be added to the field, in the natural unit
  527. * for that field (e.g., days for the day fields, hours for the hour
  528. * field.)
  529. * @param status Output param set to success/failure code on exit. If any value
  530. * previously set in the time field is invalid or restricted by
  531. * leniency, this will be set to an error status.
  532. * @stable ICU 2.6.
  533. */
  534. virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
  535. #ifndef U_HIDE_DEPRECATED_API
  536. /**
  537. * Time Field Rolling function. Rolls (up/down) a single unit of time on the given
  538. * time field. For example, to roll the current date up by one day, call
  539. * roll(Calendar::DATE, true). When rolling on the year or Calendar::YEAR field, it
  540. * will roll the year value in the range between getMinimum(Calendar::YEAR) and the
  541. * value returned by getMaximum(Calendar::YEAR). When rolling on the month or
  542. * Calendar::MONTH field, other fields like date might conflict and, need to be
  543. * changed. For instance, rolling the month up on the date 01/31/96 will result in
  544. * 02/29/96. Rolling up always means rolling forward in time (unless the limit of the
  545. * field is reached, in which case it may pin or wrap), so for Gregorian calendar,
  546. * starting with 100 BC and rolling the year up results in 99 BC.
  547. * When eras have a definite beginning and end (as in the Chinese calendar, or as in
  548. * most eras in the Japanese calendar) then rolling the year past either limit of the
  549. * era will cause the year to wrap around. When eras only have a limit at one end,
  550. * then attempting to roll the year past that limit will result in pinning the year
  551. * at that limit. Note that for most calendars in which era 0 years move forward in
  552. * time (such as Buddhist, Hebrew, or Islamic), it is possible for add or roll to
  553. * result in negative years for era 0 (that is the only way to represent years before
  554. * the calendar epoch).
  555. * When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the
  556. * hour value in the range between 0 and 23, which is zero-based.
  557. * <P>
  558. * NOTE: Do not use this method -- use roll(EDateFields, int, UErrorCode&) instead.
  559. *
  560. * @param field The time field.
  561. * @param up Indicates if the value of the specified time field is to be rolled
  562. * up or rolled down. Use true if rolling up, false otherwise.
  563. * @param status Output param set to success/failure code on exit. If any value
  564. * previously set in the time field is invalid or restricted by
  565. * leniency, this will be set to an error status.
  566. * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, UBool up, UErrorCode& status) instead.
  567. */
  568. inline void roll(EDateFields field, UBool up, UErrorCode& status);
  569. #endif /* U_HIDE_DEPRECATED_API */
  570. /**
  571. * Time Field Rolling function. Rolls (up/down) a single unit of time on the given
  572. * time field. For example, to roll the current date up by one day, call
  573. * roll(Calendar::DATE, true). When rolling on the year or Calendar::YEAR field, it
  574. * will roll the year value in the range between getMinimum(Calendar::YEAR) and the
  575. * value returned by getMaximum(Calendar::YEAR). When rolling on the month or
  576. * Calendar::MONTH field, other fields like date might conflict and, need to be
  577. * changed. For instance, rolling the month up on the date 01/31/96 will result in
  578. * 02/29/96. Rolling up always means rolling forward in time (unless the limit of the
  579. * field is reached, in which case it may pin or wrap), so for Gregorian calendar,
  580. * starting with 100 BC and rolling the year up results in 99 BC.
  581. * When eras have a definite beginning and end (as in the Chinese calendar, or as in
  582. * most eras in the Japanese calendar) then rolling the year past either limit of the
  583. * era will cause the year to wrap around. When eras only have a limit at one end,
  584. * then attempting to roll the year past that limit will result in pinning the year
  585. * at that limit. Note that for most calendars in which era 0 years move forward in
  586. * time (such as Buddhist, Hebrew, or Islamic), it is possible for add or roll to
  587. * result in negative years for era 0 (that is the only way to represent years before
  588. * the calendar epoch).
  589. * When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the
  590. * hour value in the range between 0 and 23, which is zero-based.
  591. * <P>
  592. * NOTE: Do not use this method -- use roll(UCalendarDateFields, int, UErrorCode&) instead.
  593. *
  594. * @param field The time field.
  595. * @param up Indicates if the value of the specified time field is to be rolled
  596. * up or rolled down. Use true if rolling up, false otherwise.
  597. * @param status Output param set to success/failure code on exit. If any value
  598. * previously set in the time field is invalid or restricted by
  599. * leniency, this will be set to an error status.
  600. * @stable ICU 2.6.
  601. */
  602. inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status);
  603. #ifndef U_FORCE_HIDE_DEPRECATED_API
  604. /**
  605. * Time Field Rolling function. Rolls by the given amount on the given
  606. * time field. For example, to roll the current date up by one day, call
  607. * roll(Calendar::DATE, +1, status). When rolling on the month or
  608. * Calendar::MONTH field, other fields like date might conflict and, need to be
  609. * changed. For instance, rolling the month up on the date 01/31/96 will result in
  610. * 02/29/96. Rolling by a positive value always means rolling forward in time (unless
  611. * the limit of the field is reached, in which case it may pin or wrap), so for
  612. * Gregorian calendar, starting with 100 BC and rolling the year by + 1 results in 99 BC.
  613. * When eras have a definite beginning and end (as in the Chinese calendar, or as in
  614. * most eras in the Japanese calendar) then rolling the year past either limit of the
  615. * era will cause the year to wrap around. When eras only have a limit at one end,
  616. * then attempting to roll the year past that limit will result in pinning the year
  617. * at that limit. Note that for most calendars in which era 0 years move forward in
  618. * time (such as Buddhist, Hebrew, or Islamic), it is possible for add or roll to
  619. * result in negative years for era 0 (that is the only way to represent years before
  620. * the calendar epoch).
  621. * When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the
  622. * hour value in the range between 0 and 23, which is zero-based.
  623. * <P>
  624. * The only difference between roll() and add() is that roll() does not change
  625. * the value of more significant fields when it reaches the minimum or maximum
  626. * of its range, whereas add() does.
  627. *
  628. * @param field The time field.
  629. * @param amount Indicates amount to roll.
  630. * @param status Output param set to success/failure code on exit. If any value
  631. * previously set in the time field is invalid, this will be set to
  632. * an error status.
  633. * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
  634. */
  635. virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
  636. #endif // U_FORCE_HIDE_DEPRECATED_API
  637. /**
  638. * Time Field Rolling function. Rolls by the given amount on the given
  639. * time field. For example, to roll the current date up by one day, call
  640. * roll(Calendar::DATE, +1, status). When rolling on the month or
  641. * Calendar::MONTH field, other fields like date might conflict and, need to be
  642. * changed. For instance, rolling the month up on the date 01/31/96 will result in
  643. * 02/29/96. Rolling by a positive value always means rolling forward in time (unless
  644. * the limit of the field is reached, in which case it may pin or wrap), so for
  645. * Gregorian calendar, starting with 100 BC and rolling the year by + 1 results in 99 BC.
  646. * When eras have a definite beginning and end (as in the Chinese calendar, or as in
  647. * most eras in the Japanese calendar) then rolling the year past either limit of the
  648. * era will cause the year to wrap around. When eras only have a limit at one end,
  649. * then attempting to roll the year past that limit will result in pinning the year
  650. * at that limit. Note that for most calendars in which era 0 years move forward in
  651. * time (such as Buddhist, Hebrew, or Islamic), it is possible for add or roll to
  652. * result in negative years for era 0 (that is the only way to represent years before
  653. * the calendar epoch).
  654. * When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the
  655. * hour value in the range between 0 and 23, which is zero-based.
  656. * <P>
  657. * The only difference between roll() and add() is that roll() does not change
  658. * the value of more significant fields when it reaches the minimum or maximum
  659. * of its range, whereas add() does.
  660. *
  661. * @param field The time field.
  662. * @param amount Indicates amount to roll.
  663. * @param status Output param set to success/failure code on exit. If any value
  664. * previously set in the time field is invalid, this will be set to
  665. * an error status.
  666. * @stable ICU 2.6.
  667. */
  668. virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
  669. #ifndef U_FORCE_HIDE_DEPRECATED_API
  670. /**
  671. * Return the difference between the given time and the time this
  672. * calendar object is set to. If this calendar is set
  673. * <em>before</em> the given time, the returned value will be
  674. * positive. If this calendar is set <em>after</em> the given
  675. * time, the returned value will be negative. The
  676. * <code>field</code> parameter specifies the units of the return
  677. * value. For example, if <code>fieldDifference(when,
  678. * Calendar::MONTH)</code> returns 3, then this calendar is set to
  679. * 3 months before <code>when</code>, and possibly some addition
  680. * time less than one month.
  681. *
  682. * <p>As a side effect of this call, this calendar is advanced
  683. * toward <code>when</code> by the given amount. That is, calling
  684. * this method has the side effect of calling <code>add(field,
  685. * n)</code>, where <code>n</code> is the return value.
  686. *
  687. * <p>Usage: To use this method, call it first with the largest
  688. * field of interest, then with progressively smaller fields. For
  689. * example:
  690. *
  691. * <pre>
  692. * int y = cal->fieldDifference(when, Calendar::YEAR, err);
  693. * int m = cal->fieldDifference(when, Calendar::MONTH, err);
  694. * int d = cal->fieldDifference(when, Calendar::DATE, err);</pre>
  695. *
  696. * computes the difference between <code>cal</code> and
  697. * <code>when</code> in years, months, and days.
  698. *
  699. * <p>Note: <code>fieldDifference()</code> is
  700. * <em>asymmetrical</em>. That is, in the following code:
  701. *
  702. * <pre>
  703. * cal->setTime(date1, err);
  704. * int m1 = cal->fieldDifference(date2, Calendar::MONTH, err);
  705. * int d1 = cal->fieldDifference(date2, Calendar::DATE, err);
  706. * cal->setTime(date2, err);
  707. * int m2 = cal->fieldDifference(date1, Calendar::MONTH, err);
  708. * int d2 = cal->fieldDifference(date1, Calendar::DATE, err);</pre>
  709. *
  710. * one might expect that <code>m1 == -m2 && d1 == -d2</code>.
  711. * However, this is not generally the case, because of
  712. * irregularities in the underlying calendar system (e.g., the
  713. * Gregorian calendar has a varying number of days per month).
  714. *
  715. * @param when the date to compare this calendar's time to
  716. * @param field the field in which to compute the result
  717. * @param status Output param set to success/failure code on exit. If any value
  718. * previously set in the time field is invalid, this will be set to
  719. * an error status.
  720. * @return the difference, either positive or negative, between
  721. * this calendar's time and <code>when</code>, in terms of
  722. * <code>field</code>.
  723. * @deprecated ICU 2.6. Use fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status).
  724. */
  725. virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status);
  726. #endif // U_FORCE_HIDE_DEPRECATED_API
  727. /**
  728. * Return the difference between the given time and the time this
  729. * calendar object is set to. If this calendar is set
  730. * <em>before</em> the given time, the returned value will be
  731. * positive. If this calendar is set <em>after</em> the given
  732. * time, the returned value will be negative. The
  733. * <code>field</code> parameter specifies the units of the return
  734. * value. For example, if <code>fieldDifference(when,
  735. * Calendar::MONTH)</code> returns 3, then this calendar is set to
  736. * 3 months before <code>when</code>, and possibly some addition
  737. * time less than one month.
  738. *
  739. * <p>As a side effect of this call, this calendar is advanced
  740. * toward <code>when</code> by the given amount. That is, calling
  741. * this method has the side effect of calling <code>add(field,
  742. * n)</code>, where <code>n</code> is the return value.
  743. *
  744. * <p>Usage: To use this method, call it first with the largest
  745. * field of interest, then with progressively smaller fields. For
  746. * example:
  747. *
  748. * <pre>
  749. * int y = cal->fieldDifference(when, Calendar::YEAR, err);
  750. * int m = cal->fieldDifference(when, Calendar::MONTH, err);
  751. * int d = cal->fieldDifference(when, Calendar::DATE, err);</pre>
  752. *
  753. * computes the difference between <code>cal</code> and
  754. * <code>when</code> in years, months, and days.
  755. *
  756. * <p>Note: <code>fieldDifference()</code> is
  757. * <em>asymmetrical</em>. That is, in the following code:
  758. *
  759. * <pre>
  760. * cal->setTime(date1, err);
  761. * int m1 = cal->fieldDifference(date2, Calendar::MONTH, err);
  762. * int d1 = cal->fieldDifference(date2, Calendar::DATE, err);
  763. * cal->setTime(date2, err);
  764. * int m2 = cal->fieldDifference(date1, Calendar::MONTH, err);
  765. * int d2 = cal->fieldDifference(date1, Calendar::DATE, err);</pre>
  766. *
  767. * one might expect that <code>m1 == -m2 && d1 == -d2</code>.
  768. * However, this is not generally the case, because of
  769. * irregularities in the underlying calendar system (e.g., the
  770. * Gregorian calendar has a varying number of days per month).
  771. *
  772. * @param when the date to compare this calendar's time to
  773. * @param field the field in which to compute the result
  774. * @param status Output param set to success/failure code on exit. If any value
  775. * previously set in the time field is invalid, this will be set to
  776. * an error status.
  777. * @return the difference, either positive or negative, between
  778. * this calendar's time and <code>when</code>, in terms of
  779. * <code>field</code>.
  780. * @stable ICU 2.6.
  781. */
  782. virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status);
  783. /**
  784. * Sets the calendar's time zone to be the one passed in. The Calendar takes ownership
  785. * of the TimeZone; the caller is no longer responsible for deleting it. If the
  786. * given time zone is nullptr, this function has no effect.
  787. *
  788. * @param value The given time zone.
  789. * @stable ICU 2.0
  790. */
  791. void adoptTimeZone(TimeZone* value);
  792. /**
  793. * Sets the calendar's time zone to be the same as the one passed in. The TimeZone
  794. * passed in is _not_ adopted; the client is still responsible for deleting it.
  795. *
  796. * @param zone The given time zone.
  797. * @stable ICU 2.0
  798. */
  799. void setTimeZone(const TimeZone& zone);
  800. /**
  801. * Returns a reference to the time zone owned by this calendar. The returned reference
  802. * is only valid until clients make another call to adoptTimeZone or setTimeZone,
  803. * or this Calendar is destroyed.
  804. *
  805. * @return The time zone object associated with this calendar.
  806. * @stable ICU 2.0
  807. */
  808. const TimeZone& getTimeZone() const;
  809. /**
  810. * Returns the time zone owned by this calendar. The caller owns the returned object
  811. * and must delete it when done. After this call, the new time zone associated
  812. * with this Calendar is the default TimeZone as returned by TimeZone::createDefault().
  813. *
  814. * @return The time zone object which was associated with this calendar.
  815. * @stable ICU 2.0
  816. */
  817. TimeZone* orphanTimeZone();
  818. /**
  819. * Queries if the current date for this Calendar is in Daylight Savings Time.
  820. *
  821. * @param status Fill-in parameter which receives the status of this operation.
  822. * @return True if the current date for this Calendar is in Daylight Savings Time,
  823. * false, otherwise.
  824. * @stable ICU 2.0
  825. */
  826. virtual UBool inDaylightTime(UErrorCode& status) const;
  827. /**
  828. * Specifies whether or not date/time interpretation is to be lenient. With lenient
  829. * interpretation, a date such as "February 942, 1996" will be treated as being
  830. * equivalent to the 941st day after February 1, 1996. With strict interpretation,
  831. * such dates will cause an error when computing time from the time field values
  832. * representing the dates.
  833. *
  834. * @param lenient True specifies date/time interpretation to be lenient.
  835. *
  836. * @see DateFormat#setLenient
  837. * @stable ICU 2.0
  838. */
  839. void setLenient(UBool lenient);
  840. /**
  841. * Tells whether date/time interpretation is to be lenient.
  842. *
  843. * @return True tells that date/time interpretation is to be lenient.
  844. * @stable ICU 2.0
  845. */
  846. UBool isLenient() const;
  847. /**
  848. * Sets the behavior for handling wall time repeating multiple times
  849. * at negative time zone offset transitions. For example, 1:30 AM on
  850. * November 6, 2011 in US Eastern time (America/New_York) occurs twice;
  851. * 1:30 AM EDT, then 1:30 AM EST one hour later. When <code>UCAL_WALLTIME_FIRST</code>
  852. * is used, the wall time 1:30AM in this example will be interpreted as 1:30 AM EDT
  853. * (first occurrence). When <code>UCAL_WALLTIME_LAST</code> is used, it will be
  854. * interpreted as 1:30 AM EST (last occurrence). The default value is
  855. * <code>UCAL_WALLTIME_LAST</code>.
  856. * <p>
  857. * <b>Note:</b>When <code>UCAL_WALLTIME_NEXT_VALID</code> is not a valid
  858. * option for this. When the argument is neither <code>UCAL_WALLTIME_FIRST</code>
  859. * nor <code>UCAL_WALLTIME_LAST</code>, this method has no effect and will keep
  860. * the current setting.
  861. *
  862. * @param option the behavior for handling repeating wall time, either
  863. * <code>UCAL_WALLTIME_FIRST</code> or <code>UCAL_WALLTIME_LAST</code>.
  864. * @see #getRepeatedWallTimeOption
  865. * @stable ICU 49
  866. */
  867. void setRepeatedWallTimeOption(UCalendarWallTimeOption option);
  868. /**
  869. * Gets the behavior for handling wall time repeating multiple times
  870. * at negative time zone offset transitions.
  871. *
  872. * @return the behavior for handling repeating wall time, either
  873. * <code>UCAL_WALLTIME_FIRST</code> or <code>UCAL_WALLTIME_LAST</code>.
  874. * @see #setRepeatedWallTimeOption
  875. * @stable ICU 49
  876. */
  877. UCalendarWallTimeOption getRepeatedWallTimeOption() const;
  878. /**
  879. * Sets the behavior for handling skipped wall time at positive time zone offset
  880. * transitions. For example, 2:30 AM on March 13, 2011 in US Eastern time (America/New_York)
  881. * does not exist because the wall time jump from 1:59 AM EST to 3:00 AM EDT. When
  882. * <code>UCAL_WALLTIME_FIRST</code> is used, 2:30 AM is interpreted as 30 minutes before 3:00 AM
  883. * EDT, therefore, it will be resolved as 1:30 AM EST. When <code>UCAL_WALLTIME_LAST</code>
  884. * is used, 2:30 AM is interpreted as 31 minutes after 1:59 AM EST, therefore, it will be
  885. * resolved as 3:30 AM EDT. When <code>UCAL_WALLTIME_NEXT_VALID</code> is used, 2:30 AM will
  886. * be resolved as next valid wall time, that is 3:00 AM EDT. The default value is
  887. * <code>UCAL_WALLTIME_LAST</code>.
  888. * <p>
  889. * <b>Note:</b>This option is effective only when this calendar is lenient.
  890. * When the calendar is strict, such non-existing wall time will cause an error.
  891. *
  892. * @param option the behavior for handling skipped wall time at positive time zone
  893. * offset transitions, one of <code>UCAL_WALLTIME_FIRST</code>, <code>UCAL_WALLTIME_LAST</code> and
  894. * <code>UCAL_WALLTIME_NEXT_VALID</code>.
  895. * @see #getSkippedWallTimeOption
  896. *
  897. * @stable ICU 49
  898. */
  899. void setSkippedWallTimeOption(UCalendarWallTimeOption option);
  900. /**
  901. * Gets the behavior for handling skipped wall time at positive time zone offset
  902. * transitions.
  903. *
  904. * @return the behavior for handling skipped wall time, one of
  905. * <code>UCAL_WALLTIME_FIRST</code>, <code>UCAL_WALLTIME_LAST</code>
  906. * and <code>UCAL_WALLTIME_NEXT_VALID</code>.
  907. * @see #setSkippedWallTimeOption
  908. * @stable ICU 49
  909. */
  910. UCalendarWallTimeOption getSkippedWallTimeOption() const;
  911. /**
  912. * Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
  913. *
  914. * @param value The given first day of the week.
  915. * @stable ICU 2.6.
  916. */
  917. void setFirstDayOfWeek(UCalendarDaysOfWeek value);
  918. #ifndef U_HIDE_DEPRECATED_API
  919. /**
  920. * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
  921. *
  922. * @return The first day of the week.
  923. * @deprecated ICU 2.6 use the overload with error code
  924. */
  925. EDaysOfWeek getFirstDayOfWeek() const;
  926. #endif /* U_HIDE_DEPRECATED_API */
  927. /**
  928. * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
  929. *
  930. * @param status error code
  931. * @return The first day of the week.
  932. * @stable ICU 2.6
  933. */
  934. UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const;
  935. /**
  936. * Sets what the minimal days required in the first week of the year are; For
  937. * example, if the first week is defined as one that contains the first day of the
  938. * first month of a year, call the method with value 1. If it must be a full week,
  939. * use value 7.
  940. *
  941. * @param value The given minimal days required in the first week of the year.
  942. * @stable ICU 2.0
  943. */
  944. void setMinimalDaysInFirstWeek(uint8_t value);
  945. /**
  946. * Gets what the minimal days required in the first week of the year are; e.g., if
  947. * the first week is defined as one that contains the first day of the first month
  948. * of a year, getMinimalDaysInFirstWeek returns 1. If the minimal days required must
  949. * be a full week, getMinimalDaysInFirstWeek returns 7.
  950. *
  951. * @return The minimal days required in the first week of the year.
  952. * @stable ICU 2.0
  953. */
  954. uint8_t getMinimalDaysInFirstWeek() const;
  955. #ifndef U_FORCE_HIDE_DEPRECATED_API
  956. /**
  957. * Gets the minimum value for the given time field. e.g., for Gregorian
  958. * DAY_OF_MONTH, 1.
  959. *
  960. * @param field The given time field.
  961. * @return The minimum value for the given time field.
  962. * @deprecated ICU 2.6. Use getMinimum(UCalendarDateFields field) instead.
  963. */
  964. virtual int32_t getMinimum(EDateFields field) const;
  965. #endif // U_FORCE_HIDE_DEPRECATED_API
  966. /**
  967. * Gets the minimum value for the given time field. e.g., for Gregorian
  968. * DAY_OF_MONTH, 1.
  969. *
  970. * @param field The given time field.
  971. * @return The minimum value for the given time field.
  972. * @stable ICU 2.6.
  973. */
  974. virtual int32_t getMinimum(UCalendarDateFields field) const;
  975. #ifndef U_FORCE_HIDE_DEPRECATED_API
  976. /**
  977. * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
  978. * 31.
  979. *
  980. * @param field The given time field.
  981. * @return The maximum value for the given time field.
  982. * @deprecated ICU 2.6. Use getMaximum(UCalendarDateFields field) instead.
  983. */
  984. virtual int32_t getMaximum(EDateFields field) const;
  985. #endif // U_FORCE_HIDE_DEPRECATED_API
  986. /**
  987. * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
  988. * 31.
  989. *
  990. * @param field The given time field.
  991. * @return The maximum value for the given time field.
  992. * @stable ICU 2.6.
  993. */
  994. virtual int32_t getMaximum(UCalendarDateFields field) const;
  995. #ifndef U_FORCE_HIDE_DEPRECATED_API
  996. /**
  997. * Gets the highest minimum value for the given field if varies. Otherwise same as
  998. * getMinimum(). For Gregorian, no difference.
  999. *
  1000. * @param field The given time field.
  1001. * @return The highest minimum value for the given time field.
  1002. * @deprecated ICU 2.6. Use getGreatestMinimum(UCalendarDateFields field) instead.
  1003. */
  1004. virtual int32_t getGreatestMinimum(EDateFields field) const;
  1005. #endif // U_FORCE_HIDE_DEPRECATED_API
  1006. /**
  1007. * Gets the highest minimum value for the given field if varies. Otherwise same as
  1008. * getMinimum(). For Gregorian, no difference.
  1009. *
  1010. * @param field The given time field.
  1011. * @return The highest minimum value for the given time field.
  1012. * @stable ICU 2.6.
  1013. */
  1014. virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
  1015. #ifndef U_FORCE_HIDE_DEPRECATED_API
  1016. /**
  1017. * Gets the lowest maximum value for the given field if varies. Otherwise same as
  1018. * getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
  1019. *
  1020. * @param field The given time field.
  1021. * @return The lowest maximum value for the given time field.
  1022. * @deprecated ICU 2.6. Use getLeastMaximum(UCalendarDateFields field) instead.
  1023. */
  1024. virtual int32_t getLeastMaximum(EDateFields field) const;
  1025. #endif // U_FORCE_HIDE_DEPRECATED_API
  1026. /**
  1027. * Gets the lowest maximum value for the given field if varies. Otherwise same as
  1028. * getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
  1029. *
  1030. * @param field The given time field.
  1031. * @return The lowest maximum value for the given time field.
  1032. * @stable ICU 2.6.
  1033. */
  1034. virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
  1035. #ifndef U_HIDE_DEPRECATED_API
  1036. /**
  1037. * Return the minimum value that this field could have, given the current date.
  1038. * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
  1039. *
  1040. * The version of this function on Calendar uses an iterative algorithm to determine the
  1041. * actual minimum value for the field. There is almost always a more efficient way to
  1042. * accomplish this (in most cases, you can simply return getMinimum()). GregorianCalendar
  1043. * overrides this function with a more efficient implementation.
  1044. *
  1045. * @param field the field to determine the minimum of
  1046. * @param status Fill-in parameter which receives the status of this operation.
  1047. * @return the minimum of the given field for the current date of this Calendar
  1048. * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field, UErrorCode& status) instead.
  1049. */
  1050. int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
  1051. #endif /* U_HIDE_DEPRECATED_API */
  1052. /**
  1053. * Return the minimum value that this field could have, given the current date.
  1054. * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
  1055. *
  1056. * The version of this function on Calendar uses an iterative algorithm to determine the
  1057. * actual minimum value for the field. There is almost always a more efficient way to
  1058. * accomplish this (in most cases, you can simply return getMinimum()). GregorianCalendar
  1059. * overrides this function with a more efficient implementation.
  1060. *
  1061. * @param field the field to determine the minimum of
  1062. * @param status Fill-in parameter which receives the status of this operation.
  1063. * @return the minimum of the given field for the current date of this Calendar
  1064. * @stable ICU 2.6.
  1065. */
  1066. virtual int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const;
  1067. /**
  1068. * Return the maximum value that this field could have, given the current date.
  1069. * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
  1070. * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
  1071. * for some years the actual maximum for MONTH is 12, and for others 13.
  1072. *
  1073. * The version of this function on Calendar uses an iterative algorithm to determine the
  1074. * actual maximum value for the field. There is almost always a more efficient way to
  1075. * accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar
  1076. * overrides this function with a more efficient implementation.
  1077. *
  1078. * @param field the field to determine the maximum of
  1079. * @param status Fill-in parameter which receives the status of this operation.
  1080. * @return the maximum of the given field for the current date of this Calendar
  1081. * @stable ICU 2.6.
  1082. */
  1083. virtual int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
  1084. /**
  1085. * Gets the value for a given time field. Recalculate the current time field values
  1086. * if the time value has been changed by a call to setTime(). Return zero for unset
  1087. * fields if any fields have been explicitly set by a call to set(). To force a
  1088. * recomputation of all fields regardless of the previous state, call complete().
  1089. * This method is semantically const, but may alter the object in memory.
  1090. *
  1091. * @param field The given time field.
  1092. * @param status Fill-in parameter which receives the status of the operation.
  1093. * @return The value for the given time field, or zero if the field is unset,
  1094. * and set() has been called for any other field.
  1095. * @stable ICU 2.6.
  1096. */
  1097. int32_t get(UCalendarDateFields field, UErrorCode& status) const;
  1098. /**
  1099. * Determines if the given time field has a value set. This can affect in the
  1100. * resolving of time in Calendar. Unset fields have a value of zero, by definition.
  1101. *
  1102. * @param field The given time field.
  1103. * @return True if the given time field has a value set; false otherwise.
  1104. * @stable ICU 2.6.
  1105. */
  1106. UBool isSet(UCalendarDateFields field) const;
  1107. /**
  1108. * Sets the given time field with the given value.
  1109. *
  1110. * @param field The given time field.
  1111. * @param value The value to be set for the given time field.
  1112. * @stable ICU 2.6.
  1113. */
  1114. void set(UCalendarDateFields field, int32_t value);
  1115. /**
  1116. * Sets the values for the fields YEAR, MONTH, and DATE. Other field values are
  1117. * retained; call clear() first if this is not desired.
  1118. *
  1119. * @param year The value used to set the YEAR time field.
  1120. * @param month The value used to set the MONTH time field. Month value is 0-based.
  1121. * e.g., 0 for January.
  1122. * @param date The value used to set the DATE time field.
  1123. * @stable ICU 2.0
  1124. */
  1125. void set(int32_t year, int32_t month, int32_t date);
  1126. /**
  1127. * Sets the values for the fields YEAR, MONTH, DATE, HOUR_OF_DAY, and MINUTE. Other
  1128. * field values are retained; call clear() first if this is not desired.
  1129. *
  1130. * @param year The value used to set the YEAR time field.
  1131. * @param month The value used to set the MONTH time field. Month value is
  1132. * 0-based. E.g., 0 for January.
  1133. * @param date The value used to set the DATE time field.
  1134. * @param hour The value used to set the HOUR_OF_DAY time field.
  1135. * @param minute The value used to set the MINUTE time field.
  1136. * @stable ICU 2.0
  1137. */
  1138. void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute);
  1139. /**
  1140. * Sets the values for the fields YEAR, MONTH, DATE, HOUR_OF_DAY, MINUTE, and SECOND.
  1141. * Other field values are retained; call clear() first if this is not desired.
  1142. *
  1143. * @param year The value used to set the YEAR time field.
  1144. * @param month The value used to set the MONTH time field. Month value is
  1145. * 0-based. E.g., 0 for January.
  1146. * @param date The value used to set the DATE time field.
  1147. * @param hour The value used to set the HOUR_OF_DAY time field.
  1148. * @param minute The value used to set the MINUTE time field.
  1149. * @param second The value used to set the SECOND time field.
  1150. * @stable ICU 2.0
  1151. */
  1152. void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second);
  1153. /**
  1154. * Clears the values of all the time fields, making them both unset and assigning
  1155. * them a value of zero. The field values will be determined during the next
  1156. * resolving of time into time fields.
  1157. * @stable ICU 2.0
  1158. */
  1159. void clear();
  1160. /**
  1161. * Clears the value in the given time field, both making it unset and assigning it a
  1162. * value of zero. This field value will be determined during the next resolving of
  1163. * time into time fields. Clearing UCAL_ORDINAL_MONTH or UCAL_MONTH will
  1164. * clear both fields.
  1165. *
  1166. * @param field The time field to be cleared.
  1167. * @stable ICU 2.6.
  1168. */
  1169. void clear(UCalendarDateFields field);
  1170. /**
  1171. * Returns a unique class ID POLYMORPHICALLY. Pure virtual method. This method is to
  1172. * implement a simple version of RTTI, since not all C++ compilers support genuine
  1173. * RTTI. Polymorphic operator==() and clone() methods call this method.
  1174. * <P>
  1175. * Concrete subclasses of Calendar must implement getDynamicClassID() and also a
  1176. * static method and data member:
  1177. *
  1178. * static UClassID getStaticClassID() { return (UClassID)&amp;fgClassID; }
  1179. * static char fgClassID;
  1180. *
  1181. * @return The class ID for this object. All objects of a given class have the
  1182. * same class ID. Objects of other classes have different class IDs.
  1183. * @stable ICU 2.0
  1184. */
  1185. virtual UClassID getDynamicClassID() const override = 0;
  1186. /**
  1187. * Returns the calendar type name string for this Calendar object.
  1188. * The returned string is the legacy ICU calendar attribute value,
  1189. * for example, "gregorian" or "japanese".
  1190. *
  1191. * See type="old type name" for the calendar attribute of locale IDs
  1192. * at http://www.unicode.org/reports/tr35/#Key_Type_Definitions
  1193. *
  1194. * Sample code for getting the LDML/BCP 47 calendar key value:
  1195. * \code
  1196. * const char *calType = cal->getType();
  1197. * if (0 == strcmp(calType, "unknown")) {
  1198. * // deal with unknown calendar type
  1199. * } else {
  1200. * string localeID("root@calendar=");
  1201. * localeID.append(calType);
  1202. * char langTag[100];
  1203. * UErrorCode errorCode = U_ZERO_ERROR;
  1204. * int32_t length = uloc_toLanguageTag(localeID.c_str(), langTag, (int32_t)sizeof(langTag), true, &errorCode);
  1205. * if (U_FAILURE(errorCode)) {
  1206. * // deal with errors & overflow
  1207. * }
  1208. * string lang(langTag, length);
  1209. * size_t caPos = lang.find("-ca-");
  1210. * lang.erase(0, caPos + 4);
  1211. * // lang now contains the LDML calendar type
  1212. * }
  1213. * \endcode
  1214. *
  1215. * @return legacy calendar type name string
  1216. * @stable ICU 49
  1217. */
  1218. virtual const char * getType() const = 0;
  1219. /**
  1220. * Returns whether the given day of the week is a weekday, a weekend day,
  1221. * or a day that transitions from one to the other, for the locale and
  1222. * calendar system associated with this Calendar (the locale's region is
  1223. * often the most determinant factor). If a transition occurs at midnight,
  1224. * then the days before and after the transition will have the
  1225. * type UCAL_WEEKDAY or UCAL_WEEKEND. If a transition occurs at a time
  1226. * other than midnight, then the day of the transition will have
  1227. * the type UCAL_WEEKEND_ONSET or UCAL_WEEKEND_CEASE. In this case, the
  1228. * method getWeekendTransition() will return the point of
  1229. * transition.
  1230. * @param dayOfWeek The day of the week whose type is desired (UCAL_SUNDAY..UCAL_SATURDAY).
  1231. * @param status The error code for the operation.
  1232. * @return The UCalendarWeekdayType for the day of the week.
  1233. * @stable ICU 4.4
  1234. */
  1235. virtual UCalendarWeekdayType getDayOfWeekType(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
  1236. /**
  1237. * Returns the time during the day at which the weekend begins or ends in
  1238. * this calendar system. If getDayOfWeekType() returns UCAL_WEEKEND_ONSET
  1239. * for the specified dayOfWeek, return the time at which the weekend begins.
  1240. * If getDayOfWeekType() returns UCAL_WEEKEND_CEASE for the specified dayOfWeek,
  1241. * return the time at which the weekend ends. If getDayOfWeekType() returns
  1242. * some other UCalendarWeekdayType for the specified dayOfWeek, is it an error condition
  1243. * (U_ILLEGAL_ARGUMENT_ERROR).
  1244. * @param dayOfWeek The day of the week for which the weekend transition time is
  1245. * desired (UCAL_SUNDAY..UCAL_SATURDAY).
  1246. * @param status The error code for the operation.
  1247. * @return The milliseconds after midnight at which the weekend begins or ends.
  1248. * @stable ICU 4.4
  1249. */
  1250. virtual int32_t getWeekendTransition(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
  1251. /**
  1252. * Returns true if the given UDate is in the weekend in
  1253. * this calendar system.
  1254. * @param date The UDate in question.
  1255. * @param status The error code for the operation.
  1256. * @return true if the given UDate is in the weekend in
  1257. * this calendar system, false otherwise.
  1258. * @stable ICU 4.4
  1259. */
  1260. virtual UBool isWeekend(UDate date, UErrorCode &status) const;
  1261. /**
  1262. * Returns true if this Calendar's current date-time is in the weekend in
  1263. * this calendar system.
  1264. * @return true if this Calendar's current date-time is in the weekend in
  1265. * this calendar system, false otherwise.
  1266. * @stable ICU 4.4
  1267. */
  1268. virtual UBool isWeekend() const;
  1269. /**
  1270. * Returns true if the date is in a leap year. Recalculate the current time
  1271. * field values if the time value has been changed by a call to * setTime().
  1272. * This method is semantically const, but may alter the object in memory.
  1273. * A "leap year" is a year that contains more days than other years (for
  1274. * solar or lunar calendars) or more months than other years (for lunisolar
  1275. * calendars like Hebrew or Chinese), as defined in the ECMAScript Temporal
  1276. * proposal.
  1277. *
  1278. * @param status ICU Error Code
  1279. * @return True if the date in the fields is in a Temporal proposal
  1280. * defined leap year. False otherwise.
  1281. * @stable ICU 73
  1282. */
  1283. virtual bool inTemporalLeapYear(UErrorCode& status) const;
  1284. /**
  1285. * Gets The Temporal monthCode value corresponding to the month for the date.
  1286. * The value is a string identifier that starts with the literal grapheme
  1287. * "M" followed by two graphemes representing the zero-padded month number
  1288. * of the current month in a normal (non-leap) year and suffixed by an
  1289. * optional literal grapheme "L" if this is a leap month in a lunisolar
  1290. * calendar. The 25 possible values are "M01" .. "M13" and "M01L" .. "M12L".
  1291. * For the Hebrew calendar, the values are "M01" .. "M12" for non-leap year, and
  1292. * "M01" .. "M05", "M05L", "M06" .. "M12" for leap year.
  1293. * For the Chinese calendar, the values are "M01" .. "M12" for non-leap year and
  1294. * in leap year with another monthCode in "M01L" .. "M12L".
  1295. * For Coptic and Ethiopian calendar, the Temporal monthCode values for any
  1296. * years are "M01" to "M13".
  1297. *
  1298. * @param status ICU Error Code
  1299. * @return One of 25 possible strings in {"M01".."M13", "M01L".."M12L"}.
  1300. * @stable ICU 73
  1301. */
  1302. virtual const char* getTemporalMonthCode(UErrorCode& status) const;
  1303. /**
  1304. * Sets The Temporal monthCode which is a string identifier that starts
  1305. * with the literal grapheme "M" followed by two graphemes representing
  1306. * the zero-padded month number of the current month in a normal
  1307. * (non-leap) year and suffixed by an optional literal grapheme "L" if this
  1308. * is a leap month in a lunisolar calendar. The 25 possible values are
  1309. * "M01" .. "M13" and "M01L" .. "M12L". For Hebrew calendar, the values are
  1310. * "M01" .. "M12" for non-leap years, and "M01" .. "M05", "M05L", "M06"
  1311. * .. "M12" for leap year.
  1312. * For the Chinese calendar, the values are "M01" .. "M12" for non-leap year and
  1313. * in leap year with another monthCode in "M01L" .. "M12L".
  1314. * For Coptic and Ethiopian calendar, the Temporal monthCode values for any
  1315. * years are "M01" to "M13".
  1316. *
  1317. * @param temporalMonth The value to be set for temporal monthCode.
  1318. * @param status ICU Error Code
  1319. *
  1320. * @stable ICU 73
  1321. */
  1322. virtual void setTemporalMonthCode(const char* temporalMonth, UErrorCode& status);
  1323. protected:
  1324. /**
  1325. * Constructs a Calendar with the default time zone as returned by
  1326. * TimeZone::createInstance(), and the default locale.
  1327. *
  1328. * @param success Indicates the status of Calendar object construction. Returns
  1329. * U_ZERO_ERROR if constructed successfully.
  1330. * @stable ICU 2.0
  1331. */
  1332. Calendar(UErrorCode& success);
  1333. /**
  1334. * Copy constructor
  1335. *
  1336. * @param source Calendar object to be copied from
  1337. * @stable ICU 2.0
  1338. */
  1339. Calendar(const Calendar& source);
  1340. /**
  1341. * Default assignment operator
  1342. *
  1343. * @param right Calendar object to be copied
  1344. * @stable ICU 2.0
  1345. */
  1346. Calendar& operator=(const Calendar& right);
  1347. /**
  1348. * Constructs a Calendar with the given time zone and locale. Clients are no longer
  1349. * responsible for deleting the given time zone object after it's adopted.
  1350. *
  1351. * @param zone The given time zone.
  1352. * @param aLocale The given locale.
  1353. * @param success Indicates the status of Calendar object construction. Returns
  1354. * U_ZERO_ERROR if constructed successfully.
  1355. * @stable ICU 2.0
  1356. */
  1357. Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
  1358. /**
  1359. * Constructs a Calendar with the given time zone and locale.
  1360. *
  1361. * @param zone The given time zone.
  1362. * @param aLocale The given locale.
  1363. * @param success Indicates the status of Calendar object construction. Returns
  1364. * U_ZERO_ERROR if constructed successfully.
  1365. * @stable ICU 2.0
  1366. */
  1367. Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
  1368. /**
  1369. * Converts Calendar's time field values to GMT as milliseconds.
  1370. *
  1371. * @param status Output param set to success/failure code on exit. If any value
  1372. * previously set in the time field is invalid or restricted by
  1373. * leniency, this will be set to an error status.
  1374. * @stable ICU 2.0
  1375. */
  1376. virtual void computeTime(UErrorCode& status);
  1377. /**
  1378. * Converts GMT as milliseconds to time field values. This allows you to sync up the
  1379. * time field values with a new time that is set for the calendar. This method
  1380. * does NOT recompute the time first; to recompute the time, then the fields, use
  1381. * the method complete().
  1382. *
  1383. * @param status Output param set to success/failure code on exit. If any value
  1384. * previously set in the time field is invalid or restricted by
  1385. * leniency, this will be set to an error status.
  1386. * @stable ICU 2.0
  1387. */
  1388. virtual void computeFields(UErrorCode& status);
  1389. /**
  1390. * Gets this Calendar's current time as a long.
  1391. *
  1392. * @param status Output param set to success/failure code on exit. If any value
  1393. * previously set in the time field is invalid or restricted by
  1394. * leniency, this will be set to an error status.
  1395. * @return the current time as UTC milliseconds from the epoch.
  1396. * @stable ICU 2.0
  1397. */
  1398. double getTimeInMillis(UErrorCode& status) const;
  1399. /**
  1400. * Sets this Calendar's current time from the given long value.
  1401. * @param millis the new time in UTC milliseconds from the epoch.
  1402. * @param status Output param set to success/failure code on exit. If any value
  1403. * previously set in the time field is invalid or restricted by
  1404. * leniency, this will be set to an error status.
  1405. * @stable ICU 2.0
  1406. */
  1407. void setTimeInMillis( double millis, UErrorCode& status );
  1408. /**
  1409. * Recomputes the current time from currently set fields, and then fills in any
  1410. * unset fields in the time field list.
  1411. *
  1412. * @param status Output param set to success/failure code on exit. If any value
  1413. * previously set in the time field is invalid or restricted by
  1414. * leniency, this will be set to an error status.
  1415. * @stable ICU 2.0
  1416. */
  1417. void complete(UErrorCode& status);
  1418. #ifndef U_HIDE_DEPRECATED_API
  1419. /**
  1420. * Gets the value for a given time field. Subclasses can use this function to get
  1421. * field values without forcing recomputation of time.
  1422. *
  1423. * @param field The given time field.
  1424. * @return The value for the given time field.
  1425. * @deprecated ICU 2.6. Use internalGet(UCalendarDateFields field) instead.
  1426. */
  1427. inline int32_t internalGet(EDateFields field) const {return fFields[field];}
  1428. #endif /* U_HIDE_DEPRECATED_API */
  1429. #ifndef U_HIDE_INTERNAL_API
  1430. /**
  1431. * Gets the value for a given time field. Subclasses can use this function to get
  1432. * field values without forcing recomputation of time. If the field's stamp is UNSET,
  1433. * the defaultValue is used.
  1434. *
  1435. * @param field The given time field.
  1436. * @param defaultValue a default value used if the field is unset.
  1437. * @return The value for the given time field.
  1438. * @internal
  1439. */
  1440. inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
  1441. /**
  1442. * Gets the value for a given time field. Subclasses can use this function to get
  1443. * field values without forcing recomputation of time.
  1444. *
  1445. * @param field The given time field.
  1446. * @return The value for the given time field.
  1447. * @internal
  1448. */
  1449. inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
  1450. /**
  1451. * The year in this calendar is counting from 1 backward if the era is 0.
  1452. * @return The year in era 0 of this calendar is counting backward from 1.
  1453. * @internal
  1454. */
  1455. virtual bool isEra0CountingBackward() const { return false; }
  1456. #endif /* U_HIDE_INTERNAL_API */
  1457. /**
  1458. * Use this function instead of internalGet(UCAL_MONTH). The implementation
  1459. * check the timestamp of UCAL_MONTH and UCAL_ORDINAL_MONTH and use the
  1460. * one set later. The subclass should override it to conver the value of UCAL_ORDINAL_MONTH
  1461. * to UCAL_MONTH correctly if UCAL_ORDINAL_MONTH has higher priority.
  1462. *
  1463. * @return The value for the UCAL_MONTH.
  1464. * @internal
  1465. */
  1466. virtual int32_t internalGetMonth(UErrorCode& status) const;
  1467. /**
  1468. * Use this function instead of internalGet(UCAL_MONTH, defaultValue). The implementation
  1469. * check the timestamp of UCAL_MONTH and UCAL_ORDINAL_MONTH and use the
  1470. * one set later. The subclass should override it to conver the value of UCAL_ORDINAL_MONTH
  1471. * to UCAL_MONTH correctly if UCAL_ORDINAL_MONTH has higher priority.
  1472. *
  1473. * @param defaultValue a default value used if the UCAL_MONTH and
  1474. * UCAL_ORDINAL are both unset.
  1475. * @param status Output param set to failure code on function return
  1476. * when this function fails.
  1477. * @return The value for the UCAL_MONTH.
  1478. * @internal
  1479. */
  1480. virtual int32_t internalGetMonth(int32_t defaultValue, UErrorCode& status) const;
  1481. #ifndef U_HIDE_DEPRECATED_API
  1482. /**
  1483. * Sets the value for a given time field. This is a fast internal method for
  1484. * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
  1485. * flags.
  1486. *
  1487. * @param field The given time field.
  1488. * @param value The value for the given time field.
  1489. * @deprecated ICU 2.6. Use internalSet(UCalendarDateFields field, int32_t value) instead.
  1490. */
  1491. void internalSet(EDateFields field, int32_t value);
  1492. #endif /* U_HIDE_DEPRECATED_API */
  1493. /**
  1494. * Sets the value for a given time field. This is a fast internal method for
  1495. * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
  1496. * flags.
  1497. *
  1498. * @param field The given time field.
  1499. * @param value The value for the given time field.
  1500. * @stable ICU 2.6.
  1501. */
  1502. inline void internalSet(UCalendarDateFields field, int32_t value);
  1503. /**
  1504. * Prepare this calendar for computing the actual minimum or maximum.
  1505. * This method modifies this calendar's fields; it is called on a
  1506. * temporary calendar.
  1507. * @internal
  1508. */
  1509. virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
  1510. /**
  1511. * Limit enums. Not in sync with UCalendarLimitType (refers to internal fields).
  1512. * @internal
  1513. */
  1514. enum ELimitType {
  1515. #ifndef U_HIDE_INTERNAL_API
  1516. UCAL_LIMIT_MINIMUM = 0,
  1517. UCAL_LIMIT_GREATEST_MINIMUM,
  1518. UCAL_LIMIT_LEAST_MAXIMUM,
  1519. UCAL_LIMIT_MAXIMUM,
  1520. UCAL_LIMIT_COUNT
  1521. #endif /* U_HIDE_INTERNAL_API */
  1522. };
  1523. /**
  1524. * Subclass API for defining limits of different types.
  1525. * Subclasses must implement this method to return limits for the
  1526. * following fields:
  1527. *
  1528. * <pre>UCAL_ERA
  1529. * UCAL_YEAR
  1530. * UCAL_MONTH
  1531. * UCAL_WEEK_OF_YEAR
  1532. * UCAL_WEEK_OF_MONTH
  1533. * UCAL_DATE (DAY_OF_MONTH on Java)
  1534. * UCAL_DAY_OF_YEAR
  1535. * UCAL_DAY_OF_WEEK_IN_MONTH
  1536. * UCAL_YEAR_WOY
  1537. * UCAL_EXTENDED_YEAR</pre>
  1538. *
  1539. * @param field one of the above field numbers
  1540. * @param limitType one of <code>MINIMUM</code>, <code>GREATEST_MINIMUM</code>,
  1541. * <code>LEAST_MAXIMUM</code>, or <code>MAXIMUM</code>
  1542. * @internal
  1543. */
  1544. virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
  1545. /**
  1546. * Return a limit for a field.
  1547. * @param field the field, from <code>0..UCAL_MAX_FIELD</code>
  1548. * @param limitType the type specifier for the limit
  1549. * @see #ELimitType
  1550. * @internal
  1551. */
  1552. virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
  1553. /**
  1554. * Return the Julian day number of day before the first day of the
  1555. * given month in the given extended year. Subclasses should override
  1556. * this method to implement their calendar system.
  1557. * @param eyear the extended year
  1558. * @param month the zero-based month, or 0 if useMonth is false
  1559. * @param useMonth if false, compute the day before the first day of
  1560. * the given year, otherwise, compute the day before the first day of
  1561. * the given month
  1562. * @param status Output param set to failure code on function return
  1563. * when this function fails.
  1564. * @return the Julian day number of the day before the first
  1565. * day of the given month and year
  1566. * @internal
  1567. */
  1568. virtual int64_t handleComputeMonthStart(int32_t eyear, int32_t month,
  1569. UBool useMonth, UErrorCode& status) const = 0;
  1570. /**
  1571. * Return the number of days in the given month of the given extended
  1572. * year of this calendar system. Subclasses should override this
  1573. * method if they can provide a more correct or more efficient
  1574. * implementation than the default implementation in Calendar.
  1575. * @internal
  1576. */
  1577. virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month, UErrorCode& status) const ;
  1578. /**
  1579. * Return the number of days in the given extended year of this
  1580. * calendar system. Subclasses should override this method if they can
  1581. * provide a more correct or more efficient implementation than the
  1582. * default implementation in Calendar.
  1583. * @stable ICU 2.0
  1584. */
  1585. virtual int32_t handleGetYearLength(int32_t eyear) const;
  1586. /**
  1587. * Return the extended year defined by the current fields. This will
  1588. * use the UCAL_EXTENDED_YEAR field or the UCAL_YEAR and supra-year fields (such
  1589. * as UCAL_ERA) specific to the calendar system, depending on which set of
  1590. * fields is newer.
  1591. * @param status ICU Error Code
  1592. * @return the extended year
  1593. * @internal
  1594. */
  1595. virtual int32_t handleGetExtendedYear(UErrorCode& status) = 0;
  1596. /**
  1597. * Subclasses may override this. This method calls
  1598. * handleGetMonthLength() to obtain the calendar-specific month
  1599. * length.
  1600. * @param bestField which field to use to calculate the date
  1601. * @param status ICU Error Code
  1602. * @return julian day specified by calendar fields.
  1603. * @internal
  1604. */
  1605. virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField, UErrorCode &status);
  1606. /**
  1607. * Subclasses must override this to convert from week fields
  1608. * (YEAR_WOY and WEEK_OF_YEAR) to an extended year in the case
  1609. * where YEAR, EXTENDED_YEAR are not set.
  1610. * The Calendar implementation assumes yearWoy is in extended gregorian form
  1611. * @return the extended year, UCAL_EXTENDED_YEAR
  1612. * @internal
  1613. */
  1614. virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy, UErrorCode& status);
  1615. /**
  1616. * Validate a single field of this calendar. Subclasses should
  1617. * override this method to validate any calendar-specific fields.
  1618. * Generic fields can be handled by `Calendar::validateField()`.
  1619. * @internal
  1620. */
  1621. virtual void validateField(UCalendarDateFields field, UErrorCode &status);
  1622. #ifndef U_HIDE_INTERNAL_API
  1623. /**
  1624. * Compute the Julian day from fields. Will determine whether to use
  1625. * the JULIAN_DAY field directly, or other fields.
  1626. * @param status ICU Error Code
  1627. * @return the julian day
  1628. * @internal
  1629. */
  1630. int32_t computeJulianDay(UErrorCode &status);
  1631. /**
  1632. * Compute the milliseconds in the day from the fields. This is a
  1633. * value from 0 to 23:59:59.999 inclusive, unless fields are out of
  1634. * range, in which case it can be an arbitrary value. This value
  1635. * reflects local zone wall time.
  1636. * @internal
  1637. */
  1638. double computeMillisInDay();
  1639. /**
  1640. * This method can assume EXTENDED_YEAR has been set.
  1641. * @param millis milliseconds of the date fields
  1642. * @param millisInDay milliseconds of the time fields; may be out
  1643. * or range.
  1644. * @param ec Output param set to failure code on function return
  1645. * when this function fails.
  1646. * @internal
  1647. */
  1648. int32_t computeZoneOffset(double millis, double millisInDay, UErrorCode &ec);
  1649. /**
  1650. * Determine the best stamp in a range.
  1651. * @param start first enum to look at
  1652. * @param end last enum to look at
  1653. * @param bestSoFar stamp prior to function call
  1654. * @return the stamp value of the best stamp
  1655. * @internal
  1656. */
  1657. int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
  1658. /**
  1659. * Marker for end of resolve set (row or group). Value for field resolution tables.
  1660. *
  1661. * @see #resolveFields
  1662. * @internal
  1663. */
  1664. static constexpr int32_t kResolveSTOP = -1;
  1665. /**
  1666. * Value to be bitwised "ORed" against resolve table field values for remapping.
  1667. * Example: (UCAL_DATE | kResolveRemap) in 1st column will cause 'UCAL_DATE' to be returned,
  1668. * but will not examine the value of UCAL_DATE.
  1669. * Value for field resolution tables.
  1670. *
  1671. * @see #resolveFields
  1672. * @internal
  1673. */
  1674. static constexpr int32_t kResolveRemap = 32;
  1675. /**
  1676. * Precedence table for Dates
  1677. * @see #resolveFields
  1678. * @internal
  1679. */
  1680. static const UFieldResolutionTable kDatePrecedence[];
  1681. /**
  1682. * Precedence table for Year
  1683. * @see #resolveFields
  1684. * @internal
  1685. */
  1686. static const UFieldResolutionTable kYearPrecedence[];
  1687. /**
  1688. * Precedence table for Day of Week
  1689. * @see #resolveFields
  1690. * @internal
  1691. */
  1692. static const UFieldResolutionTable kDOWPrecedence[];
  1693. /**
  1694. * Precedence table for Months
  1695. * @see #resolveFields
  1696. * @internal
  1697. */
  1698. static const UFieldResolutionTable kMonthPrecedence[];
  1699. /**
  1700. * Given a precedence table, return the newest field combination in
  1701. * the table, or UCAL_FIELD_COUNT if none is found.
  1702. *
  1703. * <p>The precedence table is a 3-dimensional array of integers. It
  1704. * may be thought of as an array of groups. Each group is an array of
  1705. * lines. Each line is an array of field numbers. Within a line, if
  1706. * all fields are set, then the time stamp of the line is taken to be
  1707. * the stamp of the most recently set field. If any field of a line is
  1708. * unset, then the line fails to match. Within a group, the line with
  1709. * the newest time stamp is selected. The first field of the line is
  1710. * returned to indicate which line matched.
  1711. *
  1712. * <p>In some cases, it may be desirable to map a line to field that
  1713. * whose stamp is NOT examined. For example, if the best field is
  1714. * DAY_OF_WEEK then the DAY_OF_WEEK_IN_MONTH algorithm may be used. In
  1715. * order to do this, insert the value <code>kResolveRemap | F</code> at
  1716. * the start of the line, where <code>F</code> is the desired return
  1717. * field value. This field will NOT be examined; it only determines
  1718. * the return value if the other fields in the line are the newest.
  1719. *
  1720. * <p>If all lines of a group contain at least one unset field, then no
  1721. * line will match, and the group as a whole will fail to match. In
  1722. * that case, the next group will be processed. If all groups fail to
  1723. * match, then UCAL_FIELD_COUNT is returned.
  1724. * @internal
  1725. */
  1726. UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable) const;
  1727. #endif /* U_HIDE_INTERNAL_API */
  1728. /**
  1729. * @internal
  1730. */
  1731. virtual const UFieldResolutionTable* getFieldResolutionTable() const;
  1732. #ifndef U_HIDE_INTERNAL_API
  1733. /**
  1734. * Return the field that is newer, either defaultField, or
  1735. * alternateField. If neither is newer or neither is set, return defaultField.
  1736. * @internal
  1737. */
  1738. UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
  1739. #endif /* U_HIDE_INTERNAL_API */
  1740. private:
  1741. /**
  1742. * Helper function for calculating limits by trial and error
  1743. * @param field The field being investigated
  1744. * @param startValue starting (least max) value of field
  1745. * @param endValue ending (greatest max) value of field
  1746. * @param status return type
  1747. * @internal (private)
  1748. */
  1749. int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
  1750. protected:
  1751. /**
  1752. * The flag which indicates if the current time is set in the calendar.
  1753. * @stable ICU 2.0
  1754. */
  1755. UBool fIsTimeSet;
  1756. /**
  1757. * True if the fields are in sync with the currently set time of this Calendar.
  1758. * If false, then the next attempt to get the value of a field will
  1759. * force a recomputation of all fields from the current value of the time
  1760. * field.
  1761. * <P>
  1762. * This should really be named areFieldsInSync, but the old name is retained
  1763. * for backward compatibility.
  1764. * @stable ICU 2.0
  1765. */
  1766. UBool fAreFieldsSet;
  1767. /**
  1768. * True if all of the fields have been set. This is initially false, and set to
  1769. * true by computeFields().
  1770. * @stable ICU 2.0
  1771. */
  1772. UBool fAreAllFieldsSet;
  1773. /**
  1774. * True if all fields have been virtually set, but have not yet been
  1775. * computed. This occurs only in setTimeInMillis(). A calendar set
  1776. * to this state will compute all fields from the time if it becomes
  1777. * necessary, but otherwise will delay such computation.
  1778. * @stable ICU 3.0
  1779. */
  1780. UBool fAreFieldsVirtuallySet;
  1781. /**
  1782. * Get the current time without recomputing.
  1783. *
  1784. * @return the current time without recomputing.
  1785. * @stable ICU 2.0
  1786. */
  1787. UDate internalGetTime() const { return fTime; }
  1788. /**
  1789. * Set the current time without affecting flags or fields.
  1790. *
  1791. * @param time The time to be set
  1792. * @return the current time without recomputing.
  1793. * @stable ICU 2.0
  1794. */
  1795. void internalSetTime(UDate time) { fTime = time; }
  1796. /**
  1797. * The time fields containing values into which the millis is computed.
  1798. * @stable ICU 2.0
  1799. */
  1800. int32_t fFields[UCAL_FIELD_COUNT];
  1801. #ifndef U_FORCE_HIDE_DEPRECATED_API
  1802. /**
  1803. * The flags which tell if a specified time field for the calendar is set.
  1804. * @deprecated ICU 2.8 use (fStamp[n]!=kUnset)
  1805. */
  1806. UBool fIsSet[UCAL_FIELD_COUNT];
  1807. #endif // U_FORCE_HIDE_DEPRECATED_API
  1808. /** Special values of stamp[]
  1809. * @stable ICU 2.0
  1810. */
  1811. enum {
  1812. kUnset = 0,
  1813. kInternallySet,
  1814. kMinimumUserStamp
  1815. };
  1816. /**
  1817. * Pseudo-time-stamps which specify when each field was set. There
  1818. * are two special values, UNSET and INTERNALLY_SET. Values from
  1819. * MINIMUM_USER_SET to Integer.MAX_VALUE are legal user set values.
  1820. * @stable ICU 2.0
  1821. */
  1822. int32_t fStamp[UCAL_FIELD_COUNT];
  1823. /**
  1824. * Subclasses may override this method to compute several fields
  1825. * specific to each calendar system. These are:
  1826. *
  1827. * <ul><li>ERA
  1828. * <li>YEAR
  1829. * <li>MONTH
  1830. * <li>DAY_OF_MONTH
  1831. * <li>DAY_OF_YEAR
  1832. * <li>EXTENDED_YEAR</ul>
  1833. *
  1834. * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields, which
  1835. * will be set when this method is called. Subclasses can also call
  1836. * the getGregorianXxx() methods to obtain Gregorian calendar
  1837. * equivalents for the given Julian day.
  1838. *
  1839. * <p>In addition, subclasses should compute any subclass-specific
  1840. * fields, that is, fields from BASE_FIELD_COUNT to
  1841. * getFieldCount() - 1.
  1842. *
  1843. * <p>The default implementation in <code>Calendar</code> implements
  1844. * a pure proleptic Gregorian calendar.
  1845. * @internal
  1846. */
  1847. virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
  1848. #ifndef U_HIDE_INTERNAL_API
  1849. /**
  1850. * Return the extended year on the Gregorian calendar as computed by
  1851. * <code>computeGregorianFields()</code>.
  1852. * @internal
  1853. */
  1854. int32_t getGregorianYear() const {
  1855. return fGregorianYear;
  1856. }
  1857. /**
  1858. * Return the month (0-based) on the Gregorian calendar as computed by
  1859. * <code>computeGregorianFields()</code>.
  1860. * @internal
  1861. */
  1862. int32_t getGregorianMonth() const {
  1863. return fGregorianMonth;
  1864. }
  1865. /**
  1866. * Return the day of year (1-based) on the Gregorian calendar as
  1867. * computed by <code>computeGregorianFields()</code>.
  1868. * @internal
  1869. */
  1870. int32_t getGregorianDayOfYear() const {
  1871. return fGregorianDayOfYear;
  1872. }
  1873. /**
  1874. * Return the day of month (1-based) on the Gregorian calendar as
  1875. * computed by <code>computeGregorianFields()</code>.
  1876. * @internal
  1877. */
  1878. int32_t getGregorianDayOfMonth() const {
  1879. return fGregorianDayOfMonth;
  1880. }
  1881. #endif /* U_HIDE_INTERNAL_API */
  1882. /**
  1883. * Called by computeJulianDay. Returns the default month (0-based) for the year,
  1884. * taking year and era into account. Defaults to 0 for Gregorian, which doesn't care.
  1885. * @param eyear The extended year
  1886. * @param status Output param set to failure code on function return
  1887. * when this function fails.
  1888. * @internal
  1889. */
  1890. virtual int32_t getDefaultMonthInYear(int32_t eyear, UErrorCode& status);
  1891. /**
  1892. * Called by computeJulianDay. Returns the default day (1-based) for the month,
  1893. * taking currently-set year and era into account. Defaults to 1 for Gregorian.
  1894. * @param eyear the extended year
  1895. * @param month the month in the year
  1896. * @internal
  1897. */
  1898. virtual int32_t getDefaultDayInMonth(int32_t eyear, int32_t month);
  1899. //-------------------------------------------------------------------------
  1900. // Protected utility methods for use by subclasses. These are very handy
  1901. // for implementing add, roll, and computeFields.
  1902. //-------------------------------------------------------------------------
  1903. /**
  1904. * Adjust the specified field so that it is within
  1905. * the allowable range for the date to which this calendar is set.
  1906. * For example, in a Gregorian calendar pinning the {@link #UCalendarDateFields DAY_OF_MONTH}
  1907. * field for a calendar set to April 31 would cause it to be set
  1908. * to April 30.
  1909. * <p>
  1910. * <b>Subclassing:</b>
  1911. * <br>
  1912. * This utility method is intended for use by subclasses that need to implement
  1913. * their own overrides of {@link #roll roll} and {@link #add add}.
  1914. * <p>
  1915. * <b>Note:</b>
  1916. * <code>pinField</code> is implemented in terms of
  1917. * {@link #getActualMinimum getActualMinimum}
  1918. * and {@link #getActualMaximum getActualMaximum}. If either of those methods uses
  1919. * a slow, iterative algorithm for a particular field, it would be
  1920. * unwise to attempt to call <code>pinField</code> for that field. If you
  1921. * really do need to do so, you should override this method to do
  1922. * something more efficient for that field.
  1923. * <p>
  1924. * @param field The calendar field whose value should be pinned.
  1925. * @param status Output param set to failure code on function return
  1926. * when this function fails.
  1927. *
  1928. * @see #getActualMinimum
  1929. * @see #getActualMaximum
  1930. * @stable ICU 2.0
  1931. */
  1932. virtual void pinField(UCalendarDateFields field, UErrorCode& status);
  1933. /**
  1934. * Return the week number of a day, within a period. This may be the week number in
  1935. * a year or the week number in a month. Usually this will be a value >= 1, but if
  1936. * some initial days of the period are excluded from week 1, because
  1937. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1, then
  1938. * the week number will be zero for those
  1939. * initial days. This method requires the day number and day of week for some
  1940. * known date in the period in order to determine the day of week
  1941. * on the desired day.
  1942. * <p>
  1943. * <b>Subclassing:</b>
  1944. * <br>
  1945. * This method is intended for use by subclasses in implementing their
  1946. * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
  1947. * It is often useful in {@link #getActualMinimum getActualMinimum} and
  1948. * {@link #getActualMaximum getActualMaximum} as well.
  1949. * <p>
  1950. * This variant is handy for computing the week number of some other
  1951. * day of a period (often the first or last day of the period) when its day
  1952. * of the week is not known but the day number and day of week for some other
  1953. * day in the period (e.g. the current date) <em>is</em> known.
  1954. * <p>
  1955. * @param desiredDay The {@link #UCalendarDateFields DAY_OF_YEAR} or
  1956. * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
  1957. * Should be 1 for the first day of the period.
  1958. *
  1959. * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR}
  1960. * or {@link #UCalendarDateFields DAY_OF_MONTH} for a day in the period whose
  1961. * {@link #UCalendarDateFields DAY_OF_WEEK} is specified by the
  1962. * <code>knownDayOfWeek</code> parameter.
  1963. * Should be 1 for first day of period.
  1964. *
  1965. * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
  1966. * corresponding to the <code>knownDayOfPeriod</code> parameter.
  1967. * 1-based with 1=Sunday.
  1968. *
  1969. * @return The week number (one-based), or zero if the day falls before
  1970. * the first week because
  1971. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
  1972. * is more than one.
  1973. *
  1974. * @stable ICU 2.8
  1975. */
  1976. int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
  1977. #ifndef U_HIDE_INTERNAL_API
  1978. /**
  1979. * Return the week number of a day, within a period. This may be the week number in
  1980. * a year, or the week number in a month. Usually this will be a value >= 1, but if
  1981. * some initial days of the period are excluded from week 1, because
  1982. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1,
  1983. * then the week number will be zero for those
  1984. * initial days. This method requires the day of week for the given date in order to
  1985. * determine the result.
  1986. * <p>
  1987. * <b>Subclassing:</b>
  1988. * <br>
  1989. * This method is intended for use by subclasses in implementing their
  1990. * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
  1991. * It is often useful in {@link #getActualMinimum getActualMinimum} and
  1992. * {@link #getActualMaximum getActualMaximum} as well.
  1993. * <p>
  1994. * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR} or
  1995. * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
  1996. * Should be 1 for the first day of the period.
  1997. *
  1998. * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
  1999. * corresponding to the <code>dayOfPeriod</code> parameter.
  2000. * 1-based with 1=Sunday.
  2001. *
  2002. * @return The week number (one-based), or zero if the day falls before
  2003. * the first week because
  2004. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
  2005. * is more than one.
  2006. * @internal
  2007. */
  2008. inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
  2009. /**
  2010. * returns the local DOW, valid range 0..6
  2011. * @internal
  2012. */
  2013. int32_t getLocalDOW(UErrorCode& status);
  2014. #endif /* U_HIDE_INTERNAL_API */
  2015. private:
  2016. /**
  2017. * The next available value for fStamp[]
  2018. */
  2019. int32_t fNextStamp;// = MINIMUM_USER_STAMP;
  2020. /**
  2021. * Recalculates the time stamp array (fStamp).
  2022. * Resets fNextStamp to lowest next stamp value.
  2023. */
  2024. void recalculateStamp();
  2025. /**
  2026. * The current time set for the calendar.
  2027. */
  2028. UDate fTime;
  2029. /**
  2030. * @see #setLenient
  2031. */
  2032. UBool fLenient;
  2033. /**
  2034. * Time zone affects the time calculation done by Calendar. Calendar subclasses use
  2035. * the time zone data to produce the local time. Always set; never nullptr.
  2036. */
  2037. TimeZone* fZone;
  2038. /**
  2039. * Option for repeated wall time
  2040. * @see #setRepeatedWallTimeOption
  2041. */
  2042. UCalendarWallTimeOption fRepeatedWallTime;
  2043. /**
  2044. * Option for skipped wall time
  2045. * @see #setSkippedWallTimeOption
  2046. */
  2047. UCalendarWallTimeOption fSkippedWallTime;
  2048. /**
  2049. * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent. They are
  2050. * used to figure out the week count for a specific date for a given locale. These
  2051. * must be set when a Calendar is constructed. For example, in US locale,
  2052. * firstDayOfWeek is SUNDAY; minimalDaysInFirstWeek is 1. They are used to figure
  2053. * out the week count for a specific date for a given locale. These must be set when
  2054. * a Calendar is constructed.
  2055. */
  2056. UCalendarDaysOfWeek fFirstDayOfWeek;
  2057. uint8_t fMinimalDaysInFirstWeek;
  2058. UCalendarDaysOfWeek fWeekendOnset;
  2059. int32_t fWeekendOnsetMillis;
  2060. UCalendarDaysOfWeek fWeekendCease;
  2061. int32_t fWeekendCeaseMillis;
  2062. /**
  2063. * Sets firstDayOfWeek and minimalDaysInFirstWeek. Called at Calendar construction
  2064. * time.
  2065. *
  2066. * @param desiredLocale The given locale.
  2067. * @param type The calendar type identifier, e.g: gregorian, buddhist, etc.
  2068. * @param success Indicates the status of setting the week count data from
  2069. * the resource for the given locale. Returns U_ZERO_ERROR if
  2070. * constructed successfully.
  2071. */
  2072. void setWeekData(const Locale& desiredLocale, const char *type, UErrorCode& success);
  2073. /**
  2074. * Recompute the time and update the status fields isTimeSet
  2075. * and areFieldsSet. Callers should check isTimeSet and only
  2076. * call this method if isTimeSet is false.
  2077. *
  2078. * @param status Output param set to success/failure code on exit. If any value
  2079. * previously set in the time field is invalid or restricted by
  2080. * leniency, this will be set to an error status.
  2081. */
  2082. void updateTime(UErrorCode& status);
  2083. /**
  2084. * The Gregorian year, as computed by computeGregorianFields() and
  2085. * returned by getGregorianYear().
  2086. * @see #computeGregorianFields
  2087. */
  2088. int32_t fGregorianYear;
  2089. /**
  2090. * The Gregorian month, as computed by computeGregorianFields() and
  2091. * returned by getGregorianMonth().
  2092. * @see #computeGregorianFields
  2093. */
  2094. int32_t fGregorianMonth;
  2095. /**
  2096. * The Gregorian day of the year, as computed by
  2097. * computeGregorianFields() and returned by getGregorianDayOfYear().
  2098. * @see #computeGregorianFields
  2099. */
  2100. int32_t fGregorianDayOfYear;
  2101. /**
  2102. * The Gregorian day of the month, as computed by
  2103. * computeGregorianFields() and returned by getGregorianDayOfMonth().
  2104. * @see #computeGregorianFields
  2105. */
  2106. int32_t fGregorianDayOfMonth;
  2107. /* calculations */
  2108. /**
  2109. * Compute the Gregorian calendar year, month, and day of month from
  2110. * the given Julian day. These values are not stored in fields, but in
  2111. * member variables gregorianXxx. Also compute the DAY_OF_WEEK and
  2112. * DOW_LOCAL fields.
  2113. */
  2114. void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
  2115. protected:
  2116. /**
  2117. * Compute the Gregorian calendar year, month, and day of month from the
  2118. * Julian day. These values are not stored in fields, but in member
  2119. * variables gregorianXxx. They are used for time zone computations and by
  2120. * subclasses that are Gregorian derivatives. Subclasses may call this
  2121. * method to perform a Gregorian calendar millis->fields computation.
  2122. */
  2123. void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
  2124. private:
  2125. /**
  2126. * Compute the fields WEEK_OF_YEAR, YEAR_WOY, WEEK_OF_MONTH,
  2127. * DAY_OF_WEEK_IN_MONTH, and DOW_LOCAL from EXTENDED_YEAR, YEAR,
  2128. * DAY_OF_WEEK, and DAY_OF_YEAR. The latter fields are computed by the
  2129. * subclass based on the calendar system.
  2130. *
  2131. * <p>The YEAR_WOY field is computed simplistically. It is equal to YEAR
  2132. * most of the time, but at the year boundary it may be adjusted to YEAR-1
  2133. * or YEAR+1 to reflect the overlap of a week into an adjacent year. In
  2134. * this case, a simple increment or decrement is performed on YEAR, even
  2135. * though this may yield an invalid YEAR value. For instance, if the YEAR
  2136. * is part of a calendar system with an N-year cycle field CYCLE, then
  2137. * incrementing the YEAR may involve incrementing CYCLE and setting YEAR
  2138. * back to 0 or 1. This is not handled by this code, and in fact cannot be
  2139. * simply handled without having subclasses define an entire parallel set of
  2140. * fields for fields larger than or equal to a year. This additional
  2141. * complexity is not warranted, since the intention of the YEAR_WOY field is
  2142. * to support ISO 8601 notation, so it will typically be used with a
  2143. * proleptic Gregorian calendar, which has no field larger than a year.
  2144. */
  2145. void computeWeekFields(UErrorCode &ec);
  2146. /**
  2147. * Ensure that each field is within its valid range by calling {@link
  2148. * #validateField(int, int&)} on each field that has been set. This method
  2149. * should only be called if this calendar is not lenient.
  2150. * @see #isLenient
  2151. * @see #validateField(int, int&)
  2152. */
  2153. void validateFields(UErrorCode &status);
  2154. /**
  2155. * Validate a single field of this calendar given its minimum and
  2156. * maximum allowed value. If the field is out of range,
  2157. * <code>U_ILLEGAL_ARGUMENT_ERROR</code> will be set. Subclasses may
  2158. * use this method in their implementation of {@link
  2159. * #validateField(int, int&)}.
  2160. */
  2161. void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
  2162. protected:
  2163. #ifndef U_HIDE_INTERNAL_API
  2164. /**
  2165. * Convert a quasi Julian date to the day of the week. The Julian date used here is
  2166. * not a true Julian date, since it is measured from midnight, not noon. Return
  2167. * value is one-based.
  2168. *
  2169. * @param julian The given Julian date number.
  2170. * @return Day number from 1..7 (SUN..SAT).
  2171. * @internal
  2172. */
  2173. static uint8_t julianDayToDayOfWeek(int32_t julian);
  2174. #endif /* U_HIDE_INTERNAL_API */
  2175. private:
  2176. char validLocale[ULOC_FULLNAME_CAPACITY];
  2177. char actualLocale[ULOC_FULLNAME_CAPACITY];
  2178. public:
  2179. #if !UCONFIG_NO_SERVICE
  2180. /**
  2181. * INTERNAL FOR 2.6 -- Registration.
  2182. */
  2183. #ifndef U_HIDE_INTERNAL_API
  2184. /**
  2185. * Return a StringEnumeration over the locales available at the time of the call,
  2186. * including registered locales.
  2187. * @return a StringEnumeration over the locales available at the time of the call
  2188. * @internal
  2189. */
  2190. static StringEnumeration* getAvailableLocales();
  2191. /**
  2192. * Register a new Calendar factory. The factory will be adopted.
  2193. * INTERNAL in 2.6
  2194. *
  2195. * Because ICU may choose to cache Calendars internally, this must
  2196. * be called at application startup, prior to any calls to
  2197. * Calendar::createInstance to avoid undefined behavior.
  2198. *
  2199. * @param toAdopt the factory instance to be adopted
  2200. * @param status the in/out status code, no special meanings are assigned
  2201. * @return a registry key that can be used to unregister this factory
  2202. * @internal
  2203. */
  2204. static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
  2205. /**
  2206. * Unregister a previously-registered CalendarFactory using the key returned from the
  2207. * register call. Key becomes invalid after a successful call and should not be used again.
  2208. * The CalendarFactory corresponding to the key will be deleted.
  2209. * INTERNAL in 2.6
  2210. *
  2211. * Because ICU may choose to cache Calendars internally, this should
  2212. * be called during application shutdown, after all calls to
  2213. * Calendar::createInstance to avoid undefined behavior.
  2214. *
  2215. * @param key the registry key returned by a previous call to registerFactory
  2216. * @param status the in/out status code, no special meanings are assigned
  2217. * @return true if the factory for the key was successfully unregistered
  2218. * @internal
  2219. */
  2220. static UBool unregister(URegistryKey key, UErrorCode& status);
  2221. #endif /* U_HIDE_INTERNAL_API */
  2222. /**
  2223. * Multiple Calendar Implementation
  2224. * @internal
  2225. */
  2226. friend class CalendarFactory;
  2227. /**
  2228. * Multiple Calendar Implementation
  2229. * @internal
  2230. */
  2231. friend class CalendarService;
  2232. /**
  2233. * Multiple Calendar Implementation
  2234. * @internal
  2235. */
  2236. friend class DefaultCalendarFactory;
  2237. #endif /* !UCONFIG_NO_SERVICE */
  2238. /**
  2239. * @return true if this calendar has a default century (i.e. 03 -> 2003)
  2240. * @internal
  2241. */
  2242. virtual UBool haveDefaultCentury() const = 0;
  2243. /**
  2244. * @return the start of the default century, as a UDate
  2245. * @internal
  2246. */
  2247. virtual UDate defaultCenturyStart() const = 0;
  2248. /**
  2249. * @return the beginning year of the default century, as a year
  2250. * @internal
  2251. */
  2252. virtual int32_t defaultCenturyStartYear() const = 0;
  2253. /** Get the locale for this calendar object. You can choose between valid and actual locale.
  2254. * @param type type of the locale we're looking for (valid or actual)
  2255. * @param status error code for the operation
  2256. * @return the locale
  2257. * @stable ICU 2.8
  2258. */
  2259. Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
  2260. /**
  2261. * @return The related Gregorian year; will be obtained by modifying the value
  2262. * obtained by get from UCAL_EXTENDED_YEAR field
  2263. * @internal
  2264. */
  2265. virtual int32_t getRelatedYear(UErrorCode &status) const;
  2266. /**
  2267. * @param year The related Gregorian year to set; will be modified as necessary then
  2268. * set in UCAL_EXTENDED_YEAR field
  2269. * @internal
  2270. */
  2271. virtual void setRelatedYear(int32_t year);
  2272. #ifndef U_HIDE_INTERNAL_API
  2273. /** Get the locale for this calendar object. You can choose between valid and actual locale.
  2274. * @param type type of the locale we're looking for (valid or actual)
  2275. * @param status error code for the operation
  2276. * @return the locale
  2277. * @internal
  2278. */
  2279. const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
  2280. #endif /* U_HIDE_INTERNAL_API */
  2281. private:
  2282. /**
  2283. * Cast TimeZone used by this object to BasicTimeZone, or nullptr if the TimeZone
  2284. * is not an instance of BasicTimeZone.
  2285. */
  2286. BasicTimeZone* getBasicTimeZone() const;
  2287. /**
  2288. * Find the previous zone transition near the given time.
  2289. * @param base The base time, inclusive
  2290. * @param transitionTime Receives the result time
  2291. * @param status The error status
  2292. * @return true if a transition is found.
  2293. */
  2294. UBool getImmediatePreviousZoneTransition(UDate base, UDate *transitionTime, UErrorCode& status) const;
  2295. public:
  2296. #ifndef U_HIDE_INTERNAL_API
  2297. /**
  2298. * Creates a new Calendar from a Locale for the cache.
  2299. * This method does not set the time or timezone in returned calendar.
  2300. * @param locale the locale.
  2301. * @param status any error returned here.
  2302. * @return the new Calendar object with no time or timezone set.
  2303. * @internal For ICU use only.
  2304. */
  2305. static Calendar * U_EXPORT2 makeInstance(
  2306. const Locale &locale, UErrorCode &status);
  2307. /**
  2308. * Get the calendar type for given locale.
  2309. * @param locale the locale
  2310. * @param typeBuffer calendar type returned here
  2311. * @param typeBufferSize The size of typeBuffer in bytes. If the type
  2312. * can't fit in the buffer, this method sets status to
  2313. * U_BUFFER_OVERFLOW_ERROR
  2314. * @param status error, if any, returned here.
  2315. * @internal For ICU use only.
  2316. */
  2317. static void U_EXPORT2 getCalendarTypeFromLocale(
  2318. const Locale &locale,
  2319. char *typeBuffer,
  2320. int32_t typeBufferSize,
  2321. UErrorCode &status);
  2322. #endif /* U_HIDE_INTERNAL_API */
  2323. };
  2324. // -------------------------------------
  2325. inline Calendar*
  2326. Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
  2327. {
  2328. // since the Locale isn't specified, use the default locale
  2329. return createInstance(zone, Locale::getDefault(), errorCode);
  2330. }
  2331. // -------------------------------------
  2332. inline void
  2333. Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
  2334. {
  2335. roll(field, (int32_t)(up ? +1 : -1), status);
  2336. }
  2337. #ifndef U_HIDE_DEPRECATED_API
  2338. inline void
  2339. Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
  2340. {
  2341. roll((UCalendarDateFields) field, up, status);
  2342. }
  2343. #endif /* U_HIDE_DEPRECATED_API */
  2344. // -------------------------------------
  2345. /**
  2346. * Fast method for subclasses. The caller must maintain fUserSetDSTOffset and
  2347. * fUserSetZoneOffset, as well as the isSet[] array.
  2348. */
  2349. inline void
  2350. Calendar::internalSet(UCalendarDateFields field, int32_t value)
  2351. {
  2352. fFields[field] = value;
  2353. fStamp[field] = kInternallySet;
  2354. fIsSet[field] = true; // Remove later
  2355. }
  2356. /**
  2357. * Macro for the class to declare it override
  2358. * haveDefaultCentury, defaultCenturyStart, and
  2359. * defaultCenturyStartYear functions in this class.
  2360. * @internal
  2361. */
  2362. #define DECLARE_OVERRIDE_SYSTEM_DEFAULT_CENTURY \
  2363. virtual UBool haveDefaultCentury() const override; \
  2364. virtual UDate defaultCenturyStart() const override; \
  2365. virtual int32_t defaultCenturyStartYear() const override;
  2366. #ifndef U_HIDE_INTERNAL_API
  2367. inline int32_t Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
  2368. {
  2369. return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
  2370. }
  2371. #endif /* U_HIDE_INTERNAL_API */
  2372. U_NAMESPACE_END
  2373. #endif /* #if !UCONFIG_NO_FORMATTING */
  2374. #endif /* U_SHOW_CPLUSPLUS_API */
  2375. #endif // _CALENDAR