**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Globalization / GregorianCalendar.cs
blobf51ad72f492adb662bfcbd33644b7f6ac9db3cb7
1 // GregorianCalendar.cs
2 //
3 // (C) Ulrich Kunitz 2002
4 //
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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 {
31 using System;
33 /// <summary>
34 /// This is the Gregorian calendar.
35 /// </summary>
36 /// <remarks>
37 /// <para>The Gregorian calendar supports only the Common Era from
38 /// the Gregorian year 1 to the Gregorian year 9999.
39 /// </para>
40 /// <para>The implementation uses the
41 /// <see cref="N:CalendricalCalculations"/> namespace.
42 /// </para>
43 /// </remarks>
44 [Serializable]
45 [MonoTODO ("Fix serialization compatibility with MS.NET")]
46 public class GregorianCalendar : Calendar {
47 /// <summary>
48 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
49 /// respective.
50 /// </summary>
51 public const int ADEra = 1;
53 /// <value>Overridden. Gives the eras supported by the Gregorian
54 /// calendar as an array of integers.
55 /// </value>
56 public override int[] Eras {
57 get {
58 return new int[] { ADEra };
62 int twoDigitYearMax = 2029;
64 public override int TwoDigitYearMax
66 get {
67 return twoDigitYearMax;
69 set {
70 M_ArgumentInRange ("value", value, 100, M_MaxYear);
72 twoDigitYearMax = value;
76 /// <summary>
77 /// A protected member storing the
78 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
79 /// </summary>
80 internal GregorianCalendarTypes M_CalendarType;
82 /// <value>
83 /// The property stores the
84 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
85 /// </value>
86 public virtual GregorianCalendarTypes CalendarType {
87 get { return M_CalendarType; }
88 set {
89 // mscorlib 1:0:33000:0 doesn't check anything here
90 M_CalendarType = value;
94 /// <summary>
95 /// A protected method checking the era number.
96 /// </summary>
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"/>.
101 /// </exception>
102 internal void M_CheckEra(ref int era) {
103 if (era == CurrentEra)
104 era = ADEra;
105 if (era != ADEra)
106 throw new ArgumentException("Era value was not valid.");
109 /// <summary>
110 /// A protected method checking calendar year and the era number.
111 /// </summary>
112 /// <param name="year">An integer representing the calendar year.
113 /// </param>
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"/>.
118 /// </exception>
119 /// <exception cref="T:System.ArgumentOutOfRangeException">
120 /// The exception is thrown if the calendar year is outside of
121 /// the allowed range.
122 /// </exception>
123 internal override void M_CheckYE(int year, ref int era) {
124 M_CheckEra(ref era);
125 M_ArgumentInRange("year", year, 1, 9999);
128 /// <summary>
129 /// A protected method checking the calendar year, month, and
130 /// era number.
131 /// </summary>
132 /// <param name="year">An integer representing the calendar year.
133 /// </param>
134 /// <param name="month">An integer giving the calendar month.
135 /// </param>
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"/>.
140 /// </exception>
141 /// <exception cref="T:System.ArgumentOutOfRangeException">
142 /// The exception is thrown if the calendar year or month is
143 /// outside of the allowed range.
144 /// </exception>
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.");
152 /// <summary>
153 /// A protected method checking the calendar day, month, and year
154 /// and the era number.
155 /// </summary>
156 /// <param name="year">An integer representing the calendar year.
157 /// </param>
158 /// <param name="month">An integer giving the calendar month.
159 /// </param>
160 /// <param name="day">An integer giving the calendar day.
161 /// </param>
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"/>.
166 /// </exception>
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.
170 /// </exception>
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));
178 /// <summary>
179 /// Overridden. Adds months to a given date.
180 /// </summary>
181 /// <param name="time">The
182 /// <see cref="T:System.DateTime"/> to which to add
183 /// months.
184 /// </param>
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);
193 [MonoTODO]
194 public override DateTime AddWeeks(DateTime time, int weeks)
196 throw new NotImplementedException();
199 /// <summary>
200 /// Overridden. Adds years to a given date.
201 /// </summary>
202 /// <param name="time">The
203 /// <see cref="T:System.DateTime"/> to which to add
204 /// years.
205 /// </param>
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);
214 /// <summary>
215 /// Overridden. Gets the day of the month from
216 /// <paramref name="time"/>.
217 /// </summary>
218 /// <param name="time">The
219 /// <see cref="T:System.DateTime"/> that specifies a
220 /// date.
221 /// </param>
222 /// <returns>An integer giving the day of months, starting with 1.
223 /// </returns>
224 public override int GetDayOfMonth(DateTime time) {
225 return CCGregorianCalendar.GetDayOfMonth(time);
228 /// <summary>
229 /// Overridden. Gets the day of the week from the specified date.
230 /// </summary>
231 /// <param name="time">The
232 /// <see cref="T:System.DateTime"/> that specifies a
233 /// date.
234 /// </param>
235 /// <returns>An integer giving the day of months, starting with 1.
236 /// </returns>
237 public override DayOfWeek GetDayOfWeek(DateTime time) {
238 int rd = CCFixed.FromDateTime(time);
239 return (DayOfWeek)CCFixed.day_of_week(rd);
242 /// <summary>
243 /// Overridden. Gives the number of the day in the year.
244 /// </summary>
245 /// <param name="time">The
246 /// <see cref="T:System.DateTime"/> that specifies a
247 /// date.
248 /// </param>
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);
255 /// <summary>
256 /// Overridden. Gives the number of days in the specified month
257 /// of the given year and era.
258 /// </summary>
259 /// <param name="year">An integer that gives the year.
260 /// </param>
261 /// <param name="month">An integer that gives the month, starting
262 /// with 1.</param>
263 /// <param name="era">An intger that gives the era of the specified
264 /// year.</param>
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.
271 /// </exception>
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);
278 /// <summary>
279 /// Overridden. Gives the number of days of the specified
280 /// year of the given era.
281 /// </summary>
282 /// <param name="year">An integer that specifies the year.
283 /// </param>
284 /// <param name="era">An ineger that specifies the era.
285 /// </param>
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.
291 /// </exception>
292 public override int GetDaysInYear(int year, int era) {
293 M_CheckYE(year, ref era);
294 return CCGregorianCalendar.GetDaysInYear(year);
298 /// <summary>
299 /// Overridden. Gives the era of the specified date.
300 /// </summary>
301 /// <param name="time">The
302 /// <see cref="T:System.DateTime"/> that specifies a
303 /// date.
304 /// </param>
305 /// <returns>An integer representing the era of the calendar.
306 /// </returns>
307 public override int GetEra(DateTime time) {
308 return ADEra;
311 /// <summary>
312 /// Overridden. Gives the number of the month of the specified
313 /// date.
314 /// </summary>
315 /// <param name="time">The
316 /// <see cref="T:System.DateTime"/> that specifies a
317 /// date.
318 /// </param>
319 /// <returns>An integer representing the month,
320 /// starting with 1.</returns>
321 public override int GetMonth(DateTime time) {
322 return CCGregorianCalendar.GetMonth(time);
325 /// <summary>
326 /// Overridden. Gives the number of months in the specified year
327 /// and era.
328 /// </summary>
329 /// <param name="year">An integer that specifies the year.
330 /// </param>
331 /// <param name="era">An integer that specifies the era.
332 /// </param>
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.
337 /// </exception>
338 public override int GetMonthsInYear(int year, int era) {
339 M_CheckYE(year, ref era);
340 return 12;
343 /// <summary>
344 /// Overridden. Gives the number of the year of the specified
345 /// date.
346 /// </summary>
347 /// <param name="time">The
348 /// <see cref="T:System.DateTime"/> that specifies a
349 /// date.
350 /// </param>
351 /// <returns>An integer representing the year,
352 /// starting with 1.</returns>
353 public override int GetYear(DateTime time) {
354 return CCGregorianCalendar.GetYear(time);
357 /// <summary>
358 /// Overridden. Tells whether the given day
359 /// is a leap day.
360 /// </summary>
361 /// <param name="year">An integer that specifies the year in the
362 /// given era.
363 /// </param>
364 /// <param name="month">An integer that specifies the month.
365 /// </param>
366 /// <param name="day">An integer that specifies the day.
367 /// </param>
368 /// <param name="era">An integer that specifies the era.
369 /// </param>
370 /// <returns>A boolean that tells whether the given day is a leap
371 /// day.
372 /// </returns>
373 /// <exception cref="T:System.ArgumentOutOfRangeException">
374 /// The exception is thrown, if the year, month, day, or era is not
375 /// valid.
376 /// </exception>
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);
384 /// <summary>
385 /// Overridden. Tells whether the given month
386 /// is a leap month.
387 /// </summary>
388 /// <param name="year">An integer that specifies the year in the
389 /// given era.
390 /// </param>
391 /// <param name="month">An integer that specifies the month.
392 /// </param>
393 /// <param name="era">An integer that specifies the era.
394 /// </param>
395 /// <returns>A boolean that tells whether the given month is a leap
396 /// month.
397 /// </returns>
398 /// <exception cref="T:System.ArgumentOutOfRangeException">
399 /// The exception is thrown, if the year, month, or era is not
400 /// valid.
401 /// </exception>
402 public override bool IsLeapMonth(int year, int month, int era) {
403 M_CheckYME(year, month, ref era);
404 return false;
407 /// <summary>
408 /// Overridden. Tells whether the given year
409 /// is a leap year.
410 /// </summary>
411 /// <param name="year">An integer that specifies the year in the
412 /// given era.
413 /// </param>
414 /// <param name="era">An integer that specifies the era.
415 /// </param>
416 /// <returns>A boolean that tells whether the given year is a leap
417 /// year.
418 /// </returns>
419 /// <exception cref="T:System.ArgumentOutOfRangeException">
420 /// The exception is thrown, if the year or era is not
421 /// valid.
422 /// </exception>
423 public override bool IsLeapYear(int year, int era) {
424 M_CheckYE(year, ref era);
425 return CCGregorianCalendar.is_leap_year(year);
428 /// <summary>
429 /// Overridden. Creates the
430 /// <see cref="T:System.DateTime"/> from the parameters.
431 /// </summary>
432 /// <param name="year">An integer that gives the year in the
433 /// <paramref name="era"/>.
434 /// </param>
435 /// <param name="month">An integer that specifies the month.
436 /// </param>
437 /// <param name="day">An integer that specifies the day.
438 /// </param>
439 /// <param name="hour">An integer that specifies the hour.
440 /// </param>
441 /// <param name="minute">An integer that specifies the minute.
442 /// </param>
443 /// <param name="second">An integer that gives the second.
444 /// </param>
445 /// <param name="milliseconds">An integer that gives the
446 /// milliseconds.
447 /// </param>
448 /// <param name="era">An integer that specifies the era.
449 /// </param>
450 /// <returns>
451 /// <see cref="T:system.DateTime"/> representig the date and time.
452 /// </returns>
453 /// <exception cref="T:System.ArgumentOutOfRangeException">
454 /// The exception is thrown, if at least one of the parameters
455 /// is out of range.
456 /// </exception>
457 public override DateTime ToDateTime(int year, int month, int day,
458 int hour, int minute, int second, int milliseconds,
459 int era)
461 M_CheckYMDE(year, month, day, ref era);
462 M_CheckHMSM(hour, minute, second, milliseconds);
463 return CCGregorianCalendar.ToDateTime(
464 year, month, day,
465 hour, minute, second, milliseconds);
468 [MonoTODO]
469 public override int ToFourDigitYear(int year)
471 throw new NotImplementedException();
474 /// <summary>
475 /// Constructor that sets the
476 /// Gregorian calendar type (
477 /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
478 /// </summary>
479 /// <param name="type">The parameter specifies the Gregorian
480 /// calendar type.
481 /// </param>
482 public GregorianCalendar(GregorianCalendarTypes type) {
483 CalendarType = type;
484 M_AbbrEraNames = new string[] {"C.E."};
485 M_EraNames = new string[] {"Common Era"};
486 if (M_TwoDigitYearMax == 99)
487 M_TwoDigitYearMax = 2029;
490 /// <summary>
491 /// Default constructor. Sets the Gregorian calendar type to
492 /// <see
493 /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
494 /// </summary>
495 public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
496 } // class GregorianCalendar
498 } // namespace System.Globalization