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