gregocal.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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-2016, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. *******************************************************************************
  8. *
  9. * File GREGOCAL.CPP
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 02/05/97 clhuang Creation.
  15. * 03/28/97 aliu Made highly questionable fix to computeFields to
  16. * handle DST correctly.
  17. * 04/22/97 aliu Cleaned up code drastically. Added monthLength().
  18. * Finished unimplemented parts of computeTime() for
  19. * week-based date determination. Removed quetionable
  20. * fix and wrote correct fix for computeFields() and
  21. * daylight time handling. Rewrote inDaylightTime()
  22. * and computeFields() to handle sensitive Daylight to
  23. * Standard time transitions correctly.
  24. * 05/08/97 aliu Added code review changes. Fixed isLeapYear() to
  25. * not cutover.
  26. * 08/12/97 aliu Added equivalentTo. Misc other fixes. Updated
  27. * add() from Java source.
  28. * 07/28/98 stephen Sync up with JDK 1.2
  29. * 09/14/98 stephen Changed type of kOneDay, kOneWeek to double.
  30. * Fixed bug in roll()
  31. * 10/15/99 aliu Fixed j31, incorrect WEEK_OF_YEAR computation.
  32. * 10/15/99 aliu Fixed j32, cannot set date to Feb 29 2000 AD.
  33. * {JDK bug 4210209 4209272}
  34. * 11/15/99 weiv Added YEAR_WOY and DOW_LOCAL computation
  35. * to timeToFields method, updated kMinValues, kMaxValues & kLeastMaxValues
  36. * 12/09/99 aliu Fixed j81, calculation errors and roll bugs
  37. * in year of cutover.
  38. * 01/24/2000 aliu Revised computeJulianDay for YEAR YEAR_WOY WOY.
  39. ********************************************************************************
  40. */
  41. #include "unicode/utypes.h"
  42. #include <float.h>
  43. #if !UCONFIG_NO_FORMATTING
  44. #include "unicode/gregocal.h"
  45. #include "gregoimp.h"
  46. #include "umutex.h"
  47. #include "uassert.h"
  48. // *****************************************************************************
  49. // class GregorianCalendar
  50. // *****************************************************************************
  51. /**
  52. * Note that the Julian date used here is not a true Julian date, since
  53. * it is measured from midnight, not noon. This value is the Julian
  54. * day number of January 1, 1970 (Gregorian calendar) at noon UTC. [LIU]
  55. */
  56. static const int16_t kNumDays[]
  57. = {0,31,59,90,120,151,181,212,243,273,304,334}; // 0-based, for day-in-year
  58. static const int16_t kLeapNumDays[]
  59. = {0,31,60,91,121,152,182,213,244,274,305,335}; // 0-based, for day-in-year
  60. static const int8_t kMonthLength[]
  61. = {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based
  62. static const int8_t kLeapMonthLength[]
  63. = {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based
  64. // setTimeInMillis() limits the Julian day range to +/-7F000000.
  65. // This would seem to limit the year range to:
  66. // ms=+183882168921600000 jd=7f000000 December 20, 5828963 AD
  67. // ms=-184303902528000000 jd=81000000 September 20, 5838270 BC
  68. // HOWEVER, CalendarRegressionTest/Test4167060 shows that the actual
  69. // range limit on the year field is smaller (~ +/-140000). [alan 3.0]
  70. static const int32_t kGregorianCalendarLimits[UCAL_FIELD_COUNT][4] = {
  71. // Minimum Greatest Least Maximum
  72. // Minimum Maximum
  73. { 0, 0, 1, 1}, // ERA
  74. { 1, 1, 140742, 144683}, // YEAR
  75. { 0, 0, 11, 11}, // MONTH
  76. { 1, 1, 52, 53}, // WEEK_OF_YEAR
  77. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
  78. { 1, 1, 28, 31}, // DAY_OF_MONTH
  79. { 1, 1, 365, 366}, // DAY_OF_YEAR
  80. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
  81. { -1, -1, 4, 5}, // DAY_OF_WEEK_IN_MONTH
  82. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
  83. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
  84. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
  85. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
  86. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
  87. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
  88. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
  89. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
  90. { -140742, -140742, 140742, 144683}, // YEAR_WOY
  91. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
  92. { -140742, -140742, 140742, 144683}, // EXTENDED_YEAR
  93. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
  94. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
  95. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
  96. { 0, 0, 11, 11}, // ORDINAL_MONTH
  97. };
  98. /*
  99. * <pre>
  100. * Greatest Least
  101. * Field name Minimum Minimum Maximum Maximum
  102. * ---------- ------- ------- ------- -------
  103. * ERA 0 0 1 1
  104. * YEAR 1 1 140742 144683
  105. * MONTH 0 0 11 11
  106. * WEEK_OF_YEAR 1 1 52 53
  107. * WEEK_OF_MONTH 0 0 4 6
  108. * DAY_OF_MONTH 1 1 28 31
  109. * DAY_OF_YEAR 1 1 365 366
  110. * DAY_OF_WEEK 1 1 7 7
  111. * DAY_OF_WEEK_IN_MONTH -1 -1 4 5
  112. * AM_PM 0 0 1 1
  113. * HOUR 0 0 11 11
  114. * HOUR_OF_DAY 0 0 23 23
  115. * MINUTE 0 0 59 59
  116. * SECOND 0 0 59 59
  117. * MILLISECOND 0 0 999 999
  118. * ZONE_OFFSET -12* -12* 12* 12*
  119. * DST_OFFSET 0 0 1* 1*
  120. * YEAR_WOY 1 1 140742 144683
  121. * DOW_LOCAL 1 1 7 7
  122. * </pre>
  123. * (*) In units of one-hour
  124. */
  125. #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
  126. #include <stdio.h>
  127. #endif
  128. U_NAMESPACE_BEGIN
  129. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(GregorianCalendar)
  130. // 00:00:00 UTC, October 15, 1582, expressed in ms from the epoch.
  131. // Note that only Italy and other Catholic countries actually
  132. // observed this cutover. Most other countries followed in
  133. // the next few centuries, some as late as 1928. [LIU]
  134. // in Java, -12219292800000L
  135. //const UDate GregorianCalendar::kPapalCutover = -12219292800000L;
  136. static const uint32_t kCutoverJulianDay = 2299161;
  137. static const UDate kPapalCutover = (2299161.0 - kEpochStartAsJulianDay) * U_MILLIS_PER_DAY;
  138. //static const UDate kPapalCutoverJulian = (2299161.0 - kEpochStartAsJulianDay);
  139. // -------------------------------------
  140. GregorianCalendar::GregorianCalendar(UErrorCode& status)
  141. : Calendar(status),
  142. fGregorianCutover(kPapalCutover),
  143. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  144. fIsGregorian(true), fInvertGregorian(false)
  145. {
  146. setTimeInMillis(getNow(), status);
  147. }
  148. // -------------------------------------
  149. GregorianCalendar::GregorianCalendar(TimeZone* zone, UErrorCode& status)
  150. : Calendar(zone, Locale::getDefault(), status),
  151. fGregorianCutover(kPapalCutover),
  152. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  153. fIsGregorian(true), fInvertGregorian(false)
  154. {
  155. setTimeInMillis(getNow(), status);
  156. }
  157. // -------------------------------------
  158. GregorianCalendar::GregorianCalendar(const TimeZone& zone, UErrorCode& status)
  159. : Calendar(zone, Locale::getDefault(), status),
  160. fGregorianCutover(kPapalCutover),
  161. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  162. fIsGregorian(true), fInvertGregorian(false)
  163. {
  164. setTimeInMillis(getNow(), status);
  165. }
  166. // -------------------------------------
  167. GregorianCalendar::GregorianCalendar(const Locale& aLocale, UErrorCode& status)
  168. : Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, status),
  169. fGregorianCutover(kPapalCutover),
  170. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  171. fIsGregorian(true), fInvertGregorian(false)
  172. {
  173. setTimeInMillis(getNow(), status);
  174. }
  175. // -------------------------------------
  176. GregorianCalendar::GregorianCalendar(TimeZone* zone, const Locale& aLocale,
  177. UErrorCode& status)
  178. : Calendar(zone, aLocale, status),
  179. fGregorianCutover(kPapalCutover),
  180. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  181. fIsGregorian(true), fInvertGregorian(false)
  182. {
  183. setTimeInMillis(getNow(), status);
  184. }
  185. // -------------------------------------
  186. GregorianCalendar::GregorianCalendar(const TimeZone& zone, const Locale& aLocale,
  187. UErrorCode& status)
  188. : Calendar(zone, aLocale, status),
  189. fGregorianCutover(kPapalCutover),
  190. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  191. fIsGregorian(true), fInvertGregorian(false)
  192. {
  193. setTimeInMillis(getNow(), status);
  194. }
  195. // -------------------------------------
  196. GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date,
  197. UErrorCode& status)
  198. : Calendar(TimeZone::createDefault(), Locale::getDefault(), status),
  199. fGregorianCutover(kPapalCutover),
  200. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  201. fIsGregorian(true), fInvertGregorian(false)
  202. {
  203. set(UCAL_ERA, AD);
  204. set(UCAL_YEAR, year);
  205. set(UCAL_MONTH, month);
  206. set(UCAL_DATE, date);
  207. }
  208. // -------------------------------------
  209. GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date,
  210. int32_t hour, int32_t minute, UErrorCode& status)
  211. : Calendar(TimeZone::createDefault(), Locale::getDefault(), status),
  212. fGregorianCutover(kPapalCutover),
  213. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  214. fIsGregorian(true), fInvertGregorian(false)
  215. {
  216. set(UCAL_ERA, AD);
  217. set(UCAL_YEAR, year);
  218. set(UCAL_MONTH, month);
  219. set(UCAL_DATE, date);
  220. set(UCAL_HOUR_OF_DAY, hour);
  221. set(UCAL_MINUTE, minute);
  222. }
  223. // -------------------------------------
  224. GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date,
  225. int32_t hour, int32_t minute, int32_t second,
  226. UErrorCode& status)
  227. : Calendar(TimeZone::createDefault(), Locale::getDefault(), status),
  228. fGregorianCutover(kPapalCutover),
  229. fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582),
  230. fIsGregorian(true), fInvertGregorian(false)
  231. {
  232. set(UCAL_ERA, AD);
  233. set(UCAL_YEAR, year);
  234. set(UCAL_MONTH, month);
  235. set(UCAL_DATE, date);
  236. set(UCAL_HOUR_OF_DAY, hour);
  237. set(UCAL_MINUTE, minute);
  238. set(UCAL_SECOND, second);
  239. }
  240. // -------------------------------------
  241. GregorianCalendar::~GregorianCalendar()
  242. {
  243. }
  244. // -------------------------------------
  245. GregorianCalendar::GregorianCalendar(const GregorianCalendar &source)
  246. : Calendar(source),
  247. fGregorianCutover(source.fGregorianCutover),
  248. fCutoverJulianDay(source.fCutoverJulianDay), fNormalizedGregorianCutover(source.fNormalizedGregorianCutover), fGregorianCutoverYear(source.fGregorianCutoverYear),
  249. fIsGregorian(source.fIsGregorian), fInvertGregorian(source.fInvertGregorian)
  250. {
  251. }
  252. // -------------------------------------
  253. GregorianCalendar* GregorianCalendar::clone() const
  254. {
  255. return new GregorianCalendar(*this);
  256. }
  257. // -------------------------------------
  258. GregorianCalendar &
  259. GregorianCalendar::operator=(const GregorianCalendar &right)
  260. {
  261. if (this != &right)
  262. {
  263. Calendar::operator=(right);
  264. fGregorianCutover = right.fGregorianCutover;
  265. fNormalizedGregorianCutover = right.fNormalizedGregorianCutover;
  266. fGregorianCutoverYear = right.fGregorianCutoverYear;
  267. fCutoverJulianDay = right.fCutoverJulianDay;
  268. }
  269. return *this;
  270. }
  271. // -------------------------------------
  272. UBool GregorianCalendar::isEquivalentTo(const Calendar& other) const
  273. {
  274. // Calendar override.
  275. return Calendar::isEquivalentTo(other) &&
  276. fGregorianCutover == ((GregorianCalendar*)&other)->fGregorianCutover;
  277. }
  278. // -------------------------------------
  279. void
  280. GregorianCalendar::setGregorianChange(UDate date, UErrorCode& status)
  281. {
  282. if (U_FAILURE(status))
  283. return;
  284. // Precompute two internal variables which we use to do the actual
  285. // cutover computations. These are the normalized cutover, which is the
  286. // midnight at or before the cutover, and the cutover year. The
  287. // normalized cutover is in pure date milliseconds; it contains no time
  288. // of day or timezone component, and it used to compare against other
  289. // pure date values.
  290. double cutoverDay = ClockMath::floorDivide(date, (double)kOneDay);
  291. // Handle the rare case of numeric overflow where the user specifies a time
  292. // outside of INT32_MIN .. INT32_MAX number of days.
  293. if (cutoverDay <= INT32_MIN) {
  294. cutoverDay = INT32_MIN;
  295. fGregorianCutover = fNormalizedGregorianCutover = cutoverDay * kOneDay;
  296. } else if (cutoverDay >= INT32_MAX) {
  297. cutoverDay = INT32_MAX;
  298. fGregorianCutover = fNormalizedGregorianCutover = cutoverDay * kOneDay;
  299. } else {
  300. fNormalizedGregorianCutover = cutoverDay * kOneDay;
  301. fGregorianCutover = date;
  302. }
  303. // Normalize the year so BC values are represented as 0 and negative
  304. // values.
  305. GregorianCalendar *cal = new GregorianCalendar(getTimeZone(), status);
  306. /* test for nullptr */
  307. if (cal == 0) {
  308. status = U_MEMORY_ALLOCATION_ERROR;
  309. return;
  310. }
  311. if(U_FAILURE(status))
  312. return;
  313. cal->setTime(date, status);
  314. fGregorianCutoverYear = cal->get(UCAL_YEAR, status);
  315. if (cal->get(UCAL_ERA, status) == BC)
  316. fGregorianCutoverYear = 1 - fGregorianCutoverYear;
  317. fCutoverJulianDay = (int32_t)cutoverDay;
  318. delete cal;
  319. }
  320. void GregorianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) {
  321. int32_t eyear, month, dayOfMonth, dayOfYear, unusedRemainder;
  322. if(U_FAILURE(status)) {
  323. return;
  324. }
  325. #if defined (U_DEBUG_CAL)
  326. fprintf(stderr, "%s:%d: jd%d- (greg's %d)- [cut=%d]\n",
  327. __FILE__, __LINE__, julianDay, getGregorianDayOfYear(), fCutoverJulianDay);
  328. #endif
  329. if (julianDay >= fCutoverJulianDay) {
  330. month = getGregorianMonth();
  331. dayOfMonth = getGregorianDayOfMonth();
  332. dayOfYear = getGregorianDayOfYear();
  333. eyear = getGregorianYear();
  334. } else {
  335. // The Julian epoch day (not the same as Julian Day)
  336. // is zero on Saturday December 30, 0 (Gregorian).
  337. int32_t julianEpochDay = julianDay - (kJan1_1JulianDay - 2);
  338. eyear = (int32_t) ClockMath::floorDivide((4.0*julianEpochDay) + 1464.0, (int32_t) 1461, &unusedRemainder);
  339. // Compute the Julian calendar day number for January 1, eyear
  340. int32_t january1 = 365*(eyear-1) + ClockMath::floorDivide(eyear-1, (int32_t)4);
  341. dayOfYear = (julianEpochDay - january1); // 0-based
  342. // Julian leap years occurred historically every 4 years starting
  343. // with 8 AD. Before 8 AD the spacing is irregular; every 3 years
  344. // from 45 BC to 9 BC, and then none until 8 AD. However, we don't
  345. // implement this historical detail; instead, we implement the
  346. // computationally cleaner proleptic calendar, which assumes
  347. // consistent 4-year cycles throughout time.
  348. UBool isLeap = ((eyear&0x3) == 0); // equiv. to (eyear%4 == 0)
  349. // Common Julian/Gregorian calculation
  350. int32_t correction = 0;
  351. int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1
  352. if (dayOfYear >= march1) {
  353. correction = isLeap ? 1 : 2;
  354. }
  355. month = (12 * (dayOfYear + correction) + 6) / 367; // zero-based month
  356. dayOfMonth = dayOfYear - (isLeap?kLeapNumDays[month]:kNumDays[month]) + 1; // one-based DOM
  357. ++dayOfYear;
  358. #if defined (U_DEBUG_CAL)
  359. // fprintf(stderr, "%d - %d[%d] + 1\n", dayOfYear, isLeap?kLeapNumDays[month]:kNumDays[month], month );
  360. // fprintf(stderr, "%s:%d: greg's HCF %d -> %d/%d/%d not %d/%d/%d\n",
  361. // __FILE__, __LINE__,julianDay,
  362. // eyear,month,dayOfMonth,
  363. // getGregorianYear(), getGregorianMonth(), getGregorianDayOfMonth() );
  364. fprintf(stderr, "%s:%d: doy %d (greg's %d)- [cut=%d]\n",
  365. __FILE__, __LINE__, dayOfYear, getGregorianDayOfYear(), fCutoverJulianDay);
  366. #endif
  367. }
  368. // [j81] if we are after the cutover in its year, shift the day of the year
  369. if((eyear == fGregorianCutoverYear) && (julianDay >= fCutoverJulianDay)) {
  370. //from handleComputeMonthStart
  371. int32_t gregShift = Grego::gregorianShift(eyear);
  372. #if defined (U_DEBUG_CAL)
  373. fprintf(stderr, "%s:%d: gregorian shift %d ::: doy%d => %d [cut=%d]\n",
  374. __FILE__, __LINE__,gregShift, dayOfYear, dayOfYear+gregShift, fCutoverJulianDay);
  375. #endif
  376. dayOfYear += gregShift;
  377. }
  378. internalSet(UCAL_MONTH, month);
  379. internalSet(UCAL_ORDINAL_MONTH, month);
  380. internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
  381. internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
  382. internalSet(UCAL_EXTENDED_YEAR, eyear);
  383. int32_t era = AD;
  384. if (eyear < 1) {
  385. era = BC;
  386. eyear = 1 - eyear;
  387. }
  388. internalSet(UCAL_ERA, era);
  389. internalSet(UCAL_YEAR, eyear);
  390. }
  391. // -------------------------------------
  392. UDate
  393. GregorianCalendar::getGregorianChange() const
  394. {
  395. return fGregorianCutover;
  396. }
  397. // -------------------------------------
  398. UBool
  399. GregorianCalendar::isLeapYear(int32_t year) const
  400. {
  401. // MSVC complains bitterly if we try to use Grego::isLeapYear here
  402. // NOTE: year&0x3 == year%4
  403. return (year >= fGregorianCutoverYear ?
  404. (((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0))) : // Gregorian
  405. ((year&0x3) == 0)); // Julian
  406. }
  407. // -------------------------------------
  408. int32_t GregorianCalendar::handleComputeJulianDay(UCalendarDateFields bestField)
  409. {
  410. fInvertGregorian = false;
  411. int32_t jd = Calendar::handleComputeJulianDay(bestField);
  412. if((bestField == UCAL_WEEK_OF_YEAR) && // if we are doing WOY calculations, we are counting relative to Jan 1 *julian*
  413. (internalGet(UCAL_EXTENDED_YEAR)==fGregorianCutoverYear) &&
  414. jd >= fCutoverJulianDay) {
  415. fInvertGregorian = true; // So that the Julian Jan 1 will be used in handleComputeMonthStart
  416. return Calendar::handleComputeJulianDay(bestField);
  417. }
  418. // The following check handles portions of the cutover year BEFORE the
  419. // cutover itself happens.
  420. //if ((fIsGregorian==true) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */
  421. if ((fIsGregorian) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */
  422. #if defined (U_DEBUG_CAL)
  423. fprintf(stderr, "%s:%d: jd [invert] %d\n",
  424. __FILE__, __LINE__, jd);
  425. #endif
  426. fInvertGregorian = true;
  427. jd = Calendar::handleComputeJulianDay(bestField);
  428. #if defined (U_DEBUG_CAL)
  429. fprintf(stderr, "%s:%d: fIsGregorian %s, fInvertGregorian %s - ",
  430. __FILE__, __LINE__,fIsGregorian?"T":"F", fInvertGregorian?"T":"F");
  431. fprintf(stderr, " jd NOW %d\n",
  432. jd);
  433. #endif
  434. } else {
  435. #if defined (U_DEBUG_CAL)
  436. fprintf(stderr, "%s:%d: jd [==] %d - %sfIsGregorian %sfInvertGregorian, %d\n",
  437. __FILE__, __LINE__, jd, fIsGregorian?"T":"F", fInvertGregorian?"T":"F", bestField);
  438. #endif
  439. }
  440. if(fIsGregorian && (internalGet(UCAL_EXTENDED_YEAR) == fGregorianCutoverYear)) {
  441. int32_t gregShift = Grego::gregorianShift(internalGet(UCAL_EXTENDED_YEAR));
  442. if (bestField == UCAL_DAY_OF_YEAR) {
  443. #if defined (U_DEBUG_CAL)
  444. fprintf(stderr, "%s:%d: [DOY%d] gregorian shift of JD %d += %d\n",
  445. __FILE__, __LINE__, fFields[bestField],jd, gregShift);
  446. #endif
  447. jd -= gregShift;
  448. } else if ( bestField == UCAL_WEEK_OF_MONTH ) {
  449. int32_t weekShift = 14;
  450. #if defined (U_DEBUG_CAL)
  451. fprintf(stderr, "%s:%d: [WOY/WOM] gregorian week shift of %d += %d\n",
  452. __FILE__, __LINE__, jd, weekShift);
  453. #endif
  454. jd += weekShift; // shift by weeks for week based fields.
  455. }
  456. }
  457. return jd;
  458. }
  459. int32_t GregorianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month,
  460. UBool /* useMonth */) const
  461. {
  462. GregorianCalendar *nonConstThis = (GregorianCalendar*)this; // cast away const
  463. // If the month is out of range, adjust it into range, and
  464. // modify the extended year value accordingly.
  465. if (month < 0 || month > 11) {
  466. eyear += ClockMath::floorDivide(month, 12, &month);
  467. }
  468. UBool isLeap = eyear%4 == 0;
  469. int64_t y = (int64_t)eyear-1;
  470. int64_t julianDay = 365*y + ClockMath::floorDivide(y, (int64_t)4) + (kJan1_1JulianDay - 3);
  471. nonConstThis->fIsGregorian = (eyear >= fGregorianCutoverYear);
  472. #if defined (U_DEBUG_CAL)
  473. fprintf(stderr, "%s:%d: (hcms%d/%d) fIsGregorian %s, fInvertGregorian %s\n",
  474. __FILE__, __LINE__, eyear,month, fIsGregorian?"T":"F", fInvertGregorian?"T":"F");
  475. #endif
  476. if (fInvertGregorian) {
  477. nonConstThis->fIsGregorian = !fIsGregorian;
  478. }
  479. if (fIsGregorian) {
  480. isLeap = isLeap && ((eyear%100 != 0) || (eyear%400 == 0));
  481. // Add 2 because Gregorian calendar starts 2 days after
  482. // Julian calendar
  483. int32_t gregShift = Grego::gregorianShift(eyear);
  484. #if defined (U_DEBUG_CAL)
  485. fprintf(stderr, "%s:%d: (hcms%d/%d) gregorian shift of %d += %d\n",
  486. __FILE__, __LINE__, eyear, month, julianDay, gregShift);
  487. #endif
  488. julianDay += gregShift;
  489. }
  490. // At this point julianDay indicates the day BEFORE the first
  491. // day of January 1, <eyear> of either the Julian or Gregorian
  492. // calendar.
  493. if (month != 0) {
  494. julianDay += isLeap?kLeapNumDays[month]:kNumDays[month];
  495. }
  496. return static_cast<int32_t>(julianDay);
  497. }
  498. int32_t GregorianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const
  499. {
  500. // If the month is out of range, adjust it into range, and
  501. // modify the extended year value accordingly.
  502. if (month < 0 || month > 11) {
  503. extendedYear += ClockMath::floorDivide(month, 12, &month);
  504. }
  505. return isLeapYear(extendedYear) ? kLeapMonthLength[month] : kMonthLength[month];
  506. }
  507. int32_t GregorianCalendar::handleGetYearLength(int32_t eyear) const {
  508. return isLeapYear(eyear) ? 366 : 365;
  509. }
  510. int32_t
  511. GregorianCalendar::monthLength(int32_t month) const
  512. {
  513. int32_t year = internalGet(UCAL_EXTENDED_YEAR);
  514. return handleGetMonthLength(year, month);
  515. }
  516. // -------------------------------------
  517. int32_t
  518. GregorianCalendar::monthLength(int32_t month, int32_t year) const
  519. {
  520. return isLeapYear(year) ? kLeapMonthLength[month] : kMonthLength[month];
  521. }
  522. // -------------------------------------
  523. int32_t
  524. GregorianCalendar::yearLength(int32_t year) const
  525. {
  526. return isLeapYear(year) ? 366 : 365;
  527. }
  528. // -------------------------------------
  529. int32_t
  530. GregorianCalendar::yearLength() const
  531. {
  532. return isLeapYear(internalGet(UCAL_YEAR)) ? 366 : 365;
  533. }
  534. // -------------------------------------
  535. /**
  536. * After adjustments such as add(MONTH), add(YEAR), we don't want the
  537. * month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar
  538. * 3, we want it to go to Feb 28. Adjustments which might run into this
  539. * problem call this method to retain the proper month.
  540. */
  541. void
  542. GregorianCalendar::pinDayOfMonth()
  543. {
  544. int32_t monthLen = monthLength(internalGetMonth());
  545. int32_t dom = internalGet(UCAL_DATE);
  546. if(dom > monthLen)
  547. set(UCAL_DATE, monthLen);
  548. }
  549. // -------------------------------------
  550. UBool
  551. GregorianCalendar::validateFields() const
  552. {
  553. for (int32_t field = 0; field < UCAL_FIELD_COUNT; field++) {
  554. // Ignore DATE and DAY_OF_YEAR which are handled below
  555. if (field != UCAL_DATE &&
  556. field != UCAL_DAY_OF_YEAR &&
  557. isSet((UCalendarDateFields)field) &&
  558. ! boundsCheck(internalGet((UCalendarDateFields)field), (UCalendarDateFields)field))
  559. return false;
  560. }
  561. // Values differ in Least-Maximum and Maximum should be handled
  562. // specially.
  563. if (isSet(UCAL_DATE)) {
  564. int32_t date = internalGet(UCAL_DATE);
  565. if (date < getMinimum(UCAL_DATE) ||
  566. date > monthLength(internalGetMonth())) {
  567. return false;
  568. }
  569. }
  570. if (isSet(UCAL_DAY_OF_YEAR)) {
  571. int32_t days = internalGet(UCAL_DAY_OF_YEAR);
  572. if (days < 1 || days > yearLength()) {
  573. return false;
  574. }
  575. }
  576. // Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero.
  577. // We've checked against minimum and maximum above already.
  578. if (isSet(UCAL_DAY_OF_WEEK_IN_MONTH) &&
  579. 0 == internalGet(UCAL_DAY_OF_WEEK_IN_MONTH)) {
  580. return false;
  581. }
  582. return true;
  583. }
  584. // -------------------------------------
  585. UBool
  586. GregorianCalendar::boundsCheck(int32_t value, UCalendarDateFields field) const
  587. {
  588. return value >= getMinimum(field) && value <= getMaximum(field);
  589. }
  590. // -------------------------------------
  591. UDate
  592. GregorianCalendar::getEpochDay(UErrorCode& status)
  593. {
  594. complete(status);
  595. // Divide by 1000 (convert to seconds) in order to prevent overflow when
  596. // dealing with UDate(Long.MIN_VALUE) and UDate(Long.MAX_VALUE).
  597. double wallSec = internalGetTime()/1000 + (internalGet(UCAL_ZONE_OFFSET) + internalGet(UCAL_DST_OFFSET))/1000;
  598. return ClockMath::floorDivide(wallSec, kOneDay/1000.0);
  599. }
  600. // -------------------------------------
  601. // -------------------------------------
  602. /**
  603. * Compute the julian day number of the day BEFORE the first day of
  604. * January 1, year 1 of the given calendar. If julianDay == 0, it
  605. * specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian
  606. * or Gregorian).
  607. */
  608. double GregorianCalendar::computeJulianDayOfYear(UBool isGregorian,
  609. int32_t year, UBool& isLeap)
  610. {
  611. isLeap = year%4 == 0;
  612. int32_t y = year - 1;
  613. double julianDay = 365.0*y + ClockMath::floorDivide(y, 4) + (kJan1_1JulianDay - 3);
  614. if (isGregorian) {
  615. isLeap = isLeap && ((year%100 != 0) || (year%400 == 0));
  616. // Add 2 because Gregorian calendar starts 2 days after Julian calendar
  617. julianDay += Grego::gregorianShift(year);
  618. }
  619. return julianDay;
  620. }
  621. // /**
  622. // * Compute the day of week, relative to the first day of week, from
  623. // * 0..6, of the current DOW_LOCAL or DAY_OF_WEEK fields. This is
  624. // * equivalent to get(DOW_LOCAL) - 1.
  625. // */
  626. // int32_t GregorianCalendar::computeRelativeDOW() const {
  627. // int32_t relDow = 0;
  628. // if (fStamp[UCAL_DOW_LOCAL] > fStamp[UCAL_DAY_OF_WEEK]) {
  629. // relDow = internalGet(UCAL_DOW_LOCAL) - 1; // 1-based
  630. // } else if (fStamp[UCAL_DAY_OF_WEEK] != kUnset) {
  631. // relDow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek();
  632. // if (relDow < 0) relDow += 7;
  633. // }
  634. // return relDow;
  635. // }
  636. // /**
  637. // * Compute the day of week, relative to the first day of week,
  638. // * from 0..6 of the given julian day.
  639. // */
  640. // int32_t GregorianCalendar::computeRelativeDOW(double julianDay) const {
  641. // int32_t relDow = julianDayToDayOfWeek(julianDay) - getFirstDayOfWeek();
  642. // if (relDow < 0) {
  643. // relDow += 7;
  644. // }
  645. // return relDow;
  646. // }
  647. // /**
  648. // * Compute the DOY using the WEEK_OF_YEAR field and the julian day
  649. // * of the day BEFORE January 1 of a year (a return value from
  650. // * computeJulianDayOfYear).
  651. // */
  652. // int32_t GregorianCalendar::computeDOYfromWOY(double julianDayOfYear) const {
  653. // // Compute DOY from day of week plus week of year
  654. // // Find the day of the week for the first of this year. This
  655. // // is zero-based, with 0 being the locale-specific first day of
  656. // // the week. Add 1 to get first day of year.
  657. // int32_t fdy = computeRelativeDOW(julianDayOfYear + 1);
  658. // return
  659. // // Compute doy of first (relative) DOW of WOY 1
  660. // (((7 - fdy) < getMinimalDaysInFirstWeek())
  661. // ? (8 - fdy) : (1 - fdy))
  662. // // Adjust for the week number.
  663. // + (7 * (internalGet(UCAL_WEEK_OF_YEAR) - 1))
  664. // // Adjust for the DOW
  665. // + computeRelativeDOW();
  666. // }
  667. // -------------------------------------
  668. double
  669. GregorianCalendar::millisToJulianDay(UDate millis)
  670. {
  671. return (double)kEpochStartAsJulianDay + ClockMath::floorDivide(millis, (double)kOneDay);
  672. }
  673. // -------------------------------------
  674. UDate
  675. GregorianCalendar::julianDayToMillis(double julian)
  676. {
  677. return (UDate) ((julian - kEpochStartAsJulianDay) * (double) kOneDay);
  678. }
  679. // -------------------------------------
  680. int32_t
  681. GregorianCalendar::aggregateStamp(int32_t stamp_a, int32_t stamp_b)
  682. {
  683. return (((stamp_a != kUnset && stamp_b != kUnset)
  684. ? uprv_max(stamp_a, stamp_b)
  685. : (int32_t)kUnset));
  686. }
  687. // -------------------------------------
  688. /**
  689. * Roll a field by a signed amount.
  690. * Note: This will be made public later. [LIU]
  691. */
  692. void
  693. GregorianCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) {
  694. roll((UCalendarDateFields) field, amount, status);
  695. }
  696. void
  697. GregorianCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) UPRV_NO_SANITIZE_UNDEFINED {
  698. if((amount == 0) || U_FAILURE(status)) {
  699. return;
  700. }
  701. // J81 processing. (gregorian cutover)
  702. UBool inCutoverMonth = false;
  703. int32_t cMonthLen=0; // 'c' for cutover; in days
  704. int32_t cDayOfMonth=0; // no discontinuity: [0, cMonthLen)
  705. double cMonthStart=0.0; // in ms
  706. // Common code - see if we're in the cutover month of the cutover year
  707. if(get(UCAL_EXTENDED_YEAR, status) == fGregorianCutoverYear) {
  708. switch (field) {
  709. case UCAL_DAY_OF_MONTH:
  710. case UCAL_WEEK_OF_MONTH:
  711. {
  712. int32_t max = monthLength(internalGetMonth());
  713. UDate t = internalGetTime();
  714. // We subtract 1 from the DAY_OF_MONTH to make it zero-based, and an
  715. // additional 10 if we are after the cutover. Thus the monthStart
  716. // value will be correct iff we actually are in the cutover month.
  717. cDayOfMonth = internalGet(UCAL_DAY_OF_MONTH) - ((t >= fGregorianCutover) ? 10 : 0);
  718. cMonthStart = t - ((cDayOfMonth - 1) * kOneDay);
  719. // A month containing the cutover is 10 days shorter.
  720. if ((cMonthStart < fGregorianCutover) &&
  721. (cMonthStart + (cMonthLen=(max-10))*kOneDay >= fGregorianCutover)) {
  722. inCutoverMonth = true;
  723. }
  724. }
  725. break;
  726. default:
  727. ;
  728. }
  729. }
  730. switch (field) {
  731. case UCAL_WEEK_OF_YEAR: {
  732. // Unlike WEEK_OF_MONTH, WEEK_OF_YEAR never shifts the day of the
  733. // week. Also, rolling the week of the year can have seemingly
  734. // strange effects simply because the year of the week of year
  735. // may be different from the calendar year. For example, the
  736. // date Dec 28, 1997 is the first day of week 1 of 1998 (if
  737. // weeks start on Sunday and the minimal days in first week is
  738. // <= 3).
  739. int32_t woy = get(UCAL_WEEK_OF_YEAR, status);
  740. // Get the ISO year, which matches the week of year. This
  741. // may be one year before or after the calendar year.
  742. int32_t isoYear = get(UCAL_YEAR_WOY, status);
  743. int32_t isoDoy = internalGet(UCAL_DAY_OF_YEAR);
  744. if (internalGetMonth() == UCAL_JANUARY) {
  745. if (woy >= 52) {
  746. isoDoy += handleGetYearLength(isoYear);
  747. }
  748. } else {
  749. if (woy == 1) {
  750. isoDoy -= handleGetYearLength(isoYear - 1);
  751. }
  752. }
  753. woy += amount;
  754. // Do fast checks to avoid unnecessary computation:
  755. if (woy < 1 || woy > 52) {
  756. // Determine the last week of the ISO year.
  757. // We do this using the standard formula we use
  758. // everywhere in this file. If we can see that the
  759. // days at the end of the year are going to fall into
  760. // week 1 of the next year, we drop the last week by
  761. // subtracting 7 from the last day of the year.
  762. int32_t lastDoy = handleGetYearLength(isoYear);
  763. int32_t lastRelDow = (lastDoy - isoDoy + internalGet(UCAL_DAY_OF_WEEK) -
  764. getFirstDayOfWeek()) % 7;
  765. if (lastRelDow < 0) lastRelDow += 7;
  766. if ((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) lastDoy -= 7;
  767. int32_t lastWoy = weekNumber(lastDoy, lastRelDow + 1);
  768. woy = ((woy + lastWoy - 1) % lastWoy) + 1;
  769. }
  770. set(UCAL_WEEK_OF_YEAR, woy);
  771. set(UCAL_YEAR_WOY,isoYear);
  772. return;
  773. }
  774. case UCAL_DAY_OF_MONTH:
  775. if( !inCutoverMonth ) {
  776. Calendar::roll(field, amount, status);
  777. return;
  778. } else {
  779. // [j81] 1582 special case for DOM
  780. // The default computation works except when the current month
  781. // contains the Gregorian cutover. We handle this special case
  782. // here. [j81 - aliu]
  783. double monthLen = cMonthLen * kOneDay;
  784. double msIntoMonth = uprv_fmod(internalGetTime() - cMonthStart +
  785. amount * kOneDay, monthLen);
  786. if (msIntoMonth < 0) {
  787. msIntoMonth += monthLen;
  788. }
  789. #if defined (U_DEBUG_CAL)
  790. fprintf(stderr, "%s:%d: roll DOM %d -> %.0lf ms \n",
  791. __FILE__, __LINE__,amount, cMonthLen, cMonthStart+msIntoMonth);
  792. #endif
  793. setTimeInMillis(cMonthStart + msIntoMonth, status);
  794. return;
  795. }
  796. case UCAL_WEEK_OF_MONTH:
  797. if( !inCutoverMonth ) {
  798. Calendar::roll(field, amount, status);
  799. return;
  800. } else {
  801. #if defined (U_DEBUG_CAL)
  802. fprintf(stderr, "%s:%d: roll WOM %d ??????????????????? \n",
  803. __FILE__, __LINE__,amount);
  804. #endif
  805. // NOTE: following copied from the old
  806. // GregorianCalendar::roll( WEEK_OF_MONTH ) code
  807. // This is tricky, because during the roll we may have to shift
  808. // to a different day of the week. For example:
  809. // s m t w r f s
  810. // 1 2 3 4 5
  811. // 6 7 8 9 10 11 12
  812. // When rolling from the 6th or 7th back one week, we go to the
  813. // 1st (assuming that the first partial week counts). The same
  814. // thing happens at the end of the month.
  815. // The other tricky thing is that we have to figure out whether
  816. // the first partial week actually counts or not, based on the
  817. // minimal first days in the week. And we have to use the
  818. // correct first day of the week to delineate the week
  819. // boundaries.
  820. // Here's our algorithm. First, we find the real boundaries of
  821. // the month. Then we discard the first partial week if it
  822. // doesn't count in this locale. Then we fill in the ends with
  823. // phantom days, so that the first partial week and the last
  824. // partial week are full weeks. We then have a nice square
  825. // block of weeks. We do the usual rolling within this block,
  826. // as is done elsewhere in this method. If we wind up on one of
  827. // the phantom days that we added, we recognize this and pin to
  828. // the first or the last day of the month. Easy, eh?
  829. // Another wrinkle: To fix jitterbug 81, we have to make all this
  830. // work in the oddball month containing the Gregorian cutover.
  831. // This month is 10 days shorter than usual, and also contains
  832. // a discontinuity in the days; e.g., the default cutover month
  833. // is Oct 1582, and goes from day of month 4 to day of month 15.
  834. // Normalize the DAY_OF_WEEK so that 0 is the first day of the week
  835. // in this locale. We have dow in 0..6.
  836. int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek();
  837. if (dow < 0)
  838. dow += 7;
  839. // Find the day of month, compensating for cutover discontinuity.
  840. int32_t dom = cDayOfMonth;
  841. // Find the day of the week (normalized for locale) for the first
  842. // of the month.
  843. int32_t fdm = (dow - dom + 1) % 7;
  844. if (fdm < 0)
  845. fdm += 7;
  846. // Get the first day of the first full week of the month,
  847. // including phantom days, if any. Figure out if the first week
  848. // counts or not; if it counts, then fill in phantom days. If
  849. // not, advance to the first real full week (skip the partial week).
  850. int32_t start;
  851. if ((7 - fdm) < getMinimalDaysInFirstWeek())
  852. start = 8 - fdm; // Skip the first partial week
  853. else
  854. start = 1 - fdm; // This may be zero or negative
  855. // Get the day of the week (normalized for locale) for the last
  856. // day of the month.
  857. int32_t monthLen = cMonthLen;
  858. int32_t ldm = (monthLen - dom + dow) % 7;
  859. // We know monthLen >= DAY_OF_MONTH so we skip the += 7 step here.
  860. // Get the limit day for the blocked-off rectangular month; that
  861. // is, the day which is one past the last day of the month,
  862. // after the month has already been filled in with phantom days
  863. // to fill out the last week. This day has a normalized DOW of 0.
  864. int32_t limit = monthLen + 7 - ldm;
  865. // Now roll between start and (limit - 1).
  866. int32_t gap = limit - start;
  867. int32_t newDom = (dom + amount*7 - start) % gap;
  868. if (newDom < 0)
  869. newDom += gap;
  870. newDom += start;
  871. // Finally, pin to the real start and end of the month.
  872. if (newDom < 1)
  873. newDom = 1;
  874. if (newDom > monthLen)
  875. newDom = monthLen;
  876. // Set the DAY_OF_MONTH. We rely on the fact that this field
  877. // takes precedence over everything else (since all other fields
  878. // are also set at this point). If this fact changes (if the
  879. // disambiguation algorithm changes) then we will have to unset
  880. // the appropriate fields here so that DAY_OF_MONTH is attended
  881. // to.
  882. // If we are in the cutover month, manipulate ms directly. Don't do
  883. // this in general because it doesn't work across DST boundaries
  884. // (details, details). This takes care of the discontinuity.
  885. setTimeInMillis(cMonthStart + (newDom-1)*kOneDay, status);
  886. return;
  887. }
  888. default:
  889. Calendar::roll(field, amount, status);
  890. return;
  891. }
  892. }
  893. // -------------------------------------
  894. /**
  895. * Return the minimum value that this field could have, given the current date.
  896. * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
  897. * @param field the time field.
  898. * @return the minimum value that this field could have, given the current date.
  899. * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field) instead.
  900. */
  901. int32_t GregorianCalendar::getActualMinimum(EDateFields field) const
  902. {
  903. return getMinimum((UCalendarDateFields)field);
  904. }
  905. int32_t GregorianCalendar::getActualMinimum(EDateFields field, UErrorCode& /* status */) const
  906. {
  907. return getMinimum((UCalendarDateFields)field);
  908. }
  909. /**
  910. * Return the minimum value that this field could have, given the current date.
  911. * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
  912. * @param field the time field.
  913. * @return the minimum value that this field could have, given the current date.
  914. * @draft ICU 2.6.
  915. */
  916. int32_t GregorianCalendar::getActualMinimum(UCalendarDateFields field, UErrorCode& /* status */) const
  917. {
  918. return getMinimum(field);
  919. }
  920. // ------------------------------------
  921. /**
  922. * Old year limits were least max 292269054, max 292278994.
  923. */
  924. /**
  925. * @stable ICU 2.0
  926. */
  927. int32_t GregorianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
  928. return kGregorianCalendarLimits[field][limitType];
  929. }
  930. /**
  931. * Return the maximum value that this field could have, given the current date.
  932. * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
  933. * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
  934. * for some years the actual maximum for MONTH is 12, and for others 13.
  935. * @stable ICU 2.0
  936. */
  937. int32_t GregorianCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const
  938. {
  939. /* It is a known limitation that the code here (and in getActualMinimum)
  940. * won't behave properly at the extreme limits of GregorianCalendar's
  941. * representable range (except for the code that handles the YEAR
  942. * field). That's because the ends of the representable range are at
  943. * odd spots in the year. For calendars with the default Gregorian
  944. * cutover, these limits are Sun Dec 02 16:47:04 GMT 292269055 BC to Sun
  945. * Aug 17 07:12:55 GMT 292278994 AD, somewhat different for non-GMT
  946. * zones. As a result, if the calendar is set to Aug 1 292278994 AD,
  947. * the actual maximum of DAY_OF_MONTH is 17, not 30. If the date is Mar
  948. * 31 in that year, the actual maximum month might be Jul, whereas is
  949. * the date is Mar 15, the actual maximum might be Aug -- depending on
  950. * the precise semantics that are desired. Similar considerations
  951. * affect all fields. Nonetheless, this effect is sufficiently arcane
  952. * that we permit it, rather than complicating the code to handle such
  953. * intricacies. - liu 8/20/98
  954. * UPDATE: No longer true, since we have pulled in the limit values on
  955. * the year. - Liu 11/6/00 */
  956. switch (field) {
  957. case UCAL_YEAR:
  958. /* The year computation is no different, in principle, from the
  959. * others, however, the range of possible maxima is large. In
  960. * addition, the way we know we've exceeded the range is different.
  961. * For these reasons, we use the special case code below to handle
  962. * this field.
  963. *
  964. * The actual maxima for YEAR depend on the type of calendar:
  965. *
  966. * Gregorian = May 17, 292275056 BC - Aug 17, 292278994 AD
  967. * Julian = Dec 2, 292269055 BC - Jan 3, 292272993 AD
  968. * Hybrid = Dec 2, 292269055 BC - Aug 17, 292278994 AD
  969. *
  970. * We know we've exceeded the maximum when either the month, date,
  971. * time, or era changes in response to setting the year. We don't
  972. * check for month, date, and time here because the year and era are
  973. * sufficient to detect an invalid year setting. NOTE: If code is
  974. * added to check the month and date in the future for some reason,
  975. * Feb 29 must be allowed to shift to Mar 1 when setting the year.
  976. */
  977. {
  978. if(U_FAILURE(status)) return 0;
  979. Calendar *cal = clone();
  980. if(!cal) {
  981. status = U_MEMORY_ALLOCATION_ERROR;
  982. return 0;
  983. }
  984. cal->setLenient(true);
  985. int32_t era = cal->get(UCAL_ERA, status);
  986. UDate d = cal->getTime(status);
  987. /* Perform a binary search, with the invariant that lowGood is a
  988. * valid year, and highBad is an out of range year.
  989. */
  990. int32_t lowGood = kGregorianCalendarLimits[UCAL_YEAR][1];
  991. int32_t highBad = kGregorianCalendarLimits[UCAL_YEAR][2]+1;
  992. while ((lowGood + 1) < highBad) {
  993. int32_t y = (lowGood + highBad) / 2;
  994. cal->set(UCAL_YEAR, y);
  995. if (cal->get(UCAL_YEAR, status) == y && cal->get(UCAL_ERA, status) == era) {
  996. lowGood = y;
  997. } else {
  998. highBad = y;
  999. cal->setTime(d, status); // Restore original fields
  1000. }
  1001. }
  1002. delete cal;
  1003. return lowGood;
  1004. }
  1005. default:
  1006. return Calendar::getActualMaximum(field,status);
  1007. }
  1008. }
  1009. int32_t GregorianCalendar::handleGetExtendedYear() {
  1010. // the year to return
  1011. int32_t year = kEpochYear;
  1012. // year field to use
  1013. int32_t yearField = UCAL_EXTENDED_YEAR;
  1014. // There are three separate fields which could be used to
  1015. // derive the proper year. Use the one most recently set.
  1016. if (fStamp[yearField] < fStamp[UCAL_YEAR])
  1017. yearField = UCAL_YEAR;
  1018. if (fStamp[yearField] < fStamp[UCAL_YEAR_WOY])
  1019. yearField = UCAL_YEAR_WOY;
  1020. // based on the "best" year field, get the year
  1021. switch(yearField) {
  1022. case UCAL_EXTENDED_YEAR:
  1023. year = internalGet(UCAL_EXTENDED_YEAR, kEpochYear);
  1024. break;
  1025. case UCAL_YEAR:
  1026. {
  1027. // The year defaults to the epoch start, the era to AD
  1028. int32_t era = internalGet(UCAL_ERA, AD);
  1029. if (era == BC) {
  1030. year = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year
  1031. } else {
  1032. year = internalGet(UCAL_YEAR, kEpochYear);
  1033. }
  1034. }
  1035. break;
  1036. case UCAL_YEAR_WOY:
  1037. year = handleGetExtendedYearFromWeekFields(internalGet(UCAL_YEAR_WOY), internalGet(UCAL_WEEK_OF_YEAR));
  1038. #if defined (U_DEBUG_CAL)
  1039. // if(internalGet(UCAL_YEAR_WOY) != year) {
  1040. fprintf(stderr, "%s:%d: hGEYFWF[%d,%d] -> %d\n",
  1041. __FILE__, __LINE__,internalGet(UCAL_YEAR_WOY),internalGet(UCAL_WEEK_OF_YEAR),year);
  1042. //}
  1043. #endif
  1044. break;
  1045. default:
  1046. year = kEpochYear;
  1047. }
  1048. return year;
  1049. }
  1050. int32_t GregorianCalendar::handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy)
  1051. {
  1052. // convert year to extended form
  1053. int32_t era = internalGet(UCAL_ERA, AD);
  1054. if(era == BC) {
  1055. yearWoy = 1 - yearWoy;
  1056. }
  1057. return Calendar::handleGetExtendedYearFromWeekFields(yearWoy, woy);
  1058. }
  1059. // -------------------------------------
  1060. /**
  1061. * Return the ERA. We need a special method for this because the
  1062. * default ERA is AD, but a zero (unset) ERA is BC.
  1063. */
  1064. int32_t
  1065. GregorianCalendar::internalGetEra() const {
  1066. return isSet(UCAL_ERA) ? internalGet(UCAL_ERA) : (int32_t)AD;
  1067. }
  1068. const char *
  1069. GregorianCalendar::getType() const {
  1070. //static const char kGregorianType = "gregorian";
  1071. return "gregorian";
  1072. }
  1073. /**
  1074. * The system maintains a static default century start date and Year. They are
  1075. * initialized the first time they are used. Once the system default century date
  1076. * and year are set, they do not change.
  1077. */
  1078. static UDate gSystemDefaultCenturyStart = DBL_MIN;
  1079. static int32_t gSystemDefaultCenturyStartYear = -1;
  1080. static icu::UInitOnce gSystemDefaultCenturyInit {};
  1081. UBool GregorianCalendar::haveDefaultCentury() const
  1082. {
  1083. return true;
  1084. }
  1085. static void U_CALLCONV
  1086. initializeSystemDefaultCentury()
  1087. {
  1088. // initialize systemDefaultCentury and systemDefaultCenturyYear based
  1089. // on the current time. They'll be set to 80 years before
  1090. // the current time.
  1091. UErrorCode status = U_ZERO_ERROR;
  1092. GregorianCalendar calendar(status);
  1093. if (U_SUCCESS(status)) {
  1094. calendar.setTime(Calendar::getNow(), status);
  1095. calendar.add(UCAL_YEAR, -80, status);
  1096. gSystemDefaultCenturyStart = calendar.getTime(status);
  1097. gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
  1098. }
  1099. // We have no recourse upon failure unless we want to propagate the failure
  1100. // out.
  1101. }
  1102. UDate GregorianCalendar::defaultCenturyStart() const {
  1103. // lazy-evaluate systemDefaultCenturyStart
  1104. umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
  1105. return gSystemDefaultCenturyStart;
  1106. }
  1107. int32_t GregorianCalendar::defaultCenturyStartYear() const {
  1108. // lazy-evaluate systemDefaultCenturyStartYear
  1109. umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
  1110. return gSystemDefaultCenturyStartYear;
  1111. }
  1112. U_NAMESPACE_END
  1113. #endif /* #if !UCONFIG_NO_FORMATTING */
  1114. //eof