1 // GregorianCalendar.cs
3 // (C) Ulrich Kunitz 2002
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace System.Globalization {
34 /// This is the Gregorian calendar.
37 /// <para>The Gregorian calendar supports only the Common Era from
38 /// the Gregorian year 1 to the Gregorian year 9999.
40 /// <para>The implementation uses the
41 /// <see cref="N:CalendricalCalculations"/> namespace.
45 [MonoTODO ("Fix serialization compatibility with MS.NET")]
46 public class GregorianCalendar : Calendar {
48 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
51 public const int ADEra = 1;
53 /// <value>Overridden. Gives the eras supported by the Gregorian
54 /// calendar as an array of integers.
56 public override int[] Eras {
58 return new int[] { ADEra };
63 int twoDigitYearMax = 2029;
65 public override int TwoDigitYearMax
68 return twoDigitYearMax;
72 M_ArgumentInRange ("value", value, 100, M_MaxYear);
74 twoDigitYearMax = value;
79 /// A protected member storing the
80 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
83 internal GregorianCalendarTypes M_CalendarType;
86 /// The property stores the
87 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
89 public virtual GregorianCalendarTypes CalendarType {
90 get { return M_CalendarType; }
93 // mscorlib 1:0:3300:0 doesn't check anything here
94 M_CalendarType = value;
99 /// A protected method checking the era number.
101 /// <param name="era">The era number.</param>
102 /// <exception name="T:System.ArgumentException">
103 /// The exception is thrown if the era is not equal
104 /// <see cref="M:ADEra"/>.
106 internal void M_CheckEra(ref int era) {
107 if (era == CurrentEra)
110 throw new ArgumentException("Era value was not valid.");
114 /// A protected method checking calendar year and the era number.
116 /// <param name="year">An integer representing the calendar year.
118 /// <param name="era">The era number.</param>
119 /// <exception cref="T:System.ArgumentException">
120 /// The exception is thrown if the era is not equal
121 /// <see cref="M:ADEra"/>.
123 /// <exception cref="T:System.ArgumentOutOfRangeException">
124 /// The exception is thrown if the calendar year is outside of
125 /// the allowed range.
127 internal override void M_CheckYE(int year, ref int era) {
129 M_ArgumentInRange("year", year, 1, 9999);
133 /// A protected method checking the calendar year, month, and
136 /// <param name="year">An integer representing the calendar year.
138 /// <param name="month">An integer giving the calendar month.
140 /// <param name="era">The era number.</param>
141 /// <exception cref="T:System.ArgumentException">
142 /// The exception is thrown if the era is not equal
143 /// <see cref="M:ADEra"/>.
145 /// <exception cref="T:System.ArgumentOutOfRangeException">
146 /// The exception is thrown if the calendar year or month is
147 /// outside of the allowed range.
149 internal void M_CheckYME(int year, int month, ref int era) {
150 M_CheckYE(year, ref era);
151 if (month < 1 || month > 12)
152 throw new ArgumentOutOfRangeException("month",
153 "Month must be between one and twelve.");
157 /// A protected method checking the calendar day, month, and year
158 /// and the era number.
160 /// <param name="year">An integer representing the calendar year.
162 /// <param name="month">An integer giving the calendar month.
164 /// <param name="day">An integer giving the calendar day.
166 /// <param name="era">The era number.</param>
167 /// <exception cref="T:System.ArgumentException">
168 /// The exception is thrown if the era is not equal
169 /// <see cref="M:ADEra"/>.
171 /// <exception cref="T:System.ArgumentOutOfRangeException">
172 /// The exception is thrown if the calendar year, month, or day is
173 /// outside of the allowed range.
175 internal void M_CheckYMDE(int year, int month, int day, ref int era)
177 M_CheckYME(year, month, ref era);
178 M_ArgumentInRange("day", day, 1,
179 GetDaysInMonth(year, month, era));
183 /// Overridden. Adds months to a given date.
185 /// <param name="time">The
186 /// <see cref="T:System.DateTime"/> to which to add
189 /// <param name="months">The number of months to add.</param>
190 /// <returns>A new <see cref="T:System.DateTime"/> value, that
191 /// results from adding <paramref name="months"/> to the specified
192 /// DateTime.</returns>
193 public override DateTime AddMonths(DateTime time, int months) {
194 return CCGregorianCalendar.AddMonths(time, months);
198 /// Overridden. Adds years to a given date.
200 /// <param name="time">The
201 /// <see cref="T:System.DateTime"/> to which to add
204 /// <param name="years">The number of years to add.</param>
205 /// <returns>A new <see cref="T:System.DateTime"/> value, that
206 /// results from adding <paramref name="years"/> to the specified
207 /// DateTime.</returns>
208 public override DateTime AddYears(DateTime time, int years) {
209 return CCGregorianCalendar.AddYears(time, years);
213 /// Overridden. Gets the day of the month from
214 /// <paramref name="time"/>.
216 /// <param name="time">The
217 /// <see cref="T:System.DateTime"/> that specifies a
220 /// <returns>An integer giving the day of months, starting with 1.
222 public override int GetDayOfMonth(DateTime time) {
223 return CCGregorianCalendar.GetDayOfMonth(time);
227 /// Overridden. Gets the day of the week from the specified date.
229 /// <param name="time">The
230 /// <see cref="T:System.DateTime"/> that specifies a
233 /// <returns>An integer giving the day of months, starting with 1.
235 public override DayOfWeek GetDayOfWeek(DateTime time) {
236 int rd = CCFixed.FromDateTime(time);
237 return (DayOfWeek)CCFixed.day_of_week(rd);
241 /// Overridden. Gives the number of the day in the year.
243 /// <param name="time">The
244 /// <see cref="T:System.DateTime"/> that specifies a
247 /// <returns>An integer representing the day of the year,
248 /// starting with 1.</returns>
249 public override int GetDayOfYear(DateTime time) {
250 return CCGregorianCalendar.GetDayOfYear(time);
254 /// Overridden. Gives the number of days in the specified month
255 /// of the given year and era.
257 /// <param name="year">An integer that gives the year.
259 /// <param name="month">An integer that gives the month, starting
261 /// <param name="era">An intger that gives the era of the specified
263 /// <returns>An integer that gives the number of days of the
264 /// specified month.</returns>
265 /// <exception cref="T:System.ArgumentOutOfRangeException">
266 /// The exception is thrown, if <paramref name="month"/>,
267 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
268 /// the allowed range.
270 public override int GetDaysInMonth(int year, int month, int era) {
271 // mscorlib doesn't check year, probably a bug; we do
272 M_CheckYME(year, month, ref era);
273 return CCGregorianCalendar.GetDaysInMonth(year, month);
277 /// Overridden. Gives the number of days of the specified
278 /// year of the given era.
280 /// <param name="year">An integer that specifies the year.
282 /// <param name="era">An ineger that specifies the era.
284 /// <returns>An integer that gives the number of days of the
285 /// specified year.</returns>
286 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
287 /// The exception is thrown, if
288 /// <paramref name="year"/> is outside the allowed range.
290 public override int GetDaysInYear(int year, int era) {
291 M_CheckYE(year, ref era);
292 return CCGregorianCalendar.GetDaysInYear(year);
297 /// Overridden. Gives the era of the specified date.
299 /// <param name="time">The
300 /// <see cref="T:System.DateTime"/> that specifies a
303 /// <returns>An integer representing the era of the calendar.
305 public override int GetEra(DateTime time) {
310 /// Overridden. Gives the number of the month of the specified
313 /// <param name="time">The
314 /// <see cref="T:System.DateTime"/> that specifies a
317 /// <returns>An integer representing the month,
318 /// starting with 1.</returns>
319 public override int GetMonth(DateTime time) {
320 return CCGregorianCalendar.GetMonth(time);
324 /// Overridden. Gives the number of months in the specified year
327 /// <param name="year">An integer that specifies the year.
329 /// <param name="era">An integer that specifies the era.
331 /// <returns>An integer that gives the number of the months in the
332 /// specified year.</returns>
333 /// <exception cref="T:System.ArgumentOutOfRangeException">
334 /// The exception is thrown, if the year or the era are not valid.
336 public override int GetMonthsInYear(int year, int era) {
337 M_CheckYE(year, ref era);
342 /// Overridden. Gives the number of the year of the specified
345 /// <param name="time">The
346 /// <see cref="T:System.DateTime"/> that specifies a
349 /// <returns>An integer representing the year,
350 /// starting with 1.</returns>
351 public override int GetYear(DateTime time) {
352 return CCGregorianCalendar.GetYear(time);
356 /// Overridden. Tells whether the given day
359 /// <param name="year">An integer that specifies the year in the
362 /// <param name="month">An integer that specifies the month.
364 /// <param name="day">An integer that specifies the day.
366 /// <param name="era">An integer that specifies the era.
368 /// <returns>A boolean that tells whether the given day is a leap
371 /// <exception cref="T:System.ArgumentOutOfRangeException">
372 /// The exception is thrown, if the year, month, day, or era is not
375 public override bool IsLeapDay(int year, int month, int day, int era)
377 M_CheckYMDE(year, month, day, ref era);
378 return CCGregorianCalendar.IsLeapDay(year, month, day);
383 /// Overridden. Tells whether the given month
386 /// <param name="year">An integer that specifies the year in the
389 /// <param name="month">An integer that specifies the month.
391 /// <param name="era">An integer that specifies the era.
393 /// <returns>A boolean that tells whether the given month is a leap
396 /// <exception cref="T:System.ArgumentOutOfRangeException">
397 /// The exception is thrown, if the year, month, or era is not
400 public override bool IsLeapMonth(int year, int month, int era) {
401 M_CheckYME(year, month, ref era);
406 /// Overridden. Tells whether the given year
409 /// <param name="year">An integer that specifies the year in the
412 /// <param name="era">An integer that specifies the era.
414 /// <returns>A boolean that tells whether the given year is a leap
417 /// <exception cref="T:System.ArgumentOutOfRangeException">
418 /// The exception is thrown, if the year or era is not
421 public override bool IsLeapYear(int year, int era) {
422 M_CheckYE(year, ref era);
423 return CCGregorianCalendar.is_leap_year(year);
427 /// Overridden. Creates the
428 /// <see cref="T:System.DateTime"/> from the parameters.
430 /// <param name="year">An integer that gives the year in the
431 /// <paramref name="era"/>.
433 /// <param name="month">An integer that specifies the month.
435 /// <param name="day">An integer that specifies the day.
437 /// <param name="hour">An integer that specifies the hour.
439 /// <param name="minute">An integer that specifies the minute.
441 /// <param name="second">An integer that gives the second.
443 /// <param name="milliseconds">An integer that gives the
446 /// <param name="era">An integer that specifies the era.
449 /// <see cref="T:system.DateTime"/> representig the date and time.
451 /// <exception cref="T:System.ArgumentOutOfRangeException">
452 /// The exception is thrown, if at least one of the parameters
455 public override DateTime ToDateTime(int year, int month, int day,
456 int hour, int minute, int second, int milliseconds,
459 M_CheckYMDE(year, month, day, ref era);
460 M_CheckHMSM(hour, minute, second, milliseconds);
461 return CCGregorianCalendar.ToDateTime(
463 hour, minute, second, milliseconds);
467 public override int ToFourDigitYear(int year)
469 throw new NotImplementedException();
473 /// Constructor that sets the
474 /// Gregorian calendar type (
475 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
477 /// <param name="type">The parameter specifies the Gregorian
480 public GregorianCalendar(GregorianCalendarTypes type) {
482 M_AbbrEraNames = new string[] {"C.E."};
483 M_EraNames = new string[] {"Common Era"};
484 if (M_TwoDigitYearMax == 99)
485 M_TwoDigitYearMax = 2029;
489 public override CalendarAlgorithmType AlgorithmType {
491 return CalendarAlgorithmType.SolarCalendar;
495 static DateTime Min = new DateTime (1, 1, 1, 0, 0, 0);
496 static DateTime Max = new DateTime (9999, 12, 31, 11, 59, 59);
498 public override DateTime MinSupportedDateTime {
504 public override DateTime MaxSupportedDateTime {
512 /// Default constructor. Sets the Gregorian calendar type to
514 /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
516 public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
517 } // class GregorianCalendar
519 } // namespace System.Globalization