mscorlib(4.0.0.0) API with additions
GregorianCalendar.cs
3 
4 namespace System.Globalization
5 {
8  [ComVisible(true)]
9  public class GregorianCalendar : Calendar
10  {
12  public const int ADEra = 1;
13 
14  internal const int DatePartYear = 0;
15 
16  internal const int DatePartDayOfYear = 1;
17 
18  internal const int DatePartMonth = 2;
19 
20  internal const int DatePartDay = 3;
21 
22  internal const int MaxYear = 9999;
23 
24  internal GregorianCalendarTypes m_type;
25 
26  internal static readonly int[] DaysToMonth365 = new int[13]
27  {
28  0,
29  31,
30  59,
31  90,
32  120,
33  151,
34  181,
35  212,
36  243,
37  273,
38  304,
39  334,
40  365
41  };
42 
43  internal static readonly int[] DaysToMonth366 = new int[13]
44  {
45  0,
46  31,
47  60,
48  91,
49  121,
50  152,
51  182,
52  213,
53  244,
54  274,
55  305,
56  335,
57  366
58  };
59 
60  private static volatile Calendar s_defaultInstance;
61 
62  private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 2029;
63 
66  [ComVisible(false)]
67  public override DateTime MinSupportedDateTime
68  {
69  get
70  {
71  return DateTime.MinValue;
72  }
73  }
74 
77  [ComVisible(false)]
78  public override DateTime MaxSupportedDateTime
79  {
80  get
81  {
82  return DateTime.MaxValue;
83  }
84  }
85 
88  [ComVisible(false)]
89  public override CalendarAlgorithmType AlgorithmType
90  {
91  get
92  {
93  return CalendarAlgorithmType.SolarCalendar;
94  }
95  }
96 
101  public virtual GregorianCalendarTypes CalendarType
102  {
103  get
104  {
105  return m_type;
106  }
107  set
108  {
109  VerifyWritable();
110  switch (value)
111  {
112  case GregorianCalendarTypes.Localized:
113  case GregorianCalendarTypes.USEnglish:
114  case GregorianCalendarTypes.MiddleEastFrench:
115  case GregorianCalendarTypes.Arabic:
116  case GregorianCalendarTypes.TransliteratedEnglish:
117  case GregorianCalendarTypes.TransliteratedFrench:
118  m_type = value;
119  break;
120  default:
121  throw new ArgumentOutOfRangeException("m_type", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
122  }
123  }
124  }
125 
126  internal override int ID => (int)m_type;
127 
130  public override int[] Eras => new int[1]
131  {
132  1
133  };
134 
139  public override int TwoDigitYearMax
140  {
141  get
142  {
143  if (twoDigitYearMax == -1)
144  {
145  twoDigitYearMax = Calendar.GetSystemTwoDigitYearSetting(ID, 2029);
146  }
147  return twoDigitYearMax;
148  }
149  set
150  {
151  VerifyWritable();
152  if (value < 99 || value > 9999)
153  {
154  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 99, 9999));
155  }
156  twoDigitYearMax = value;
157  }
158  }
159 
160  [OnDeserialized]
161  private void OnDeserialized(StreamingContext ctx)
162  {
163  if (m_type < GregorianCalendarTypes.Localized || m_type > GregorianCalendarTypes.TransliteratedFrench)
164  {
165  throw new SerializationException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Serialization_MemberOutOfRange"), "type", "GregorianCalendar"));
166  }
167  }
168 
169  internal static Calendar GetDefaultInstance()
170  {
171  if (s_defaultInstance == null)
172  {
173  s_defaultInstance = new GregorianCalendar();
174  }
175  return s_defaultInstance;
176  }
177 
181  {
182  }
183 
189  {
190  if (type < GregorianCalendarTypes.Localized || type > GregorianCalendarTypes.TransliteratedFrench)
191  {
192  throw new ArgumentOutOfRangeException("type", Environment.GetResourceString("ArgumentOutOfRange_Range", GregorianCalendarTypes.Localized, GregorianCalendarTypes.TransliteratedFrench));
193  }
194  m_type = type;
195  }
196 
197  internal virtual int GetDatePart(long ticks, int part)
198  {
199  int num = (int)(ticks / 864000000000L);
200  int num2 = num / 146097;
201  num -= num2 * 146097;
202  int num3 = num / 36524;
203  if (num3 == 4)
204  {
205  num3 = 3;
206  }
207  num -= num3 * 36524;
208  int num4 = num / 1461;
209  num -= num4 * 1461;
210  int num5 = num / 365;
211  if (num5 == 4)
212  {
213  num5 = 3;
214  }
215  if (part == 0)
216  {
217  return num2 * 400 + num3 * 100 + num4 * 4 + num5 + 1;
218  }
219  num -= num5 * 365;
220  if (part == 1)
221  {
222  return num + 1;
223  }
224  int[] array = (num5 == 3 && (num4 != 24 || num3 == 3)) ? DaysToMonth366 : DaysToMonth365;
225  int i;
226  for (i = num >> 6; num >= array[i]; i++)
227  {
228  }
229  if (part == 2)
230  {
231  return i;
232  }
233  return num - array[i - 1] + 1;
234  }
235 
236  internal static long GetAbsoluteDate(int year, int month, int day)
237  {
238  if (year >= 1 && year <= 9999 && month >= 1 && month <= 12)
239  {
240  int[] array = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? DaysToMonth366 : DaysToMonth365;
241  if (day >= 1 && day <= array[month] - array[month - 1])
242  {
243  int num = year - 1;
244  int num2 = num * 365 + num / 4 - num / 100 + num / 400 + array[month - 1] + day - 1;
245  return num2;
246  }
247  }
248  throw new ArgumentOutOfRangeException(null, Environment.GetResourceString("ArgumentOutOfRange_BadYearMonthDay"));
249  }
250 
251  internal virtual long DateToTicks(int year, int month, int day)
252  {
253  return GetAbsoluteDate(year, month, day) * 864000000000L;
254  }
255 
264  public override DateTime AddMonths(DateTime time, int months)
265  {
266  if (months < -120000 || months > 120000)
267  {
268  throw new ArgumentOutOfRangeException("months", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), -120000, 120000));
269  }
270  int datePart = GetDatePart(time.Ticks, 0);
271  int datePart2 = GetDatePart(time.Ticks, 2);
272  int num = GetDatePart(time.Ticks, 3);
273  int num2 = datePart2 - 1 + months;
274  if (num2 >= 0)
275  {
276  datePart2 = num2 % 12 + 1;
277  datePart += num2 / 12;
278  }
279  else
280  {
281  datePart2 = 12 + (num2 + 1) % 12;
282  datePart += (num2 - 11) / 12;
283  }
284  int[] array = (datePart % 4 == 0 && (datePart % 100 != 0 || datePart % 400 == 0)) ? DaysToMonth366 : DaysToMonth365;
285  int num3 = array[datePart2] - array[datePart2 - 1];
286  if (num > num3)
287  {
288  num = num3;
289  }
290  long ticks = DateToTicks(datePart, datePart2, num) + time.Ticks % 864000000000L;
291  Calendar.CheckAddResult(ticks, MinSupportedDateTime, MaxSupportedDateTime);
292  return new DateTime(ticks);
293  }
294 
300  public override DateTime AddYears(DateTime time, int years)
301  {
302  return AddMonths(time, years * 12);
303  }
304 
308  public override int GetDayOfMonth(DateTime time)
309  {
310  return GetDatePart(time.Ticks, 3);
311  }
312 
316  public override DayOfWeek GetDayOfWeek(DateTime time)
317  {
318  return (DayOfWeek)((int)(time.Ticks / 864000000000L + 1) % 7);
319  }
320 
324  public override int GetDayOfYear(DateTime time)
325  {
326  return GetDatePart(time.Ticks, 1);
327  }
328 
338  public override int GetDaysInMonth(int year, int month, int era)
339  {
340  if (era == 0 || era == 1)
341  {
342  if (year < 1 || year > 9999)
343  {
344  throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Range", 1, 9999));
345  }
346  if (month < 1 || month > 12)
347  {
348  throw new ArgumentOutOfRangeException("month", Environment.GetResourceString("ArgumentOutOfRange_Month"));
349  }
350  int[] array = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? DaysToMonth366 : DaysToMonth365;
351  return array[month] - array[month - 1];
352  }
353  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
354  }
355 
363  public override int GetDaysInYear(int year, int era)
364  {
365  if (era == 0 || era == 1)
366  {
367  if (year >= 1 && year <= 9999)
368  {
369  if (year % 4 != 0 || (year % 100 == 0 && year % 400 != 0))
370  {
371  return 365;
372  }
373  return 366;
374  }
375  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, 9999));
376  }
377  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
378  }
379 
383  public override int GetEra(DateTime time)
384  {
385  return 1;
386  }
387 
391  public override int GetMonth(DateTime time)
392  {
393  return GetDatePart(time.Ticks, 2);
394  }
395 
403  public override int GetMonthsInYear(int year, int era)
404  {
405  if (era == 0 || era == 1)
406  {
407  if (year >= 1 && year <= 9999)
408  {
409  return 12;
410  }
411  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, 9999));
412  }
413  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
414  }
415 
419  public override int GetYear(DateTime time)
420  {
421  return GetDatePart(time.Ticks, 0);
422  }
423 
436  public override bool IsLeapDay(int year, int month, int day, int era)
437  {
438  if (month < 1 || month > 12)
439  {
440  throw new ArgumentOutOfRangeException("month", Environment.GetResourceString("ArgumentOutOfRange_Range", 1, 12));
441  }
442  if (era != 0 && era != 1)
443  {
444  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
445  }
446  if (year < 1 || year > 9999)
447  {
448  throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Range", 1, 9999));
449  }
450  if (day < 1 || day > GetDaysInMonth(year, month))
451  {
452  throw new ArgumentOutOfRangeException("day", Environment.GetResourceString("ArgumentOutOfRange_Range", 1, GetDaysInMonth(year, month)));
453  }
454  if (!IsLeapYear(year))
455  {
456  return false;
457  }
458  if (month == 2 && day == 29)
459  {
460  return true;
461  }
462  return false;
463  }
464 
472  [ComVisible(false)]
473  public override int GetLeapMonth(int year, int era)
474  {
475  if (era != 0 && era != 1)
476  {
477  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
478  }
479  if (year < 1 || year > 9999)
480  {
481  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, 9999));
482  }
483  return 0;
484  }
485 
495  public override bool IsLeapMonth(int year, int month, int era)
496  {
497  if (era != 0 && era != 1)
498  {
499  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
500  }
501  if (year < 1 || year > 9999)
502  {
503  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, 9999));
504  }
505  if (month < 1 || month > 12)
506  {
507  throw new ArgumentOutOfRangeException("month", Environment.GetResourceString("ArgumentOutOfRange_Range", 1, 12));
508  }
509  return false;
510  }
511 
520  public override bool IsLeapYear(int year, int era)
521  {
522  if (era == 0 || era == 1)
523  {
524  if (year >= 1 && year <= 9999)
525  {
526  if (year % 4 == 0)
527  {
528  if (year % 100 == 0)
529  {
530  return year % 400 == 0;
531  }
532  return true;
533  }
534  return false;
535  }
536  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, 9999));
537  }
538  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
539  }
540 
560  public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
561  {
562  if (era == 0 || era == 1)
563  {
564  return new DateTime(year, month, day, hour, minute, second, millisecond);
565  }
566  throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
567  }
568 
569  internal override bool TryToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era, out DateTime result)
570  {
571  if (era == 0 || era == 1)
572  {
573  return DateTime.TryCreate(year, month, day, hour, minute, second, millisecond, out result);
574  }
575  result = DateTime.MinValue;
576  return false;
577  }
578 
584  public override int ToFourDigitYear(int year)
585  {
586  if (year < 0)
587  {
588  throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
589  }
590  if (year > 9999)
591  {
592  throw new ArgumentOutOfRangeException("year", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, 9999));
593  }
594  return base.ToFourDigitYear(year);
595  }
596  }
597 }
override bool IsLeapYear(int year, int era)
Determines whether the specified year in the specified era is a leap year.
override int TwoDigitYearMax
Gets or sets the last year of a 100-year range that can be represented by a 2-digit year.
Represents time in divisions, such as weeks, months, and years.
Definition: Calendar.cs:11
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.
Refers to the localized version of the Gregorian calendar, based on the language of the T:System....
override int GetMonthsInYear(int year, int era)
Returns the number of months in the specified year in the specified era.
static readonly DateTime MinValue
Represents the smallest possible value of T:System.DateTime. This field is read-only.
Definition: DateTime.cs:109
Represents the Gregorian calendar.
override DateTime MaxSupportedDateTime
Gets the latest date and time supported by the T:System.Globalization.GregorianCalendar type.
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 GetDayOfMonth(DateTime time)
Returns the day of the month in the specified T:System.DateTime.
override DateTime MinSupportedDateTime
Gets the earliest date and time supported by the T:System.Globalization.GregorianCalendar type.
GregorianCalendarTypes
Defines the different language versions of the Gregorian calendar.
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 int GetLeapMonth(int year, int era)
Calculates the leap month for a specified year and era.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
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.
GregorianCalendar(GregorianCalendarTypes type)
Initializes a new instance of the T:System.Globalization.GregorianCalendar class using the specified ...
override int [] Eras
Gets the list of eras in the T:System.Globalization.GregorianCalendar.
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 int GetEra(DateTime time)
Returns the era in the specified T:System.DateTime.
override int GetDayOfYear(DateTime time)
Returns the day of the year in the specified T:System.DateTime.
const int ADEra
Represents the current era. This field is constant.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
override int GetYear(DateTime time)
Returns the year in the specified T:System.DateTime.
override CalendarAlgorithmType AlgorithmType
Gets a value that indicates whether the current calendar is solar-based, lunar-based,...
static readonly DateTime MaxValue
Represents the largest possible value of T:System.DateTime. This field is read-only.
Definition: DateTime.cs:113
override int GetMonth(DateTime time)
Returns the month in the specified T:System.DateTime.
Format character that affects the layout of text or the operation of text processes,...
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.
The exception thrown when an error occurs during serialization or deserialization.
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....
override int ToFourDigitYear(int year)
Converts the specified year to a four-digit year by using the P:System.Globalization....
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
override DayOfWeek GetDayOfWeek(DateTime time)
Returns the day of the week in the specified T:System.DateTime.
CalendarAlgorithmType
Specifies whether a calendar is solar-based, lunar-based, or lunisolar-based.
Specifies that the class can be serialized.
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....
virtual GregorianCalendarTypes CalendarType
Gets or sets the T:System.Globalization.GregorianCalendarTypes value that denotes the language versio...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
override int GetDaysInYear(int year, int era)
Returns the number of days in the specified year in the specified era.
DayOfWeek
Specifies the day of the week.
Definition: DayOfWeek.cs:9
long Ticks
Gets the number of ticks that represent the date and time of this instance.
Definition: DateTime.cs:315
GregorianCalendar()
Initializes a new instance of the T:System.Globalization.GregorianCalendar class using the default T:...