indiancal.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. * Copyright (C) 2003-2014, International Business Machines Corporation
  5. * and others. All Rights Reserved.
  6. ******************************************************************************
  7. *
  8. * File INDIANCAL.CPP
  9. *****************************************************************************
  10. */
  11. #include "indiancal.h"
  12. #include <stdlib.h>
  13. #if !UCONFIG_NO_FORMATTING
  14. #include "mutex.h"
  15. #include <float.h>
  16. #include "gregoimp.h" // Math
  17. #include "uhash.h"
  18. // Debugging
  19. #ifdef U_DEBUG_INDIANCAL
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #endif
  23. U_NAMESPACE_BEGIN
  24. // Implementation of the IndianCalendar class
  25. //-------------------------------------------------------------------------
  26. // Constructors...
  27. //-------------------------------------------------------------------------
  28. IndianCalendar* IndianCalendar::clone() const {
  29. return new IndianCalendar(*this);
  30. }
  31. IndianCalendar::IndianCalendar(const Locale& aLocale, UErrorCode& success)
  32. : Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)
  33. {
  34. setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
  35. }
  36. IndianCalendar::IndianCalendar(const IndianCalendar& other) : Calendar(other) {
  37. }
  38. IndianCalendar::~IndianCalendar()
  39. {
  40. }
  41. const char *IndianCalendar::getType() const {
  42. return "indian";
  43. }
  44. static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
  45. // Minimum Greatest Least Maximum
  46. // Minimum Maximum
  47. { 0, 0, 0, 0}, // ERA
  48. { -5000000, -5000000, 5000000, 5000000}, // YEAR
  49. { 0, 0, 11, 11}, // MONTH
  50. { 1, 1, 52, 53}, // WEEK_OF_YEAR
  51. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
  52. { 1, 1, 30, 31}, // DAY_OF_MONTH
  53. { 1, 1, 365, 366}, // DAY_OF_YEAR
  54. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
  55. { -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH
  56. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
  57. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
  58. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
  59. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
  60. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
  61. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
  62. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
  63. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
  64. { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY
  65. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
  66. { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR
  67. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
  68. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
  69. {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
  70. { 0, 0, 11, 11}, // ORDINAL_MONTH
  71. };
  72. static const int32_t INDIAN_ERA_START = 78;
  73. static const int32_t INDIAN_YEAR_START = 80;
  74. int32_t IndianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
  75. return LIMITS[field][limitType];
  76. }
  77. /*
  78. * Determine whether the given gregorian year is a Leap year
  79. */
  80. static UBool isGregorianLeap(int32_t year)
  81. {
  82. return Grego::isLeapYear(year);
  83. }
  84. //----------------------------------------------------------------------
  85. // Calendar framework
  86. //----------------------------------------------------------------------
  87. /*
  88. * Return the length (in days) of the given month.
  89. *
  90. * @param eyear The year in Saka Era
  91. * @param month The month(0-based) in Indian calendar
  92. */
  93. int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month) const {
  94. if (month < 0 || month > 11) {
  95. eyear += ClockMath::floorDivide(month, 12, &month);
  96. }
  97. if (isGregorianLeap(eyear + INDIAN_ERA_START) && month == 0) {
  98. return 31;
  99. }
  100. if (month >= 1 && month <= 5) {
  101. return 31;
  102. }
  103. return 30;
  104. }
  105. /*
  106. * Return the number of days in the given Indian year
  107. *
  108. * @param eyear The year in Saka Era.
  109. */
  110. int32_t IndianCalendar::handleGetYearLength(int32_t eyear) const {
  111. return isGregorianLeap(eyear + INDIAN_ERA_START) ? 366 : 365;
  112. }
  113. /*
  114. * Returns the Julian Day corresponding to gregorian date
  115. *
  116. * @param year The Gregorian year
  117. * @param month The month in Gregorian Year, 0 based.
  118. * @param date The date in Gregorian day in month
  119. */
  120. static double gregorianToJD(int32_t year, int32_t month, int32_t date) {
  121. return Grego::fieldsToDay(year, month, date) + kEpochStartAsJulianDay - 0.5;
  122. }
  123. /*
  124. * Returns the Gregorian Date corresponding to a given Julian Day
  125. * Month is 0 based.
  126. * @param jd The Julian Day
  127. */
  128. static int32_t* jdToGregorian(double jd, int32_t gregorianDate[3]) {
  129. int32_t gdow;
  130. Grego::dayToFields(jd - kEpochStartAsJulianDay,
  131. gregorianDate[0], gregorianDate[1], gregorianDate[2], gdow);
  132. return gregorianDate;
  133. }
  134. //-------------------------------------------------------------------------
  135. // Functions for converting from field values to milliseconds....
  136. //-------------------------------------------------------------------------
  137. static double IndianToJD(int32_t year, int32_t month, int32_t date) {
  138. int32_t leapMonth, gyear, m;
  139. double start, jd;
  140. gyear = year + INDIAN_ERA_START;
  141. if(isGregorianLeap(gyear)) {
  142. leapMonth = 31;
  143. start = gregorianToJD(gyear, 2 /* The third month in 0 based month */, 21);
  144. }
  145. else {
  146. leapMonth = 30;
  147. start = gregorianToJD(gyear, 2 /* The third month in 0 based month */, 22);
  148. }
  149. if (month == 1) {
  150. jd = start + (date - 1);
  151. } else {
  152. jd = start + leapMonth;
  153. m = month - 2;
  154. //m = Math.min(m, 5);
  155. if (m > 5) {
  156. m = 5;
  157. }
  158. jd += m * 31;
  159. if (month >= 8) {
  160. m = month - 7;
  161. jd += m * 30;
  162. }
  163. jd += date - 1;
  164. }
  165. return jd;
  166. }
  167. /*
  168. * Return JD of start of given month/year of Indian Calendar
  169. * @param eyear The year in Indian Calendar measured from Saka Era (78 AD).
  170. * @param month The month in Indian calendar
  171. */
  172. int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */ ) const {
  173. //month is 0 based; converting it to 1-based
  174. int32_t imonth;
  175. // If the month is out of range, adjust it into range, and adjust the extended year accordingly
  176. if (month < 0 || month > 11) {
  177. eyear += (int32_t)ClockMath::floorDivide(month, 12, &month);
  178. }
  179. if(month == 12){
  180. imonth = 1;
  181. } else {
  182. imonth = month + 1;
  183. }
  184. double jd = IndianToJD(eyear ,imonth, 1);
  185. return (int32_t)jd;
  186. }
  187. //-------------------------------------------------------------------------
  188. // Functions for converting from milliseconds to field values
  189. //-------------------------------------------------------------------------
  190. int32_t IndianCalendar::handleGetExtendedYear() {
  191. int32_t year;
  192. if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
  193. year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
  194. } else {
  195. year = internalGet(UCAL_YEAR, 1); // Default to year 1
  196. }
  197. return year;
  198. }
  199. /*
  200. * Override Calendar to compute several fields specific to the Indian
  201. * calendar system. These are:
  202. *
  203. * <ul><li>ERA
  204. * <li>YEAR
  205. * <li>MONTH
  206. * <li>DAY_OF_MONTH
  207. * <li>EXTENDED_YEAR</ul>
  208. *
  209. * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
  210. * method is called. The getGregorianXxx() methods return Gregorian
  211. * calendar equivalents for the given Julian day.
  212. */
  213. void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& /* status */) {
  214. double jdAtStartOfGregYear;
  215. int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday;
  216. int32_t gregorianYear; // Stores gregorian date corresponding to Julian day;
  217. int32_t gd[3];
  218. gregorianYear = jdToGregorian(julianDay, gd)[0]; // Gregorian date for Julian day
  219. IndianYear = gregorianYear - INDIAN_ERA_START; // Year in Saka era
  220. jdAtStartOfGregYear = gregorianToJD(gregorianYear, 0, 1); // JD at start of Gregorian year
  221. yday = (int32_t)(julianDay - jdAtStartOfGregYear); // Day number in Gregorian year (starting from 0)
  222. if (yday < INDIAN_YEAR_START) {
  223. // Day is at the end of the preceding Saka year
  224. IndianYear -= 1;
  225. leapMonth = isGregorianLeap(gregorianYear - 1) ? 31 : 30; // Days in leapMonth this year, previous Gregorian year
  226. yday += leapMonth + (31 * 5) + (30 * 3) + 10;
  227. } else {
  228. leapMonth = isGregorianLeap(gregorianYear) ? 31 : 30; // Days in leapMonth this year
  229. yday -= INDIAN_YEAR_START;
  230. }
  231. if (yday < leapMonth) {
  232. IndianMonth = 0;
  233. IndianDayOfMonth = yday + 1;
  234. } else {
  235. mday = yday - leapMonth;
  236. if (mday < (31 * 5)) {
  237. IndianMonth = (int32_t)uprv_floor(mday / 31) + 1;
  238. IndianDayOfMonth = (mday % 31) + 1;
  239. } else {
  240. mday -= 31 * 5;
  241. IndianMonth = (int32_t)uprv_floor(mday / 30) + 6;
  242. IndianDayOfMonth = (mday % 30) + 1;
  243. }
  244. }
  245. internalSet(UCAL_ERA, 0);
  246. internalSet(UCAL_EXTENDED_YEAR, IndianYear);
  247. internalSet(UCAL_YEAR, IndianYear);
  248. internalSet(UCAL_MONTH, IndianMonth);
  249. internalSet(UCAL_ORDINAL_MONTH, IndianMonth);
  250. internalSet(UCAL_DAY_OF_MONTH, IndianDayOfMonth);
  251. internalSet(UCAL_DAY_OF_YEAR, yday + 1); // yday is 0-based
  252. }
  253. constexpr uint32_t kIndianRelatedYearDiff = 79;
  254. int32_t IndianCalendar::getRelatedYear(UErrorCode &status) const
  255. {
  256. int32_t year = get(UCAL_EXTENDED_YEAR, status);
  257. if (U_FAILURE(status)) {
  258. return 0;
  259. }
  260. return year + kIndianRelatedYearDiff;
  261. }
  262. void IndianCalendar::setRelatedYear(int32_t year)
  263. {
  264. // set extended year
  265. set(UCAL_EXTENDED_YEAR, year - kIndianRelatedYearDiff);
  266. }
  267. /**
  268. * The system maintains a static default century start date and Year. They are
  269. * initialized the first time they are used. Once the system default century date
  270. * and year are set, they do not change.
  271. */
  272. static UDate gSystemDefaultCenturyStart = DBL_MIN;
  273. static int32_t gSystemDefaultCenturyStartYear = -1;
  274. static icu::UInitOnce gSystemDefaultCenturyInit {};
  275. UBool IndianCalendar::haveDefaultCentury() const
  276. {
  277. return true;
  278. }
  279. static void U_CALLCONV
  280. initializeSystemDefaultCentury()
  281. {
  282. // initialize systemDefaultCentury and systemDefaultCenturyYear based
  283. // on the current time. They'll be set to 80 years before
  284. // the current time.
  285. UErrorCode status = U_ZERO_ERROR;
  286. IndianCalendar calendar ( Locale ( "@calendar=Indian" ), status);
  287. if ( U_SUCCESS ( status ) ) {
  288. calendar.setTime ( Calendar::getNow(), status );
  289. calendar.add ( UCAL_YEAR, -80, status );
  290. UDate newStart = calendar.getTime ( status );
  291. int32_t newYear = calendar.get ( UCAL_YEAR, status );
  292. gSystemDefaultCenturyStart = newStart;
  293. gSystemDefaultCenturyStartYear = newYear;
  294. }
  295. // We have no recourse upon failure.
  296. }
  297. UDate
  298. IndianCalendar::defaultCenturyStart() const
  299. {
  300. // lazy-evaluate systemDefaultCenturyStart
  301. umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
  302. return gSystemDefaultCenturyStart;
  303. }
  304. int32_t
  305. IndianCalendar::defaultCenturyStartYear() const
  306. {
  307. // lazy-evaluate systemDefaultCenturyStartYear
  308. umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
  309. return gSystemDefaultCenturyStartYear;
  310. }
  311. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar)
  312. U_NAMESPACE_END
  313. #endif