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 };
38 int twoDigitYearMax = 2029;
40 public override int TwoDigitYearMax
43 return twoDigitYearMax;
46 M_ArgumentInRange ("value", value, 100, M_MaxYear);
48 twoDigitYearMax = value;
53 /// A protected member storing the
54 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
56 internal GregorianCalendarTypes M_CalendarType;
59 /// The property stores the
60 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
62 public virtual GregorianCalendarTypes CalendarType {
63 get { return M_CalendarType; }
65 // mscorlib 1:0:33000:0 doesn't check anything here
66 M_CalendarType = value;
71 /// A protected method checking the era number.
73 /// <param name="era">The era number.</param>
74 /// <exception name="T:System.ArgumentException">
75 /// The exception is thrown if the era is not equal
76 /// <see cref="M:ADEra"/>.
78 internal void M_CheckEra(ref int era) {
79 if (era == CurrentEra)
82 throw new ArgumentException("Era value was not valid.");
86 /// A protected method checking calendar year and the era number.
88 /// <param name="year">An integer representing the calendar year.
90 /// <param name="era">The era number.</param>
91 /// <exception cref="T:System.ArgumentException">
92 /// The exception is thrown if the era is not equal
93 /// <see cref="M:ADEra"/>.
95 /// <exception cref="T:System.ArgumentOutOfRangeException">
96 /// The exception is thrown if the calendar year is outside of
97 /// the allowed range.
99 internal override void M_CheckYE(int year, ref int era) {
101 M_ArgumentInRange("year", year, 1, 9999);
105 /// A protected method checking the calendar year, month, and
108 /// <param name="year">An integer representing the calendar year.
110 /// <param name="month">An integer giving the calendar month.
112 /// <param name="era">The era number.</param>
113 /// <exception cref="T:System.ArgumentException">
114 /// The exception is thrown if the era is not equal
115 /// <see cref="M:ADEra"/>.
117 /// <exception cref="T:System.ArgumentOutOfRangeException">
118 /// The exception is thrown if the calendar year or month is
119 /// outside of the allowed range.
121 internal void M_CheckYME(int year, int month, ref int era) {
122 M_CheckYE(year, ref era);
123 if (month < 1 || month > 12)
124 throw new ArgumentOutOfRangeException("month",
125 "Month must be between one and twelve.");
129 /// A protected method checking the calendar day, month, and year
130 /// and the era number.
132 /// <param name="year">An integer representing the calendar year.
134 /// <param name="month">An integer giving the calendar month.
136 /// <param name="day">An integer giving the calendar day.
138 /// <param name="era">The era number.</param>
139 /// <exception cref="T:System.ArgumentException">
140 /// The exception is thrown if the era is not equal
141 /// <see cref="M:ADEra"/>.
143 /// <exception cref="T:System.ArgumentOutOfRangeException">
144 /// The exception is thrown if the calendar year, month, or day is
145 /// outside of the allowed range.
147 internal void M_CheckYMDE(int year, int month, int day, ref int era)
149 M_CheckYME(year, month, ref era);
150 M_ArgumentInRange("day", day, 1,
151 GetDaysInMonth(year, month, era));
155 /// Overridden. Adds months to a given date.
157 /// <param name="time">The
158 /// <see cref="T:System.DateTime"/> to which to add
161 /// <param name="months">The number of months to add.</param>
162 /// <returns>A new <see cref="T:System.DateTime"/> value, that
163 /// results from adding <paramref name="months"/> to the specified
164 /// DateTime.</returns>
165 public override DateTime AddMonths(DateTime time, int months) {
166 return CCGregorianCalendar.AddMonths(time, months);
170 public override DateTime AddWeeks(DateTime time, int weeks)
172 throw new NotImplementedException();
176 /// Overridden. Adds years to a given date.
178 /// <param name="time">The
179 /// <see cref="T:System.DateTime"/> to which to add
182 /// <param name="years">The number of years to add.</param>
183 /// <returns>A new <see cref="T:System.DateTime"/> value, that
184 /// results from adding <paramref name="years"/> to the specified
185 /// DateTime.</returns>
186 public override DateTime AddYears(DateTime time, int years) {
187 return CCGregorianCalendar.AddYears(time, years);
191 /// Overridden. Gets the day of the month from
192 /// <paramref name="time"/>.
194 /// <param name="time">The
195 /// <see cref="T:System.DateTime"/> that specifies a
198 /// <returns>An integer giving the day of months, starting with 1.
200 public override int GetDayOfMonth(DateTime time) {
201 return CCGregorianCalendar.GetDayOfMonth(time);
205 /// Overridden. Gets the day of the week from the specified date.
207 /// <param name="time">The
208 /// <see cref="T:System.DateTime"/> that specifies a
211 /// <returns>An integer giving the day of months, starting with 1.
213 public override DayOfWeek GetDayOfWeek(DateTime time) {
214 int rd = CCFixed.FromDateTime(time);
215 return (DayOfWeek)CCFixed.day_of_week(rd);
219 /// Overridden. Gives the number of the day in the year.
221 /// <param name="time">The
222 /// <see cref="T:System.DateTime"/> that specifies a
225 /// <returns>An integer representing the day of the year,
226 /// starting with 1.</returns>
227 public override int GetDayOfYear(DateTime time) {
228 return CCGregorianCalendar.GetDayOfYear(time);
232 /// Overridden. Gives the number of days in the specified month
233 /// of the given year and era.
235 /// <param name="year">An integer that gives the year.
237 /// <param name="month">An integer that gives the month, starting
239 /// <param name="era">An intger that gives the era of the specified
241 /// <returns>An integer that gives the number of days of the
242 /// specified month.</returns>
243 /// <exception cref="T:System.ArgumentOutOfRangeException">
244 /// The exception is thrown, if <paramref name="month"/>,
245 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
246 /// the allowed range.
248 public override int GetDaysInMonth(int year, int month, int era) {
249 // mscorlib doesn't check year, probably a bug; we do
250 M_CheckYME(year, month, ref era);
251 return CCGregorianCalendar.GetDaysInMonth(year, month);
255 /// Overridden. Gives the number of days of the specified
256 /// year of the given era.
258 /// <param name="year">An integer that specifies the year.
260 /// <param name="era">An ineger that specifies the era.
262 /// <returns>An integer that gives the number of days of the
263 /// specified year.</returns>
264 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
265 /// The exception is thrown, if
266 /// <paramref name="year"/> is outside the allowed range.
268 public override int GetDaysInYear(int year, int era) {
269 M_CheckYE(year, ref era);
270 return CCGregorianCalendar.GetDaysInYear(year);
275 /// Overridden. Gives the era of the specified date.
277 /// <param name="time">The
278 /// <see cref="T:System.DateTime"/> that specifies a
281 /// <returns>An integer representing the era of the calendar.
283 public override int GetEra(DateTime time) {
288 /// Overridden. Gives the number of the month of the specified
291 /// <param name="time">The
292 /// <see cref="T:System.DateTime"/> that specifies a
295 /// <returns>An integer representing the month,
296 /// starting with 1.</returns>
297 public override int GetMonth(DateTime time) {
298 return CCGregorianCalendar.GetMonth(time);
302 /// Overridden. Gives the number of months in the specified year
305 /// <param name="year">An integer that specifies the year.
307 /// <param name="era">An integer that specifies the era.
309 /// <returns>An integer that gives the number of the months in the
310 /// specified year.</returns>
311 /// <exception cref="T:System.ArgumentOutOfRangeException">
312 /// The exception is thrown, if the year or the era are not valid.
314 public override int GetMonthsInYear(int year, int era) {
315 M_CheckYE(year, ref era);
320 /// Overridden. Gives the number of the year of the specified
323 /// <param name="time">The
324 /// <see cref="T:System.DateTime"/> that specifies a
327 /// <returns>An integer representing the year,
328 /// starting with 1.</returns>
329 public override int GetYear(DateTime time) {
330 return CCGregorianCalendar.GetYear(time);
334 /// Overridden. Tells whether the given day
337 /// <param name="year">An integer that specifies the year in the
340 /// <param name="month">An integer that specifies the month.
342 /// <param name="day">An integer that specifies the day.
344 /// <param name="era">An integer that specifies the era.
346 /// <returns>A boolean that tells whether the given day is a leap
349 /// <exception cref="T:System.ArgumentOutOfRangeException">
350 /// The exception is thrown, if the year, month, day, or era is not
353 public override bool IsLeapDay(int year, int month, int day, int era)
355 M_CheckYMDE(year, month, day, ref era);
356 return CCGregorianCalendar.IsLeapDay(year, month, day);
361 /// Overridden. Tells whether the given month
364 /// <param name="year">An integer that specifies the year in the
367 /// <param name="month">An integer that specifies the month.
369 /// <param name="era">An integer that specifies the era.
371 /// <returns>A boolean that tells whether the given month is a leap
374 /// <exception cref="T:System.ArgumentOutOfRangeException">
375 /// The exception is thrown, if the year, month, or era is not
378 public override bool IsLeapMonth(int year, int month, int era) {
379 M_CheckYME(year, month, ref era);
384 /// Overridden. Tells whether the given year
387 /// <param name="year">An integer that specifies the year in the
390 /// <param name="era">An integer that specifies the era.
392 /// <returns>A boolean that tells whether the given year is a leap
395 /// <exception cref="T:System.ArgumentOutOfRangeException">
396 /// The exception is thrown, if the year or era is not
399 public override bool IsLeapYear(int year, int era) {
400 M_CheckYE(year, ref era);
401 return CCGregorianCalendar.is_leap_year(year);
405 /// Overridden. Creates the
406 /// <see cref="T:System.DateTime"/> from the parameters.
408 /// <param name="year">An integer that gives the year in the
409 /// <paramref name="era"/>.
411 /// <param name="month">An integer that specifies the month.
413 /// <param name="day">An integer that specifies the day.
415 /// <param name="hour">An integer that specifies the hour.
417 /// <param name="minute">An integer that specifies the minute.
419 /// <param name="second">An integer that gives the second.
421 /// <param name="milliseconds">An integer that gives the
424 /// <param name="era">An integer that specifies the era.
427 /// <see cref="T:system.DateTime"/> representig the date and time.
429 /// <exception cref="T:System.ArgumentOutOfRangeException">
430 /// The exception is thrown, if at least one of the parameters
433 public override DateTime ToDateTime(int year, int month, int day,
434 int hour, int minute, int second, int milliseconds,
437 M_CheckYMDE(year, month, day, ref era);
438 M_CheckHMSM(hour, minute, second, milliseconds);
439 return CCGregorianCalendar.ToDateTime(
441 hour, minute, second, milliseconds);
445 public override int ToFourDigitYear(int year)
447 throw new NotImplementedException();
451 /// Constructor that sets the
452 /// Gregorian calendar type (
453 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
455 /// <param name="type">The parameter specifies the Gregorian
458 public GregorianCalendar(GregorianCalendarTypes type) {
460 M_AbbrEraNames = new string[] {"C.E."};
461 M_EraNames = new string[] {"Common Era"};
462 if (M_TwoDigitYearMax == 99)
463 M_TwoDigitYearMax = 2029;
467 /// Default constructor. Sets the Gregorian calendar type to
469 /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
471 public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
472 } // class GregorianCalendar
474 } // namespace System.Globalization