HijriCalendar.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Globalization
  5. {
  6. /// <remarks>
  7. /// Rules for the Hijri calendar:
  8. /// - The Hijri calendar is a strictly Lunar calendar.
  9. /// - Days begin at sunset.
  10. /// - Islamic Year 1 (Muharram 1, 1 A.H.) is equivalent to absolute date
  11. /// 227015 (Friday, July 16, 622 C.E. - Julian).
  12. /// - Leap Years occur in the 2, 5, 7, 10, 13, 16, 18, 21, 24, 26, &amp; 29th
  13. /// years of a 30-year cycle. Year = leap iff ((11y+14) mod 30 &lt; 11).
  14. /// - There are 12 months which contain alternately 30 and 29 days.
  15. /// - The 12th month, Dhu al-Hijjah, contains 30 days instead of 29 days
  16. /// in a leap year.
  17. /// - Common years have 354 days. Leap years have 355 days.
  18. /// - There are 10,631 days in a 30-year cycle.
  19. /// - The Islamic months are:
  20. /// 1. Muharram (30 days) 7. Rajab (30 days)
  21. /// 2. Safar (29 days) 8. Sha'ban (29 days)
  22. /// 3. Rabi I (30 days) 9. Ramadan (30 days)
  23. /// 4. Rabi II (29 days) 10. Shawwal (29 days)
  24. /// 5. Jumada I (30 days) 11. Dhu al-Qada (30 days)
  25. /// 6. Jumada II (29 days) 12. Dhu al-Hijjah (29 days) {30}
  26. ///
  27. /// NOTENOTE
  28. /// The calculation of the HijriCalendar is based on the absolute date. And the
  29. /// absolute date means the number of days from January 1st, 1 A.D.
  30. /// Therefore, we do not support the days before the January 1st, 1 A.D.
  31. ///
  32. /// Calendar support range:
  33. /// Calendar Minimum Maximum
  34. /// ========== ========== ==========
  35. /// Gregorian 0622/07/18 9999/12/31
  36. /// Hijri 0001/01/01 9666/04/03
  37. /// </remarks>
  38. public partial class HijriCalendar : Calendar
  39. {
  40. public static readonly int HijriEra = 1;
  41. private const int DatePartYear = 0;
  42. private const int DatePartDayOfYear = 1;
  43. private const int DatePartMonth = 2;
  44. private const int DatePartDay = 3;
  45. private const int MinAdvancedHijri = -2;
  46. private const int MaxAdvancedHijri = 2;
  47. private static readonly int[] s_hijriMonthDays = { 0, 30, 59, 89, 118, 148, 177, 207, 236, 266, 295, 325, 355 };
  48. private int _hijriAdvance = int.MinValue;
  49. // DateTime.MaxValue = Hijri calendar (year:9666, month: 4, day: 3).
  50. private const int MaxCalendarYear = 9666;
  51. private const int MaxCalendarMonth = 4;
  52. // Hijri calendar (year: 1, month: 1, day:1 ) = Gregorian (year: 622, month: 7, day: 18)
  53. // This is the minimal Gregorian date that we support in the HijriCalendar.
  54. private static readonly DateTime s_calendarMinValue = new DateTime(622, 7, 18);
  55. private static readonly DateTime s_calendarMaxValue = DateTime.MaxValue;
  56. public override DateTime MinSupportedDateTime => s_calendarMinValue;
  57. public override DateTime MaxSupportedDateTime => s_calendarMaxValue;
  58. public override CalendarAlgorithmType AlgorithmType => CalendarAlgorithmType.LunarCalendar;
  59. public HijriCalendar()
  60. {
  61. }
  62. internal override CalendarId ID => CalendarId.HIJRI;
  63. protected override int DaysInYearBeforeMinSupportedYear
  64. {
  65. get
  66. {
  67. // the year before the 1st year of the cycle would have been the 30th year
  68. // of the previous cycle which is not a leap year. Common years have 354 days.
  69. return 354;
  70. }
  71. }
  72. private long GetAbsoluteDateHijri(int y, int m, int d)
  73. {
  74. return (long)(DaysUpToHijriYear(y) + s_hijriMonthDays[m - 1] + d - 1 - HijriAdjustment);
  75. }
  76. private long DaysUpToHijriYear(int HijriYear)
  77. {
  78. // Compute the number of years up to the current 30 year cycle.
  79. int numYear30 = ((HijriYear - 1) / 30) * 30;
  80. // Compute the number of years left. This is the number of years
  81. // into the 30 year cycle for the given year.
  82. int numYearsLeft = HijriYear - numYear30 - 1;
  83. // Compute the number of absolute days up to the given year.
  84. long numDays = ((numYear30 * 10631L) / 30L) + 227013L;
  85. while (numYearsLeft > 0)
  86. {
  87. // Common year is 354 days, and leap year is 355 days.
  88. numDays += 354 + (IsLeapYear(numYearsLeft, CurrentEra) ? 1 : 0);
  89. numYearsLeft--;
  90. }
  91. return numDays;
  92. }
  93. public int HijriAdjustment
  94. {
  95. get
  96. {
  97. if (_hijriAdvance == int.MinValue)
  98. {
  99. // Never been set before. Use the system value from registry.
  100. _hijriAdvance = GetHijriDateAdjustment();
  101. }
  102. return _hijriAdvance;
  103. }
  104. set
  105. {
  106. if (value < MinAdvancedHijri || value > MaxAdvancedHijri)
  107. {
  108. throw new ArgumentOutOfRangeException(
  109. nameof(value),
  110. value,
  111. SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, MinAdvancedHijri, MaxAdvancedHijri));
  112. }
  113. VerifyWritable();
  114. _hijriAdvance = value;
  115. }
  116. }
  117. internal static void CheckTicksRange(long ticks)
  118. {
  119. if (ticks < s_calendarMinValue.Ticks || ticks > s_calendarMaxValue.Ticks)
  120. {
  121. throw new ArgumentOutOfRangeException(
  122. "time",
  123. ticks,
  124. SR.Format(
  125. CultureInfo.InvariantCulture,
  126. SR.ArgumentOutOfRange_CalendarRange,
  127. s_calendarMinValue,
  128. s_calendarMaxValue));
  129. }
  130. }
  131. internal static void CheckEraRange(int era)
  132. {
  133. if (era != CurrentEra && era != HijriEra)
  134. {
  135. throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue);
  136. }
  137. }
  138. internal static void CheckYearRange(int year, int era)
  139. {
  140. CheckEraRange(era);
  141. if (year < 1 || year > MaxCalendarYear)
  142. {
  143. throw new ArgumentOutOfRangeException(
  144. nameof(year),
  145. SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarYear));
  146. }
  147. }
  148. internal static void CheckYearMonthRange(int year, int month, int era)
  149. {
  150. CheckYearRange(year, era);
  151. if (year == MaxCalendarYear)
  152. {
  153. if (month > MaxCalendarMonth)
  154. {
  155. throw new ArgumentOutOfRangeException(
  156. nameof(month),
  157. month,
  158. SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarMonth));
  159. }
  160. }
  161. if (month < 1 || month > 12)
  162. {
  163. throw new ArgumentOutOfRangeException(nameof(month), month, SR.ArgumentOutOfRange_Month);
  164. }
  165. }
  166. /// <summary>
  167. /// First, we get the absolute date (the number of days from January 1st, 1 A.C) for the given ticks.
  168. /// Use the formula (((AbsoluteDate - 227013) * 30) / 10631) + 1, we can a rough value for the Hijri year.
  169. /// In order to get the exact Hijri year, we compare the exact absolute date for HijriYear and (HijriYear + 1).
  170. /// From here, we can get the correct Hijri year.
  171. /// </summary>
  172. internal virtual int GetDatePart(long ticks, int part)
  173. {
  174. CheckTicksRange(ticks);
  175. // Get the absolute date. The absolute date is the number of days from January 1st, 1 A.D.
  176. // 1/1/0001 is absolute date 1.
  177. long numDays = ticks / GregorianCalendar.TicksPerDay + 1;
  178. // See how much we need to backup or advance
  179. numDays += HijriAdjustment;
  180. // Calculate the appromixate Hijri Year from this magic formula.
  181. int hijriYear = (int)(((numDays - 227013) * 30) / 10631) + 1;
  182. long daysToHijriYear = DaysUpToHijriYear(hijriYear); // The absolute date for HijriYear
  183. long daysOfHijriYear = GetDaysInYear(hijriYear, CurrentEra); // The number of days for (HijriYear+1) year.
  184. if (numDays < daysToHijriYear)
  185. {
  186. daysToHijriYear -= daysOfHijriYear;
  187. hijriYear--;
  188. }
  189. else if (numDays == daysToHijriYear)
  190. {
  191. hijriYear--;
  192. daysToHijriYear -= GetDaysInYear(hijriYear, CurrentEra);
  193. }
  194. else
  195. {
  196. if (numDays > daysToHijriYear + daysOfHijriYear)
  197. {
  198. daysToHijriYear += daysOfHijriYear;
  199. hijriYear++;
  200. }
  201. }
  202. if (part == DatePartYear)
  203. {
  204. return hijriYear;
  205. }
  206. // Calculate the Hijri Month.
  207. int hijriMonth = 1;
  208. numDays -= daysToHijriYear;
  209. if (part == DatePartDayOfYear)
  210. {
  211. return ((int)numDays);
  212. }
  213. while ((hijriMonth <= 12) && (numDays > s_hijriMonthDays[hijriMonth - 1]))
  214. {
  215. hijriMonth++;
  216. }
  217. hijriMonth--;
  218. if (part == DatePartMonth)
  219. {
  220. return hijriMonth;
  221. }
  222. // Calculate the Hijri Day.
  223. int hijriDay = (int)(numDays - s_hijriMonthDays[hijriMonth - 1]);
  224. if (part == DatePartDay)
  225. {
  226. return hijriDay;
  227. }
  228. // Incorrect part value.
  229. throw new InvalidOperationException(SR.InvalidOperation_DateTimeParsing);
  230. }
  231. public override DateTime AddMonths(DateTime time, int months)
  232. {
  233. if (months < -120000 || months > 120000)
  234. {
  235. throw new ArgumentOutOfRangeException(
  236. nameof(months),
  237. months,
  238. SR.Format(SR.ArgumentOutOfRange_Range, -120000, 120000));
  239. }
  240. // Get the date in Hijri calendar.
  241. int y = GetDatePart(time.Ticks, DatePartYear);
  242. int m = GetDatePart(time.Ticks, DatePartMonth);
  243. int d = GetDatePart(time.Ticks, DatePartDay);
  244. int i = m - 1 + months;
  245. if (i >= 0)
  246. {
  247. m = i % 12 + 1;
  248. y = y + i / 12;
  249. }
  250. else
  251. {
  252. m = 12 + (i + 1) % 12;
  253. y = y + (i - 11) / 12;
  254. }
  255. int days = GetDaysInMonth(y, m);
  256. if (d > days)
  257. {
  258. d = days;
  259. }
  260. long ticks = GetAbsoluteDateHijri(y, m, d) * TicksPerDay + (time.Ticks % TicksPerDay);
  261. Calendar.CheckAddResult(ticks, MinSupportedDateTime, MaxSupportedDateTime);
  262. return new DateTime(ticks);
  263. }
  264. public override DateTime AddYears(DateTime time, int years)
  265. {
  266. return (AddMonths(time, years * 12));
  267. }
  268. public override int GetDayOfMonth(DateTime time)
  269. {
  270. return GetDatePart(time.Ticks, DatePartDay);
  271. }
  272. public override DayOfWeek GetDayOfWeek(DateTime time)
  273. {
  274. return (DayOfWeek)((int)(time.Ticks / TicksPerDay + 1) % 7);
  275. }
  276. public override int GetDayOfYear(DateTime time)
  277. {
  278. return GetDatePart(time.Ticks, DatePartDayOfYear);
  279. }
  280. public override int GetDaysInMonth(int year, int month, int era)
  281. {
  282. CheckYearMonthRange(year, month, era);
  283. if (month == 12)
  284. {
  285. // For the 12th month, leap year has 30 days, and common year has 29 days.
  286. return IsLeapYear(year, CurrentEra) ? 30 : 29;
  287. }
  288. // Other months contain 30 and 29 days alternatively. The 1st month has 30 days.
  289. return ((month % 2) == 1) ? 30 : 29;
  290. }
  291. public override int GetDaysInYear(int year, int era)
  292. {
  293. CheckYearRange(year, era);
  294. // Common years have 354 days. Leap years have 355 days.
  295. return IsLeapYear(year, CurrentEra) ? 355 : 354;
  296. }
  297. public override int GetEra(DateTime time)
  298. {
  299. CheckTicksRange(time.Ticks);
  300. return HijriEra;
  301. }
  302. public override int[] Eras => new int[] { HijriEra };
  303. public override int GetMonth(DateTime time)
  304. {
  305. return GetDatePart(time.Ticks, DatePartMonth);
  306. }
  307. public override int GetMonthsInYear(int year, int era)
  308. {
  309. CheckYearRange(year, era);
  310. return 12;
  311. }
  312. public override int GetYear(DateTime time)
  313. {
  314. return GetDatePart(time.Ticks, DatePartYear);
  315. }
  316. public override bool IsLeapDay(int year, int month, int day, int era)
  317. {
  318. // The year/month/era value checking is done in GetDaysInMonth().
  319. int daysInMonth = GetDaysInMonth(year, month, era);
  320. if (day < 1 || day > daysInMonth)
  321. {
  322. throw new ArgumentOutOfRangeException(
  323. nameof(day),
  324. day,
  325. SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month));
  326. }
  327. return IsLeapYear(year, era) && month == 12 && day == 30;
  328. }
  329. public override int GetLeapMonth(int year, int era)
  330. {
  331. CheckYearRange(year, era);
  332. return 0;
  333. }
  334. public override bool IsLeapMonth(int year, int month, int era)
  335. {
  336. CheckYearMonthRange(year, month, era);
  337. return false;
  338. }
  339. public override bool IsLeapYear(int year, int era)
  340. {
  341. CheckYearRange(year, era);
  342. return (((year * 11) + 14) % 30) < 11;
  343. }
  344. public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  345. {
  346. // The year/month/era checking is done in GetDaysInMonth().
  347. int daysInMonth = GetDaysInMonth(year, month, era);
  348. if (day < 1 || day > daysInMonth)
  349. {
  350. throw new ArgumentOutOfRangeException(
  351. nameof(day),
  352. day,
  353. SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month));
  354. }
  355. long lDate = GetAbsoluteDateHijri(year, month, day);
  356. if (lDate < 0)
  357. {
  358. throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadYearMonthDay);
  359. }
  360. return new DateTime(lDate * GregorianCalendar.TicksPerDay + TimeToTicks(hour, minute, second, millisecond));
  361. }
  362. private const int DefaultTwoDigitYearMax = 1451;
  363. public override int TwoDigitYearMax
  364. {
  365. get
  366. {
  367. if (_twoDigitYearMax == -1)
  368. {
  369. _twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DefaultTwoDigitYearMax);
  370. }
  371. return _twoDigitYearMax;
  372. }
  373. set
  374. {
  375. VerifyWritable();
  376. if (value < 99 || value > MaxCalendarYear)
  377. {
  378. throw new ArgumentOutOfRangeException(
  379. nameof(value),
  380. value,
  381. SR.Format(SR.ArgumentOutOfRange_Range, 99, MaxCalendarYear));
  382. }
  383. _twoDigitYearMax = value;
  384. }
  385. }
  386. public override int ToFourDigitYear(int year)
  387. {
  388. if (year < 0)
  389. {
  390. throw new ArgumentOutOfRangeException(nameof(year), year, SR.ArgumentOutOfRange_NeedNonNegNum);
  391. }
  392. if (year < 100)
  393. {
  394. return base.ToFourDigitYear(year);
  395. }
  396. if (year > MaxCalendarYear)
  397. {
  398. throw new ArgumentOutOfRangeException(
  399. nameof(year),
  400. year,
  401. SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarYear));
  402. }
  403. return year;
  404. }
  405. }
  406. }