calendar.h 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  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(void);
  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(void) 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(void);
  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(void) 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(void) 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(void) 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(void) 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(void) 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(void);
  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.
  1164. *
  1165. * @param field The time field to be cleared.
  1166. * @stable ICU 2.6.
  1167. */
  1168. void clear(UCalendarDateFields field);
  1169. /**
  1170. * Returns a unique class ID POLYMORPHICALLY. Pure virtual method. This method is to
  1171. * implement a simple version of RTTI, since not all C++ compilers support genuine
  1172. * RTTI. Polymorphic operator==() and clone() methods call this method.
  1173. * <P>
  1174. * Concrete subclasses of Calendar must implement getDynamicClassID() and also a
  1175. * static method and data member:
  1176. *
  1177. * static UClassID getStaticClassID() { return (UClassID)&amp;fgClassID; }
  1178. * static char fgClassID;
  1179. *
  1180. * @return The class ID for this object. All objects of a given class have the
  1181. * same class ID. Objects of other classes have different class IDs.
  1182. * @stable ICU 2.0
  1183. */
  1184. virtual UClassID getDynamicClassID(void) const override = 0;
  1185. /**
  1186. * Returns the calendar type name string for this Calendar object.
  1187. * The returned string is the legacy ICU calendar attribute value,
  1188. * for example, "gregorian" or "japanese".
  1189. *
  1190. * See type="old type name" for the calendar attribute of locale IDs
  1191. * at http://www.unicode.org/reports/tr35/#Key_Type_Definitions
  1192. *
  1193. * Sample code for getting the LDML/BCP 47 calendar key value:
  1194. * \code
  1195. * const char *calType = cal->getType();
  1196. * if (0 == strcmp(calType, "unknown")) {
  1197. * // deal with unknown calendar type
  1198. * } else {
  1199. * string localeID("root@calendar=");
  1200. * localeID.append(calType);
  1201. * char langTag[100];
  1202. * UErrorCode errorCode = U_ZERO_ERROR;
  1203. * int32_t length = uloc_toLanguageTag(localeID.c_str(), langTag, (int32_t)sizeof(langTag), true, &errorCode);
  1204. * if (U_FAILURE(errorCode)) {
  1205. * // deal with errors & overflow
  1206. * }
  1207. * string lang(langTag, length);
  1208. * size_t caPos = lang.find("-ca-");
  1209. * lang.erase(0, caPos + 4);
  1210. * // lang now contains the LDML calendar type
  1211. * }
  1212. * \endcode
  1213. *
  1214. * @return legacy calendar type name string
  1215. * @stable ICU 49
  1216. */
  1217. virtual const char * getType() const = 0;
  1218. /**
  1219. * Returns whether the given day of the week is a weekday, a weekend day,
  1220. * or a day that transitions from one to the other, for the locale and
  1221. * calendar system associated with this Calendar (the locale's region is
  1222. * often the most determinant factor). If a transition occurs at midnight,
  1223. * then the days before and after the transition will have the
  1224. * type UCAL_WEEKDAY or UCAL_WEEKEND. If a transition occurs at a time
  1225. * other than midnight, then the day of the transition will have
  1226. * the type UCAL_WEEKEND_ONSET or UCAL_WEEKEND_CEASE. In this case, the
  1227. * method getWeekendTransition() will return the point of
  1228. * transition.
  1229. * @param dayOfWeek The day of the week whose type is desired (UCAL_SUNDAY..UCAL_SATURDAY).
  1230. * @param status The error code for the operation.
  1231. * @return The UCalendarWeekdayType for the day of the week.
  1232. * @stable ICU 4.4
  1233. */
  1234. virtual UCalendarWeekdayType getDayOfWeekType(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
  1235. /**
  1236. * Returns the time during the day at which the weekend begins or ends in
  1237. * this calendar system. If getDayOfWeekType() returns UCAL_WEEKEND_ONSET
  1238. * for the specified dayOfWeek, return the time at which the weekend begins.
  1239. * If getDayOfWeekType() returns UCAL_WEEKEND_CEASE for the specified dayOfWeek,
  1240. * return the time at which the weekend ends. If getDayOfWeekType() returns
  1241. * some other UCalendarWeekdayType for the specified dayOfWeek, is it an error condition
  1242. * (U_ILLEGAL_ARGUMENT_ERROR).
  1243. * @param dayOfWeek The day of the week for which the weekend transition time is
  1244. * desired (UCAL_SUNDAY..UCAL_SATURDAY).
  1245. * @param status The error code for the operation.
  1246. * @return The milliseconds after midnight at which the weekend begins or ends.
  1247. * @stable ICU 4.4
  1248. */
  1249. virtual int32_t getWeekendTransition(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
  1250. /**
  1251. * Returns true if the given UDate is in the weekend in
  1252. * this calendar system.
  1253. * @param date The UDate in question.
  1254. * @param status The error code for the operation.
  1255. * @return true if the given UDate is in the weekend in
  1256. * this calendar system, false otherwise.
  1257. * @stable ICU 4.4
  1258. */
  1259. virtual UBool isWeekend(UDate date, UErrorCode &status) const;
  1260. /**
  1261. * Returns true if this Calendar's current date-time is in the weekend in
  1262. * this calendar system.
  1263. * @return true if this Calendar's current date-time is in the weekend in
  1264. * this calendar system, false otherwise.
  1265. * @stable ICU 4.4
  1266. */
  1267. virtual UBool isWeekend(void) const;
  1268. #ifndef U_FORCE_HIDE_DRAFT_API
  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. * @draft 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. * @draft 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. * @draft ICU 73
  1321. */
  1322. virtual void setTemporalMonthCode(const char* temporalMonth, UErrorCode& status);
  1323. #endif // U_FORCE_HIDE_DRAFT_API
  1324. protected:
  1325. /**
  1326. * Constructs a Calendar with the default time zone as returned by
  1327. * TimeZone::createInstance(), and the default locale.
  1328. *
  1329. * @param success Indicates the status of Calendar object construction. Returns
  1330. * U_ZERO_ERROR if constructed successfully.
  1331. * @stable ICU 2.0
  1332. */
  1333. Calendar(UErrorCode& success);
  1334. /**
  1335. * Copy constructor
  1336. *
  1337. * @param source Calendar object to be copied from
  1338. * @stable ICU 2.0
  1339. */
  1340. Calendar(const Calendar& source);
  1341. /**
  1342. * Default assignment operator
  1343. *
  1344. * @param right Calendar object to be copied
  1345. * @stable ICU 2.0
  1346. */
  1347. Calendar& operator=(const Calendar& right);
  1348. /**
  1349. * Constructs a Calendar with the given time zone and locale. Clients are no longer
  1350. * responsible for deleting the given time zone object after it's adopted.
  1351. *
  1352. * @param zone The given time zone.
  1353. * @param aLocale The given locale.
  1354. * @param success Indicates the status of Calendar object construction. Returns
  1355. * U_ZERO_ERROR if constructed successfully.
  1356. * @stable ICU 2.0
  1357. */
  1358. Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
  1359. /**
  1360. * Constructs a Calendar with the given time zone and locale.
  1361. *
  1362. * @param zone The given time zone.
  1363. * @param aLocale The given locale.
  1364. * @param success Indicates the status of Calendar object construction. Returns
  1365. * U_ZERO_ERROR if constructed successfully.
  1366. * @stable ICU 2.0
  1367. */
  1368. Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
  1369. /**
  1370. * Converts Calendar's time field values to GMT as milliseconds.
  1371. *
  1372. * @param status Output param set to success/failure code on exit. If any value
  1373. * previously set in the time field is invalid or restricted by
  1374. * leniency, this will be set to an error status.
  1375. * @stable ICU 2.0
  1376. */
  1377. virtual void computeTime(UErrorCode& status);
  1378. /**
  1379. * Converts GMT as milliseconds to time field values. This allows you to sync up the
  1380. * time field values with a new time that is set for the calendar. This method
  1381. * does NOT recompute the time first; to recompute the time, then the fields, use
  1382. * the method complete().
  1383. *
  1384. * @param status Output param set to success/failure code on exit. If any value
  1385. * previously set in the time field is invalid or restricted by
  1386. * leniency, this will be set to an error status.
  1387. * @stable ICU 2.0
  1388. */
  1389. virtual void computeFields(UErrorCode& status);
  1390. /**
  1391. * Gets this Calendar's current time as a long.
  1392. *
  1393. * @param status Output param set to success/failure code on exit. If any value
  1394. * previously set in the time field is invalid or restricted by
  1395. * leniency, this will be set to an error status.
  1396. * @return the current time as UTC milliseconds from the epoch.
  1397. * @stable ICU 2.0
  1398. */
  1399. double getTimeInMillis(UErrorCode& status) const;
  1400. /**
  1401. * Sets this Calendar's current time from the given long value.
  1402. * @param millis the new time in UTC milliseconds from the epoch.
  1403. * @param status Output param set to success/failure code on exit. If any value
  1404. * previously set in the time field is invalid or restricted by
  1405. * leniency, this will be set to an error status.
  1406. * @stable ICU 2.0
  1407. */
  1408. void setTimeInMillis( double millis, UErrorCode& status );
  1409. /**
  1410. * Recomputes the current time from currently set fields, and then fills in any
  1411. * unset fields in the time field list.
  1412. *
  1413. * @param status Output param set to success/failure code on exit. If any value
  1414. * previously set in the time field is invalid or restricted by
  1415. * leniency, this will be set to an error status.
  1416. * @stable ICU 2.0
  1417. */
  1418. void complete(UErrorCode& status);
  1419. #ifndef U_HIDE_DEPRECATED_API
  1420. /**
  1421. * Gets the value for a given time field. Subclasses can use this function to get
  1422. * field values without forcing recomputation of time.
  1423. *
  1424. * @param field The given time field.
  1425. * @return The value for the given time field.
  1426. * @deprecated ICU 2.6. Use internalGet(UCalendarDateFields field) instead.
  1427. */
  1428. inline int32_t internalGet(EDateFields field) const {return fFields[field];}
  1429. #endif /* U_HIDE_DEPRECATED_API */
  1430. #ifndef U_HIDE_INTERNAL_API
  1431. /**
  1432. * Gets the value for a given time field. Subclasses can use this function to get
  1433. * field values without forcing recomputation of time. If the field's stamp is UNSET,
  1434. * the defaultValue is used.
  1435. *
  1436. * @param field The given time field.
  1437. * @param defaultValue a default value used if the field is unset.
  1438. * @return The value for the given time field.
  1439. * @internal
  1440. */
  1441. inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
  1442. /**
  1443. * Gets the value for a given time field. Subclasses can use this function to get
  1444. * field values without forcing recomputation of time.
  1445. *
  1446. * @param field The given time field.
  1447. * @return The value for the given time field.
  1448. * @internal
  1449. */
  1450. inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
  1451. #endif /* U_HIDE_INTERNAL_API */
  1452. /**
  1453. * Use this function instead of internalGet(UCAL_MONTH). The implementation
  1454. * check the timestamp of UCAL_MONTH and UCAL_ORDINAL_MONTH and use the
  1455. * one set later. The subclass should override it to conver the value of UCAL_ORDINAL_MONTH
  1456. * to UCAL_MONTH correctly if UCAL_ORDINAL_MONTH has higher priority.
  1457. *
  1458. * @return The value for the UCAL_MONTH.
  1459. * @internal
  1460. */
  1461. virtual int32_t internalGetMonth() const;
  1462. /**
  1463. * Use this function instead of internalGet(UCAL_MONTH, defaultValue). The implementation
  1464. * check the timestamp of UCAL_MONTH and UCAL_ORDINAL_MONTH and use the
  1465. * one set later. The subclass should override it to conver the value of UCAL_ORDINAL_MONTH
  1466. * to UCAL_MONTH correctly if UCAL_ORDINAL_MONTH has higher priority.
  1467. *
  1468. * @param defaultValue a default value used if the UCAL_MONTH and
  1469. * UCAL_ORDINAL are both unset.
  1470. * @return The value for the UCAL_MONTH.
  1471. * @internal
  1472. */
  1473. virtual int32_t internalGetMonth(int32_t defaultValue) const;
  1474. #ifndef U_HIDE_DEPRECATED_API
  1475. /**
  1476. * Sets the value for a given time field. This is a fast internal method for
  1477. * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
  1478. * flags.
  1479. *
  1480. * @param field The given time field.
  1481. * @param value The value for the given time field.
  1482. * @deprecated ICU 2.6. Use internalSet(UCalendarDateFields field, int32_t value) instead.
  1483. */
  1484. void internalSet(EDateFields field, int32_t value);
  1485. #endif /* U_HIDE_DEPRECATED_API */
  1486. /**
  1487. * Sets the value for a given time field. This is a fast internal method for
  1488. * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
  1489. * flags.
  1490. *
  1491. * @param field The given time field.
  1492. * @param value The value for the given time field.
  1493. * @stable ICU 2.6.
  1494. */
  1495. inline void internalSet(UCalendarDateFields field, int32_t value);
  1496. /**
  1497. * Prepare this calendar for computing the actual minimum or maximum.
  1498. * This method modifies this calendar's fields; it is called on a
  1499. * temporary calendar.
  1500. * @internal
  1501. */
  1502. virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
  1503. /**
  1504. * Limit enums. Not in sync with UCalendarLimitType (refers to internal fields).
  1505. * @internal
  1506. */
  1507. enum ELimitType {
  1508. #ifndef U_HIDE_INTERNAL_API
  1509. UCAL_LIMIT_MINIMUM = 0,
  1510. UCAL_LIMIT_GREATEST_MINIMUM,
  1511. UCAL_LIMIT_LEAST_MAXIMUM,
  1512. UCAL_LIMIT_MAXIMUM,
  1513. UCAL_LIMIT_COUNT
  1514. #endif /* U_HIDE_INTERNAL_API */
  1515. };
  1516. /**
  1517. * Subclass API for defining limits of different types.
  1518. * Subclasses must implement this method to return limits for the
  1519. * following fields:
  1520. *
  1521. * <pre>UCAL_ERA
  1522. * UCAL_YEAR
  1523. * UCAL_MONTH
  1524. * UCAL_WEEK_OF_YEAR
  1525. * UCAL_WEEK_OF_MONTH
  1526. * UCAL_DATE (DAY_OF_MONTH on Java)
  1527. * UCAL_DAY_OF_YEAR
  1528. * UCAL_DAY_OF_WEEK_IN_MONTH
  1529. * UCAL_YEAR_WOY
  1530. * UCAL_EXTENDED_YEAR</pre>
  1531. *
  1532. * @param field one of the above field numbers
  1533. * @param limitType one of <code>MINIMUM</code>, <code>GREATEST_MINIMUM</code>,
  1534. * <code>LEAST_MAXIMUM</code>, or <code>MAXIMUM</code>
  1535. * @internal
  1536. */
  1537. virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
  1538. /**
  1539. * Return a limit for a field.
  1540. * @param field the field, from <code>0..UCAL_MAX_FIELD</code>
  1541. * @param limitType the type specifier for the limit
  1542. * @see #ELimitType
  1543. * @internal
  1544. */
  1545. virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
  1546. /**
  1547. * Return the Julian day number of day before the first day of the
  1548. * given month in the given extended year. Subclasses should override
  1549. * this method to implement their calendar system.
  1550. * @param eyear the extended year
  1551. * @param month the zero-based month, or 0 if useMonth is false
  1552. * @param useMonth if false, compute the day before the first day of
  1553. * the given year, otherwise, compute the day before the first day of
  1554. * the given month
  1555. * @return the Julian day number of the day before the first
  1556. * day of the given month and year
  1557. * @internal
  1558. */
  1559. virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
  1560. UBool useMonth) const = 0;
  1561. /**
  1562. * Return the number of days in the given month of the given extended
  1563. * year of this calendar system. Subclasses should override this
  1564. * method if they can provide a more correct or more efficient
  1565. * implementation than the default implementation in Calendar.
  1566. * @internal
  1567. */
  1568. virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ;
  1569. /**
  1570. * Return the number of days in the given extended year of this
  1571. * calendar system. Subclasses should override this method if they can
  1572. * provide a more correct or more efficient implementation than the
  1573. * default implementation in Calendar.
  1574. * @stable ICU 2.0
  1575. */
  1576. virtual int32_t handleGetYearLength(int32_t eyear) const;
  1577. /**
  1578. * Return the extended year defined by the current fields. This will
  1579. * use the UCAL_EXTENDED_YEAR field or the UCAL_YEAR and supra-year fields (such
  1580. * as UCAL_ERA) specific to the calendar system, depending on which set of
  1581. * fields is newer.
  1582. * @return the extended year
  1583. * @internal
  1584. */
  1585. virtual int32_t handleGetExtendedYear() = 0;
  1586. /**
  1587. * Subclasses may override this. This method calls
  1588. * handleGetMonthLength() to obtain the calendar-specific month
  1589. * length.
  1590. * @param bestField which field to use to calculate the date
  1591. * @return julian day specified by calendar fields.
  1592. * @internal
  1593. */
  1594. virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField);
  1595. /**
  1596. * Subclasses must override this to convert from week fields
  1597. * (YEAR_WOY and WEEK_OF_YEAR) to an extended year in the case
  1598. * where YEAR, EXTENDED_YEAR are not set.
  1599. * The Calendar implementation assumes yearWoy is in extended gregorian form
  1600. * @return the extended year, UCAL_EXTENDED_YEAR
  1601. * @internal
  1602. */
  1603. virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
  1604. /**
  1605. * Validate a single field of this calendar. Subclasses should
  1606. * override this method to validate any calendar-specific fields.
  1607. * Generic fields can be handled by `Calendar::validateField()`.
  1608. * @internal
  1609. */
  1610. virtual void validateField(UCalendarDateFields field, UErrorCode &status);
  1611. #ifndef U_HIDE_INTERNAL_API
  1612. /**
  1613. * Compute the Julian day from fields. Will determine whether to use
  1614. * the JULIAN_DAY field directly, or other fields.
  1615. * @return the julian day
  1616. * @internal
  1617. */
  1618. int32_t computeJulianDay();
  1619. /**
  1620. * Compute the milliseconds in the day from the fields. This is a
  1621. * value from 0 to 23:59:59.999 inclusive, unless fields are out of
  1622. * range, in which case it can be an arbitrary value. This value
  1623. * reflects local zone wall time.
  1624. * @internal
  1625. */
  1626. double computeMillisInDay();
  1627. /**
  1628. * This method can assume EXTENDED_YEAR has been set.
  1629. * @param millis milliseconds of the date fields
  1630. * @param millisInDay milliseconds of the time fields; may be out
  1631. * or range.
  1632. * @param ec Output param set to failure code on function return
  1633. * when this function fails.
  1634. * @internal
  1635. */
  1636. int32_t computeZoneOffset(double millis, double millisInDay, UErrorCode &ec);
  1637. /**
  1638. * Determine the best stamp in a range.
  1639. * @param start first enum to look at
  1640. * @param end last enum to look at
  1641. * @param bestSoFar stamp prior to function call
  1642. * @return the stamp value of the best stamp
  1643. * @internal
  1644. */
  1645. int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
  1646. /**
  1647. * Marker for end of resolve set (row or group). Value for field resolution tables.
  1648. *
  1649. * @see #resolveFields
  1650. * @internal
  1651. */
  1652. static constexpr int32_t kResolveSTOP = -1;
  1653. /**
  1654. * Value to be bitwised "ORed" against resolve table field values for remapping.
  1655. * Example: (UCAL_DATE | kResolveRemap) in 1st column will cause 'UCAL_DATE' to be returned,
  1656. * but will not examine the value of UCAL_DATE.
  1657. * Value for field resolution tables.
  1658. *
  1659. * @see #resolveFields
  1660. * @internal
  1661. */
  1662. static constexpr int32_t kResolveRemap = 32;
  1663. /**
  1664. * Precedence table for Dates
  1665. * @see #resolveFields
  1666. * @internal
  1667. */
  1668. static const UFieldResolutionTable kDatePrecedence[];
  1669. /**
  1670. * Precedence table for Year
  1671. * @see #resolveFields
  1672. * @internal
  1673. */
  1674. static const UFieldResolutionTable kYearPrecedence[];
  1675. /**
  1676. * Precedence table for Day of Week
  1677. * @see #resolveFields
  1678. * @internal
  1679. */
  1680. static const UFieldResolutionTable kDOWPrecedence[];
  1681. /**
  1682. * Precedence table for Months
  1683. * @see #resolveFields
  1684. * @internal
  1685. */
  1686. static const UFieldResolutionTable kMonthPrecedence[];
  1687. /**
  1688. * Given a precedence table, return the newest field combination in
  1689. * the table, or UCAL_FIELD_COUNT if none is found.
  1690. *
  1691. * <p>The precedence table is a 3-dimensional array of integers. It
  1692. * may be thought of as an array of groups. Each group is an array of
  1693. * lines. Each line is an array of field numbers. Within a line, if
  1694. * all fields are set, then the time stamp of the line is taken to be
  1695. * the stamp of the most recently set field. If any field of a line is
  1696. * unset, then the line fails to match. Within a group, the line with
  1697. * the newest time stamp is selected. The first field of the line is
  1698. * returned to indicate which line matched.
  1699. *
  1700. * <p>In some cases, it may be desirable to map a line to field that
  1701. * whose stamp is NOT examined. For example, if the best field is
  1702. * DAY_OF_WEEK then the DAY_OF_WEEK_IN_MONTH algorithm may be used. In
  1703. * order to do this, insert the value <code>kResolveRemap | F</code> at
  1704. * the start of the line, where <code>F</code> is the desired return
  1705. * field value. This field will NOT be examined; it only determines
  1706. * the return value if the other fields in the line are the newest.
  1707. *
  1708. * <p>If all lines of a group contain at least one unset field, then no
  1709. * line will match, and the group as a whole will fail to match. In
  1710. * that case, the next group will be processed. If all groups fail to
  1711. * match, then UCAL_FIELD_COUNT is returned.
  1712. * @internal
  1713. */
  1714. UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable) const;
  1715. #endif /* U_HIDE_INTERNAL_API */
  1716. /**
  1717. * @internal
  1718. */
  1719. virtual const UFieldResolutionTable* getFieldResolutionTable() const;
  1720. #ifndef U_HIDE_INTERNAL_API
  1721. /**
  1722. * Return the field that is newer, either defaultField, or
  1723. * alternateField. If neither is newer or neither is set, return defaultField.
  1724. * @internal
  1725. */
  1726. UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
  1727. #endif /* U_HIDE_INTERNAL_API */
  1728. private:
  1729. /**
  1730. * Helper function for calculating limits by trial and error
  1731. * @param field The field being investigated
  1732. * @param startValue starting (least max) value of field
  1733. * @param endValue ending (greatest max) value of field
  1734. * @param status return type
  1735. * @internal (private)
  1736. */
  1737. int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
  1738. protected:
  1739. /**
  1740. * The flag which indicates if the current time is set in the calendar.
  1741. * @stable ICU 2.0
  1742. */
  1743. UBool fIsTimeSet;
  1744. /**
  1745. * True if the fields are in sync with the currently set time of this Calendar.
  1746. * If false, then the next attempt to get the value of a field will
  1747. * force a recomputation of all fields from the current value of the time
  1748. * field.
  1749. * <P>
  1750. * This should really be named areFieldsInSync, but the old name is retained
  1751. * for backward compatibility.
  1752. * @stable ICU 2.0
  1753. */
  1754. UBool fAreFieldsSet;
  1755. /**
  1756. * True if all of the fields have been set. This is initially false, and set to
  1757. * true by computeFields().
  1758. * @stable ICU 2.0
  1759. */
  1760. UBool fAreAllFieldsSet;
  1761. /**
  1762. * True if all fields have been virtually set, but have not yet been
  1763. * computed. This occurs only in setTimeInMillis(). A calendar set
  1764. * to this state will compute all fields from the time if it becomes
  1765. * necessary, but otherwise will delay such computation.
  1766. * @stable ICU 3.0
  1767. */
  1768. UBool fAreFieldsVirtuallySet;
  1769. /**
  1770. * Get the current time without recomputing.
  1771. *
  1772. * @return the current time without recomputing.
  1773. * @stable ICU 2.0
  1774. */
  1775. UDate internalGetTime(void) const { return fTime; }
  1776. /**
  1777. * Set the current time without affecting flags or fields.
  1778. *
  1779. * @param time The time to be set
  1780. * @return the current time without recomputing.
  1781. * @stable ICU 2.0
  1782. */
  1783. void internalSetTime(UDate time) { fTime = time; }
  1784. /**
  1785. * The time fields containing values into which the millis is computed.
  1786. * @stable ICU 2.0
  1787. */
  1788. int32_t fFields[UCAL_FIELD_COUNT];
  1789. #ifndef U_FORCE_HIDE_DEPRECATED_API
  1790. /**
  1791. * The flags which tell if a specified time field for the calendar is set.
  1792. * @deprecated ICU 2.8 use (fStamp[n]!=kUnset)
  1793. */
  1794. UBool fIsSet[UCAL_FIELD_COUNT];
  1795. #endif // U_FORCE_HIDE_DEPRECATED_API
  1796. /** Special values of stamp[]
  1797. * @stable ICU 2.0
  1798. */
  1799. enum {
  1800. kUnset = 0,
  1801. kInternallySet,
  1802. kMinimumUserStamp
  1803. };
  1804. /**
  1805. * Pseudo-time-stamps which specify when each field was set. There
  1806. * are two special values, UNSET and INTERNALLY_SET. Values from
  1807. * MINIMUM_USER_SET to Integer.MAX_VALUE are legal user set values.
  1808. * @stable ICU 2.0
  1809. */
  1810. int32_t fStamp[UCAL_FIELD_COUNT];
  1811. /**
  1812. * Subclasses may override this method to compute several fields
  1813. * specific to each calendar system. These are:
  1814. *
  1815. * <ul><li>ERA
  1816. * <li>YEAR
  1817. * <li>MONTH
  1818. * <li>DAY_OF_MONTH
  1819. * <li>DAY_OF_YEAR
  1820. * <li>EXTENDED_YEAR</ul>
  1821. *
  1822. * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields, which
  1823. * will be set when this method is called. Subclasses can also call
  1824. * the getGregorianXxx() methods to obtain Gregorian calendar
  1825. * equivalents for the given Julian day.
  1826. *
  1827. * <p>In addition, subclasses should compute any subclass-specific
  1828. * fields, that is, fields from BASE_FIELD_COUNT to
  1829. * getFieldCount() - 1.
  1830. *
  1831. * <p>The default implementation in <code>Calendar</code> implements
  1832. * a pure proleptic Gregorian calendar.
  1833. * @internal
  1834. */
  1835. virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
  1836. #ifndef U_HIDE_INTERNAL_API
  1837. /**
  1838. * Return the extended year on the Gregorian calendar as computed by
  1839. * <code>computeGregorianFields()</code>.
  1840. * @internal
  1841. */
  1842. int32_t getGregorianYear() const {
  1843. return fGregorianYear;
  1844. }
  1845. /**
  1846. * Return the month (0-based) on the Gregorian calendar as computed by
  1847. * <code>computeGregorianFields()</code>.
  1848. * @internal
  1849. */
  1850. int32_t getGregorianMonth() const {
  1851. return fGregorianMonth;
  1852. }
  1853. /**
  1854. * Return the day of year (1-based) on the Gregorian calendar as
  1855. * computed by <code>computeGregorianFields()</code>.
  1856. * @internal
  1857. */
  1858. int32_t getGregorianDayOfYear() const {
  1859. return fGregorianDayOfYear;
  1860. }
  1861. /**
  1862. * Return the day of month (1-based) on the Gregorian calendar as
  1863. * computed by <code>computeGregorianFields()</code>.
  1864. * @internal
  1865. */
  1866. int32_t getGregorianDayOfMonth() const {
  1867. return fGregorianDayOfMonth;
  1868. }
  1869. #endif /* U_HIDE_INTERNAL_API */
  1870. /**
  1871. * Called by computeJulianDay. Returns the default month (0-based) for the year,
  1872. * taking year and era into account. Defaults to 0 for Gregorian, which doesn't care.
  1873. * @param eyear The extended year
  1874. * @internal
  1875. */
  1876. virtual int32_t getDefaultMonthInYear(int32_t eyear) ;
  1877. /**
  1878. * Called by computeJulianDay. Returns the default day (1-based) for the month,
  1879. * taking currently-set year and era into account. Defaults to 1 for Gregorian.
  1880. * @param eyear the extended year
  1881. * @param month the month in the year
  1882. * @internal
  1883. */
  1884. virtual int32_t getDefaultDayInMonth(int32_t eyear, int32_t month);
  1885. //-------------------------------------------------------------------------
  1886. // Protected utility methods for use by subclasses. These are very handy
  1887. // for implementing add, roll, and computeFields.
  1888. //-------------------------------------------------------------------------
  1889. /**
  1890. * Adjust the specified field so that it is within
  1891. * the allowable range for the date to which this calendar is set.
  1892. * For example, in a Gregorian calendar pinning the {@link #UCalendarDateFields DAY_OF_MONTH}
  1893. * field for a calendar set to April 31 would cause it to be set
  1894. * to April 30.
  1895. * <p>
  1896. * <b>Subclassing:</b>
  1897. * <br>
  1898. * This utility method is intended for use by subclasses that need to implement
  1899. * their own overrides of {@link #roll roll} and {@link #add add}.
  1900. * <p>
  1901. * <b>Note:</b>
  1902. * <code>pinField</code> is implemented in terms of
  1903. * {@link #getActualMinimum getActualMinimum}
  1904. * and {@link #getActualMaximum getActualMaximum}. If either of those methods uses
  1905. * a slow, iterative algorithm for a particular field, it would be
  1906. * unwise to attempt to call <code>pinField</code> for that field. If you
  1907. * really do need to do so, you should override this method to do
  1908. * something more efficient for that field.
  1909. * <p>
  1910. * @param field The calendar field whose value should be pinned.
  1911. * @param status Output param set to failure code on function return
  1912. * when this function fails.
  1913. *
  1914. * @see #getActualMinimum
  1915. * @see #getActualMaximum
  1916. * @stable ICU 2.0
  1917. */
  1918. virtual void pinField(UCalendarDateFields field, UErrorCode& status);
  1919. /**
  1920. * Return the week number of a day, within a period. This may be the week number in
  1921. * a year or the week number in a month. Usually this will be a value >= 1, but if
  1922. * some initial days of the period are excluded from week 1, because
  1923. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1, then
  1924. * the week number will be zero for those
  1925. * initial days. This method requires the day number and day of week for some
  1926. * known date in the period in order to determine the day of week
  1927. * on the desired day.
  1928. * <p>
  1929. * <b>Subclassing:</b>
  1930. * <br>
  1931. * This method is intended for use by subclasses in implementing their
  1932. * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
  1933. * It is often useful in {@link #getActualMinimum getActualMinimum} and
  1934. * {@link #getActualMaximum getActualMaximum} as well.
  1935. * <p>
  1936. * This variant is handy for computing the week number of some other
  1937. * day of a period (often the first or last day of the period) when its day
  1938. * of the week is not known but the day number and day of week for some other
  1939. * day in the period (e.g. the current date) <em>is</em> known.
  1940. * <p>
  1941. * @param desiredDay The {@link #UCalendarDateFields DAY_OF_YEAR} or
  1942. * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
  1943. * Should be 1 for the first day of the period.
  1944. *
  1945. * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR}
  1946. * or {@link #UCalendarDateFields DAY_OF_MONTH} for a day in the period whose
  1947. * {@link #UCalendarDateFields DAY_OF_WEEK} is specified by the
  1948. * <code>knownDayOfWeek</code> parameter.
  1949. * Should be 1 for first day of period.
  1950. *
  1951. * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
  1952. * corresponding to the <code>knownDayOfPeriod</code> parameter.
  1953. * 1-based with 1=Sunday.
  1954. *
  1955. * @return The week number (one-based), or zero if the day falls before
  1956. * the first week because
  1957. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
  1958. * is more than one.
  1959. *
  1960. * @stable ICU 2.8
  1961. */
  1962. int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
  1963. #ifndef U_HIDE_INTERNAL_API
  1964. /**
  1965. * Return the week number of a day, within a period. This may be the week number in
  1966. * a year, or the week number in a month. Usually this will be a value >= 1, but if
  1967. * some initial days of the period are excluded from week 1, because
  1968. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1,
  1969. * then the week number will be zero for those
  1970. * initial days. This method requires the day of week for the given date in order to
  1971. * determine the result.
  1972. * <p>
  1973. * <b>Subclassing:</b>
  1974. * <br>
  1975. * This method is intended for use by subclasses in implementing their
  1976. * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
  1977. * It is often useful in {@link #getActualMinimum getActualMinimum} and
  1978. * {@link #getActualMaximum getActualMaximum} as well.
  1979. * <p>
  1980. * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR} or
  1981. * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
  1982. * Should be 1 for the first day of the period.
  1983. *
  1984. * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
  1985. * corresponding to the <code>dayOfPeriod</code> parameter.
  1986. * 1-based with 1=Sunday.
  1987. *
  1988. * @return The week number (one-based), or zero if the day falls before
  1989. * the first week because
  1990. * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
  1991. * is more than one.
  1992. * @internal
  1993. */
  1994. inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
  1995. /**
  1996. * returns the local DOW, valid range 0..6
  1997. * @internal
  1998. */
  1999. int32_t getLocalDOW();
  2000. #endif /* U_HIDE_INTERNAL_API */
  2001. private:
  2002. /**
  2003. * The next available value for fStamp[]
  2004. */
  2005. int32_t fNextStamp;// = MINIMUM_USER_STAMP;
  2006. /**
  2007. * Recalculates the time stamp array (fStamp).
  2008. * Resets fNextStamp to lowest next stamp value.
  2009. */
  2010. void recalculateStamp();
  2011. /**
  2012. * The current time set for the calendar.
  2013. */
  2014. UDate fTime;
  2015. /**
  2016. * @see #setLenient
  2017. */
  2018. UBool fLenient;
  2019. /**
  2020. * Time zone affects the time calculation done by Calendar. Calendar subclasses use
  2021. * the time zone data to produce the local time. Always set; never nullptr.
  2022. */
  2023. TimeZone* fZone;
  2024. /**
  2025. * Option for repeated wall time
  2026. * @see #setRepeatedWallTimeOption
  2027. */
  2028. UCalendarWallTimeOption fRepeatedWallTime;
  2029. /**
  2030. * Option for skipped wall time
  2031. * @see #setSkippedWallTimeOption
  2032. */
  2033. UCalendarWallTimeOption fSkippedWallTime;
  2034. /**
  2035. * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent. They are
  2036. * used to figure out the week count for a specific date for a given locale. These
  2037. * must be set when a Calendar is constructed. For example, in US locale,
  2038. * firstDayOfWeek is SUNDAY; minimalDaysInFirstWeek is 1. They are used to figure
  2039. * out the week count for a specific date for a given locale. These must be set when
  2040. * a Calendar is constructed.
  2041. */
  2042. UCalendarDaysOfWeek fFirstDayOfWeek;
  2043. uint8_t fMinimalDaysInFirstWeek;
  2044. UCalendarDaysOfWeek fWeekendOnset;
  2045. int32_t fWeekendOnsetMillis;
  2046. UCalendarDaysOfWeek fWeekendCease;
  2047. int32_t fWeekendCeaseMillis;
  2048. /**
  2049. * Sets firstDayOfWeek and minimalDaysInFirstWeek. Called at Calendar construction
  2050. * time.
  2051. *
  2052. * @param desiredLocale The given locale.
  2053. * @param type The calendar type identifier, e.g: gregorian, buddhist, etc.
  2054. * @param success Indicates the status of setting the week count data from
  2055. * the resource for the given locale. Returns U_ZERO_ERROR if
  2056. * constructed successfully.
  2057. */
  2058. void setWeekData(const Locale& desiredLocale, const char *type, UErrorCode& success);
  2059. /**
  2060. * Recompute the time and update the status fields isTimeSet
  2061. * and areFieldsSet. Callers should check isTimeSet and only
  2062. * call this method if isTimeSet is false.
  2063. *
  2064. * @param status Output param set to success/failure code on exit. If any value
  2065. * previously set in the time field is invalid or restricted by
  2066. * leniency, this will be set to an error status.
  2067. */
  2068. void updateTime(UErrorCode& status);
  2069. /**
  2070. * The Gregorian year, as computed by computeGregorianFields() and
  2071. * returned by getGregorianYear().
  2072. * @see #computeGregorianFields
  2073. */
  2074. int32_t fGregorianYear;
  2075. /**
  2076. * The Gregorian month, as computed by computeGregorianFields() and
  2077. * returned by getGregorianMonth().
  2078. * @see #computeGregorianFields
  2079. */
  2080. int32_t fGregorianMonth;
  2081. /**
  2082. * The Gregorian day of the year, as computed by
  2083. * computeGregorianFields() and returned by getGregorianDayOfYear().
  2084. * @see #computeGregorianFields
  2085. */
  2086. int32_t fGregorianDayOfYear;
  2087. /**
  2088. * The Gregorian day of the month, as computed by
  2089. * computeGregorianFields() and returned by getGregorianDayOfMonth().
  2090. * @see #computeGregorianFields
  2091. */
  2092. int32_t fGregorianDayOfMonth;
  2093. /* calculations */
  2094. /**
  2095. * Compute the Gregorian calendar year, month, and day of month from
  2096. * the given Julian day. These values are not stored in fields, but in
  2097. * member variables gregorianXxx. Also compute the DAY_OF_WEEK and
  2098. * DOW_LOCAL fields.
  2099. */
  2100. void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
  2101. protected:
  2102. /**
  2103. * Compute the Gregorian calendar year, month, and day of month from the
  2104. * Julian day. These values are not stored in fields, but in member
  2105. * variables gregorianXxx. They are used for time zone computations and by
  2106. * subclasses that are Gregorian derivatives. Subclasses may call this
  2107. * method to perform a Gregorian calendar millis->fields computation.
  2108. */
  2109. void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
  2110. private:
  2111. /**
  2112. * Compute the fields WEEK_OF_YEAR, YEAR_WOY, WEEK_OF_MONTH,
  2113. * DAY_OF_WEEK_IN_MONTH, and DOW_LOCAL from EXTENDED_YEAR, YEAR,
  2114. * DAY_OF_WEEK, and DAY_OF_YEAR. The latter fields are computed by the
  2115. * subclass based on the calendar system.
  2116. *
  2117. * <p>The YEAR_WOY field is computed simplistically. It is equal to YEAR
  2118. * most of the time, but at the year boundary it may be adjusted to YEAR-1
  2119. * or YEAR+1 to reflect the overlap of a week into an adjacent year. In
  2120. * this case, a simple increment or decrement is performed on YEAR, even
  2121. * though this may yield an invalid YEAR value. For instance, if the YEAR
  2122. * is part of a calendar system with an N-year cycle field CYCLE, then
  2123. * incrementing the YEAR may involve incrementing CYCLE and setting YEAR
  2124. * back to 0 or 1. This is not handled by this code, and in fact cannot be
  2125. * simply handled without having subclasses define an entire parallel set of
  2126. * fields for fields larger than or equal to a year. This additional
  2127. * complexity is not warranted, since the intention of the YEAR_WOY field is
  2128. * to support ISO 8601 notation, so it will typically be used with a
  2129. * proleptic Gregorian calendar, which has no field larger than a year.
  2130. */
  2131. void computeWeekFields(UErrorCode &ec);
  2132. /**
  2133. * Ensure that each field is within its valid range by calling {@link
  2134. * #validateField(int, int&)} on each field that has been set. This method
  2135. * should only be called if this calendar is not lenient.
  2136. * @see #isLenient
  2137. * @see #validateField(int, int&)
  2138. */
  2139. void validateFields(UErrorCode &status);
  2140. /**
  2141. * Validate a single field of this calendar given its minimum and
  2142. * maximum allowed value. If the field is out of range,
  2143. * <code>U_ILLEGAL_ARGUMENT_ERROR</code> will be set. Subclasses may
  2144. * use this method in their implementation of {@link
  2145. * #validateField(int, int&)}.
  2146. */
  2147. void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
  2148. protected:
  2149. #ifndef U_HIDE_INTERNAL_API
  2150. /**
  2151. * Convert a quasi Julian date to the day of the week. The Julian date used here is
  2152. * not a true Julian date, since it is measured from midnight, not noon. Return
  2153. * value is one-based.
  2154. *
  2155. * @param julian The given Julian date number.
  2156. * @return Day number from 1..7 (SUN..SAT).
  2157. * @internal
  2158. */
  2159. static uint8_t julianDayToDayOfWeek(double julian);
  2160. #endif /* U_HIDE_INTERNAL_API */
  2161. private:
  2162. char validLocale[ULOC_FULLNAME_CAPACITY];
  2163. char actualLocale[ULOC_FULLNAME_CAPACITY];
  2164. public:
  2165. #if !UCONFIG_NO_SERVICE
  2166. /**
  2167. * INTERNAL FOR 2.6 -- Registration.
  2168. */
  2169. #ifndef U_HIDE_INTERNAL_API
  2170. /**
  2171. * Return a StringEnumeration over the locales available at the time of the call,
  2172. * including registered locales.
  2173. * @return a StringEnumeration over the locales available at the time of the call
  2174. * @internal
  2175. */
  2176. static StringEnumeration* getAvailableLocales(void);
  2177. /**
  2178. * Register a new Calendar factory. The factory will be adopted.
  2179. * INTERNAL in 2.6
  2180. *
  2181. * Because ICU may choose to cache Calendars internally, this must
  2182. * be called at application startup, prior to any calls to
  2183. * Calendar::createInstance to avoid undefined behavior.
  2184. *
  2185. * @param toAdopt the factory instance to be adopted
  2186. * @param status the in/out status code, no special meanings are assigned
  2187. * @return a registry key that can be used to unregister this factory
  2188. * @internal
  2189. */
  2190. static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
  2191. /**
  2192. * Unregister a previously-registered CalendarFactory using the key returned from the
  2193. * register call. Key becomes invalid after a successful call and should not be used again.
  2194. * The CalendarFactory corresponding to the key will be deleted.
  2195. * INTERNAL in 2.6
  2196. *
  2197. * Because ICU may choose to cache Calendars internally, this should
  2198. * be called during application shutdown, after all calls to
  2199. * Calendar::createInstance to avoid undefined behavior.
  2200. *
  2201. * @param key the registry key returned by a previous call to registerFactory
  2202. * @param status the in/out status code, no special meanings are assigned
  2203. * @return true if the factory for the key was successfully unregistered
  2204. * @internal
  2205. */
  2206. static UBool unregister(URegistryKey key, UErrorCode& status);
  2207. #endif /* U_HIDE_INTERNAL_API */
  2208. /**
  2209. * Multiple Calendar Implementation
  2210. * @internal
  2211. */
  2212. friend class CalendarFactory;
  2213. /**
  2214. * Multiple Calendar Implementation
  2215. * @internal
  2216. */
  2217. friend class CalendarService;
  2218. /**
  2219. * Multiple Calendar Implementation
  2220. * @internal
  2221. */
  2222. friend class DefaultCalendarFactory;
  2223. #endif /* !UCONFIG_NO_SERVICE */
  2224. /**
  2225. * @return true if this calendar has a default century (i.e. 03 -> 2003)
  2226. * @internal
  2227. */
  2228. virtual UBool haveDefaultCentury() const = 0;
  2229. /**
  2230. * @return the start of the default century, as a UDate
  2231. * @internal
  2232. */
  2233. virtual UDate defaultCenturyStart() const = 0;
  2234. /**
  2235. * @return the beginning year of the default century, as a year
  2236. * @internal
  2237. */
  2238. virtual int32_t defaultCenturyStartYear() const = 0;
  2239. /** Get the locale for this calendar object. You can choose between valid and actual locale.
  2240. * @param type type of the locale we're looking for (valid or actual)
  2241. * @param status error code for the operation
  2242. * @return the locale
  2243. * @stable ICU 2.8
  2244. */
  2245. Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
  2246. /**
  2247. * @return The related Gregorian year; will be obtained by modifying the value
  2248. * obtained by get from UCAL_EXTENDED_YEAR field
  2249. * @internal
  2250. */
  2251. virtual int32_t getRelatedYear(UErrorCode &status) const;
  2252. /**
  2253. * @param year The related Gregorian year to set; will be modified as necessary then
  2254. * set in UCAL_EXTENDED_YEAR field
  2255. * @internal
  2256. */
  2257. virtual void setRelatedYear(int32_t year);
  2258. #ifndef U_HIDE_INTERNAL_API
  2259. /** Get the locale for this calendar object. You can choose between valid and actual locale.
  2260. * @param type type of the locale we're looking for (valid or actual)
  2261. * @param status error code for the operation
  2262. * @return the locale
  2263. * @internal
  2264. */
  2265. const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
  2266. #endif /* U_HIDE_INTERNAL_API */
  2267. private:
  2268. /**
  2269. * Cast TimeZone used by this object to BasicTimeZone, or nullptr if the TimeZone
  2270. * is not an instance of BasicTimeZone.
  2271. */
  2272. BasicTimeZone* getBasicTimeZone() const;
  2273. /**
  2274. * Find the previous zone transition near the given time.
  2275. * @param base The base time, inclusive
  2276. * @param transitionTime Receives the result time
  2277. * @param status The error status
  2278. * @return true if a transition is found.
  2279. */
  2280. UBool getImmediatePreviousZoneTransition(UDate base, UDate *transitionTime, UErrorCode& status) const;
  2281. public:
  2282. #ifndef U_HIDE_INTERNAL_API
  2283. /**
  2284. * Creates a new Calendar from a Locale for the cache.
  2285. * This method does not set the time or timezone in returned calendar.
  2286. * @param locale the locale.
  2287. * @param status any error returned here.
  2288. * @return the new Calendar object with no time or timezone set.
  2289. * @internal For ICU use only.
  2290. */
  2291. static Calendar * U_EXPORT2 makeInstance(
  2292. const Locale &locale, UErrorCode &status);
  2293. /**
  2294. * Get the calendar type for given locale.
  2295. * @param locale the locale
  2296. * @param typeBuffer calendar type returned here
  2297. * @param typeBufferSize The size of typeBuffer in bytes. If the type
  2298. * can't fit in the buffer, this method sets status to
  2299. * U_BUFFER_OVERFLOW_ERROR
  2300. * @param status error, if any, returned here.
  2301. * @internal For ICU use only.
  2302. */
  2303. static void U_EXPORT2 getCalendarTypeFromLocale(
  2304. const Locale &locale,
  2305. char *typeBuffer,
  2306. int32_t typeBufferSize,
  2307. UErrorCode &status);
  2308. #endif /* U_HIDE_INTERNAL_API */
  2309. };
  2310. // -------------------------------------
  2311. inline Calendar*
  2312. Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
  2313. {
  2314. // since the Locale isn't specified, use the default locale
  2315. return createInstance(zone, Locale::getDefault(), errorCode);
  2316. }
  2317. // -------------------------------------
  2318. inline void
  2319. Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
  2320. {
  2321. roll(field, (int32_t)(up ? +1 : -1), status);
  2322. }
  2323. #ifndef U_HIDE_DEPRECATED_API
  2324. inline void
  2325. Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
  2326. {
  2327. roll((UCalendarDateFields) field, up, status);
  2328. }
  2329. #endif /* U_HIDE_DEPRECATED_API */
  2330. // -------------------------------------
  2331. /**
  2332. * Fast method for subclasses. The caller must maintain fUserSetDSTOffset and
  2333. * fUserSetZoneOffset, as well as the isSet[] array.
  2334. */
  2335. inline void
  2336. Calendar::internalSet(UCalendarDateFields field, int32_t value)
  2337. {
  2338. fFields[field] = value;
  2339. fStamp[field] = kInternallySet;
  2340. fIsSet[field] = true; // Remove later
  2341. }
  2342. #ifndef U_HIDE_INTERNAL_API
  2343. inline int32_t Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
  2344. {
  2345. return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
  2346. }
  2347. #endif /* U_HIDE_INTERNAL_API */
  2348. U_NAMESPACE_END
  2349. #endif /* #if !UCONFIG_NO_FORMATTING */
  2350. #endif /* U_SHOW_CPLUSPLUS_API */
  2351. #endif // _CALENDAR