TaiwanCalendar.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. using System;
  5. using System.Diagnostics.CodeAnalysis;
  6. namespace System.Globalization
  7. {
  8. /*=================================TaiwanCalendar==========================
  9. **
  10. ** Taiwan calendar is based on the Gregorian calendar. And the year is an offset to Gregorian calendar.
  11. ** That is,
  12. ** Taiwan year = Gregorian year - 1911. So 1912/01/01 A.D. is Taiwan 1/01/01
  13. **
  14. ** Calendar support range:
  15. ** Calendar Minimum Maximum
  16. ** ========== ========== ==========
  17. ** Gregorian 1912/01/01 9999/12/31
  18. ** Taiwan 01/01/01 8088/12/31
  19. ============================================================================*/
  20. public class TaiwanCalendar : Calendar
  21. {
  22. //
  23. // The era value for the current era.
  24. //
  25. // Since
  26. // Gregorian Year = Era Year + yearOffset
  27. // When Gregorian Year 1912 is year 1, so that
  28. // 1912 = 1 + yearOffset
  29. // So yearOffset = 1911
  30. //m_EraInfo[0] = new EraInfo(1, new DateTime(1912, 1, 1).Ticks, 1911, 1, GregorianCalendar.MaxYear - 1911);
  31. // Initialize our era info.
  32. internal static EraInfo[] taiwanEraInfo = new EraInfo[] {
  33. new EraInfo( 1, 1912, 1, 1, 1911, 1, GregorianCalendar.MaxYear - 1911) // era #, start year/month/day, yearOffset, minEraYear
  34. };
  35. internal static volatile Calendar s_defaultInstance;
  36. internal GregorianCalendarHelper helper;
  37. /*=================================GetDefaultInstance==========================
  38. **Action: Internal method to provide a default intance of TaiwanCalendar. Used by NLS+ implementation
  39. ** and other calendars.
  40. **Returns:
  41. **Arguments:
  42. **Exceptions:
  43. ============================================================================*/
  44. internal static Calendar GetDefaultInstance()
  45. {
  46. if (s_defaultInstance == null)
  47. {
  48. s_defaultInstance = new TaiwanCalendar();
  49. }
  50. return (s_defaultInstance);
  51. }
  52. internal static readonly DateTime calendarMinValue = new DateTime(1912, 1, 1);
  53. public override DateTime MinSupportedDateTime
  54. {
  55. get
  56. {
  57. return (calendarMinValue);
  58. }
  59. }
  60. public override DateTime MaxSupportedDateTime
  61. {
  62. get
  63. {
  64. return (DateTime.MaxValue);
  65. }
  66. }
  67. public override CalendarAlgorithmType AlgorithmType
  68. {
  69. get
  70. {
  71. return CalendarAlgorithmType.SolarCalendar;
  72. }
  73. }
  74. // Return the type of the Taiwan calendar.
  75. //
  76. public TaiwanCalendar()
  77. {
  78. try
  79. {
  80. new CultureInfo("zh-TW");
  81. }
  82. catch (ArgumentException e)
  83. {
  84. throw new TypeInitializationException(this.GetType().ToString(), e);
  85. }
  86. helper = new GregorianCalendarHelper(this, taiwanEraInfo);
  87. }
  88. internal override CalendarId ID
  89. {
  90. get
  91. {
  92. return CalendarId.TAIWAN;
  93. }
  94. }
  95. public override DateTime AddMonths(DateTime time, int months)
  96. {
  97. return (helper.AddMonths(time, months));
  98. }
  99. public override DateTime AddYears(DateTime time, int years)
  100. {
  101. return (helper.AddYears(time, years));
  102. }
  103. public override int GetDaysInMonth(int year, int month, int era)
  104. {
  105. return (helper.GetDaysInMonth(year, month, era));
  106. }
  107. public override int GetDaysInYear(int year, int era)
  108. {
  109. return (helper.GetDaysInYear(year, era));
  110. }
  111. public override int GetDayOfMonth(DateTime time)
  112. {
  113. return (helper.GetDayOfMonth(time));
  114. }
  115. public override DayOfWeek GetDayOfWeek(DateTime time)
  116. {
  117. return (helper.GetDayOfWeek(time));
  118. }
  119. public override int GetDayOfYear(DateTime time)
  120. {
  121. return (helper.GetDayOfYear(time));
  122. }
  123. public override int GetMonthsInYear(int year, int era)
  124. {
  125. return (helper.GetMonthsInYear(year, era));
  126. }
  127. public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
  128. {
  129. return (helper.GetWeekOfYear(time, rule, firstDayOfWeek));
  130. }
  131. public override int GetEra(DateTime time)
  132. {
  133. return (helper.GetEra(time));
  134. }
  135. public override int GetMonth(DateTime time)
  136. {
  137. return (helper.GetMonth(time));
  138. }
  139. public override int GetYear(DateTime time)
  140. {
  141. return (helper.GetYear(time));
  142. }
  143. public override bool IsLeapDay(int year, int month, int day, int era)
  144. {
  145. return (helper.IsLeapDay(year, month, day, era));
  146. }
  147. public override bool IsLeapYear(int year, int era)
  148. {
  149. return (helper.IsLeapYear(year, era));
  150. }
  151. // Returns the leap month in a calendar year of the specified era. This method returns 0
  152. // if this calendar does not have leap month, or this year is not a leap year.
  153. //
  154. public override int GetLeapMonth(int year, int era)
  155. {
  156. return (helper.GetLeapMonth(year, era));
  157. }
  158. public override bool IsLeapMonth(int year, int month, int era)
  159. {
  160. return (helper.IsLeapMonth(year, month, era));
  161. }
  162. public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  163. {
  164. return (helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era));
  165. }
  166. public override int[] Eras
  167. {
  168. get
  169. {
  170. return (helper.Eras);
  171. }
  172. }
  173. private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 99;
  174. public override int TwoDigitYearMax
  175. {
  176. get
  177. {
  178. if (twoDigitYearMax == -1)
  179. {
  180. twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
  181. }
  182. return (twoDigitYearMax);
  183. }
  184. set
  185. {
  186. VerifyWritable();
  187. if (value < 99 || value > helper.MaxYear)
  188. {
  189. throw new ArgumentOutOfRangeException(
  190. "year",
  191. string.Format(
  192. CultureInfo.CurrentCulture,
  193. SR.ArgumentOutOfRange_Range,
  194. 99,
  195. helper.MaxYear));
  196. }
  197. twoDigitYearMax = value;
  198. }
  199. }
  200. // For Taiwan calendar, four digit year is not used.
  201. // Therefore, for any two digit number, we just return the original number.
  202. public override int ToFourDigitYear(int year)
  203. {
  204. if (year <= 0)
  205. {
  206. throw new ArgumentOutOfRangeException(nameof(year),
  207. SR.ArgumentOutOfRange_NeedPosNum);
  208. }
  209. if (year > helper.MaxYear)
  210. {
  211. throw new ArgumentOutOfRangeException(
  212. nameof(year),
  213. string.Format(
  214. CultureInfo.CurrentCulture,
  215. SR.ArgumentOutOfRange_Range,
  216. 1,
  217. helper.MaxYear));
  218. }
  219. return (year);
  220. }
  221. }
  222. }