1 // GregorianCalendar.cs
3 // (C) Ulrich Kunitz 2002
6 namespace System.Globalization {
11 /// This is the Gregorian calendar.
14 /// <para>The Gregorian calendar supports only the Common Era from
15 /// the Gregorian year 1 to the Gregorian year 9999.
17 /// <para>The implementation uses the
18 /// <see cref="N:CalendricalCalculations"/> namespace.
22 public class GregorianCalendar : Calendar {
24 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
27 public const int ADEra = 1;
29 /// <value>Overridden. Gives the eras supported by the Gregorian
30 /// calendar as an array of integers.
32 public override int[] Eras {
34 return new int[] { ADEra };
39 /// A protected member storing the
40 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
42 internal GregorianCalendarTypes M_CalendarType;
45 /// The property stores the
46 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
48 public virtual GregorianCalendarTypes CalendarType {
49 get { return M_CalendarType; }
51 // mscorlib 1:0:33000:0 doesn't check anything here
52 M_CalendarType = value;
57 /// A protected method checking the era number.
59 /// <param name="era">The era number.</param>
60 /// <exception name="T:System.ArgumentException">
61 /// The exception is thrown if the era is not equal
62 /// <see cref="M:ADEra"/>.
64 internal void M_CheckEra(ref int era) {
65 if (era == CurrentEra)
68 throw new ArgumentException("Era value was not valid.");
72 /// A protected method checking calendar year and the era number.
74 /// <param name="year">An integer representing the calendar year.
76 /// <param name="era">The era number.</param>
77 /// <exception cref="T:System.ArgumentException">
78 /// The exception is thrown if the era is not equal
79 /// <see cref="M:ADEra"/>.
81 /// <exception cref="T:System.ArgumentOutOfRangeException">
82 /// The exception is thrown if the calendar year is outside of
83 /// the allowed range.
85 internal override void M_CheckYE(int year, ref int era) {
87 M_ArgumentInRange("year", year, 1, 9999);
91 /// A protected method checking the calendar year, month, and
94 /// <param name="year">An integer representing the calendar year.
96 /// <param name="month">An integer giving the calendar month.
98 /// <param name="era">The era number.</param>
99 /// <exception cref="T:System.ArgumentException">
100 /// The exception is thrown if the era is not equal
101 /// <see cref="M:ADEra"/>.
103 /// <exception cref="T:System.ArgumentOutOfRangeException">
104 /// The exception is thrown if the calendar year or month is
105 /// outside of the allowed range.
107 internal void M_CheckYME(int year, int month, ref int era) {
108 M_CheckYE(year, ref era);
109 if (month < 1 || month > 12)
110 throw new ArgumentOutOfRangeException("month",
111 "Month must be between one and twelve.");
115 /// A protected method checking the calendar day, month, and year
116 /// and the era number.
118 /// <param name="year">An integer representing the calendar year.
120 /// <param name="month">An integer giving the calendar month.
122 /// <param name="day">An integer giving the calendar day.
124 /// <param name="era">The era number.</param>
125 /// <exception cref="T:System.ArgumentException">
126 /// The exception is thrown if the era is not equal
127 /// <see cref="M:ADEra"/>.
129 /// <exception cref="T:System.ArgumentOutOfRangeException">
130 /// The exception is thrown if the calendar year, month, or day is
131 /// outside of the allowed range.
133 internal void M_CheckYMDE(int year, int month, int day, ref int era)
135 M_CheckYME(year, month, ref era);
136 M_ArgumentInRange("day", day, 1,
137 GetDaysInMonth(year, month, era));
141 /// Overridden. Adds months to a given date.
143 /// <param name="time">The
144 /// <see cref="T:System.DateTime"/> to which to add
147 /// <param name="months">The number of months to add.</param>
148 /// <returns>A new <see cref="T:System.DateTime"/> value, that
149 /// results from adding <paramref name="months"/> to the specified
150 /// DateTime.</returns>
151 public override DateTime AddMonths(DateTime time, int months) {
152 return CCGregorianCalendar.AddMonths(time, months);
156 /// Overridden. Adds years to a given date.
158 /// <param name="time">The
159 /// <see cref="T:System.DateTime"/> to which to add
162 /// <param name="years">The number of years to add.</param>
163 /// <returns>A new <see cref="T:System.DateTime"/> value, that
164 /// results from adding <paramref name="years"/> to the specified
165 /// DateTime.</returns>
166 public override DateTime AddYears(DateTime time, int years) {
167 return CCGregorianCalendar.AddYears(time, years);
171 /// Overridden. Gets the day of the month from
172 /// <paramref name="time"/>.
174 /// <param name="time">The
175 /// <see cref="T:System.DateTime"/> that specifies a
178 /// <returns>An integer giving the day of months, starting with 1.
180 public override int GetDayOfMonth(DateTime time) {
181 return CCGregorianCalendar.GetDayOfMonth(time);
185 /// Overridden. Gets the day of the week from the specified date.
187 /// <param name="time">The
188 /// <see cref="T:System.DateTime"/> that specifies a
191 /// <returns>An integer giving the day of months, starting with 1.
193 public override DayOfWeek GetDayOfWeek(DateTime time) {
194 int rd = CCFixed.FromDateTime(time);
195 return (DayOfWeek)CCFixed.day_of_week(rd);
199 /// Overridden. Gives the number of the day in the year.
201 /// <param name="time">The
202 /// <see cref="T:System.DateTime"/> that specifies a
205 /// <returns>An integer representing the day of the year,
206 /// starting with 1.</returns>
207 public override int GetDayOfYear(DateTime time) {
208 return CCGregorianCalendar.GetDayOfYear(time);
212 /// Overridden. Gives the number of days in the specified month
213 /// of the given year and era.
215 /// <param name="year">An integer that gives the year.
217 /// <param name="month">An integer that gives the month, starting
219 /// <param name="era">An intger that gives the era of the specified
221 /// <returns>An integer that gives the number of days of the
222 /// specified month.</returns>
223 /// <exception cref="T:System.ArgumentOutOfRangeException">
224 /// The exception is thrown, if <paramref name="month"/>,
225 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
226 /// the allowed range.
228 public override int GetDaysInMonth(int year, int month, int era) {
229 // mscorlib doesn't check year, probably a bug; we do
230 M_CheckYME(year, month, ref era);
231 return CCGregorianCalendar.GetDaysInMonth(year, month);
235 /// Overridden. Gives the number of days of the specified
236 /// year of the given era.
238 /// <param name="year">An integer that specifies the year.
240 /// <param name="era">An ineger that specifies the era.
242 /// <returns>An integer that gives the number of days of the
243 /// specified year.</returns>
244 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
245 /// The exception is thrown, if
246 /// <paramref name="year"/> is outside the allowed range.
248 public override int GetDaysInYear(int year, int era) {
249 M_CheckYE(year, ref era);
250 return CCGregorianCalendar.GetDaysInYear(year);
255 /// Overridden. Gives the era of the specified date.
257 /// <param name="time">The
258 /// <see cref="T:System.DateTime"/> that specifies a
261 /// <returns>An integer representing the era of the calendar.
263 public override int GetEra(DateTime time) {
268 /// Overridden. Gives the number of the month of the specified
271 /// <param name="time">The
272 /// <see cref="T:System.DateTime"/> that specifies a
275 /// <returns>An integer representing the month,
276 /// starting with 1.</returns>
277 public override int GetMonth(DateTime time) {
278 return CCGregorianCalendar.GetMonth(time);
282 /// Overridden. Gives the number of months in the specified year
285 /// <param name="year">An integer that specifies the year.
287 /// <param name="era">An integer that specifies the era.
289 /// <returns>An integer that gives the number of the months in the
290 /// specified year.</returns>
291 /// <exception cref="T:System.ArgumentOutOfRangeException">
292 /// The exception is thrown, if the year or the era are not valid.
294 public override int GetMonthsInYear(int year, int era) {
295 M_CheckYE(year, ref era);
300 /// Overridden. Gives the number of the year of the specified
303 /// <param name="time">The
304 /// <see cref="T:System.DateTime"/> that specifies a
307 /// <returns>An integer representing the year,
308 /// starting with 1.</returns>
309 public override int GetYear(DateTime time) {
310 return CCGregorianCalendar.GetYear(time);
314 /// Overridden. Tells whether the given day
317 /// <param name="year">An integer that specifies the year in the
320 /// <param name="month">An integer that specifies the month.
322 /// <param name="day">An integer that specifies the day.
324 /// <param name="era">An integer that specifies the era.
326 /// <returns>A boolean that tells whether the given day is a leap
329 /// <exception cref="T:System.ArgumentOutOfRangeException">
330 /// The exception is thrown, if the year, month, day, or era is not
333 public override bool IsLeapDay(int year, int month, int day, int era)
335 M_CheckYMDE(year, month, day, ref era);
336 return CCGregorianCalendar.IsLeapDay(year, month, day);
341 /// Overridden. Tells whether the given month
344 /// <param name="year">An integer that specifies the year in the
347 /// <param name="month">An integer that specifies the month.
349 /// <param name="era">An integer that specifies the era.
351 /// <returns>A boolean that tells whether the given month is a leap
354 /// <exception cref="T:System.ArgumentOutOfRangeException">
355 /// The exception is thrown, if the year, month, or era is not
358 public override bool IsLeapMonth(int year, int month, int era) {
359 M_CheckYME(year, month, ref era);
364 /// Overridden. Tells whether the given year
367 /// <param name="year">An integer that specifies the year in the
370 /// <param name="era">An integer that specifies the era.
372 /// <returns>A boolean that tells whether the given year is a leap
375 /// <exception cref="T:System.ArgumentOutOfRangeException">
376 /// The exception is thrown, if the year or era is not
379 public override bool IsLeapYear(int year, int era) {
380 M_CheckYE(year, ref era);
381 return CCGregorianCalendar.is_leap_year(year);
385 /// Overridden. Creates the
386 /// <see cref="T:System.DateTime"/> from the parameters.
388 /// <param name="year">An integer that gives the year in the
389 /// <paramref name="era"/>.
391 /// <param name="month">An integer that specifies the month.
393 /// <param name="day">An integer that specifies the day.
395 /// <param name="hour">An integer that specifies the hour.
397 /// <param name="minute">An integer that specifies the minute.
399 /// <param name="second">An integer that gives the second.
401 /// <param name="milliseconds">An integer that gives the
404 /// <param name="era">An integer that specifies the era.
407 /// <see cref="T:system.DateTime"/> representig the date and time.
409 /// <exception cref="T:System.ArgumentOutOfRangeException">
410 /// The exception is thrown, if at least one of the parameters
413 public override DateTime ToDateTime(int year, int month, int day,
414 int hour, int minute, int second, int milliseconds,
417 M_CheckYMDE(year, month, day, ref era);
418 M_CheckHMSM(hour, minute, second, milliseconds);
419 return CCGregorianCalendar.ToDateTime(
421 hour, minute, second, milliseconds);
425 /// Constructor that sets the
426 /// Gregorian calendar type (
427 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
429 /// <param name="type">The parameter specifies the Gregorian
432 public GregorianCalendar(GregorianCalendarTypes type) {
434 M_AbbrEraNames = new string[] {"C.E."};
435 M_EraNames = new string[] {"Common Era"};
436 if (M_TwoDigitYearMax == 99)
437 M_TwoDigitYearMax = 2029;
441 /// Default constructor. Sets the Gregorian calendar type to
443 /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
445 public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
446 } // class GregorianCalendar
448 } // namespace System.Globalization