JapaneseCalendar.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /// JapaneseCalendar is based on Gregorian calendar. The month and day values are the same as
  8. /// Gregorian calendar. However, the year value is an offset to the Gregorian
  9. /// year based on the era.
  10. ///
  11. /// This system is adopted by Emperor Meiji in 1868. The year value is counted based on the reign of an emperor,
  12. /// and the era begins on the day an emperor ascends the throne and continues until his death or his abdication.
  13. /// The era changes at 12:00AM.
  14. ///
  15. /// For example, the current era is Reiwa. It started on 2019/5/1 A.D. Therefore, Gregorian year 2019 is also Reiwa 1st.
  16. /// 2019/5/1 A.D. is also Reiwa 1st 5/1.
  17. ///
  18. /// Any date in the year during which era is changed can be reckoned in either era. For example,
  19. /// 2019/1/1 can be 1/1 Reiwa 1st year or 1/1 Heisei 31st year.
  20. ///
  21. /// Note:
  22. /// The DateTime can be represented by the JapaneseCalendar are limited to two factors:
  23. /// 1. The min value and max value of DateTime class.
  24. /// 2. The available era information.
  25. /// </summary>
  26. /// <remarks>
  27. /// Calendar support range:
  28. /// Calendar Minimum Maximum
  29. /// ========== ========== ==========
  30. /// Gregorian 1868/09/08 9999/12/31
  31. /// Japanese Meiji 01/01 Reiwa 7981/12/31
  32. /// </remarks>
  33. public partial class JapaneseCalendar : Calendar
  34. {
  35. private static readonly DateTime s_calendarMinValue = new DateTime(1868, 9, 8);
  36. public override DateTime MinSupportedDateTime => s_calendarMinValue;
  37. public override DateTime MaxSupportedDateTime => DateTime.MaxValue;
  38. public override CalendarAlgorithmType AlgorithmType => CalendarAlgorithmType.SolarCalendar;
  39. // Using a field initializer rather than a static constructor so that the whole class can be lazy
  40. // init.
  41. private static volatile EraInfo[]? s_japaneseEraInfo;
  42. // m_EraInfo must be listed in reverse chronological order. The most recent era
  43. // should be the first element.
  44. // That is, m_EraInfo[0] contains the most recent era.
  45. //
  46. // We know about 4 built-in eras, however users may add additional era(s) from the
  47. // registry, by adding values to HKLM\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras
  48. // we don't read the registry and instead we call WinRT to get the needed informatio
  49. //
  50. // Registry values look like:
  51. // yyyy.mm.dd=era_abbrev_english_englishabbrev
  52. //
  53. // Where yyyy.mm.dd is the registry value name, and also the date of the era start.
  54. // yyyy, mm, and dd are the year, month & day the era begins (4, 2 & 2 digits long)
  55. // era is the Japanese Era name
  56. // abbrev is the Abbreviated Japanese Era Name
  57. // english is the English name for the Era (unused)
  58. // englishabbrev is the Abbreviated English name for the era.
  59. // . is a delimiter, but the value of . doesn't matter.
  60. // '_' marks the space between the japanese era name, japanese abbreviated era name
  61. // english name, and abbreviated english names.
  62. internal static EraInfo[] GetEraInfo()
  63. {
  64. // See if we need to build it
  65. return s_japaneseEraInfo ??
  66. (s_japaneseEraInfo = GetJapaneseEras()) ??
  67. // See if we have to use the built-in eras
  68. (s_japaneseEraInfo = new EraInfo[]
  69. {
  70. new EraInfo(5, 2019, 5, 1, 2018, 1, GregorianCalendar.MaxYear - 2018, "\x4ee4\x548c", "\x4ee4", "R"),
  71. new EraInfo(4, 1989, 1, 8, 1988, 1, 2019 - 1988, "\x5e73\x6210", "\x5e73", "H"),
  72. new EraInfo(3, 1926, 12, 25, 1925, 1, 1989 - 1925, "\x662d\x548c", "\x662d", "S"),
  73. new EraInfo(2, 1912, 7, 30, 1911, 1, 1926 - 1911, "\x5927\x6b63", "\x5927", "T"),
  74. new EraInfo(1, 1868, 1, 1, 1867, 1, 1912 - 1867, "\x660e\x6cbb", "\x660e", "M")
  75. });
  76. }
  77. internal static volatile Calendar? s_defaultInstance;
  78. internal GregorianCalendarHelper _helper;
  79. internal static Calendar GetDefaultInstance()
  80. {
  81. return s_defaultInstance ?? (s_defaultInstance = new JapaneseCalendar());
  82. }
  83. public JapaneseCalendar()
  84. {
  85. try
  86. {
  87. new CultureInfo("ja-JP");
  88. }
  89. catch (ArgumentException e)
  90. {
  91. throw new TypeInitializationException(this.GetType().ToString(), e);
  92. }
  93. _helper = new GregorianCalendarHelper(this, GetEraInfo());
  94. }
  95. internal override CalendarId ID => CalendarId.JAPAN;
  96. public override DateTime AddMonths(DateTime time, int months)
  97. {
  98. return _helper.AddMonths(time, months);
  99. }
  100. public override DateTime AddYears(DateTime time, int years)
  101. {
  102. return _helper.AddYears(time, years);
  103. }
  104. public override int GetDaysInMonth(int year, int month, int era)
  105. {
  106. return _helper.GetDaysInMonth(year, month, era);
  107. }
  108. public override int GetDaysInYear(int year, int era)
  109. {
  110. return _helper.GetDaysInYear(year, era);
  111. }
  112. public override int GetDayOfMonth(DateTime time)
  113. {
  114. return _helper.GetDayOfMonth(time);
  115. }
  116. public override DayOfWeek GetDayOfWeek(DateTime time)
  117. {
  118. return _helper.GetDayOfWeek(time);
  119. }
  120. public override int GetDayOfYear(DateTime time)
  121. {
  122. return _helper.GetDayOfYear(time);
  123. }
  124. public override int GetMonthsInYear(int year, int era)
  125. {
  126. return _helper.GetMonthsInYear(year, era);
  127. }
  128. public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
  129. {
  130. return _helper.GetWeekOfYear(time, rule, firstDayOfWeek);
  131. }
  132. public override int GetEra(DateTime time)
  133. {
  134. return _helper.GetEra(time);
  135. }
  136. public override int GetMonth(DateTime time)
  137. {
  138. return _helper.GetMonth(time);
  139. }
  140. public override int GetYear(DateTime time)
  141. {
  142. return _helper.GetYear(time);
  143. }
  144. public override bool IsLeapDay(int year, int month, int day, int era)
  145. {
  146. return _helper.IsLeapDay(year, month, day, era);
  147. }
  148. public override bool IsLeapYear(int year, int era)
  149. {
  150. return _helper.IsLeapYear(year, era);
  151. }
  152. public override int GetLeapMonth(int year, int era)
  153. {
  154. return _helper.GetLeapMonth(year, era);
  155. }
  156. public override bool IsLeapMonth(int year, int month, int era)
  157. {
  158. return _helper.IsLeapMonth(year, month, era);
  159. }
  160. public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  161. {
  162. return _helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era);
  163. }
  164. /// <summary>
  165. /// For Japanese calendar, four digit year is not used. Few emperors will live for more than one hundred years.
  166. /// Therefore, for any two digit number, we just return the original number.
  167. /// </summary>
  168. public override int ToFourDigitYear(int year)
  169. {
  170. if (year <= 0)
  171. {
  172. throw new ArgumentOutOfRangeException(nameof(year), year, SR.ArgumentOutOfRange_NeedPosNum);
  173. }
  174. if (year > _helper.MaxYear)
  175. {
  176. throw new ArgumentOutOfRangeException(
  177. nameof(year),
  178. year,
  179. SR.Format(SR.ArgumentOutOfRange_Range, 1, _helper.MaxYear));
  180. }
  181. return year;
  182. }
  183. public override int[] Eras => _helper.Eras;
  184. /// <summary>
  185. /// Return the various era strings
  186. /// Note: The arrays are backwards of the eras
  187. /// </summary>
  188. internal static string[] EraNames()
  189. {
  190. EraInfo[] eras = GetEraInfo();
  191. string[] eraNames = new string[eras.Length];
  192. for (int i = 0; i < eras.Length; i++)
  193. {
  194. // Strings are in chronological order, eras are backwards order.
  195. eraNames[i] = eras[eras.Length - i - 1].eraName!;
  196. }
  197. return eraNames;
  198. }
  199. internal static string[] AbbrevEraNames()
  200. {
  201. EraInfo[] eras = GetEraInfo();
  202. string[] erasAbbrev = new string[eras.Length];
  203. for (int i = 0; i < eras.Length; i++)
  204. {
  205. // Strings are in chronological order, eras are backwards order.
  206. erasAbbrev[i] = eras[eras.Length - i - 1].abbrevEraName!;
  207. }
  208. return erasAbbrev;
  209. }
  210. internal static string[] EnglishEraNames()
  211. {
  212. EraInfo[] eras = GetEraInfo();
  213. string[] erasEnglish = new string[eras.Length];
  214. for (int i = 0; i < eras.Length; i++)
  215. {
  216. // Strings are in chronological order, eras are backwards order.
  217. erasEnglish[i] = eras[eras.Length - i - 1].englishEraName!;
  218. }
  219. return erasEnglish;
  220. }
  221. private const int DefaultTwoDigitYearMax = 99;
  222. internal override bool IsValidYear(int year, int era)
  223. {
  224. return _helper.IsValidYear(year, era);
  225. }
  226. public override int TwoDigitYearMax
  227. {
  228. get
  229. {
  230. if (_twoDigitYearMax == -1)
  231. {
  232. _twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DefaultTwoDigitYearMax);
  233. }
  234. return _twoDigitYearMax;
  235. }
  236. set
  237. {
  238. VerifyWritable();
  239. if (value < 99 || value > _helper.MaxYear)
  240. {
  241. throw new ArgumentOutOfRangeException(
  242. nameof(value),
  243. value,
  244. SR.Format(SR.ArgumentOutOfRange_Range, 99, _helper.MaxYear));
  245. }
  246. _twoDigitYearMax = value;
  247. }
  248. }
  249. }
  250. }