JapaneseCalendar.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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() => s_defaultInstance ??= new JapaneseCalendar();
  80. public JapaneseCalendar()
  81. {
  82. try
  83. {
  84. new CultureInfo("ja-JP");
  85. }
  86. catch (ArgumentException e)
  87. {
  88. throw new TypeInitializationException(this.GetType().ToString(), e);
  89. }
  90. _helper = new GregorianCalendarHelper(this, GetEraInfo());
  91. }
  92. internal override CalendarId ID => CalendarId.JAPAN;
  93. public override DateTime AddMonths(DateTime time, int months)
  94. {
  95. return _helper.AddMonths(time, months);
  96. }
  97. public override DateTime AddYears(DateTime time, int years)
  98. {
  99. return _helper.AddYears(time, years);
  100. }
  101. public override int GetDaysInMonth(int year, int month, int era)
  102. {
  103. return _helper.GetDaysInMonth(year, month, era);
  104. }
  105. public override int GetDaysInYear(int year, int era)
  106. {
  107. return _helper.GetDaysInYear(year, era);
  108. }
  109. public override int GetDayOfMonth(DateTime time)
  110. {
  111. return _helper.GetDayOfMonth(time);
  112. }
  113. public override DayOfWeek GetDayOfWeek(DateTime time)
  114. {
  115. return _helper.GetDayOfWeek(time);
  116. }
  117. public override int GetDayOfYear(DateTime time)
  118. {
  119. return _helper.GetDayOfYear(time);
  120. }
  121. public override int GetMonthsInYear(int year, int era)
  122. {
  123. return _helper.GetMonthsInYear(year, era);
  124. }
  125. public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
  126. {
  127. return _helper.GetWeekOfYear(time, rule, firstDayOfWeek);
  128. }
  129. public override int GetEra(DateTime time)
  130. {
  131. return _helper.GetEra(time);
  132. }
  133. public override int GetMonth(DateTime time)
  134. {
  135. return _helper.GetMonth(time);
  136. }
  137. public override int GetYear(DateTime time)
  138. {
  139. return _helper.GetYear(time);
  140. }
  141. public override bool IsLeapDay(int year, int month, int day, int era)
  142. {
  143. return _helper.IsLeapDay(year, month, day, era);
  144. }
  145. public override bool IsLeapYear(int year, int era)
  146. {
  147. return _helper.IsLeapYear(year, era);
  148. }
  149. public override int GetLeapMonth(int year, int era)
  150. {
  151. return _helper.GetLeapMonth(year, era);
  152. }
  153. public override bool IsLeapMonth(int year, int month, int era)
  154. {
  155. return _helper.IsLeapMonth(year, month, era);
  156. }
  157. public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  158. {
  159. return _helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era);
  160. }
  161. /// <summary>
  162. /// For Japanese calendar, four digit year is not used. Few emperors will live for more than one hundred years.
  163. /// Therefore, for any two digit number, we just return the original number.
  164. /// </summary>
  165. public override int ToFourDigitYear(int year)
  166. {
  167. if (year <= 0)
  168. {
  169. throw new ArgumentOutOfRangeException(nameof(year), year, SR.ArgumentOutOfRange_NeedPosNum);
  170. }
  171. if (year > _helper.MaxYear)
  172. {
  173. throw new ArgumentOutOfRangeException(
  174. nameof(year),
  175. year,
  176. SR.Format(SR.ArgumentOutOfRange_Range, 1, _helper.MaxYear));
  177. }
  178. return year;
  179. }
  180. public override int[] Eras => _helper.Eras;
  181. /// <summary>
  182. /// Return the various era strings
  183. /// Note: The arrays are backwards of the eras
  184. /// </summary>
  185. internal static string[] EraNames()
  186. {
  187. EraInfo[] eras = GetEraInfo();
  188. string[] eraNames = new string[eras.Length];
  189. for (int i = 0; i < eras.Length; i++)
  190. {
  191. // Strings are in chronological order, eras are backwards order.
  192. eraNames[i] = eras[eras.Length - i - 1].eraName!;
  193. }
  194. return eraNames;
  195. }
  196. internal static string[] AbbrevEraNames()
  197. {
  198. EraInfo[] eras = GetEraInfo();
  199. string[] erasAbbrev = new string[eras.Length];
  200. for (int i = 0; i < eras.Length; i++)
  201. {
  202. // Strings are in chronological order, eras are backwards order.
  203. erasAbbrev[i] = eras[eras.Length - i - 1].abbrevEraName!;
  204. }
  205. return erasAbbrev;
  206. }
  207. internal static string[] EnglishEraNames()
  208. {
  209. EraInfo[] eras = GetEraInfo();
  210. string[] erasEnglish = new string[eras.Length];
  211. for (int i = 0; i < eras.Length; i++)
  212. {
  213. // Strings are in chronological order, eras are backwards order.
  214. erasEnglish[i] = eras[eras.Length - i - 1].englishEraName!;
  215. }
  216. return erasEnglish;
  217. }
  218. private const int DefaultTwoDigitYearMax = 99;
  219. internal override bool IsValidYear(int year, int era)
  220. {
  221. return _helper.IsValidYear(year, era);
  222. }
  223. public override int TwoDigitYearMax
  224. {
  225. get
  226. {
  227. if (_twoDigitYearMax == -1)
  228. {
  229. _twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DefaultTwoDigitYearMax);
  230. }
  231. return _twoDigitYearMax;
  232. }
  233. set
  234. {
  235. VerifyWritable();
  236. if (value < 99 || value > _helper.MaxYear)
  237. {
  238. throw new ArgumentOutOfRangeException(
  239. nameof(value),
  240. value,
  241. SR.Format(SR.ArgumentOutOfRange_Range, 99, _helper.MaxYear));
  242. }
  243. _twoDigitYearMax = value;
  244. }
  245. }
  246. }
  247. }