Fix IDE0025 (use expression body for properties)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Globalization / HijriCalendar.cs
blob20149af38dd61e71d212fd97470fd34155277ede
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.
5 namespace System.Globalization
7 /// <remarks>
8 /// Rules for the Hijri calendar:
9 /// - The Hijri calendar is a strictly Lunar calendar.
10 /// - Days begin at sunset.
11 /// - Islamic Year 1 (Muharram 1, 1 A.H.) is equivalent to absolute date
12 /// 227015 (Friday, July 16, 622 C.E. - Julian).
13 /// - Leap Years occur in the 2, 5, 7, 10, 13, 16, 18, 21, 24, 26, &amp; 29th
14 /// years of a 30-year cycle. Year = leap iff ((11y+14) mod 30 &lt; 11).
15 /// - There are 12 months which contain alternately 30 and 29 days.
16 /// - The 12th month, Dhu al-Hijjah, contains 30 days instead of 29 days
17 /// in a leap year.
18 /// - Common years have 354 days. Leap years have 355 days.
19 /// - There are 10,631 days in a 30-year cycle.
20 /// - The Islamic months are:
21 /// 1. Muharram (30 days) 7. Rajab (30 days)
22 /// 2. Safar (29 days) 8. Sha'ban (29 days)
23 /// 3. Rabi I (30 days) 9. Ramadan (30 days)
24 /// 4. Rabi II (29 days) 10. Shawwal (29 days)
25 /// 5. Jumada I (30 days) 11. Dhu al-Qada (30 days)
26 /// 6. Jumada II (29 days) 12. Dhu al-Hijjah (29 days) {30}
27 ///
28 /// NOTENOTE
29 /// The calculation of the HijriCalendar is based on the absolute date. And the
30 /// absolute date means the number of days from January 1st, 1 A.D.
31 /// Therefore, we do not support the days before the January 1st, 1 A.D.
32 ///
33 /// Calendar support range:
34 /// Calendar Minimum Maximum
35 /// ========== ========== ==========
36 /// Gregorian 0622/07/18 9999/12/31
37 /// Hijri 0001/01/01 9666/04/03
38 /// </remarks>
40 public partial class HijriCalendar : Calendar
42 public static readonly int HijriEra = 1;
44 private const int DatePartYear = 0;
45 private const int DatePartDayOfYear = 1;
46 private const int DatePartMonth = 2;
47 private const int DatePartDay = 3;
49 private const int MinAdvancedHijri = -2;
50 private const int MaxAdvancedHijri = 2;
52 private static readonly int[] s_hijriMonthDays = { 0, 30, 59, 89, 118, 148, 177, 207, 236, 266, 295, 325, 355 };
54 private int _hijriAdvance = int.MinValue;
56 // DateTime.MaxValue = Hijri calendar (year:9666, month: 4, day: 3).
57 private const int MaxCalendarYear = 9666;
58 private const int MaxCalendarMonth = 4;
60 // Hijri calendar (year: 1, month: 1, day:1 ) = Gregorian (year: 622, month: 7, day: 18)
61 // This is the minimal Gregorian date that we support in the HijriCalendar.
62 private static readonly DateTime s_calendarMinValue = new DateTime(622, 7, 18);
63 private static readonly DateTime s_calendarMaxValue = DateTime.MaxValue;
65 public override DateTime MinSupportedDateTime => s_calendarMinValue;
67 public override DateTime MaxSupportedDateTime => s_calendarMaxValue;
69 public override CalendarAlgorithmType AlgorithmType => CalendarAlgorithmType.LunarCalendar;
71 public HijriCalendar()
75 internal override CalendarId ID => CalendarId.HIJRI;
77 protected override int DaysInYearBeforeMinSupportedYear =>
78 // the year before the 1st year of the cycle would have been the 30th year
79 // of the previous cycle which is not a leap year. Common years have 354 days.
80 354;
82 private long GetAbsoluteDateHijri(int y, int m, int d)
84 return (long)(DaysUpToHijriYear(y) + s_hijriMonthDays[m - 1] + d - 1 - HijriAdjustment);
87 private long DaysUpToHijriYear(int HijriYear)
89 // Compute the number of years up to the current 30 year cycle.
90 int numYear30 = ((HijriYear - 1) / 30) * 30;
92 // Compute the number of years left. This is the number of years
93 // into the 30 year cycle for the given year.
94 int numYearsLeft = HijriYear - numYear30 - 1;
96 // Compute the number of absolute days up to the given year.
97 long numDays = ((numYear30 * 10631L) / 30L) + 227013L;
98 while (numYearsLeft > 0)
100 // Common year is 354 days, and leap year is 355 days.
101 numDays += 354 + (IsLeapYear(numYearsLeft, CurrentEra) ? 1 : 0);
102 numYearsLeft--;
105 return numDays;
108 public int HijriAdjustment
112 if (_hijriAdvance == int.MinValue)
114 // Never been set before. Use the system value from registry.
115 _hijriAdvance = GetHijriDateAdjustment();
118 return _hijriAdvance;
123 if (value < MinAdvancedHijri || value > MaxAdvancedHijri)
125 throw new ArgumentOutOfRangeException(
126 nameof(value),
127 value,
128 SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, MinAdvancedHijri, MaxAdvancedHijri));
131 VerifyWritable();
132 _hijriAdvance = value;
136 internal static void CheckTicksRange(long ticks)
138 if (ticks < s_calendarMinValue.Ticks || ticks > s_calendarMaxValue.Ticks)
140 throw new ArgumentOutOfRangeException(
141 "time",
142 ticks,
143 SR.Format(
144 CultureInfo.InvariantCulture,
145 SR.ArgumentOutOfRange_CalendarRange,
146 s_calendarMinValue,
147 s_calendarMaxValue));
151 internal static void CheckEraRange(int era)
153 if (era != CurrentEra && era != HijriEra)
155 throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue);
159 internal static void CheckYearRange(int year, int era)
161 CheckEraRange(era);
162 if (year < 1 || year > MaxCalendarYear)
164 throw new ArgumentOutOfRangeException(
165 nameof(year),
166 SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarYear));
170 internal static void CheckYearMonthRange(int year, int month, int era)
172 CheckYearRange(year, era);
173 if (year == MaxCalendarYear)
175 if (month > MaxCalendarMonth)
177 throw new ArgumentOutOfRangeException(
178 nameof(month),
179 month,
180 SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarMonth));
184 if (month < 1 || month > 12)
186 throw new ArgumentOutOfRangeException(nameof(month), month, SR.ArgumentOutOfRange_Month);
190 /// <summary>
191 /// First, we get the absolute date (the number of days from January 1st, 1 A.C) for the given ticks.
192 /// Use the formula (((AbsoluteDate - 227013) * 30) / 10631) + 1, we can a rough value for the Hijri year.
193 /// In order to get the exact Hijri year, we compare the exact absolute date for HijriYear and (HijriYear + 1).
194 /// From here, we can get the correct Hijri year.
195 /// </summary>
196 internal virtual int GetDatePart(long ticks, int part)
198 CheckTicksRange(ticks);
200 // Get the absolute date. The absolute date is the number of days from January 1st, 1 A.D.
201 // 1/1/0001 is absolute date 1.
202 long numDays = ticks / GregorianCalendar.TicksPerDay + 1;
204 // See how much we need to backup or advance
205 numDays += HijriAdjustment;
207 // Calculate the appromixate Hijri Year from this magic formula.
208 int hijriYear = (int)(((numDays - 227013) * 30) / 10631) + 1;
210 long daysToHijriYear = DaysUpToHijriYear(hijriYear); // The absolute date for HijriYear
211 long daysOfHijriYear = GetDaysInYear(hijriYear, CurrentEra); // The number of days for (HijriYear+1) year.
213 if (numDays < daysToHijriYear)
215 daysToHijriYear -= daysOfHijriYear;
216 hijriYear--;
218 else if (numDays == daysToHijriYear)
220 hijriYear--;
221 daysToHijriYear -= GetDaysInYear(hijriYear, CurrentEra);
223 else
225 if (numDays > daysToHijriYear + daysOfHijriYear)
227 daysToHijriYear += daysOfHijriYear;
228 hijriYear++;
231 if (part == DatePartYear)
233 return hijriYear;
236 // Calculate the Hijri Month.
237 int hijriMonth = 1;
238 numDays -= daysToHijriYear;
240 if (part == DatePartDayOfYear)
242 return ((int)numDays);
245 while ((hijriMonth <= 12) && (numDays > s_hijriMonthDays[hijriMonth - 1]))
247 hijriMonth++;
249 hijriMonth--;
251 if (part == DatePartMonth)
253 return hijriMonth;
256 // Calculate the Hijri Day.
257 int hijriDay = (int)(numDays - s_hijriMonthDays[hijriMonth - 1]);
259 if (part == DatePartDay)
261 return hijriDay;
264 // Incorrect part value.
265 throw new InvalidOperationException(SR.InvalidOperation_DateTimeParsing);
268 public override DateTime AddMonths(DateTime time, int months)
270 if (months < -120000 || months > 120000)
272 throw new ArgumentOutOfRangeException(
273 nameof(months),
274 months,
275 SR.Format(SR.ArgumentOutOfRange_Range, -120000, 120000));
278 // Get the date in Hijri calendar.
279 int y = GetDatePart(time.Ticks, DatePartYear);
280 int m = GetDatePart(time.Ticks, DatePartMonth);
281 int d = GetDatePart(time.Ticks, DatePartDay);
282 int i = m - 1 + months;
283 if (i >= 0)
285 m = i % 12 + 1;
286 y += i / 12;
288 else
290 m = 12 + (i + 1) % 12;
291 y += (i - 11) / 12;
294 int days = GetDaysInMonth(y, m);
295 if (d > days)
297 d = days;
300 long ticks = GetAbsoluteDateHijri(y, m, d) * TicksPerDay + (time.Ticks % TicksPerDay);
301 Calendar.CheckAddResult(ticks, MinSupportedDateTime, MaxSupportedDateTime);
302 return new DateTime(ticks);
305 public override DateTime AddYears(DateTime time, int years)
307 return (AddMonths(time, years * 12));
310 public override int GetDayOfMonth(DateTime time)
312 return GetDatePart(time.Ticks, DatePartDay);
315 public override DayOfWeek GetDayOfWeek(DateTime time)
317 return (DayOfWeek)((int)(time.Ticks / TicksPerDay + 1) % 7);
320 public override int GetDayOfYear(DateTime time)
322 return GetDatePart(time.Ticks, DatePartDayOfYear);
325 public override int GetDaysInMonth(int year, int month, int era)
327 CheckYearMonthRange(year, month, era);
328 if (month == 12)
330 // For the 12th month, leap year has 30 days, and common year has 29 days.
331 return IsLeapYear(year, CurrentEra) ? 30 : 29;
334 // Other months contain 30 and 29 days alternatively. The 1st month has 30 days.
335 return ((month % 2) == 1) ? 30 : 29;
338 public override int GetDaysInYear(int year, int era)
340 CheckYearRange(year, era);
341 // Common years have 354 days. Leap years have 355 days.
342 return IsLeapYear(year, CurrentEra) ? 355 : 354;
345 public override int GetEra(DateTime time)
347 CheckTicksRange(time.Ticks);
348 return HijriEra;
351 public override int[] Eras => new int[] { HijriEra };
353 public override int GetMonth(DateTime time)
355 return GetDatePart(time.Ticks, DatePartMonth);
358 public override int GetMonthsInYear(int year, int era)
360 CheckYearRange(year, era);
361 return 12;
364 public override int GetYear(DateTime time)
366 return GetDatePart(time.Ticks, DatePartYear);
369 public override bool IsLeapDay(int year, int month, int day, int era)
371 // The year/month/era value checking is done in GetDaysInMonth().
372 int daysInMonth = GetDaysInMonth(year, month, era);
373 if (day < 1 || day > daysInMonth)
375 throw new ArgumentOutOfRangeException(
376 nameof(day),
377 day,
378 SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month));
381 return IsLeapYear(year, era) && month == 12 && day == 30;
384 public override int GetLeapMonth(int year, int era)
386 CheckYearRange(year, era);
387 return 0;
390 public override bool IsLeapMonth(int year, int month, int era)
392 CheckYearMonthRange(year, month, era);
393 return false;
396 public override bool IsLeapYear(int year, int era)
398 CheckYearRange(year, era);
399 return (((year * 11) + 14) % 30) < 11;
402 public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
404 // The year/month/era checking is done in GetDaysInMonth().
405 int daysInMonth = GetDaysInMonth(year, month, era);
406 if (day < 1 || day > daysInMonth)
408 throw new ArgumentOutOfRangeException(
409 nameof(day),
410 day,
411 SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month));
414 long lDate = GetAbsoluteDateHijri(year, month, day);
415 if (lDate < 0)
417 throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadYearMonthDay);
420 return new DateTime(lDate * GregorianCalendar.TicksPerDay + TimeToTicks(hour, minute, second, millisecond));
423 private const int DefaultTwoDigitYearMax = 1451;
425 public override int TwoDigitYearMax
429 if (_twoDigitYearMax == -1)
431 _twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DefaultTwoDigitYearMax);
434 return _twoDigitYearMax;
438 VerifyWritable();
439 if (value < 99 || value > MaxCalendarYear)
441 throw new ArgumentOutOfRangeException(
442 nameof(value),
443 value,
444 SR.Format(SR.ArgumentOutOfRange_Range, 99, MaxCalendarYear));
447 _twoDigitYearMax = value;
451 public override int ToFourDigitYear(int year)
453 if (year < 0)
455 throw new ArgumentOutOfRangeException(nameof(year), year, SR.ArgumentOutOfRange_NeedNonNegNum);
458 if (year < 100)
460 return base.ToFourDigitYear(year);
463 if (year > MaxCalendarYear)
465 throw new ArgumentOutOfRangeException(
466 nameof(year),
467 year,
468 SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarYear));
470 return year;