6 // Copyright (c) Microsoft Corporation. All rights reserved.
9 namespace System.Globalization {
12 using System.Diagnostics.CodeAnalysis;
13 using System.Diagnostics.Contracts;
14 using Microsoft.Win32;
15 using PermissionSet = System.Security.PermissionSet;
16 using System.Security.Permissions;
18 /*=================================JapaneseCalendar==========================
20 ** JapaneseCalendar is based on Gregorian calendar. The month and day values are the same as
21 ** Gregorian calendar. However, the year value is an offset to the Gregorian
22 ** year based on the era.
24 ** This system is adopted by Emperor Meiji in 1868. The year value is counted based on the reign of an emperor,
25 ** and the era begins on the day an emperor ascends the throne and continues until his death.
26 ** The era changes at 12:00AM.
28 ** For example, the current era is Heisei. It started on 1989/1/8 A.D. Therefore, Gregorian year 1989 is also Heisei 1st.
29 ** 1989/1/8 A.D. is also Heisei 1st 1/8.
31 ** Any date in the year during which era is changed can be reckoned in either era. For example,
32 ** 1989/1/1 can be 1/1 Heisei 1st year or 1/1 Showa 64th year.
35 ** The DateTime can be represented by the JapaneseCalendar are limited to two factors:
36 ** 1. The min value and max value of DateTime class.
37 ** 2. The available era information.
39 ** Calendar support range:
40 ** Calendar Minimum Maximum
41 ** ========== ========== ==========
42 ** Gregorian 1868/09/08 9999/12/31
43 ** Japanese Meiji 01/01 Heisei 8011/12/31
44 ============================================================================*/
48 [System.Runtime.InteropServices.ComVisible(true)]
49 public class JapaneseCalendar : Calendar
51 internal static readonly DateTime calendarMinValue = new DateTime(1868, 9, 8);
54 [System.Runtime.InteropServices.ComVisible(false)]
55 public override DateTime MinSupportedDateTime
59 return (calendarMinValue);
63 [System.Runtime.InteropServices.ComVisible(false)]
64 public override DateTime MaxSupportedDateTime
68 return (DateTime.MaxValue);
72 // Return the type of the Japanese calendar.
75 [System.Runtime.InteropServices.ComVisible(false)]
76 public override CalendarAlgorithmType AlgorithmType
80 return CalendarAlgorithmType.SolarCalendar;
85 // Using a field initializer rather than a static constructor so that the whole class can be lazy
87 static internal volatile EraInfo[] japaneseEraInfo;
89 private const string c_japaneseErasHive = @"System\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras";
90 private const string c_japaneseErasHivePermissionList = @"HKEY_LOCAL_MACHINE\" + c_japaneseErasHive;
95 // m_EraInfo must be listed in reverse chronological order. The most recent era
96 // should be the first element.
97 // That is, m_EraInfo[0] contains the most recent era.
99 // We know about 4 built-in eras, however users may add additional era(s) from the
100 // registry, by adding values to HKLM\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras
102 // Registry values look like:
103 // yyyy.mm.dd=era_abbrev_english_englishabbrev
105 // Where yyyy.mm.dd is the registry value name, and also the date of the era start.
106 // yyyy, mm, and dd are the year, month & day the era begins (4, 2 & 2 digits long)
107 // era is the Japanese Era name
108 // abbrev is the Abbreviated Japanese Era Name
109 // english is the English name for the Era (unused)
110 // englishabbrev is the Abbreviated English name for the era.
111 // . is a delimiter, but the value of . doesn't matter.
112 // '_' marks the space between the japanese era name, japanese abbreviated era name
113 // english name, and abbreviated english names.
115 internal static EraInfo[] GetEraInfo()
117 // See if we need to build it
118 if (japaneseEraInfo == null)
120 // See if we have any eras from the registry
121 japaneseEraInfo = GetErasFromRegistry();
123 // See if we have to use the built-in eras
124 if (japaneseEraInfo == null)
126 // We know about some built-in ranges
127 EraInfo[] defaultEraRanges = new EraInfo[4];
128 defaultEraRanges[0] = new EraInfo( 4, 1989, 1, 8, 1988, 1, GregorianCalendar.MaxYear - 1988,
129 "\x5e73\x6210", "\x5e73", "H"); // era #4 start year/month/day, yearOffset, minEraYear
130 defaultEraRanges[1] = new EraInfo( 3, 1926, 12, 25, 1925, 1, 1989-1925,
131 "\x662d\x548c", "\x662d", "S"); // era #3,start year/month/day, yearOffset, minEraYear
132 defaultEraRanges[2] = new EraInfo( 2, 1912, 7, 30, 1911, 1, 1926-1911,
133 "\x5927\x6b63", "\x5927", "T"); // era #2,start year/month/day, yearOffset, minEraYear
134 defaultEraRanges[3] = new EraInfo( 1, 1868, 1, 1, 1867, 1, 1912-1867,
135 "\x660e\x6cbb", "\x660e", "M"); // era #1,start year/month/day, yearOffset, minEraYear
137 // Remember the ranges we built
138 japaneseEraInfo = defaultEraRanges;
142 // return the era we found/made
143 return japaneseEraInfo;
147 // GetErasFromRegistry()
149 // We know about 4 built-in eras, however users may add additional era(s) from the
150 // registry, by adding values to HKLM\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras
152 // Registry values look like:
153 // yyyy.mm.dd=era_abbrev_english_englishabbrev
155 // Where yyyy.mm.dd is the registry value name, and also the date of the era start.
156 // yyyy, mm, and dd are the year, month & day the era begins (4, 2 & 2 digits long)
157 // era is the Japanese Era name
158 // abbrev is the Abbreviated Japanese Era Name
159 // english is the English name for the Era (unused)
160 // englishabbrev is the Abbreviated English name for the era.
161 // . is a delimiter, but the value of . doesn't matter.
162 // '_' marks the space between the japanese era name, japanese abbreviated era name
163 // english name, and abbreviated english names.
164 [System.Security.SecuritySafeCritical] // auto-generated
165 private static EraInfo[] GetErasFromRegistry()
167 // Look in the registry key and see if we can find any ranges
169 EraInfo[] registryEraRanges = null;
173 // Need to access registry
174 PermissionSet permSet = new PermissionSet(PermissionState.None);
175 permSet.AddPermission(new RegistryPermission(RegistryPermissionAccess.Read, c_japaneseErasHivePermissionList));
177 RegistryKey key = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE).OpenSubKey(c_japaneseErasHive, false);
179 // Abort if we didn't find anything
180 if (key == null) return null;
182 // Look up the values in our reg key
183 String[] valueNames = key.GetValueNames();
184 if (valueNames != null && valueNames.Length > 0)
186 registryEraRanges = new EraInfo[valueNames.Length];
188 // Loop through the registry and read in all the values
189 for (int i = 0; i < valueNames.Length; i++)
191 // See if the era is a valid date
192 EraInfo era = GetEraFromValue(valueNames[i], key.GetValue(valueNames[i]).ToString());
194 // continue if not valid
195 if (era == null) continue;
197 // Remember we found one.
198 registryEraRanges[iFoundEras] = era;
203 catch (System.Security.SecurityException)
205 // If we weren't allowed to read, then just ignore the error
208 catch (System.IO.IOException)
210 // If key is being deleted just ignore the error
213 catch (System.UnauthorizedAccessException)
215 // Registry access rights permissions, just ignore the error
220 // If we didn't have valid eras, then fail
221 // should have at least 4 eras
223 if (iFoundEras < 4) return null;
226 // Now we have eras, clean them up.
228 // Clean up array length
229 Array.Resize(ref registryEraRanges, iFoundEras);
232 Array.Sort(registryEraRanges, CompareEraRanges);
234 // Clean up era information
235 for (int i = 0; i < registryEraRanges.Length; i++)
237 // eras count backwards from length to 1 (and are 1 based indexes into string arrays)
238 registryEraRanges[i].era = registryEraRanges.Length - i;
240 // update max era year
243 // First range is 'til the end of the calendar
244 registryEraRanges[0].maxEraYear = GregorianCalendar.MaxYear - registryEraRanges[0].yearOffset;
248 // Rest are until the next era (remember most recent era is first in array)
249 registryEraRanges[i].maxEraYear = registryEraRanges[i-1].yearOffset + 1 - registryEraRanges[i].yearOffset;
254 return registryEraRanges;
258 // Compare two era ranges, eg just the ticks
259 // Remember the era array is supposed to be in reverse chronological order
261 private static int CompareEraRanges(EraInfo a, EraInfo b)
263 return b.ticks.CompareTo(a.ticks);
269 // Parse the registry value name/data pair into an era
271 // Registry values look like:
272 // yyyy.mm.dd=era_abbrev_english_englishabbrev
274 // Where yyyy.mm.dd is the registry value name, and also the date of the era start.
275 // yyyy, mm, and dd are the year, month & day the era begins (4, 2 & 2 digits long)
276 // era is the Japanese Era name
277 // abbrev is the Abbreviated Japanese Era Name
278 // english is the English name for the Era (unused)
279 // englishabbrev is the Abbreviated English name for the era.
280 // . is a delimiter, but the value of . doesn't matter.
281 // '_' marks the space between the japanese era name, japanese abbreviated era name
282 // english name, and abbreviated english names.
283 private static EraInfo GetEraFromValue(String value, String data)
286 if (value == null || data == null) return null;
291 // Need exactly 10 characters in name for date
292 // yyyy.mm.dd although the . can be any character
293 if (value.Length != 10) return null;
299 if (!Number.TryParseInt32(value.Substring(0,4), NumberStyles.None, NumberFormatInfo.InvariantInfo, out year) ||
300 !Number.TryParseInt32(value.Substring(5,2), NumberStyles.None, NumberFormatInfo.InvariantInfo, out month) ||
301 !Number.TryParseInt32(value.Substring(8,2), NumberStyles.None, NumberFormatInfo.InvariantInfo, out day))
303 // Couldn't convert integer, fail
310 // Needs to be a certain length e_a_E_A at least (7 chars, exactly 4 groups)
311 String[] names = data.Split(new char[] {'_'});
313 // Should have exactly 4 parts
315 // 1 - Abbreviated Era Name
316 // 2 - English Era Name
317 // 3 - Abbreviated English Era Name
318 if (names.Length != 4) return null;
320 // Each part should have data in it
321 if (names[0].Length == 0 ||
322 names[1].Length == 0 ||
323 names[2].Length == 0 ||
324 names[3].Length == 0)
328 // Now we have an era we can build
329 // Note that the era # and max era year need cleaned up after sorting
330 // Don't use the full English Era Name (names[2])
332 return new EraInfo( 0, year, month, day, year - 1, 1, 0,
333 names[0], names[1], names[3]);
336 internal static volatile Calendar s_defaultInstance;
337 internal GregorianCalendarHelper helper;
339 /*=================================GetDefaultInstance==========================
340 **Action: Internal method to provide a default intance of JapaneseCalendar. Used by NLS+ implementation
341 ** and other calendars.
345 ============================================================================*/
347 internal static Calendar GetDefaultInstance() {
348 if (s_defaultInstance == null) {
349 s_defaultInstance = new JapaneseCalendar();
351 return (s_defaultInstance);
355 public JapaneseCalendar() {
357 new CultureInfo("ja-JP");
358 } catch (ArgumentException e) {
359 throw new TypeInitializationException(this.GetType().FullName, e);
361 helper = new GregorianCalendarHelper(this, GetEraInfo());
364 internal override int ID {
371 public override DateTime AddMonths(DateTime time, int months) {
372 return (helper.AddMonths(time, months));
376 public override DateTime AddYears(DateTime time, int years) {
377 return (helper.AddYears(time, years));
380 /*=================================GetDaysInMonth==========================
381 **Action: Returns the number of days in the month given by the year and month arguments.
382 **Returns: The number of days in the given month.
384 ** year The year in Japanese calendar.
386 ** era The Japanese era value.
388 ** ArgumentException If month is less than 1 or greater * than 12.
389 ============================================================================*/
392 public override int GetDaysInMonth(int year, int month, int era) {
393 return (helper.GetDaysInMonth(year, month, era));
397 public override int GetDaysInYear(int year, int era) {
398 return (helper.GetDaysInYear(year, era));
402 public override int GetDayOfMonth(DateTime time) {
403 return (helper.GetDayOfMonth(time));
407 public override DayOfWeek GetDayOfWeek(DateTime time) {
408 return (helper.GetDayOfWeek(time));
412 public override int GetDayOfYear(DateTime time)
414 return (helper.GetDayOfYear(time));
418 public override int GetMonthsInYear(int year, int era)
420 return (helper.GetMonthsInYear(year, era));
424 [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems.
425 [System.Runtime.InteropServices.ComVisible(false)]
426 public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
428 return (helper.GetWeekOfYear(time, rule, firstDayOfWeek));
431 /*=================================GetEra==========================
432 **Action: Get the era value of the specified time.
433 **Returns: The era value for the specified time.
435 ** time the specified date time.
436 **Exceptions: ArgumentOutOfRangeException if time is out of the valid era ranges.
437 ============================================================================*/
440 public override int GetEra(DateTime time) {
441 return (helper.GetEra(time));
445 public override int GetMonth(DateTime time) {
446 return (helper.GetMonth(time));
450 public override int GetYear(DateTime time) {
451 return (helper.GetYear(time));
455 public override bool IsLeapDay(int year, int month, int day, int era)
457 return (helper.IsLeapDay(year, month, day, era));
461 public override bool IsLeapYear(int year, int era) {
462 return (helper.IsLeapYear(year, era));
465 // Returns the leap month in a calendar year of the specified era. This method returns 0
466 // if this calendar does not have leap month, or this year is not a leap year.
469 [System.Runtime.InteropServices.ComVisible(false)]
470 public override int GetLeapMonth(int year, int era)
472 return (helper.GetLeapMonth(year, era));
476 public override bool IsLeapMonth(int year, int month, int era) {
477 return (helper.IsLeapMonth(year, month, era));
481 public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era) {
482 return (helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era));
485 // For Japanese calendar, four digit year is not used. Few emperors will live for more than one hundred years.
486 // Therefore, for any two digit number, we just return the original number.
488 public override int ToFourDigitYear(int year) {
490 throw new ArgumentOutOfRangeException("year",
491 Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
493 Contract.EndContractBlock();
495 if (year > helper.MaxYear) {
496 throw new ArgumentOutOfRangeException(
499 CultureInfo.CurrentCulture,
500 Environment.GetResourceString("ArgumentOutOfRange_Range"),
508 public override int[] Eras {
510 return (helper.Eras);
515 // Return the various era strings
516 // Note: The arrays are backwards of the eras
518 internal static String[] EraNames()
520 EraInfo[] eras = GetEraInfo();
521 String[] eraNames = new String[eras.Length];
523 for (int i = 0; i < eras.Length; i++)
525 // Strings are in chronological order, eras are backwards order.
526 eraNames[i] = eras[eras.Length - i - 1].eraName;
532 internal static String[] AbbrevEraNames()
534 EraInfo[] eras = GetEraInfo();
535 String[] erasAbbrev = new String[eras.Length];
537 for (int i = 0; i < eras.Length; i++)
539 // Strings are in chronological order, eras are backwards order.
540 erasAbbrev[i] = eras[eras.Length - i - 1].abbrevEraName;
546 internal static String[] EnglishEraNames()
548 EraInfo[] eras = GetEraInfo();
549 String[] erasEnglish = new String[eras.Length];
551 for (int i = 0; i < eras.Length; i++)
553 // Strings are in chronological order, eras are backwards order.
554 erasEnglish[i] = eras[eras.Length - i - 1].englishEraName;
560 private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 99;
562 internal override bool IsValidYear(int year, int era) {
563 return helper.IsValidYear(year, era);
566 public override int TwoDigitYearMax {
568 if (twoDigitYearMax == -1) {
569 twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
571 return (twoDigitYearMax);
576 if (value < 99 || value > helper.MaxYear)
578 throw new ArgumentOutOfRangeException(
581 CultureInfo.CurrentCulture,
582 Environment.GetResourceString("ArgumentOutOfRange_Range"),
586 twoDigitYearMax = value;