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.
47 [MonoLimitation ("Serialization format not compatible with .NET")]
48 public class GregorianCalendar : Calendar {
51 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
54 public const int ADEra = 1;
56 /// <value>Overridden. Gives the eras supported by the Gregorian
57 /// calendar as an array of integers.
59 public override int[] Eras {
61 return new int[] { ADEra };
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_type;
86 /// The property stores the
87 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
89 public virtual GregorianCalendarTypes CalendarType {
90 get { return m_type; }
93 // mscorlib 1:0:3300:0 doesn't check anything here
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 public override int GetLeapMonth (int year, int era)
316 /// Overridden. Gives the number of the month of the specified
319 /// <param name="time">The
320 /// <see cref="T:System.DateTime"/> that specifies a
323 /// <returns>An integer representing the month,
324 /// starting with 1.</returns>
325 public override int GetMonth(DateTime time) {
326 return CCGregorianCalendar.GetMonth(time);
330 /// Overridden. Gives the number of months in the specified year
333 /// <param name="year">An integer that specifies the year.
335 /// <param name="era">An integer that specifies the era.
337 /// <returns>An integer that gives the number of the months in the
338 /// specified year.</returns>
339 /// <exception cref="T:System.ArgumentOutOfRangeException">
340 /// The exception is thrown, if the year or the era are not valid.
342 public override int GetMonthsInYear(int year, int era) {
343 M_CheckYE(year, ref era);
348 public override int GetWeekOfYear (DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
350 return base.GetWeekOfYear (time, rule, firstDayOfWeek);
354 /// Overridden. Gives the number of the year of the specified
357 /// <param name="time">The
358 /// <see cref="T:System.DateTime"/> that specifies a
361 /// <returns>An integer representing the year,
362 /// starting with 1.</returns>
363 public override int GetYear(DateTime time) {
364 return CCGregorianCalendar.GetYear(time);
368 /// Overridden. Tells whether the given day
371 /// <param name="year">An integer that specifies the year in the
374 /// <param name="month">An integer that specifies the month.
376 /// <param name="day">An integer that specifies the day.
378 /// <param name="era">An integer that specifies the era.
380 /// <returns>A boolean that tells whether the given day is a leap
383 /// <exception cref="T:System.ArgumentOutOfRangeException">
384 /// The exception is thrown, if the year, month, day, or era is not
387 public override bool IsLeapDay(int year, int month, int day, int era)
389 M_CheckYMDE(year, month, day, ref era);
390 return CCGregorianCalendar.IsLeapDay(year, month, day);
395 /// Overridden. Tells whether the given month
398 /// <param name="year">An integer that specifies the year in the
401 /// <param name="month">An integer that specifies the month.
403 /// <param name="era">An integer that specifies the era.
405 /// <returns>A boolean that tells whether the given month is a leap
408 /// <exception cref="T:System.ArgumentOutOfRangeException">
409 /// The exception is thrown, if the year, month, or era is not
412 public override bool IsLeapMonth(int year, int month, int era) {
413 M_CheckYME(year, month, ref era);
418 /// Overridden. Tells whether the given year
421 /// <param name="year">An integer that specifies the year in the
424 /// <param name="era">An integer that specifies the era.
426 /// <returns>A boolean that tells whether the given year is a leap
429 /// <exception cref="T:System.ArgumentOutOfRangeException">
430 /// The exception is thrown, if the year or era is not
433 public override bool IsLeapYear(int year, int era) {
434 M_CheckYE(year, ref era);
435 return CCGregorianCalendar.is_leap_year(year);
439 /// Overridden. Creates the
440 /// <see cref="T:System.DateTime"/> from the parameters.
442 /// <param name="year">An integer that gives the year in the
443 /// <paramref name="era"/>.
445 /// <param name="month">An integer that specifies the month.
447 /// <param name="day">An integer that specifies the day.
449 /// <param name="hour">An integer that specifies the hour.
451 /// <param name="minute">An integer that specifies the minute.
453 /// <param name="second">An integer that gives the second.
455 /// <param name="milliseconds">An integer that gives the
458 /// <param name="era">An integer that specifies the era.
461 /// <see cref="T:system.DateTime"/> representig the date and time.
463 /// <exception cref="T:System.ArgumentOutOfRangeException">
464 /// The exception is thrown, if at least one of the parameters
467 public override DateTime ToDateTime(int year, int month, int day,
468 int hour, int minute, int second, int millisecond,
471 M_CheckYMDE(year, month, day, ref era);
472 M_CheckHMSM(hour, minute, second, millisecond);
473 return CCGregorianCalendar.ToDateTime(
475 hour, minute, second, millisecond);
478 public override int ToFourDigitYear(int year)
480 return base.ToFourDigitYear (year);
484 /// Constructor that sets the
485 /// Gregorian calendar type (
486 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
488 /// <param name="type">The parameter specifies the Gregorian
491 public GregorianCalendar(GregorianCalendarTypes type) {
493 M_AbbrEraNames = new string[] {"AD"};
494 M_EraNames = new string[] {"A.D."};
495 if (twoDigitYearMax == 99)
496 twoDigitYearMax = 2029;
501 public override CalendarAlgorithmType AlgorithmType {
503 return CalendarAlgorithmType.SolarCalendar;
508 static DateTime? Min, Max;
511 public override DateTime MinSupportedDateTime {
514 Min = new DateTime (1, 1, 1, 0, 0, 0);
520 public override DateTime MaxSupportedDateTime {
523 Max = new DateTime (9999, 12, 31, 11, 59, 59);
529 /// Default constructor. Sets the Gregorian calendar type to
531 /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
533 public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
534 } // class GregorianCalendar
536 } // namespace System.Globalization