mscorlib(4.0.0.0) API with additions
JapaneseCalendar.cs
1 using Microsoft.Win32;
2 using System.IO;
4 using System.Security;
6 
7 namespace System.Globalization
8 {
10  [Serializable]
11  [ComVisible(true)]
12  public class JapaneseCalendar : Calendar
13  {
14  internal static readonly DateTime calendarMinValue = new DateTime(1868, 9, 8);
15 
16  internal static volatile EraInfo[] japaneseEraInfo;
17 
18  private const string c_japaneseErasHive = "System\\CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras";
19 
20  private const string c_japaneseErasHivePermissionList = "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras";
21 
22  internal static volatile Calendar s_defaultInstance;
23 
24  internal GregorianCalendarHelper helper;
25 
26  private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 99;
27 
30  [ComVisible(false)]
31  public override DateTime MinSupportedDateTime
32  {
33  get
34  {
35  return calendarMinValue;
36  }
37  }
38 
41  [ComVisible(false)]
42  public override DateTime MaxSupportedDateTime
43  {
44  get
45  {
46  return DateTime.MaxValue;
47  }
48  }
49 
52  [ComVisible(false)]
53  public override CalendarAlgorithmType AlgorithmType
54  {
55  get
56  {
57  return CalendarAlgorithmType.SolarCalendar;
58  }
59  }
60 
61  internal override int ID => 3;
62 
65  public override int[] Eras => helper.Eras;
66 
71  public override int TwoDigitYearMax
72  {
73  get
74  {
75  if (twoDigitYearMax == -1)
76  {
77  twoDigitYearMax = Calendar.GetSystemTwoDigitYearSetting(ID, 99);
78  }
79  return twoDigitYearMax;
80  }
81  set
82  {
83  VerifyWritable();
84  if (value < 99 || value > helper.MaxYear)
85  {
86  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 99, helper.MaxYear));
87  }
88  twoDigitYearMax = value;
89  }
90  }
91 
92  internal static EraInfo[] GetEraInfo()
93  {
94  if (japaneseEraInfo == null)
95  {
96  japaneseEraInfo = GetErasFromRegistry();
97  if (japaneseEraInfo == null)
98  {
99  japaneseEraInfo = new EraInfo[4]
100  {
101  new EraInfo(4, 1989, 1, 8, 1988, 1, 8011, "平成", "平", "H"),
102  new EraInfo(3, 1926, 12, 25, 1925, 1, 64, "昭和", "昭", "S"),
103  new EraInfo(2, 1912, 7, 30, 1911, 1, 15, "大正", "大", "T"),
104  new EraInfo(1, 1868, 1, 1, 1867, 1, 45, "明治", "明", "M")
105  };
106  }
107  }
108  return japaneseEraInfo;
109  }
110 
111  [SecuritySafeCritical]
112  private static EraInfo[] GetErasFromRegistry()
113  {
114  int num = 0;
115  EraInfo[] array = null;
116  try
117  {
118  PermissionSet permissionSet = new PermissionSet(PermissionState.None);
119  permissionSet.AddPermission(new RegistryPermission(RegistryPermissionAccess.Read, "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras"));
120  permissionSet.Assert();
121  RegistryKey registryKey = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE).OpenSubKey("System\\CurrentControlSet\\Control\\Nls\\Calendars\\Japanese\\Eras", writable: false);
122  if (registryKey == null)
123  {
124  return null;
125  }
126  string[] valueNames = registryKey.GetValueNames();
127  if (valueNames != null && valueNames.Length != 0)
128  {
129  array = new EraInfo[valueNames.Length];
130  for (int i = 0; i < valueNames.Length; i++)
131  {
132  EraInfo eraFromValue = GetEraFromValue(valueNames[i], registryKey.GetValue(valueNames[i]).ToString());
133  if (eraFromValue != null)
134  {
135  array[num] = eraFromValue;
136  num++;
137  }
138  }
139  }
140  }
141  catch (SecurityException)
142  {
143  return null;
144  }
145  catch (IOException)
146  {
147  return null;
148  }
149  catch (UnauthorizedAccessException)
150  {
151  return null;
152  }
153  if (num < 4)
154  {
155  return null;
156  }
157  Array.Resize(ref array, num);
158  Array.Sort(array, CompareEraRanges);
159  for (int j = 0; j < array.Length; j++)
160  {
161  array[j].era = array.Length - j;
162  if (j == 0)
163  {
164  array[0].maxEraYear = 9999 - array[0].yearOffset;
165  }
166  else
167  {
168  array[j].maxEraYear = array[j - 1].yearOffset + 1 - array[j].yearOffset;
169  }
170  }
171  return array;
172  }
173 
174  private static int CompareEraRanges(EraInfo a, EraInfo b)
175  {
176  return b.ticks.CompareTo(a.ticks);
177  }
178 
179  private static EraInfo GetEraFromValue(string value, string data)
180  {
181  if (value == null || data == null)
182  {
183  return null;
184  }
185  if (value.Length != 10)
186  {
187  return null;
188  }
189  if (!Number.TryParseInt32(value.Substring(0, 4), NumberStyles.None, NumberFormatInfo.InvariantInfo, out int result) || !Number.TryParseInt32(value.Substring(5, 2), NumberStyles.None, NumberFormatInfo.InvariantInfo, out int result2) || !Number.TryParseInt32(value.Substring(8, 2), NumberStyles.None, NumberFormatInfo.InvariantInfo, out int result3))
190  {
191  return null;
192  }
193  string[] array = data.Split('_');
194  if (array.Length != 4)
195  {
196  return null;
197  }
198  if (array[0].Length == 0 || array[1].Length == 0 || array[2].Length == 0 || array[3].Length == 0)
199  {
200  return null;
201  }
202  return new EraInfo(0, result, result2, result3, result - 1, 1, 0, array[0], array[1], array[3]);
203  }
204 
205  internal static Calendar GetDefaultInstance()
206  {
207  if (s_defaultInstance == null)
208  {
209  s_defaultInstance = new JapaneseCalendar();
210  }
211  return s_defaultInstance;
212  }
213 
217  {
218  try
219  {
220  new CultureInfo("ja-JP");
221  }
222  catch (ArgumentException innerException)
223  {
224  throw new TypeInitializationException(GetType().FullName, innerException);
225  }
226  helper = new GregorianCalendarHelper(this, GetEraInfo());
227  }
228 
237  public override DateTime AddMonths(DateTime time, int months)
238  {
239  return helper.AddMonths(time, months);
240  }
241 
250  public override DateTime AddYears(DateTime time, int years)
251  {
252  return helper.AddYears(time, years);
253  }
254 
264  public override int GetDaysInMonth(int year, int month, int era)
265  {
266  return helper.GetDaysInMonth(year, month, era);
267  }
268 
276  public override int GetDaysInYear(int year, int era)
277  {
278  return helper.GetDaysInYear(year, era);
279  }
280 
284  public override int GetDayOfMonth(DateTime time)
285  {
286  return helper.GetDayOfMonth(time);
287  }
288 
292  public override DayOfWeek GetDayOfWeek(DateTime time)
293  {
294  return helper.GetDayOfWeek(time);
295  }
296 
300  public override int GetDayOfYear(DateTime time)
301  {
302  return helper.GetDayOfYear(time);
303  }
304 
312  public override int GetMonthsInYear(int year, int era)
313  {
314  return helper.GetMonthsInYear(year, era);
315  }
316 
325  [ComVisible(false)]
326  public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
327  {
328  return helper.GetWeekOfYear(time, rule, firstDayOfWeek);
329  }
330 
335  public override int GetEra(DateTime time)
336  {
337  return helper.GetEra(time);
338  }
339 
343  public override int GetMonth(DateTime time)
344  {
345  return helper.GetMonth(time);
346  }
347 
351  public override int GetYear(DateTime time)
352  {
353  return helper.GetYear(time);
354  }
355 
368  public override bool IsLeapDay(int year, int month, int day, int era)
369  {
370  return helper.IsLeapDay(year, month, day, era);
371  }
372 
381  public override bool IsLeapYear(int year, int era)
382  {
383  return helper.IsLeapYear(year, era);
384  }
385 
392  [ComVisible(false)]
393  public override int GetLeapMonth(int year, int era)
394  {
395  return helper.GetLeapMonth(year, era);
396  }
397 
407  public override bool IsLeapMonth(int year, int month, int era)
408  {
409  return helper.IsLeapMonth(year, month, era);
410  }
411 
431  public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
432  {
433  return helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era);
434  }
435 
441  public override int ToFourDigitYear(int year)
442  {
443  if (year <= 0)
444  {
445  throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
446  }
447  if (year > helper.MaxYear)
448  {
449  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, helper.MaxYear));
450  }
451  return year;
452  }
453 
454  internal static string[] EraNames()
455  {
456  EraInfo[] eraInfo = GetEraInfo();
457  string[] array = new string[eraInfo.Length];
458  for (int i = 0; i < eraInfo.Length; i++)
459  {
460  array[i] = eraInfo[eraInfo.Length - i - 1].eraName;
461  }
462  return array;
463  }
464 
465  internal static string[] AbbrevEraNames()
466  {
467  EraInfo[] eraInfo = GetEraInfo();
468  string[] array = new string[eraInfo.Length];
469  for (int i = 0; i < eraInfo.Length; i++)
470  {
471  array[i] = eraInfo[eraInfo.Length - i - 1].abbrevEraName;
472  }
473  return array;
474  }
475 
476  internal static string[] EnglishEraNames()
477  {
478  EraInfo[] eraInfo = GetEraInfo();
479  string[] array = new string[eraInfo.Length];
480  for (int i = 0; i < eraInfo.Length; i++)
481  {
482  array[i] = eraInfo[eraInfo.Length - i - 1].englishEraName;
483  }
484  return array;
485  }
486 
487  internal override bool IsValidYear(int year, int era)
488  {
489  return helper.IsValidYear(year, era);
490  }
491  }
492 }
override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
Returns the week of the year that includes the date in the specified T:System.DateTime.
DateTime AddYears(int value)
Returns a new T:System.DateTime that adds the specified number of years to the value of this instance...
Definition: DateTime.cs:842
The exception that is thrown as a wrapper around the exception thrown by the class initializer....
override int GetDaysInMonth(int year, int month, int era)
Returns the number of days in the specified month in the specified year in the specified era.
Represents time in divisions, such as weeks, months, and years.
Definition: Calendar.cs:11
override int [] Eras
Gets the list of eras in the T:System.Globalization.JapaneseCalendar.
Controls the ability to access registry variables. This class cannot be inherited.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
override int GetDaysInYear(int year, int era)
Returns the number of days in the specified year in the specified era.
override int GetDayOfMonth(DateTime time)
Returns the day of the month in the specified T:System.DateTime.
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
Calendar()
Initializes a new instance of the T:System.Globalization.Calendar class.
Definition: Calendar.cs:201
override DayOfWeek GetDayOfWeek(DateTime time)
Returns the day of the week in the specified T:System.DateTime.
override bool IsLeapDay(int year, int month, int day, int era)
Determines whether the specified date in the specified era is a leap day.
override bool IsLeapMonth(int year, int month, int era)
Determines whether the specified month in the specified year in the specified era is a leap month.
override DateTime AddYears(DateTime time, int years)
Returns a T:System.DateTime that is the specified number of years away from the specified T:System....
override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
Returns a T:System.DateTime that is set to the specified date and time in the specified era.
IPermission AddPermission(IPermission perm)
Adds a specified permission to the T:System.Security.PermissionSet.
NumberStyles
Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse...
Definition: NumberStyles.cs:10
Indicates that the F:System.Globalization.NumberStyles.AllowLeadingWhite, F:System....
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents a collection that can contain many different types of permissions.
override CalendarAlgorithmType AlgorithmType
Gets a value that indicates whether the current calendar is solar-based, lunar-based,...
Represents the Japanese calendar.
DateTime AddMonths(int months)
Returns a new T:System.DateTime that adds the specified number of months to the value of this instanc...
Definition: DateTime.cs:779
static readonly DateTime MaxValue
Represents the largest possible value of T:System.DateTime. This field is read-only.
Definition: DateTime.cs:113
The exception that is thrown when an I/O error occurs.
Definition: IOException.cs:10
override int GetLeapMonth(int year, int era)
Calculates the leap month for a specified year and era.
Format character that affects the layout of text or the operation of text processes,...
override DateTime AddMonths(DateTime time, int months)
Returns a T:System.DateTime that is the specified number of months away from the specified T:System....
DateTime IConvertible. ToDateTime(IFormatProvider provider)
Returns the current T:System.DateTime object.
Definition: DateTime.cs:1931
override int GetMonth(DateTime time)
Returns the month in the specified T:System.DateTime.
override int GetYear(DateTime time)
Returns the year in the specified T:System.DateTime.
override bool IsLeapYear(int year, int era)
Determines whether the specified year in the specified era is a leap year.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
override DateTime MinSupportedDateTime
Gets the earliest date and time supported by the current T:System.Globalization.JapaneseCalendar obje...
override int GetEra(DateTime time)
Returns the era in the specified T:System.DateTime.
CalendarWeekRule
Defines different rules for determining the first week of the year.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
CalendarAlgorithmType
Specifies whether a calendar is solar-based, lunar-based, or lunisolar-based.
JapaneseCalendar()
Initializes a new instance of the T:System.Globalization.JapaneseCalendar class.
Specifies that the class can be serialized.
override int GetMonthsInYear(int year, int era)
Returns the number of months in the specified year in the specified era.
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
override int ToFourDigitYear(int year)
Converts the specified year to a four-digit year by using the P:System.Globalization....
RegistryPermissionAccess
Specifies the permitted access to registry keys and values.
override int GetDayOfYear(DateTime time)
Returns the day of the year in the specified T:System.DateTime.
The exception that is thrown when a security error is detected.
DayOfWeek
Specifies the day of the week.
Definition: DayOfWeek.cs:9
override DateTime MaxSupportedDateTime
Gets the latest date and time supported by the current T:System.Globalization.JapaneseCalendar object...
override int TwoDigitYearMax
Gets or sets the last year of a 100-year range that can be represented by a 2-digit year.