TaiwanCalendar.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /// <summary>
  7. /// Taiwan calendar is based on the Gregorian calendar. And the year is an offset to Gregorian calendar.
  8. /// That is,
  9. /// Taiwan year = Gregorian year - 1911. So 1912/01/01 A.D. is Taiwan 1/01/01
  10. /// </summary>
  11. /// <remarks>
  12. /// Calendar support range:
  13. /// Calendar Minimum Maximum
  14. /// ========== ========== ==========
  15. /// Gregorian 1912/01/01 9999/12/31
  16. /// Taiwan 01/01/01 8088/12/31
  17. /// </remarks>
  18. public class TaiwanCalendar : Calendar
  19. {
  20. // Since
  21. // Gregorian Year = Era Year + yearOffset
  22. // When Gregorian Year 1912 is year 1, so that
  23. // 1912 = 1 + yearOffset
  24. // So yearOffset = 1911
  25. private static readonly EraInfo[] s_taiwanEraInfo = new EraInfo[]
  26. {
  27. new EraInfo(1, 1912, 1, 1, 1911, 1, GregorianCalendar.MaxYear - 1911) // era #, start year/month/day, yearOffset, minEraYear
  28. };
  29. private static volatile Calendar? s_defaultInstance;
  30. private readonly GregorianCalendarHelper _helper;
  31. internal static Calendar GetDefaultInstance() => s_defaultInstance ??= new TaiwanCalendar();
  32. private static readonly DateTime s_calendarMinValue = new DateTime(1912, 1, 1);
  33. public override DateTime MinSupportedDateTime => s_calendarMinValue;
  34. public override DateTime MaxSupportedDateTime => DateTime.MaxValue;
  35. public override CalendarAlgorithmType AlgorithmType => CalendarAlgorithmType.SolarCalendar;
  36. public TaiwanCalendar()
  37. {
  38. try
  39. {
  40. new CultureInfo("zh-TW");
  41. }
  42. catch (ArgumentException e)
  43. {
  44. throw new TypeInitializationException(GetType().ToString(), e);
  45. }
  46. _helper = new GregorianCalendarHelper(this, s_taiwanEraInfo);
  47. }
  48. internal override CalendarId ID => CalendarId.TAIWAN;
  49. public override DateTime AddMonths(DateTime time, int months)
  50. {
  51. return _helper.AddMonths(time, months);
  52. }
  53. public override DateTime AddYears(DateTime time, int years)
  54. {
  55. return _helper.AddYears(time, years);
  56. }
  57. public override int GetDaysInMonth(int year, int month, int era)
  58. {
  59. return _helper.GetDaysInMonth(year, month, era);
  60. }
  61. public override int GetDaysInYear(int year, int era)
  62. {
  63. return _helper.GetDaysInYear(year, era);
  64. }
  65. public override int GetDayOfMonth(DateTime time)
  66. {
  67. return _helper.GetDayOfMonth(time);
  68. }
  69. public override DayOfWeek GetDayOfWeek(DateTime time)
  70. {
  71. return _helper.GetDayOfWeek(time);
  72. }
  73. public override int GetDayOfYear(DateTime time)
  74. {
  75. return _helper.GetDayOfYear(time);
  76. }
  77. public override int GetMonthsInYear(int year, int era)
  78. {
  79. return _helper.GetMonthsInYear(year, era);
  80. }
  81. public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
  82. {
  83. return _helper.GetWeekOfYear(time, rule, firstDayOfWeek);
  84. }
  85. public override int GetEra(DateTime time)
  86. {
  87. return _helper.GetEra(time);
  88. }
  89. public override int GetMonth(DateTime time)
  90. {
  91. return _helper.GetMonth(time);
  92. }
  93. public override int GetYear(DateTime time)
  94. {
  95. return _helper.GetYear(time);
  96. }
  97. public override bool IsLeapDay(int year, int month, int day, int era)
  98. {
  99. return _helper.IsLeapDay(year, month, day, era);
  100. }
  101. public override bool IsLeapYear(int year, int era)
  102. {
  103. return _helper.IsLeapYear(year, era);
  104. }
  105. public override int GetLeapMonth(int year, int era)
  106. {
  107. return _helper.GetLeapMonth(year, era);
  108. }
  109. public override bool IsLeapMonth(int year, int month, int era)
  110. {
  111. return _helper.IsLeapMonth(year, month, era);
  112. }
  113. public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  114. {
  115. return _helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era);
  116. }
  117. public override int[] Eras => _helper.Eras;
  118. private const int DefaultTwoDigitYearMax = 99;
  119. public override int TwoDigitYearMax
  120. {
  121. get
  122. {
  123. if (_twoDigitYearMax == -1)
  124. {
  125. _twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DefaultTwoDigitYearMax);
  126. }
  127. return _twoDigitYearMax;
  128. }
  129. set
  130. {
  131. VerifyWritable();
  132. if (value < 99 || value > _helper.MaxYear)
  133. {
  134. throw new ArgumentOutOfRangeException(
  135. nameof(value),
  136. value,
  137. SR.Format(SR.ArgumentOutOfRange_Range, 99, _helper.MaxYear));
  138. }
  139. _twoDigitYearMax = value;
  140. }
  141. }
  142. /// <summary>
  143. /// For Taiwan calendar, four digit year is not used.
  144. /// Therefore, for any two digit number, we just return the original number.
  145. /// </summary>
  146. public override int ToFourDigitYear(int year)
  147. {
  148. if (year <= 0)
  149. {
  150. throw new ArgumentOutOfRangeException(nameof(year), year, SR.ArgumentOutOfRange_NeedPosNum);
  151. }
  152. if (year > _helper.MaxYear)
  153. {
  154. throw new ArgumentOutOfRangeException(
  155. nameof(year),
  156. year,
  157. SR.Format(SR.ArgumentOutOfRange_Range, 1, _helper.MaxYear));
  158. }
  159. return year;
  160. }
  161. }
  162. }