| | | 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 | | namespace System.Globalization |
| | | 5 | | { |
| | | 6 | | public abstract class EastAsianLunisolarCalendar : Calendar |
| | | 7 | | { |
| | | 8 | | private const int LeapMonth = 0; |
| | | 9 | | private const int Jan1Month = 1; |
| | | 10 | | private const int Jan1Date = 2; |
| | | 11 | | private const int nDaysPerMonth = 3; |
| | | 12 | | |
| | | 13 | | // # of days so far in the solar year |
| | 0 | 14 | | private static ReadOnlySpan<int> DaysToMonth365 => [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; |
| | 0 | 15 | | private static ReadOnlySpan<int> DaysToMonth366 => [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]; |
| | | 16 | | |
| | 0 | 17 | | public override CalendarAlgorithmType AlgorithmType => CalendarAlgorithmType.LunisolarCalendar; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Return the year number in the 60-year cycle. |
| | | 21 | | /// </summary> |
| | | 22 | | public virtual int GetSexagenaryYear(DateTime time) |
| | | 23 | | { |
| | 0 | 24 | | CheckTicksRange(time.Ticks); |
| | | 25 | | |
| | 0 | 26 | | TimeToLunar(time, out int year, out _, out _); |
| | 0 | 27 | | return ((year - 4) % 60) + 1; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Return the celestial year from the 60-year cycle. |
| | | 32 | | /// The returned value is from 1 ~ 10. |
| | | 33 | | /// </summary> |
| | | 34 | | public int GetCelestialStem(int sexagenaryYear) |
| | | 35 | | { |
| | 0 | 36 | | if (sexagenaryYear < 1 || sexagenaryYear > 60) |
| | | 37 | | { |
| | 0 | 38 | | throw new ArgumentOutOfRangeException( |
| | 0 | 39 | | nameof(sexagenaryYear), |
| | 0 | 40 | | sexagenaryYear, |
| | 0 | 41 | | SR.Format(SR.ArgumentOutOfRange_Range, 1, 60)); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | return ((sexagenaryYear - 1) % 10) + 1; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Return the Terrestial Branch from the 60-year cycle. |
| | | 49 | | /// The returned value is from 1 ~ 12. |
| | | 50 | | /// </summary> |
| | | 51 | | public int GetTerrestrialBranch(int sexagenaryYear) |
| | | 52 | | { |
| | 0 | 53 | | if (sexagenaryYear < 1 || sexagenaryYear > 60) |
| | | 54 | | { |
| | 0 | 55 | | throw new ArgumentOutOfRangeException( |
| | 0 | 56 | | nameof(sexagenaryYear), |
| | 0 | 57 | | sexagenaryYear, |
| | 0 | 58 | | SR.Format(SR.ArgumentOutOfRange_Range, 1, 60)); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return ((sexagenaryYear - 1) % 12) + 1; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | internal abstract int GetYearInfo(int LunarYear, int Index); |
| | | 65 | | internal abstract int GetYear(int year, DateTime time); |
| | | 66 | | internal abstract int GetGregorianYear(int year, int era); |
| | | 67 | | |
| | | 68 | | internal abstract int MinCalendarYear { get; } |
| | | 69 | | internal abstract int MaxCalendarYear { get; } |
| | | 70 | | internal abstract EraInfo[]? CalEraInfo { get; } |
| | | 71 | | internal abstract DateTime MinDate { get; } |
| | | 72 | | internal abstract DateTime MaxDate { get; } |
| | | 73 | | |
| | | 74 | | internal const int MaxCalendarMonth = 13; |
| | | 75 | | internal const int MaxCalendarDay = 30; |
| | | 76 | | |
| | | 77 | | internal int MinEraCalendarYear(int era) |
| | | 78 | | { |
| | 0 | 79 | | EraInfo[]? eraInfo = CalEraInfo; |
| | 0 | 80 | | if (eraInfo == null) |
| | | 81 | | { |
| | 0 | 82 | | return MinCalendarYear; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | if (era == CurrentEra) |
| | | 86 | | { |
| | 0 | 87 | | era = CurrentEraValue; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // Era has to be in the supported range otherwise we will throw exception in CheckEraRange() |
| | 0 | 91 | | if (era == GetEra(MinDate)) |
| | | 92 | | { |
| | 0 | 93 | | return GetYear(MinCalendarYear, MinDate); |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | for (int i = 0; i < eraInfo.Length; i++) |
| | | 97 | | { |
| | 0 | 98 | | if (era == eraInfo[i].era) |
| | | 99 | | { |
| | 0 | 100 | | return eraInfo[i].minEraYear; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | internal int MaxEraCalendarYear(int era) |
| | | 108 | | { |
| | 0 | 109 | | EraInfo[]? eraInfo = CalEraInfo; |
| | 0 | 110 | | if (eraInfo == null) |
| | | 111 | | { |
| | 0 | 112 | | return MaxCalendarYear; |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | if (era == CurrentEra) |
| | | 116 | | { |
| | 0 | 117 | | era = CurrentEraValue; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // Era has to be in the supported range otherwise we will throw exception in CheckEraRange() |
| | 0 | 121 | | if (era == GetEra(MaxDate)) |
| | | 122 | | { |
| | 0 | 123 | | return GetYear(MaxCalendarYear, MaxDate); |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | for (int i = 0; i < eraInfo.Length; i++) |
| | | 127 | | { |
| | 0 | 128 | | if (era == eraInfo[i].era) |
| | | 129 | | { |
| | 0 | 130 | | return eraInfo[i].maxEraYear; |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | internal EastAsianLunisolarCalendar() |
| | | 138 | | { |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | internal void CheckTicksRange(long ticks) |
| | | 142 | | { |
| | 0 | 143 | | if (ticks < MinSupportedDateTime.Ticks || ticks > MaxSupportedDateTime.Ticks) |
| | | 144 | | { |
| | 0 | 145 | | throw new ArgumentOutOfRangeException( |
| | 0 | 146 | | "time", |
| | 0 | 147 | | ticks, |
| | 0 | 148 | | SR.Format(CultureInfo.InvariantCulture, SR.ArgumentOutOfRange_CalendarRange, |
| | 0 | 149 | | MinSupportedDateTime, MaxSupportedDateTime)); |
| | | 150 | | } |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | internal void CheckEraRange(int era) |
| | | 154 | | { |
| | 0 | 155 | | if (era == CurrentEra) |
| | | 156 | | { |
| | 0 | 157 | | era = CurrentEraValue; |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | if (era < GetEra(MinDate) || era > GetEra(MaxDate)) |
| | | 161 | | { |
| | 0 | 162 | | throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue); |
| | | 163 | | } |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | internal int CheckYearRange(int year, int era) |
| | | 167 | | { |
| | 0 | 168 | | CheckEraRange(era); |
| | 0 | 169 | | year = GetGregorianYear(year, era); |
| | | 170 | | |
| | 0 | 171 | | if (year < MinCalendarYear || year > MaxCalendarYear) |
| | | 172 | | { |
| | 0 | 173 | | throw new ArgumentOutOfRangeException( |
| | 0 | 174 | | nameof(year), |
| | 0 | 175 | | year, |
| | 0 | 176 | | SR.Format(SR.ArgumentOutOfRange_Range, MinEraCalendarYear(era), MaxEraCalendarYear(era))); |
| | | 177 | | } |
| | 0 | 178 | | return year; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | internal int CheckYearMonthRange(int year, int month, int era) |
| | | 182 | | { |
| | 0 | 183 | | year = CheckYearRange(year, era); |
| | | 184 | | |
| | 0 | 185 | | if (month == 13) |
| | | 186 | | { |
| | | 187 | | // Reject if there is no leap month this year |
| | 0 | 188 | | if (GetYearInfo(year, LeapMonth) == 0) |
| | | 189 | | { |
| | 0 | 190 | | ThrowHelper.ThrowArgumentOutOfRange_Month(month); |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | if (month < 1 || month > 13) |
| | | 195 | | { |
| | 0 | 196 | | ThrowHelper.ThrowArgumentOutOfRange_Month(month); |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | return year; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | internal int InternalGetDaysInMonth(int year, int month) |
| | | 203 | | { |
| | 0 | 204 | | int mask = 0x8000; |
| | | 205 | | |
| | | 206 | | // convert the lunar day into a lunar month/date |
| | 0 | 207 | | mask >>= (month - 1); |
| | 0 | 208 | | if ((GetYearInfo(year, nDaysPerMonth) & mask) == 0) |
| | | 209 | | { |
| | 0 | 210 | | return 29; |
| | | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | return 30; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Returns the number of days in the month given by the year and |
| | | 218 | | /// month arguments. |
| | | 219 | | /// </summary> |
| | | 220 | | public override int GetDaysInMonth(int year, int month, int era) |
| | | 221 | | { |
| | 0 | 222 | | year = CheckYearMonthRange(year, month, era); |
| | 0 | 223 | | return InternalGetDaysInMonth(year, month); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | private static bool GregorianIsLeapYear(int y) |
| | | 227 | | { |
| | 0 | 228 | | if ((y % 4) != 0) |
| | | 229 | | { |
| | 0 | 230 | | return false; |
| | | 231 | | } |
| | 0 | 232 | | if ((y % 100) != 0) |
| | | 233 | | { |
| | 0 | 234 | | return true; |
| | | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | return (y % 400) == 0; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Returns the date and time converted to a DateTime value. |
| | | 242 | | /// Throws an exception if the n-tuple is invalid. |
| | | 243 | | /// </summary> |
| | | 244 | | public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisec |
| | | 245 | | { |
| | 0 | 246 | | year = CheckYearMonthRange(year, month, era); |
| | 0 | 247 | | int daysInMonth = InternalGetDaysInMonth(year, month); |
| | 0 | 248 | | if (day < 1 || day > daysInMonth) |
| | | 249 | | { |
| | 0 | 250 | | throw new ArgumentOutOfRangeException( |
| | 0 | 251 | | nameof(day), |
| | 0 | 252 | | day, |
| | 0 | 253 | | SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month)); |
| | | 254 | | } |
| | | 255 | | |
| | 0 | 256 | | if (!LunarToGregorian(year, month, day, out int gy, out int gm, out int gd)) |
| | | 257 | | { |
| | 0 | 258 | | throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadYearMonthDay); |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | return new DateTime(gy, gm, gd, hour, minute, second, millisecond); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Calculates lunar calendar info for the given gregorian year, month, date. |
| | | 266 | | /// The input date should be validated before calling this method. |
| | | 267 | | /// </summary> |
| | | 268 | | private void GregorianToLunar(int solarYear, int solarMonth, int solarDate, out int lunarYear, out int lunarMont |
| | | 269 | | { |
| | 0 | 270 | | bool isLeapYear = GregorianIsLeapYear(solarYear); |
| | | 271 | | int jan1Month; |
| | | 272 | | int jan1Date; |
| | | 273 | | |
| | | 274 | | // Calculate the day number in the solar year. |
| | 0 | 275 | | int solarDay = isLeapYear ? DaysToMonth366[solarMonth - 1] : DaysToMonth365[solarMonth - 1]; |
| | 0 | 276 | | solarDay += solarDate; |
| | | 277 | | |
| | | 278 | | // Calculate the day number in the lunar year. |
| | 0 | 279 | | int lunarDay = solarDay; |
| | 0 | 280 | | lunarYear = solarYear; |
| | 0 | 281 | | if (lunarYear == (MaxCalendarYear + 1)) |
| | | 282 | | { |
| | 0 | 283 | | lunarYear--; |
| | 0 | 284 | | lunarDay += (GregorianIsLeapYear(lunarYear) ? 366 : 365); |
| | 0 | 285 | | jan1Month = GetYearInfo(lunarYear, Jan1Month); |
| | 0 | 286 | | jan1Date = GetYearInfo(lunarYear, Jan1Date); |
| | | 287 | | } |
| | | 288 | | else |
| | | 289 | | { |
| | 0 | 290 | | jan1Month = GetYearInfo(lunarYear, Jan1Month); |
| | 0 | 291 | | jan1Date = GetYearInfo(lunarYear, Jan1Date); |
| | | 292 | | |
| | | 293 | | // check if this solar date is actually part of the previous |
| | | 294 | | // lunar year |
| | 0 | 295 | | if ((solarMonth < jan1Month) || |
| | 0 | 296 | | (solarMonth == jan1Month && solarDate < jan1Date)) |
| | | 297 | | { |
| | | 298 | | // the corresponding lunar day is actually part of the previous |
| | | 299 | | // lunar year |
| | 0 | 300 | | lunarYear--; |
| | | 301 | | |
| | | 302 | | // add a solar year to the lunar day # |
| | 0 | 303 | | lunarDay += (GregorianIsLeapYear(lunarYear) ? 366 : 365); |
| | | 304 | | |
| | | 305 | | // update the new start of year |
| | 0 | 306 | | jan1Month = GetYearInfo(lunarYear, Jan1Month); |
| | 0 | 307 | | jan1Date = GetYearInfo(lunarYear, Jan1Date); |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | // convert solar day into lunar day. |
| | | 312 | | // subtract off the beginning part of the solar year which is not |
| | | 313 | | // part of the lunar year. since this part is always in Jan or Feb, |
| | | 314 | | // we don't need to handle Leap Year (LY only affects March |
| | | 315 | | // and later). |
| | 0 | 316 | | lunarDay -= DaysToMonth365[jan1Month - 1]; |
| | 0 | 317 | | lunarDay -= (jan1Date - 1); |
| | | 318 | | |
| | | 319 | | // convert the lunar day into a lunar month/date |
| | 0 | 320 | | int mask = 0x8000; |
| | 0 | 321 | | int yearInfo = GetYearInfo(lunarYear, nDaysPerMonth); |
| | 0 | 322 | | int days = ((yearInfo & mask) != 0) ? 30 : 29; |
| | 0 | 323 | | lunarMonth = 1; |
| | 0 | 324 | | while (lunarDay > days) |
| | | 325 | | { |
| | 0 | 326 | | lunarDay -= days; |
| | 0 | 327 | | lunarMonth++; |
| | 0 | 328 | | mask >>= 1; |
| | 0 | 329 | | days = ((yearInfo & mask) != 0) ? 30 : 29; |
| | | 330 | | } |
| | 0 | 331 | | lunarDate = lunarDay; |
| | 0 | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Convert from Lunar to Gregorian |
| | | 336 | | /// </summary> |
| | | 337 | | /// <remarks> |
| | | 338 | | /// Highly inefficient, but it works based on the forward conversion |
| | | 339 | | /// </remarks> |
| | | 340 | | private bool LunarToGregorian(int lunarYear, int lunarMonth, int lunarDate, out int solarYear, out int solarMont |
| | | 341 | | { |
| | 0 | 342 | | if (lunarDate < 1 || lunarDate > 30) |
| | | 343 | | { |
| | 0 | 344 | | solarYear = 0; |
| | 0 | 345 | | solarMonth = 0; |
| | 0 | 346 | | solarDay = 0; |
| | 0 | 347 | | return false; |
| | | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | int numLunarDays = lunarDate - 1; |
| | | 351 | | |
| | | 352 | | // Add previous months days to form the total num of days from the first of the month. |
| | 0 | 353 | | for (int i = 1; i < lunarMonth; i++) |
| | | 354 | | { |
| | 0 | 355 | | numLunarDays += InternalGetDaysInMonth(lunarYear, i); |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | // Get Gregorian First of year |
| | 0 | 359 | | int jan1Month = GetYearInfo(lunarYear, Jan1Month); |
| | 0 | 360 | | int jan1Date = GetYearInfo(lunarYear, Jan1Date); |
| | | 361 | | |
| | | 362 | | // calc the solar day of year of 1 Lunar day |
| | 0 | 363 | | bool isLeapYear = GregorianIsLeapYear(lunarYear); |
| | 0 | 364 | | ReadOnlySpan<int> days = isLeapYear ? DaysToMonth366 : DaysToMonth365; |
| | | 365 | | |
| | 0 | 366 | | solarDay = jan1Date; |
| | | 367 | | |
| | 0 | 368 | | if (jan1Month > 1) |
| | | 369 | | { |
| | 0 | 370 | | solarDay += days[jan1Month - 1]; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | // Add the actual lunar day to get the solar day we want |
| | 0 | 374 | | solarDay += numLunarDays; |
| | | 375 | | |
| | 0 | 376 | | if (solarDay > (365 + (isLeapYear ? 1 : 0))) |
| | | 377 | | { |
| | 0 | 378 | | solarYear = lunarYear + 1; |
| | 0 | 379 | | solarDay -= (365 + (isLeapYear ? 1 : 0)); |
| | | 380 | | } |
| | | 381 | | else |
| | | 382 | | { |
| | 0 | 383 | | solarYear = lunarYear; |
| | | 384 | | } |
| | | 385 | | |
| | 0 | 386 | | for (solarMonth = 1; solarMonth < 12; solarMonth++) |
| | | 387 | | { |
| | 0 | 388 | | if (days[solarMonth] >= solarDay) |
| | | 389 | | { |
| | | 390 | | break; |
| | | 391 | | } |
| | | 392 | | } |
| | | 393 | | |
| | 0 | 394 | | solarDay -= days[solarMonth - 1]; |
| | 0 | 395 | | return true; |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | private DateTime LunarToTime(DateTime time, int year, int month, int day) |
| | | 399 | | { |
| | 0 | 400 | | LunarToGregorian(year, month, day, out int gy, out int gm, out int gd); |
| | 0 | 401 | | time.GetTime(out int hour, out int minute, out int second, out int millisecond); |
| | 0 | 402 | | return GregorianCalendar.GetDefaultInstance().ToDateTime(gy, gm, gd, hour, minute, second, millisecond); |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | private void TimeToLunar(DateTime time, out int year, out int month, out int day) |
| | | 406 | | { |
| | 0 | 407 | | Calendar gregorianCalendar = GregorianCalendar.GetDefaultInstance(); |
| | 0 | 408 | | int gy = gregorianCalendar.GetYear(time); |
| | 0 | 409 | | int gm = gregorianCalendar.GetMonth(time); |
| | 0 | 410 | | int gd = gregorianCalendar.GetDayOfMonth(time); |
| | | 411 | | |
| | 0 | 412 | | GregorianToLunar(gy, gm, gd, out year, out month, out day); |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Returns the DateTime resulting from adding the given number of |
| | | 417 | | /// months to the specified DateTime. The result is computed by incrementing |
| | | 418 | | /// (or decrementing) the year and month parts of the specified DateTime by |
| | | 419 | | /// value months, and, if required, adjusting the day part of the |
| | | 420 | | /// resulting date downwards to the last day of the resulting month in the |
| | | 421 | | /// resulting year. The time-of-day part of the result is the same as the |
| | | 422 | | /// time-of-day part of the specified DateTime. |
| | | 423 | | /// </summary> |
| | | 424 | | public override DateTime AddMonths(DateTime time, int months) |
| | | 425 | | { |
| | 0 | 426 | | if (months < -120000 || months > 120000) |
| | | 427 | | { |
| | 0 | 428 | | throw new ArgumentOutOfRangeException( |
| | 0 | 429 | | nameof(months), |
| | 0 | 430 | | months, |
| | 0 | 431 | | SR.Format(SR.ArgumentOutOfRange_Range, -120000, 120000)); |
| | | 432 | | } |
| | | 433 | | |
| | 0 | 434 | | CheckTicksRange(time.Ticks); |
| | 0 | 435 | | TimeToLunar(time, out int y, out int m, out int d); |
| | | 436 | | |
| | 0 | 437 | | int i = m + months; |
| | 0 | 438 | | if (i > 0) |
| | | 439 | | { |
| | 0 | 440 | | int monthsInYear = InternalIsLeapYear(y) ? 13 : 12; |
| | | 441 | | |
| | 0 | 442 | | while (i - monthsInYear > 0) |
| | | 443 | | { |
| | 0 | 444 | | i -= monthsInYear; |
| | 0 | 445 | | y++; |
| | 0 | 446 | | monthsInYear = InternalIsLeapYear(y) ? 13 : 12; |
| | | 447 | | } |
| | 0 | 448 | | m = i; |
| | | 449 | | } |
| | | 450 | | else |
| | | 451 | | { |
| | | 452 | | int monthsInYear; |
| | 0 | 453 | | while (i <= 0) |
| | | 454 | | { |
| | 0 | 455 | | monthsInYear = InternalIsLeapYear(y - 1) ? 13 : 12; |
| | 0 | 456 | | i += monthsInYear; |
| | 0 | 457 | | y--; |
| | | 458 | | } |
| | 0 | 459 | | m = i; |
| | | 460 | | } |
| | | 461 | | |
| | 0 | 462 | | int days = InternalGetDaysInMonth(y, m); |
| | 0 | 463 | | if (d > days) |
| | | 464 | | { |
| | 0 | 465 | | d = days; |
| | | 466 | | } |
| | 0 | 467 | | DateTime dt = LunarToTime(time, y, m, d); |
| | | 468 | | |
| | 0 | 469 | | CheckAddResult(dt.Ticks, MinSupportedDateTime, MaxSupportedDateTime); |
| | 0 | 470 | | return dt; |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | public override DateTime AddYears(DateTime time, int years) |
| | | 474 | | { |
| | 0 | 475 | | CheckTicksRange(time.Ticks); |
| | 0 | 476 | | TimeToLunar(time, out int y, out int m, out int d); |
| | | 477 | | |
| | 0 | 478 | | y += years; |
| | | 479 | | |
| | 0 | 480 | | if (m == 13 && !InternalIsLeapYear(y)) |
| | | 481 | | { |
| | 0 | 482 | | m = 12; |
| | 0 | 483 | | d = InternalGetDaysInMonth(y, m); |
| | | 484 | | } |
| | 0 | 485 | | int daysInMonths = InternalGetDaysInMonth(y, m); |
| | 0 | 486 | | if (d > daysInMonths) |
| | | 487 | | { |
| | 0 | 488 | | d = daysInMonths; |
| | | 489 | | } |
| | | 490 | | |
| | 0 | 491 | | DateTime dt = LunarToTime(time, y, m, d); |
| | 0 | 492 | | CheckAddResult(dt.Ticks, MinSupportedDateTime, MaxSupportedDateTime); |
| | 0 | 493 | | return dt; |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | /// <summary> |
| | | 497 | | /// Returns the day-of-year part of the specified DateTime. The returned value |
| | | 498 | | /// is an integer between 1 and [354|355 |383|384]. |
| | | 499 | | /// </summary> |
| | | 500 | | public override int GetDayOfYear(DateTime time) |
| | | 501 | | { |
| | 0 | 502 | | CheckTicksRange(time.Ticks); |
| | 0 | 503 | | TimeToLunar(time, out int y, out int m, out int d); |
| | | 504 | | |
| | 0 | 505 | | for (int i = 1; i < m; i++) |
| | | 506 | | { |
| | 0 | 507 | | d += InternalGetDaysInMonth(y, i); |
| | | 508 | | } |
| | 0 | 509 | | return d; |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | /// <summary> |
| | | 513 | | /// Returns the day-of-month part of the specified DateTime. The returned |
| | | 514 | | /// value is an integer between 1 and 29 or 30. |
| | | 515 | | /// </summary> |
| | | 516 | | public override int GetDayOfMonth(DateTime time) |
| | | 517 | | { |
| | 0 | 518 | | CheckTicksRange(time.Ticks); |
| | | 519 | | |
| | 0 | 520 | | TimeToLunar(time, out _, out _, out int d); |
| | | 521 | | |
| | 0 | 522 | | return d; |
| | | 523 | | } |
| | | 524 | | |
| | | 525 | | /// <summary> |
| | | 526 | | /// Returns the number of days in the year given by the year argument for the current era. |
| | | 527 | | /// </summary> |
| | | 528 | | public override int GetDaysInYear(int year, int era) |
| | | 529 | | { |
| | 0 | 530 | | year = CheckYearRange(year, era); |
| | | 531 | | |
| | 0 | 532 | | int days = 0; |
| | 0 | 533 | | int monthsInYear = InternalIsLeapYear(year) ? 13 : 12; |
| | | 534 | | |
| | 0 | 535 | | while (monthsInYear != 0) |
| | | 536 | | { |
| | 0 | 537 | | days += InternalGetDaysInMonth(year, monthsInYear--); |
| | | 538 | | } |
| | | 539 | | |
| | 0 | 540 | | return days; |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | /// <summary> |
| | | 544 | | /// Returns the month part of the specified DateTime. |
| | | 545 | | /// The returned value is an integer between 1 and 13. |
| | | 546 | | /// </summary> |
| | | 547 | | public override int GetMonth(DateTime time) |
| | | 548 | | { |
| | 0 | 549 | | CheckTicksRange(time.Ticks); |
| | | 550 | | |
| | 0 | 551 | | TimeToLunar(time, out _, out int m, out _); |
| | | 552 | | |
| | 0 | 553 | | return m; |
| | | 554 | | } |
| | | 555 | | |
| | | 556 | | /// <summary> |
| | | 557 | | /// Returns the year part of the specified DateTime. |
| | | 558 | | /// The returned value is an integer between 1 and MaxCalendarYear. |
| | | 559 | | /// </summary> |
| | | 560 | | public override int GetYear(DateTime time) |
| | | 561 | | { |
| | 0 | 562 | | CheckTicksRange(time.Ticks); |
| | | 563 | | |
| | 0 | 564 | | TimeToLunar(time, out int y, out _, out _); |
| | | 565 | | |
| | 0 | 566 | | return GetYear(y, time); |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | /// <summary> |
| | | 570 | | /// Returns the day-of-week part of the specified DateTime. The returned value |
| | | 571 | | /// is an integer between 0 and 6, where 0 indicates Sunday, 1 indicates |
| | | 572 | | /// Monday, 2 indicates Tuesday, 3 indicates Wednesday, 4 indicates |
| | | 573 | | /// Thursday, 5 indicates Friday, and 6 indicates Saturday. |
| | | 574 | | /// </summary> |
| | | 575 | | public override DayOfWeek GetDayOfWeek(DateTime time) |
| | | 576 | | { |
| | 0 | 577 | | CheckTicksRange(time.Ticks); |
| | 0 | 578 | | return time.DayOfWeek; |
| | | 579 | | } |
| | | 580 | | |
| | | 581 | | /// <summary> |
| | | 582 | | /// Returns the number of months in the specified year and era. |
| | | 583 | | /// </summary> |
| | | 584 | | public override int GetMonthsInYear(int year, int era) |
| | | 585 | | { |
| | 0 | 586 | | year = CheckYearRange(year, era); |
| | 0 | 587 | | return InternalIsLeapYear(year) ? 13 : 12; |
| | | 588 | | } |
| | | 589 | | |
| | | 590 | | /// <summary> |
| | | 591 | | /// Checks whether a given day in the specified era is a leap day. |
| | | 592 | | /// This method returns true if the date is a leap day, or false if not. |
| | | 593 | | /// </summary> |
| | | 594 | | public override bool IsLeapDay(int year, int month, int day, int era) |
| | | 595 | | { |
| | 0 | 596 | | year = CheckYearMonthRange(year, month, era); |
| | 0 | 597 | | int daysInMonth = InternalGetDaysInMonth(year, month); |
| | | 598 | | |
| | 0 | 599 | | if (day < 1 || day > daysInMonth) |
| | | 600 | | { |
| | 0 | 601 | | throw new ArgumentOutOfRangeException( |
| | 0 | 602 | | nameof(day), |
| | 0 | 603 | | day, |
| | 0 | 604 | | SR.Format(SR.ArgumentOutOfRange_Day, daysInMonth, month)); |
| | | 605 | | } |
| | | 606 | | |
| | 0 | 607 | | int m = GetYearInfo(year, LeapMonth); |
| | 0 | 608 | | return (m != 0) && (month == (m + 1)); |
| | | 609 | | } |
| | | 610 | | |
| | | 611 | | /// <summary> |
| | | 612 | | /// Checks whether a given month in the specified era is a leap month. |
| | | 613 | | /// This method returns true if month is a leap month, or false if not. |
| | | 614 | | /// </summary> |
| | | 615 | | public override bool IsLeapMonth(int year, int month, int era) |
| | | 616 | | { |
| | 0 | 617 | | year = CheckYearMonthRange(year, month, era); |
| | 0 | 618 | | int m = GetYearInfo(year, LeapMonth); |
| | 0 | 619 | | return (m != 0) && (month == (m + 1)); |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | /// <summary> |
| | | 623 | | /// Returns the leap month in a calendar year of the specified era. This method returns 0 |
| | | 624 | | /// if this year is not a leap year. |
| | | 625 | | /// </summary> |
| | | 626 | | public override int GetLeapMonth(int year, int era) |
| | | 627 | | { |
| | 0 | 628 | | year = CheckYearRange(year, era); |
| | 0 | 629 | | int month = GetYearInfo(year, LeapMonth); |
| | 0 | 630 | | return month > 0 ? month + 1 : 0; |
| | | 631 | | } |
| | | 632 | | |
| | | 633 | | internal bool InternalIsLeapYear(int year) |
| | | 634 | | { |
| | 0 | 635 | | return GetYearInfo(year, LeapMonth) != 0; |
| | | 636 | | } |
| | | 637 | | |
| | | 638 | | /// <summary> |
| | | 639 | | /// Checks whether a given year in the specified era is a leap year. |
| | | 640 | | /// This method returns true if year is a leap year, or false if not. |
| | | 641 | | /// </summary> |
| | | 642 | | public override bool IsLeapYear(int year, int era) |
| | | 643 | | { |
| | 0 | 644 | | year = CheckYearRange(year, era); |
| | 0 | 645 | | return InternalIsLeapYear(year); |
| | | 646 | | } |
| | | 647 | | |
| | | 648 | | private const int DefaultGregorianTwoDigitYearMax = 2049; |
| | | 649 | | |
| | | 650 | | public override int TwoDigitYearMax |
| | | 651 | | { |
| | | 652 | | get |
| | | 653 | | { |
| | 0 | 654 | | if (_twoDigitYearMax == -1) |
| | | 655 | | { |
| | 0 | 656 | | _twoDigitYearMax = GetSystemTwoDigitYearSetting(BaseCalendarID, GetYear(new DateTime(DefaultGregoria |
| | | 657 | | } |
| | | 658 | | |
| | 0 | 659 | | return _twoDigitYearMax; |
| | | 660 | | } |
| | | 661 | | set |
| | | 662 | | { |
| | 0 | 663 | | VerifyWritable(); |
| | 0 | 664 | | if (value < 99 || value > MaxCalendarYear) |
| | | 665 | | { |
| | 0 | 666 | | throw new ArgumentOutOfRangeException( |
| | 0 | 667 | | nameof(value), |
| | 0 | 668 | | value, |
| | 0 | 669 | | SR.Format(SR.ArgumentOutOfRange_Range, 99, MaxCalendarYear)); |
| | | 670 | | } |
| | | 671 | | |
| | 0 | 672 | | _twoDigitYearMax = value; |
| | 0 | 673 | | } |
| | | 674 | | } |
| | | 675 | | |
| | | 676 | | public override int ToFourDigitYear(int year) |
| | | 677 | | { |
| | 0 | 678 | | ArgumentOutOfRangeException.ThrowIfNegative(year); |
| | | 679 | | |
| | 0 | 680 | | year = base.ToFourDigitYear(year); |
| | 0 | 681 | | CheckYearRange(year, CurrentEra); |
| | 0 | 682 | | return year; |
| | | 683 | | } |
| | | 684 | | } |
| | | 685 | | } |
| | | 686 | | |