| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Globalization |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Modern Persian calendar is a solar observation based calendar. Each new year begins on the day when the vernal e |
| | | 10 | | /// The epoch is the date of the vernal equinox prior to the epoch of the Islamic calendar (March 19, 622 Julian or |
| | | 11 | | /// There is no Persian year 0. Ordinary years have 365 days. Leap years have 366 days with the last month (Esfand) |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Calendar support range: |
| | | 15 | | /// Calendar Minimum Maximum |
| | | 16 | | /// ========== ========== ========== |
| | | 17 | | /// Gregorian 0622/03/22 9999/12/31 |
| | | 18 | | /// Persian 0001/01/01 9378/10/13 |
| | | 19 | | /// </remarks> |
| | | 20 | | public class PersianCalendar : Calendar |
| | | 21 | | { |
| | 0 | 22 | | public static readonly int PersianEra = 1; |
| | | 23 | | |
| | 0 | 24 | | private static readonly long s_persianEpoch = new DateTime(622, 3, 22).Ticks / TimeSpan.TicksPerDay; |
| | | 25 | | private const int ApproximateHalfYear = 180; |
| | | 26 | | |
| | | 27 | | private const int DatePartYear = 0; |
| | | 28 | | private const int DatePartDayOfYear = 1; |
| | | 29 | | private const int DatePartMonth = 2; |
| | | 30 | | private const int DatePartDay = 3; |
| | | 31 | | private const int MonthsPerYear = 12; |
| | | 32 | | |
| | 0 | 33 | | private static ReadOnlySpan<int> DaysToMonth => [0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 366]; |
| | | 34 | | |
| | | 35 | | private const int MaxCalendarYear = 9378; |
| | | 36 | | private const int MaxCalendarMonth = 10; |
| | | 37 | | private const int MaxCalendarDay = 13; |
| | | 38 | | |
| | | 39 | | // Persian calendar (year: 1, month: 1, day:1 ) = Gregorian (year: 622, month: 3, day: 22) |
| | | 40 | | // This is the minimal Gregorian date that we support in the PersianCalendar. |
| | 0 | 41 | | private static readonly DateTime s_minDate = new DateTime(622, 3, 22); |
| | 0 | 42 | | private static readonly DateTime s_maxDate = DateTime.MaxValue; |
| | | 43 | | |
| | 0 | 44 | | public override DateTime MinSupportedDateTime => s_minDate; |
| | | 45 | | |
| | 0 | 46 | | public override DateTime MaxSupportedDateTime => s_maxDate; |
| | | 47 | | |
| | 0 | 48 | | public override CalendarAlgorithmType AlgorithmType => CalendarAlgorithmType.SolarCalendar; |
| | | 49 | | |
| | 0 | 50 | | public PersianCalendar() |
| | | 51 | | { |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | internal override CalendarId BaseCalendarID => CalendarId.GREGORIAN; |
| | | 55 | | |
| | 0 | 56 | | internal override CalendarId ID => CalendarId.PERSIAN; |
| | | 57 | | |
| | | 58 | | private static long GetAbsoluteDatePersian(int year, int month, int day) |
| | | 59 | | { |
| | 0 | 60 | | if (year < 1 || year > MaxCalendarYear || month < 1 || month > 12) |
| | | 61 | | { |
| | 0 | 62 | | throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadYearMonthDay); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // day is one based, make 0 based since this will be the number of days we add to beginning of year below |
| | 0 | 66 | | int ordinalDay = DaysInPreviousMonths(month) + day - 1; |
| | 0 | 67 | | int approximateDaysFromEpochForYearStart = (int)(CalendricalCalculationsHelper.MeanTropicalYearInDays * (yea |
| | 0 | 68 | | long yearStart = CalendricalCalculationsHelper.PersianNewYearOnOrBefore(s_persianEpoch + approximateDaysFrom |
| | 0 | 69 | | yearStart += ordinalDay; |
| | 0 | 70 | | return yearStart; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | internal static void CheckTicksRange(long ticks) |
| | | 74 | | { |
| | 0 | 75 | | if (ticks < s_minDate.Ticks || ticks > s_maxDate.Ticks) |
| | | 76 | | { |
| | 0 | 77 | | throw new ArgumentOutOfRangeException( |
| | 0 | 78 | | "time", |
| | 0 | 79 | | ticks, |
| | 0 | 80 | | SR.Format(SR.ArgumentOutOfRange_CalendarRange, s_minDate, s_maxDate)); |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | internal static void CheckEraRange(int era) |
| | | 85 | | { |
| | 0 | 86 | | if (era != CurrentEra && era != PersianEra) |
| | | 87 | | { |
| | 0 | 88 | | throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue); |
| | | 89 | | } |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | internal static void CheckYearRange(int year, int era) |
| | | 93 | | { |
| | 0 | 94 | | CheckEraRange(era); |
| | 0 | 95 | | if (year < 1 || year > MaxCalendarYear) |
| | | 96 | | { |
| | 0 | 97 | | throw new ArgumentOutOfRangeException( |
| | 0 | 98 | | nameof(year), |
| | 0 | 99 | | year, |
| | 0 | 100 | | SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarYear)); |
| | | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | internal static void CheckYearMonthRange(int year, int month, int era) |
| | | 105 | | { |
| | 0 | 106 | | CheckYearRange(year, era); |
| | 0 | 107 | | if (year == MaxCalendarYear) |
| | | 108 | | { |
| | 0 | 109 | | if (month > MaxCalendarMonth) |
| | | 110 | | { |
| | 0 | 111 | | throw new ArgumentOutOfRangeException( |
| | 0 | 112 | | nameof(month), |
| | 0 | 113 | | month, |
| | 0 | 114 | | SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarMonth)); |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | if (month < 1 || month > 12) |
| | | 119 | | { |
| | 0 | 120 | | ThrowHelper.ThrowArgumentOutOfRange_Month(month); |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | private static int MonthFromOrdinalDay(int ordinalDay) |
| | | 125 | | { |
| | 0 | 126 | | Debug.Assert(ordinalDay <= 366); |
| | 0 | 127 | | int index = 0; |
| | 0 | 128 | | while (ordinalDay > DaysToMonth[index]) |
| | | 129 | | { |
| | 0 | 130 | | index++; |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return index; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | private static int DaysInPreviousMonths(int month) |
| | | 137 | | { |
| | 0 | 138 | | Debug.Assert(1 <= month && month <= 12); |
| | | 139 | | // months are one based but for calculations use 0 based |
| | 0 | 140 | | --month; |
| | 0 | 141 | | return DaysToMonth[month]; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | private int GetDatePart(long ticks, int part) |
| | | 145 | | { |
| | 0 | 146 | | CheckTicksRange(ticks); |
| | | 147 | | |
| | | 148 | | // Get the absolute date. The absolute date is the number of days from January 1st, 1 A.D. |
| | | 149 | | // 1/1/0001 is absolute date 1. |
| | 0 | 150 | | long numDays = ticks / TimeSpan.TicksPerDay + 1; |
| | | 151 | | |
| | | 152 | | // Calculate the appromixate Persian Year. |
| | 0 | 153 | | long yearStart = CalendricalCalculationsHelper.PersianNewYearOnOrBefore(numDays); |
| | 0 | 154 | | int y = (int)(Math.Floor(((yearStart - s_persianEpoch) / CalendricalCalculationsHelper.MeanTropicalYearInDay |
| | 0 | 155 | | Debug.Assert(y >= 1); |
| | | 156 | | |
| | 0 | 157 | | if (part == DatePartYear) |
| | | 158 | | { |
| | 0 | 159 | | return y; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | // Calculate the Persian Month. |
| | 0 | 163 | | int ordinalDay = (int)(numDays - CalendricalCalculationsHelper.GetNumberOfDays(this.ToDateTime(y, 1, 1, 0, 0 |
| | 0 | 164 | | if (part == DatePartDayOfYear) |
| | | 165 | | { |
| | 0 | 166 | | return ordinalDay; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | int m = MonthFromOrdinalDay(ordinalDay); |
| | 0 | 170 | | Debug.Assert(ordinalDay >= 1); |
| | 0 | 171 | | Debug.Assert(m >= 1 && m <= 12); |
| | 0 | 172 | | if (part == DatePartMonth) |
| | | 173 | | { |
| | 0 | 174 | | return m; |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | int d = ordinalDay - DaysInPreviousMonths(m); |
| | 0 | 178 | | Debug.Assert(1 <= d); |
| | 0 | 179 | | Debug.Assert(d <= 31); |
| | | 180 | | |
| | | 181 | | // Calculate the Persian Day. |
| | 0 | 182 | | if (part == DatePartDay) |
| | | 183 | | { |
| | 0 | 184 | | return d; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | // Incorrect part value. |
| | 0 | 188 | | throw new InvalidOperationException(SR.InvalidOperation_DateTimeParsing); |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | // Exactly the same as GetDatePart, except computing all of |
| | | 192 | | // year/month/day rather than just one of them. Used when all three |
| | | 193 | | // are needed rather than redoing the computations for each. |
| | | 194 | | internal void GetDate(long ticks, out int year, out int month, out int day) |
| | | 195 | | { |
| | 0 | 196 | | CheckTicksRange(ticks); |
| | | 197 | | |
| | | 198 | | // Get the absolute date. The absolute date is the number of days from January 1st, 1 A.D. |
| | | 199 | | // 1/1/0001 is absolute date 1. |
| | 0 | 200 | | long numDays = ticks / TimeSpan.TicksPerDay + 1; |
| | | 201 | | |
| | | 202 | | // Calculate the appromixate Persian Year. |
| | 0 | 203 | | long yearStart = CalendricalCalculationsHelper.PersianNewYearOnOrBefore(numDays); |
| | 0 | 204 | | year = (int)(Math.Floor(((yearStart - s_persianEpoch) / CalendricalCalculationsHelper.MeanTropicalYearInDays |
| | 0 | 205 | | Debug.Assert(year >= 1); |
| | | 206 | | |
| | | 207 | | // Calculate the Persian Month. |
| | 0 | 208 | | int ordinalDay = (int)(numDays - CalendricalCalculationsHelper.GetNumberOfDays(this.ToDateTime(year, 1, 1, 0 |
| | | 209 | | |
| | 0 | 210 | | month = MonthFromOrdinalDay(ordinalDay); |
| | 0 | 211 | | Debug.Assert(ordinalDay >= 1); |
| | 0 | 212 | | Debug.Assert(month >= 1 && month <= 12); |
| | | 213 | | |
| | 0 | 214 | | day = ordinalDay - DaysInPreviousMonths(month); |
| | 0 | 215 | | Debug.Assert(1 <= day); |
| | 0 | 216 | | Debug.Assert(day <= 31); |
| | 0 | 217 | | } |
| | | 218 | | |
| | | 219 | | public override DateTime AddMonths(DateTime time, int months) |
| | | 220 | | { |
| | 0 | 221 | | if (months < -120000 || months > 120000) |
| | | 222 | | { |
| | 0 | 223 | | throw new ArgumentOutOfRangeException( |
| | 0 | 224 | | nameof(months), |
| | 0 | 225 | | months, |
| | 0 | 226 | | SR.Format(SR.ArgumentOutOfRange_Range, -120000, 120000)); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | // Get the date in Persian calendar. |
| | 0 | 230 | | GetDate(time.Ticks, out int y, out int m, out int d); |
| | 0 | 231 | | int i = m - 1 + months; |
| | 0 | 232 | | if (i >= 0) |
| | | 233 | | { |
| | 0 | 234 | | m = i % 12 + 1; |
| | 0 | 235 | | y += i / 12; |
| | | 236 | | } |
| | | 237 | | else |
| | | 238 | | { |
| | 0 | 239 | | m = 12 + (i + 1) % 12; |
| | 0 | 240 | | y += (i - 11) / 12; |
| | | 241 | | } |
| | 0 | 242 | | int days = GetDaysInMonth(y, m); |
| | 0 | 243 | | if (d > days) |
| | | 244 | | { |
| | 0 | 245 | | d = days; |
| | | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | long ticks = GetAbsoluteDatePersian(y, m, d) * TimeSpan.TicksPerDay + time.Ticks % TimeSpan.TicksPerDay; |
| | 0 | 249 | | CheckAddResult(ticks, MinSupportedDateTime, MaxSupportedDateTime); |
| | 0 | 250 | | return new DateTime(ticks); |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | public override DateTime AddYears(DateTime time, int years) |
| | | 254 | | { |
| | 0 | 255 | | return AddMonths(time, years * 12); |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | public override int GetDayOfMonth(DateTime time) |
| | | 259 | | { |
| | 0 | 260 | | return GetDatePart(time.Ticks, DatePartDay); |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | public override DayOfWeek GetDayOfWeek(DateTime time) => time.DayOfWeek; |
| | | 264 | | |
| | | 265 | | public override int GetDayOfYear(DateTime time) |
| | | 266 | | { |
| | 0 | 267 | | return GetDatePart(time.Ticks, DatePartDayOfYear); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | public override int GetDaysInMonth(int year, int month, int era) |
| | | 271 | | { |
| | 0 | 272 | | CheckYearMonthRange(year, month, era); |
| | | 273 | | |
| | 0 | 274 | | if ((month == MaxCalendarMonth) && (year == MaxCalendarYear)) |
| | | 275 | | { |
| | 0 | 276 | | return MaxCalendarDay; |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | int daysInMonth = DaysToMonth[month] - DaysToMonth[month - 1]; |
| | 0 | 280 | | if ((month == MonthsPerYear) && !IsLeapYear(year)) |
| | | 281 | | { |
| | 0 | 282 | | Debug.Assert(daysInMonth == 30); |
| | 0 | 283 | | --daysInMonth; |
| | | 284 | | } |
| | | 285 | | |
| | 0 | 286 | | return daysInMonth; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | public override int GetDaysInYear(int year, int era) |
| | | 290 | | { |
| | 0 | 291 | | CheckYearRange(year, era); |
| | 0 | 292 | | if (year == MaxCalendarYear) |
| | | 293 | | { |
| | 0 | 294 | | return DaysToMonth[MaxCalendarMonth - 1] + MaxCalendarDay; |
| | | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | return IsLeapYear(year, CurrentEra) ? 366 : 365; |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | public override int GetEra(DateTime time) |
| | | 301 | | { |
| | 0 | 302 | | CheckTicksRange(time.Ticks); |
| | 0 | 303 | | return PersianEra; |
| | | 304 | | } |
| | | 305 | | |
| | 0 | 306 | | public override int[] Eras => [PersianEra]; |
| | | 307 | | |
| | | 308 | | public override int GetMonth(DateTime time) |
| | | 309 | | { |
| | 0 | 310 | | return GetDatePart(time.Ticks, DatePartMonth); |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | public override int GetMonthsInYear(int year, int era) |
| | | 314 | | { |
| | 0 | 315 | | CheckYearRange(year, era); |
| | 0 | 316 | | if (year == MaxCalendarYear) |
| | | 317 | | { |
| | 0 | 318 | | return MaxCalendarMonth; |
| | | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | return 12; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | public override int GetYear(DateTime time) |
| | | 325 | | { |
| | 0 | 326 | | return GetDatePart(time.Ticks, DatePartYear); |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | public override bool IsLeapDay(int year, int month, int day, int era) |
| | | 330 | | { |
| | | 331 | | // The year/month/era value checking is done in GetDaysInMonth(). |
| | 0 | 332 | | int daysInMonth = GetDaysInMonth(year, month, era); |
| | 0 | 333 | | if (day < 1 || day > daysInMonth) |
| | | 334 | | { |
| | 0 | 335 | | throw new ArgumentOutOfRangeException( |
| | 0 | 336 | | nameof(day), |
| | 0 | 337 | | day, |
| | 0 | 338 | | SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month)); |
| | | 339 | | } |
| | | 340 | | |
| | 0 | 341 | | return IsLeapYear(year, era) && month == 12 && day == 30; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | public override int GetLeapMonth(int year, int era) |
| | | 345 | | { |
| | 0 | 346 | | CheckYearRange(year, era); |
| | 0 | 347 | | return 0; |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | public override bool IsLeapMonth(int year, int month, int era) |
| | | 351 | | { |
| | 0 | 352 | | CheckYearMonthRange(year, month, era); |
| | 0 | 353 | | return false; |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | public override bool IsLeapYear(int year, int era) |
| | | 357 | | { |
| | 0 | 358 | | CheckYearRange(year, era); |
| | | 359 | | |
| | 0 | 360 | | if (year == MaxCalendarYear) |
| | | 361 | | { |
| | 0 | 362 | | return false; |
| | | 363 | | } |
| | | 364 | | |
| | 0 | 365 | | return (GetAbsoluteDatePersian(year + 1, 1, 1) - GetAbsoluteDatePersian(year, 1, 1)) == 366; |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisec |
| | | 369 | | { |
| | | 370 | | // The year/month/era checking is done in GetDaysInMonth(). |
| | 0 | 371 | | int daysInMonth = GetDaysInMonth(year, month, era); |
| | 0 | 372 | | if (day < 1 || day > daysInMonth) |
| | | 373 | | { |
| | 0 | 374 | | throw new ArgumentOutOfRangeException( |
| | 0 | 375 | | nameof(day), |
| | 0 | 376 | | day, |
| | 0 | 377 | | SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month)); |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | long lDate = GetAbsoluteDatePersian(year, month, day); |
| | | 381 | | |
| | 0 | 382 | | if (lDate < 0) |
| | | 383 | | { |
| | 0 | 384 | | throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadYearMonthDay); |
| | | 385 | | } |
| | | 386 | | |
| | 0 | 387 | | return new DateTime(lDate * TimeSpan.TicksPerDay + TimeToTicks(hour, minute, second, millisecond)); |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | private const int DefaultTwoDigitYearMax = 1410; |
| | | 391 | | |
| | | 392 | | public override int TwoDigitYearMax |
| | | 393 | | { |
| | | 394 | | get |
| | | 395 | | { |
| | 0 | 396 | | if (_twoDigitYearMax == -1) |
| | | 397 | | { |
| | 0 | 398 | | _twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DefaultTwoDigitYearMax); |
| | | 399 | | } |
| | | 400 | | |
| | 0 | 401 | | return _twoDigitYearMax; |
| | | 402 | | } |
| | | 403 | | set |
| | | 404 | | { |
| | 0 | 405 | | VerifyWritable(); |
| | 0 | 406 | | if (value < 99 || value > MaxCalendarYear) |
| | | 407 | | { |
| | 0 | 408 | | throw new ArgumentOutOfRangeException( |
| | 0 | 409 | | nameof(value), |
| | 0 | 410 | | value, |
| | 0 | 411 | | SR.Format(SR.ArgumentOutOfRange_Range, 99, MaxCalendarYear)); |
| | | 412 | | } |
| | | 413 | | |
| | 0 | 414 | | _twoDigitYearMax = value; |
| | 0 | 415 | | } |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | public override int ToFourDigitYear(int year) |
| | | 419 | | { |
| | 0 | 420 | | ArgumentOutOfRangeException.ThrowIfNegative(year); |
| | 0 | 421 | | if (year < 100) |
| | | 422 | | { |
| | 0 | 423 | | return base.ToFourDigitYear(year); |
| | | 424 | | } |
| | | 425 | | |
| | 0 | 426 | | if (year > MaxCalendarYear) |
| | | 427 | | { |
| | 0 | 428 | | throw new ArgumentOutOfRangeException( |
| | 0 | 429 | | nameof(year), |
| | 0 | 430 | | year, |
| | 0 | 431 | | SR.Format(SR.ArgumentOutOfRange_Range, 1, MaxCalendarYear)); |
| | | 432 | | } |
| | | 433 | | |
| | 0 | 434 | | return year; |
| | | 435 | | } |
| | | 436 | | } |
| | | 437 | | } |
| | | 438 | | |