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 }
;
62 int twoDigitYearMax
= 2029;
64 public override int TwoDigitYearMax
67 return twoDigitYearMax
;
70 M_ArgumentInRange ("value", value, 100, M_MaxYear
);
72 twoDigitYearMax
= value;
77 /// A protected member storing the
78 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
80 internal GregorianCalendarTypes M_CalendarType
;
83 /// The property stores the
84 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
86 public virtual GregorianCalendarTypes CalendarType
{
87 get { return M_CalendarType; }
89 // mscorlib 1:0:33000:0 doesn't check anything here
90 M_CalendarType
= value;
95 /// A protected method checking the era number.
97 /// <param name="era">The era number.</param>
98 /// <exception name="T:System.ArgumentException">
99 /// The exception is thrown if the era is not equal
100 /// <see cref="M:ADEra"/>.
102 internal void M_CheckEra(ref int era
) {
103 if (era
== CurrentEra
)
106 throw new ArgumentException("Era value was not valid.");
110 /// A protected method checking calendar year and the era number.
112 /// <param name="year">An integer representing the calendar year.
114 /// <param name="era">The era number.</param>
115 /// <exception cref="T:System.ArgumentException">
116 /// The exception is thrown if the era is not equal
117 /// <see cref="M:ADEra"/>.
119 /// <exception cref="T:System.ArgumentOutOfRangeException">
120 /// The exception is thrown if the calendar year is outside of
121 /// the allowed range.
123 internal override void M_CheckYE(int year
, ref int era
) {
125 M_ArgumentInRange("year", year
, 1, 9999);
129 /// A protected method checking the calendar year, month, and
132 /// <param name="year">An integer representing the calendar year.
134 /// <param name="month">An integer giving the calendar month.
136 /// <param name="era">The era number.</param>
137 /// <exception cref="T:System.ArgumentException">
138 /// The exception is thrown if the era is not equal
139 /// <see cref="M:ADEra"/>.
141 /// <exception cref="T:System.ArgumentOutOfRangeException">
142 /// The exception is thrown if the calendar year or month is
143 /// outside of the allowed range.
145 internal void M_CheckYME(int year
, int month
, ref int era
) {
146 M_CheckYE(year
, ref era
);
147 if (month
< 1 || month
> 12)
148 throw new ArgumentOutOfRangeException("month",
149 "Month must be between one and twelve.");
153 /// A protected method checking the calendar day, month, and year
154 /// and the era number.
156 /// <param name="year">An integer representing the calendar year.
158 /// <param name="month">An integer giving the calendar month.
160 /// <param name="day">An integer giving the calendar day.
162 /// <param name="era">The era number.</param>
163 /// <exception cref="T:System.ArgumentException">
164 /// The exception is thrown if the era is not equal
165 /// <see cref="M:ADEra"/>.
167 /// <exception cref="T:System.ArgumentOutOfRangeException">
168 /// The exception is thrown if the calendar year, month, or day is
169 /// outside of the allowed range.
171 internal void M_CheckYMDE(int year
, int month
, int day
, ref int era
)
173 M_CheckYME(year
, month
, ref era
);
174 M_ArgumentInRange("day", day
, 1,
175 GetDaysInMonth(year
, month
, era
));
179 /// Overridden. Adds months to a given date.
181 /// <param name="time">The
182 /// <see cref="T:System.DateTime"/> to which to add
185 /// <param name="months">The number of months to add.</param>
186 /// <returns>A new <see cref="T:System.DateTime"/> value, that
187 /// results from adding <paramref name="months"/> to the specified
188 /// DateTime.</returns>
189 public override DateTime
AddMonths(DateTime time
, int months
) {
190 return CCGregorianCalendar
.AddMonths(time
, months
);
194 public override DateTime
AddWeeks(DateTime time
, int weeks
)
196 throw new NotImplementedException();
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
) {
312 /// Overridden. Gives the number of the month of the specified
315 /// <param name="time">The
316 /// <see cref="T:System.DateTime"/> that specifies a
319 /// <returns>An integer representing the month,
320 /// starting with 1.</returns>
321 public override int GetMonth(DateTime time
) {
322 return CCGregorianCalendar
.GetMonth(time
);
326 /// Overridden. Gives the number of months in the specified year
329 /// <param name="year">An integer that specifies the year.
331 /// <param name="era">An integer that specifies the era.
333 /// <returns>An integer that gives the number of the months in the
334 /// specified year.</returns>
335 /// <exception cref="T:System.ArgumentOutOfRangeException">
336 /// The exception is thrown, if the year or the era are not valid.
338 public override int GetMonthsInYear(int year
, int era
) {
339 M_CheckYE(year
, ref era
);
344 /// Overridden. Gives the number of the year of the specified
347 /// <param name="time">The
348 /// <see cref="T:System.DateTime"/> that specifies a
351 /// <returns>An integer representing the year,
352 /// starting with 1.</returns>
353 public override int GetYear(DateTime time
) {
354 return CCGregorianCalendar
.GetYear(time
);
358 /// Overridden. Tells whether the given day
361 /// <param name="year">An integer that specifies the year in the
364 /// <param name="month">An integer that specifies the month.
366 /// <param name="day">An integer that specifies the day.
368 /// <param name="era">An integer that specifies the era.
370 /// <returns>A boolean that tells whether the given day is a leap
373 /// <exception cref="T:System.ArgumentOutOfRangeException">
374 /// The exception is thrown, if the year, month, day, or era is not
377 public override bool IsLeapDay(int year
, int month
, int day
, int era
)
379 M_CheckYMDE(year
, month
, day
, ref era
);
380 return CCGregorianCalendar
.IsLeapDay(year
, month
, day
);
385 /// Overridden. Tells whether the given month
388 /// <param name="year">An integer that specifies the year in the
391 /// <param name="month">An integer that specifies the month.
393 /// <param name="era">An integer that specifies the era.
395 /// <returns>A boolean that tells whether the given month is a leap
398 /// <exception cref="T:System.ArgumentOutOfRangeException">
399 /// The exception is thrown, if the year, month, or era is not
402 public override bool IsLeapMonth(int year
, int month
, int era
) {
403 M_CheckYME(year
, month
, ref era
);
408 /// Overridden. Tells whether the given year
411 /// <param name="year">An integer that specifies the year in the
414 /// <param name="era">An integer that specifies the era.
416 /// <returns>A boolean that tells whether the given year is a leap
419 /// <exception cref="T:System.ArgumentOutOfRangeException">
420 /// The exception is thrown, if the year or era is not
423 public override bool IsLeapYear(int year
, int era
) {
424 M_CheckYE(year
, ref era
);
425 return CCGregorianCalendar
.is_leap_year(year
);
429 /// Overridden. Creates the
430 /// <see cref="T:System.DateTime"/> from the parameters.
432 /// <param name="year">An integer that gives the year in the
433 /// <paramref name="era"/>.
435 /// <param name="month">An integer that specifies the month.
437 /// <param name="day">An integer that specifies the day.
439 /// <param name="hour">An integer that specifies the hour.
441 /// <param name="minute">An integer that specifies the minute.
443 /// <param name="second">An integer that gives the second.
445 /// <param name="milliseconds">An integer that gives the
448 /// <param name="era">An integer that specifies the era.
451 /// <see cref="T:system.DateTime"/> representig the date and time.
453 /// <exception cref="T:System.ArgumentOutOfRangeException">
454 /// The exception is thrown, if at least one of the parameters
457 public override DateTime
ToDateTime(int year
, int month
, int day
,
458 int hour
, int minute
, int second
, int milliseconds
,
461 M_CheckYMDE(year
, month
, day
, ref era
);
462 M_CheckHMSM(hour
, minute
, second
, milliseconds
);
463 return CCGregorianCalendar
.ToDateTime(
465 hour
, minute
, second
, milliseconds
);
469 public override int ToFourDigitYear(int year
)
471 throw new NotImplementedException();
475 /// Constructor that sets the
476 /// Gregorian calendar type (
477 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
479 /// <param name="type">The parameter specifies the Gregorian
482 public GregorianCalendar(GregorianCalendarTypes type
) {
484 M_AbbrEraNames
= new string[] {"C.E."}
;
485 M_EraNames
= new string[] {"Common Era"}
;
486 if (M_TwoDigitYearMax
== 99)
487 M_TwoDigitYearMax
= 2029;
491 /// Default constructor. Sets the Gregorian calendar type to
493 /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
495 public GregorianCalendar() : this(GregorianCalendarTypes
.Localized
) {}
496 } // class GregorianCalendar
498 } // namespace System.Globalization