1 // System.Globalization.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 {
32 using System.Runtime.InteropServices;
35 /// This is the Gregorian calendar.
38 /// <para>The Gregorian calendar supports only the Common Era from
39 /// the Gregorian year 1 to the Gregorian year 9999.
41 /// <para>The implementation uses the
42 /// <see cref="N:CalendricalCalculations"/> namespace.
49 [MonoTODO ("Serialization format not compatible with .NET")]
50 public class GregorianCalendar : Calendar {
53 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
56 public const int ADEra = 1;
58 /// <value>Overridden. Gives the eras supported by the Gregorian
59 /// calendar as an array of integers.
61 public override int[] Eras {
63 return new int[] { ADEra };
67 public override int TwoDigitYearMax
70 return twoDigitYearMax;
74 M_ArgumentInRange ("value", value, 100, M_MaxYear);
76 twoDigitYearMax = value;
81 /// A protected member storing the
82 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
85 internal GregorianCalendarTypes m_type;
88 /// The property stores the
89 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
91 public virtual GregorianCalendarTypes CalendarType {
92 get { return m_type; }
95 // mscorlib 1:0:3300:0 doesn't check anything here
101 /// A protected method checking the era number.
103 /// <param name="era">The era number.</param>
104 /// <exception name="T:System.ArgumentException">
105 /// The exception is thrown if the era is not equal
106 /// <see cref="M:ADEra"/>.
108 internal void M_CheckEra(ref int era) {
109 if (era == CurrentEra)
112 throw new ArgumentException("Era value was not valid.");
116 /// A protected method checking calendar year and the era number.
118 /// <param name="year">An integer representing the calendar year.
120 /// <param name="era">The era number.</param>
121 /// <exception cref="T:System.ArgumentException">
122 /// The exception is thrown if the era is not equal
123 /// <see cref="M:ADEra"/>.
125 /// <exception cref="T:System.ArgumentOutOfRangeException">
126 /// The exception is thrown if the calendar year is outside of
127 /// the allowed range.
129 internal override void M_CheckYE(int year, ref int era) {
131 M_ArgumentInRange("year", year, 1, 9999);
135 /// A protected method checking the calendar year, month, and
138 /// <param name="year">An integer representing the calendar year.
140 /// <param name="month">An integer giving the calendar month.
142 /// <param name="era">The era number.</param>
143 /// <exception cref="T:System.ArgumentException">
144 /// The exception is thrown if the era is not equal
145 /// <see cref="M:ADEra"/>.
147 /// <exception cref="T:System.ArgumentOutOfRangeException">
148 /// The exception is thrown if the calendar year or month is
149 /// outside of the allowed range.
151 internal void M_CheckYME(int year, int month, ref int era) {
152 M_CheckYE(year, ref era);
153 if (month < 1 || month > 12)
154 throw new ArgumentOutOfRangeException("month",
155 "Month must be between one and twelve.");
159 /// A protected method checking the calendar day, month, and year
160 /// and the era number.
162 /// <param name="year">An integer representing the calendar year.
164 /// <param name="month">An integer giving the calendar month.
166 /// <param name="day">An integer giving the calendar day.
168 /// <param name="era">The era number.</param>
169 /// <exception cref="T:System.ArgumentException">
170 /// The exception is thrown if the era is not equal
171 /// <see cref="M:ADEra"/>.
173 /// <exception cref="T:System.ArgumentOutOfRangeException">
174 /// The exception is thrown if the calendar year, month, or day is
175 /// outside of the allowed range.
177 internal void M_CheckYMDE(int year, int month, int day, ref int era)
179 M_CheckYME(year, month, ref era);
180 M_ArgumentInRange("day", day, 1,
181 GetDaysInMonth(year, month, era));
185 /// Overridden. Adds months to a given date.
187 /// <param name="time">The
188 /// <see cref="T:System.DateTime"/> to which to add
191 /// <param name="months">The number of months to add.</param>
192 /// <returns>A new <see cref="T:System.DateTime"/> value, that
193 /// results from adding <paramref name="months"/> to the specified
194 /// DateTime.</returns>
195 public override DateTime AddMonths(DateTime time, int months) {
196 return CCGregorianCalendar.AddMonths(time, months);
200 /// Overridden. Adds years to a given date.
202 /// <param name="time">The
203 /// <see cref="T:System.DateTime"/> to which to add
206 /// <param name="years">The number of years to add.</param>
207 /// <returns>A new <see cref="T:System.DateTime"/> value, that
208 /// results from adding <paramref name="years"/> to the specified
209 /// DateTime.</returns>
210 public override DateTime AddYears(DateTime time, int years) {
211 return CCGregorianCalendar.AddYears(time, years);
215 /// Overridden. Gets the day of the month from
216 /// <paramref name="time"/>.
218 /// <param name="time">The
219 /// <see cref="T:System.DateTime"/> that specifies a
222 /// <returns>An integer giving the day of months, starting with 1.
224 public override int GetDayOfMonth(DateTime time) {
225 return CCGregorianCalendar.GetDayOfMonth(time);
229 /// Overridden. Gets the day of the week from the specified date.
231 /// <param name="time">The
232 /// <see cref="T:System.DateTime"/> that specifies a
235 /// <returns>An integer giving the day of months, starting with 1.
237 public override DayOfWeek GetDayOfWeek(DateTime time) {
238 int rd = CCFixed.FromDateTime(time);
239 return (DayOfWeek)CCFixed.day_of_week(rd);
243 /// Overridden. Gives the number of the day in the year.
245 /// <param name="time">The
246 /// <see cref="T:System.DateTime"/> that specifies a
249 /// <returns>An integer representing the day of the year,
250 /// starting with 1.</returns>
251 public override int GetDayOfYear(DateTime time) {
252 return CCGregorianCalendar.GetDayOfYear(time);
256 /// Overridden. Gives the number of days in the specified month
257 /// of the given year and era.
259 /// <param name="year">An integer that gives the year.
261 /// <param name="month">An integer that gives the month, starting
263 /// <param name="era">An intger that gives the era of the specified
265 /// <returns>An integer that gives the number of days of the
266 /// specified month.</returns>
267 /// <exception cref="T:System.ArgumentOutOfRangeException">
268 /// The exception is thrown, if <paramref name="month"/>,
269 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
270 /// the allowed range.
272 public override int GetDaysInMonth(int year, int month, int era) {
273 // mscorlib doesn't check year, probably a bug; we do
274 M_CheckYME(year, month, ref era);
275 return CCGregorianCalendar.GetDaysInMonth(year, month);
279 /// Overridden. Gives the number of days of the specified
280 /// year of the given era.
282 /// <param name="year">An integer that specifies the year.
284 /// <param name="era">An ineger that specifies the era.
286 /// <returns>An integer that gives the number of days of the
287 /// specified year.</returns>
288 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
289 /// The exception is thrown, if
290 /// <paramref name="year"/> is outside the allowed range.
292 public override int GetDaysInYear(int year, int era) {
293 M_CheckYE(year, ref era);
294 return CCGregorianCalendar.GetDaysInYear(year);
299 /// Overridden. Gives the era of the specified date.
301 /// <param name="time">The
302 /// <see cref="T:System.DateTime"/> that specifies a
305 /// <returns>An integer representing the era of the calendar.
307 public override int GetEra(DateTime time) {
313 public override int GetLeapMonth (int year, int era)
320 /// Overridden. Gives the number of the month of the specified
323 /// <param name="time">The
324 /// <see cref="T:System.DateTime"/> that specifies a
327 /// <returns>An integer representing the month,
328 /// starting with 1.</returns>
329 public override int GetMonth(DateTime time) {
330 return CCGregorianCalendar.GetMonth(time);
334 /// Overridden. Gives the number of months in the specified year
337 /// <param name="year">An integer that specifies the year.
339 /// <param name="era">An integer that specifies the era.
341 /// <returns>An integer that gives the number of the months in the
342 /// specified year.</returns>
343 /// <exception cref="T:System.ArgumentOutOfRangeException">
344 /// The exception is thrown, if the year or the era are not valid.
346 public override int GetMonthsInYear(int year, int era) {
347 M_CheckYE(year, ref era);
353 public override int GetWeekOfYear (DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
355 return base.GetWeekOfYear (time, rule, firstDayOfWeek);
360 /// Overridden. Gives the number of the year of the specified
363 /// <param name="time">The
364 /// <see cref="T:System.DateTime"/> that specifies a
367 /// <returns>An integer representing the year,
368 /// starting with 1.</returns>
369 public override int GetYear(DateTime time) {
370 return CCGregorianCalendar.GetYear(time);
374 /// Overridden. Tells whether the given day
377 /// <param name="year">An integer that specifies the year in the
380 /// <param name="month">An integer that specifies the month.
382 /// <param name="day">An integer that specifies the day.
384 /// <param name="era">An integer that specifies the era.
386 /// <returns>A boolean that tells whether the given day is a leap
389 /// <exception cref="T:System.ArgumentOutOfRangeException">
390 /// The exception is thrown, if the year, month, day, or era is not
393 public override bool IsLeapDay(int year, int month, int day, int era)
395 M_CheckYMDE(year, month, day, ref era);
396 return CCGregorianCalendar.IsLeapDay(year, month, day);
401 /// Overridden. Tells whether the given month
404 /// <param name="year">An integer that specifies the year in the
407 /// <param name="month">An integer that specifies the month.
409 /// <param name="era">An integer that specifies the era.
411 /// <returns>A boolean that tells whether the given month is a leap
414 /// <exception cref="T:System.ArgumentOutOfRangeException">
415 /// The exception is thrown, if the year, month, or era is not
418 public override bool IsLeapMonth(int year, int month, int era) {
419 M_CheckYME(year, month, ref era);
424 /// Overridden. Tells whether the given year
427 /// <param name="year">An integer that specifies the year in the
430 /// <param name="era">An integer that specifies the era.
432 /// <returns>A boolean that tells whether the given year is a leap
435 /// <exception cref="T:System.ArgumentOutOfRangeException">
436 /// The exception is thrown, if the year or era is not
439 public override bool IsLeapYear(int year, int era) {
440 M_CheckYE(year, ref era);
441 return CCGregorianCalendar.is_leap_year(year);
445 /// Overridden. Creates the
446 /// <see cref="T:System.DateTime"/> from the parameters.
448 /// <param name="year">An integer that gives the year in the
449 /// <paramref name="era"/>.
451 /// <param name="month">An integer that specifies the month.
453 /// <param name="day">An integer that specifies the day.
455 /// <param name="hour">An integer that specifies the hour.
457 /// <param name="minute">An integer that specifies the minute.
459 /// <param name="second">An integer that gives the second.
461 /// <param name="milliseconds">An integer that gives the
464 /// <param name="era">An integer that specifies the era.
467 /// <see cref="T:system.DateTime"/> representig the date and time.
469 /// <exception cref="T:System.ArgumentOutOfRangeException">
470 /// The exception is thrown, if at least one of the parameters
473 public override DateTime ToDateTime(int year, int month, int day,
474 int hour, int minute, int second, int millisecond,
477 M_CheckYMDE(year, month, day, ref era);
478 M_CheckHMSM(hour, minute, second, millisecond);
479 return CCGregorianCalendar.ToDateTime(
481 hour, minute, second, millisecond);
484 public override int ToFourDigitYear(int year)
486 return base.ToFourDigitYear (year);
490 /// Constructor that sets the
491 /// Gregorian calendar type (
492 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
494 /// <param name="type">The parameter specifies the Gregorian
497 public GregorianCalendar(GregorianCalendarTypes type) {
499 M_AbbrEraNames = new string[] {"AD"};
500 M_EraNames = new string[] {"A.D."};
501 if (twoDigitYearMax == 99)
502 twoDigitYearMax = 2029;
507 public override CalendarAlgorithmType AlgorithmType {
509 return CalendarAlgorithmType.SolarCalendar;
513 static DateTime? Min, Max;
516 public override DateTime MinSupportedDateTime {
519 Min = new DateTime (1, 1, 1, 0, 0, 0);
525 public override DateTime MaxSupportedDateTime {
528 Max = new DateTime (9999, 12, 31, 11, 59, 59);
535 /// Default constructor. Sets the Gregorian calendar type to
537 /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
539 public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
540 } // class GregorianCalendar
542 } // namespace System.Globalization