2002-04-13 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Globalization / GregorianCalendar.cs
1 // GregorianCalendar.cs
2 //
3 // (C) Ulrich Kunitz 2002
4 //
5
6 namespace System.Globalization {
7
8 using System;
9
10 /// <summary>
11 /// This is the Gregorian calendar.
12 /// </summary>
13 /// <remarks>
14 /// <para>The Gregorian calendar supports only the Common Era from
15 /// the Gregorian year 1 to the Gregorian year 9999.
16 /// </para>
17 /// <para>The implementation uses the
18 /// <see cref="N:CalendricalCalculations"/> namespace.
19 /// </para>
20 /// </remarks>
21 [Serializable]
22 public class GregorianCalendar : Calendar {
23         /// <summary>
24         /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
25         /// respective.
26         /// </summary>
27         public const int ADEra = 1;
28
29         /// <value>Overridden. Gives the eras supported by the Gregorian
30         /// calendar as an array of integers.
31         /// </value>
32         public override int[] Eras {
33                 get {
34                         return new int[] { ADEra }; 
35                 }
36         }
37
38         /// <summary>
39         /// A protected member storing the
40         /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
41         /// </summary>
42         internal GregorianCalendarTypes M_CalendarType;
43
44         /// <value>
45         /// The property stores the 
46         /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
47         /// </value>
48         public virtual GregorianCalendarTypes CalendarType {
49                 get { return M_CalendarType; }
50                 set { 
51                         // mscorlib 1:0:33000:0 doesn't check anything here
52                         M_CalendarType = value;
53                 }
54         }
55
56         /// <summary>
57         /// A protected method checking the era number.
58         /// </summary>
59         /// <param name="era">The era number.</param>
60         /// <exception name="T:System.ArgumentException">
61         /// The exception is thrown if the era is not equal
62         /// <see cref="M:ADEra"/>.
63         /// </exception>
64         internal void M_CheckEra(ref int era) {
65                 if (era == CurrentEra)
66                         era = ADEra;
67                 if (era != ADEra)
68                         throw new ArgumentException("Era value was not valid.");
69         }
70
71         /// <summary>
72         /// A protected method checking calendar year and the era number.
73         /// </summary>
74         /// <param name="year">An integer representing the calendar year.
75         /// </param>
76         /// <param name="era">The era number.</param>
77         /// <exception cref="T:System.ArgumentException">
78         /// The exception is thrown if the era is not equal
79         /// <see cref="M:ADEra"/>.
80         /// </exception>
81         /// <exception cref="T:System.ArgumentOutOfRangeException">
82         /// The exception is thrown if the calendar year is outside of
83         /// the allowed range.
84         /// </exception>
85         internal override void M_CheckYE(int year, ref int era) {
86                 M_CheckEra(ref era);
87                 M_ArgumentInRange("year", year, 1, 9999);
88         }
89
90         /// <summary>
91         /// A protected method checking the calendar year, month, and
92         /// era number.
93         /// </summary>
94         /// <param name="year">An integer representing the calendar year.
95         /// </param>
96         /// <param name="month">An integer giving the calendar month.
97         /// </param>
98         /// <param name="era">The era number.</param>
99         /// <exception cref="T:System.ArgumentException">
100         /// The exception is thrown if the era is not equal
101         /// <see cref="M:ADEra"/>.
102         /// </exception>
103         /// <exception cref="T:System.ArgumentOutOfRangeException">
104         /// The exception is thrown if the calendar year or month is
105         /// outside of the allowed range.
106         /// </exception>
107         internal void M_CheckYME(int year, int month, ref int era) {
108                 M_CheckYE(year, ref era);
109                 if (month < 1 || month > 12)
110                         throw new ArgumentOutOfRangeException("month",
111                                 "Month must be between one and twelve.");
112         }
113
114         /// <summary>
115         /// A protected method checking the calendar day, month, and year
116         /// and the era number.
117         /// </summary>
118         /// <param name="year">An integer representing the calendar year.
119         /// </param>
120         /// <param name="month">An integer giving the calendar month.
121         /// </param>
122         /// <param name="day">An integer giving the calendar day.
123         /// </param>
124         /// <param name="era">The era number.</param>
125         /// <exception cref="T:System.ArgumentException">
126         /// The exception is thrown if the era is not equal
127         /// <see cref="M:ADEra"/>.
128         /// </exception>
129         /// <exception cref="T:System.ArgumentOutOfRangeException">
130         /// The exception is thrown if the calendar year, month, or day is
131         /// outside of the allowed range.
132         /// </exception>
133         internal void M_CheckYMDE(int year, int month, int day, ref int era)
134         {
135                 M_CheckYME(year, month, ref era);
136                 M_ArgumentInRange("day", day, 1,
137                         GetDaysInMonth(year, month, era));
138         }
139
140         /// <summary>
141         /// Overridden. Adds months to a given date.
142         /// </summary>
143         /// <param name="time">The
144         /// <see cref="T:System.DateTime"/> to which to add
145         /// months.
146         /// </param>
147         /// <param name="months">The number of months to add.</param>
148         /// <returns>A new <see cref="T:System.DateTime"/> value, that
149         /// results from adding <paramref name="months"/> to the specified
150         /// DateTime.</returns>
151         public override DateTime AddMonths(DateTime time, int months) {
152                 return CCGregorianCalendar.AddMonths(time, months);
153         }
154
155         /// <summary>
156         /// Overridden. Adds years to a given date.
157         /// </summary>
158         /// <param name="time">The
159         /// <see cref="T:System.DateTime"/> to which to add
160         /// years.
161         /// </param>
162         /// <param name="years">The number of years to add.</param>
163         /// <returns>A new <see cref="T:System.DateTime"/> value, that
164         /// results from adding <paramref name="years"/> to the specified
165         /// DateTime.</returns>
166         public override DateTime AddYears(DateTime time, int years) {
167                 return CCGregorianCalendar.AddYears(time, years);
168         }
169                 
170         /// <summary>
171         /// Overridden. Gets the day of the month from
172         /// <paramref name="time"/>.
173         /// </summary>
174         /// <param name="time">The
175         /// <see cref="T:System.DateTime"/> that specifies a
176         /// date.
177         /// </param>
178         /// <returns>An integer giving the day of months, starting with 1.
179         /// </returns>
180         public override int GetDayOfMonth(DateTime time) {
181                 return CCGregorianCalendar.GetDayOfMonth(time);
182         }
183
184         /// <summary>
185         /// Overridden. Gets the day of the week from the specified date.
186         /// </summary>
187         /// <param name="time">The
188         /// <see cref="T:System.DateTime"/> that specifies a
189         /// date.
190         /// </param>
191         /// <returns>An integer giving the day of months, starting with 1.
192         /// </returns>
193         public override DayOfWeek GetDayOfWeek(DateTime time) {
194                 int rd = CCFixed.FromDateTime(time);
195                 return (DayOfWeek)CCFixed.day_of_week(rd);
196         }
197
198         /// <summary>
199         /// Overridden. Gives the number of the day in the year.
200         /// </summary>
201         /// <param name="time">The
202         /// <see cref="T:System.DateTime"/> that specifies a
203         /// date.
204         /// </param>
205         /// <returns>An integer representing the day of the year,
206         /// starting with 1.</returns>
207         public override int GetDayOfYear(DateTime time) {
208                 return CCGregorianCalendar.GetDayOfYear(time);
209         }
210
211         /// <summary>
212         /// Overridden. Gives the number of days in the specified month
213         /// of the given year and era.
214         /// </summary>
215         /// <param name="year">An integer that gives the year.
216         /// </param>
217         /// <param name="month">An integer that gives the month, starting
218         /// with 1.</param>
219         /// <param name="era">An intger that gives the era of the specified
220         /// year.</param>
221         /// <returns>An integer that gives the number of days of the
222         /// specified month.</returns>
223         /// <exception cref="T:System.ArgumentOutOfRangeException">
224         /// The exception is thrown, if <paramref name="month"/>,
225         /// <paramref name="year"/> ,or <paramref name="era"/> is outside
226         /// the allowed range.
227         /// </exception>
228         public override int GetDaysInMonth(int year, int month, int era) {
229                 // mscorlib doesn't check year, probably a bug; we do
230                 M_CheckYME(year, month, ref era);
231                 return CCGregorianCalendar.GetDaysInMonth(year, month);
232         }
233
234         /// <summary>
235         /// Overridden. Gives the number of days of the specified
236         /// year of the given era. 
237         /// </summary>
238         /// <param name="year">An integer that specifies the year. 
239         /// </param>
240         /// <param name="era">An ineger that specifies the era.
241         /// </param>
242         /// <returns>An integer that gives the number of days of the
243         /// specified year.</returns>
244         /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
245         /// The exception is thrown, if
246         /// <paramref name="year"/> is outside the allowed range.
247         /// </exception>
248         public override int GetDaysInYear(int year, int era) {
249                 M_CheckYE(year, ref era);
250                 return CCGregorianCalendar.GetDaysInYear(year);
251         }
252                 
253
254         /// <summary>
255         /// Overridden. Gives the era of the specified date.
256         /// </summary>
257         /// <param name="time">The
258         /// <see cref="T:System.DateTime"/> that specifies a
259         /// date.
260         /// </param>
261         /// <returns>An integer representing the era of the calendar.
262         /// </returns>
263         public override int GetEra(DateTime time) {
264                 return ADEra;
265         }
266
267         /// <summary>
268         /// Overridden. Gives the number of the month of the specified
269         /// date.
270         /// </summary>
271         /// <param name="time">The
272         /// <see cref="T:System.DateTime"/> that specifies a
273         /// date.
274         /// </param>
275         /// <returns>An integer representing the month, 
276         /// starting with 1.</returns>
277         public override int GetMonth(DateTime time) {
278                 return CCGregorianCalendar.GetMonth(time);
279         }
280
281         /// <summary>
282         /// Overridden. Gives the number of months in the specified year 
283         /// and era.
284         /// </summary>
285         /// <param name="year">An integer that specifies the year.
286         /// </param>
287         /// <param name="era">An integer that specifies the era.
288         /// </param>
289         /// <returns>An integer that gives the number of the months in the
290         /// specified year.</returns>
291         /// <exception cref="T:System.ArgumentOutOfRangeException">
292         /// The exception is thrown, if the year or the era are not valid.
293         /// </exception>
294         public override int GetMonthsInYear(int year, int era) {
295                 M_CheckYE(year, ref era);
296                 return 12;
297         }
298
299         /// <summary>
300         /// Overridden. Gives the number of the year of the specified
301         /// date.
302         /// </summary>
303         /// <param name="time">The
304         /// <see cref="T:System.DateTime"/> that specifies a
305         /// date.
306         /// </param>
307         /// <returns>An integer representing the year, 
308         /// starting with 1.</returns>
309         public override int GetYear(DateTime time) {
310                 return CCGregorianCalendar.GetYear(time);
311         }
312
313         /// <summary>
314         /// Overridden. Tells whether the given day 
315         /// is a leap day.
316         /// </summary>
317         /// <param name="year">An integer that specifies the year in the
318         /// given era.
319         /// </param>
320         /// <param name="month">An integer that specifies the month.
321         /// </param>
322         /// <param name="day">An integer that specifies the day.
323         /// </param>
324         /// <param name="era">An integer that specifies the era.
325         /// </param>
326         /// <returns>A boolean that tells whether the given day is a leap
327         /// day.
328         /// </returns>
329         /// <exception cref="T:System.ArgumentOutOfRangeException">
330         /// The exception is thrown, if the year, month, day, or era is not
331         /// valid.
332         /// </exception>
333         public override bool IsLeapDay(int year, int month, int day, int era)
334         {
335                 M_CheckYMDE(year, month, day, ref era);
336                 return CCGregorianCalendar.IsLeapDay(year, month, day);
337         }
338
339
340         /// <summary>
341         /// Overridden. Tells whether the given month 
342         /// is a leap month.
343         /// </summary>
344         /// <param name="year">An integer that specifies the year in the
345         /// given era.
346         /// </param>
347         /// <param name="month">An integer that specifies the month.
348         /// </param>
349         /// <param name="era">An integer that specifies the era.
350         /// </param>
351         /// <returns>A boolean that tells whether the given month is a leap
352         /// month.
353         /// </returns>
354         /// <exception cref="T:System.ArgumentOutOfRangeException">
355         /// The exception is thrown, if the year, month, or era is not
356         /// valid.
357         /// </exception>
358         public override bool IsLeapMonth(int year, int month, int era) {
359                 M_CheckYME(year, month, ref era);
360                 return false;
361         }
362
363         /// <summary>
364         /// Overridden. Tells whether the given year
365         /// is a leap year.
366         /// </summary>
367         /// <param name="year">An integer that specifies the year in the
368         /// given era.
369         /// </param>
370         /// <param name="era">An integer that specifies the era.
371         /// </param>
372         /// <returns>A boolean that tells whether the given year is a leap
373         /// year.
374         /// </returns>
375         /// <exception cref="T:System.ArgumentOutOfRangeException">
376         /// The exception is thrown, if the year or era is not
377         /// valid.
378         /// </exception>
379         public override bool IsLeapYear(int year, int era) {
380                 M_CheckYE(year, ref era);
381                 return CCGregorianCalendar.is_leap_year(year);
382         }
383
384         /// <summary>
385         /// Overridden. Creates the
386         /// <see cref="T:System.DateTime"/> from the parameters.
387         /// </summary>
388         /// <param name="year">An integer that gives the year in the
389         /// <paramref name="era"/>.
390         /// </param>
391         /// <param name="month">An integer that specifies the month.
392         /// </param>
393         /// <param name="day">An integer that specifies the day.
394         /// </param>
395         /// <param name="hour">An integer that specifies the hour.
396         /// </param>
397         /// <param name="minute">An integer that specifies the minute.
398         /// </param>
399         /// <param name="second">An integer that gives the second.
400         /// </param>
401         /// <param name="milliseconds">An integer that gives the
402         /// milliseconds.
403         /// </param>
404         /// <param name="era">An integer that specifies the era.
405         /// </param>
406         /// <returns>
407         /// <see cref="T:system.DateTime"/> representig the date and time.
408         /// </returns>
409         /// <exception cref="T:System.ArgumentOutOfRangeException">
410         /// The exception is thrown, if at least one of the parameters
411         /// is out of range.
412         /// </exception>
413         public override DateTime ToDateTime(int year, int month, int day,
414                 int hour, int minute, int second, int milliseconds,
415                 int era)
416         {
417                 M_CheckYMDE(year, month, day, ref era);
418                 M_CheckHMSM(hour, minute, second, milliseconds);
419                 return CCGregorianCalendar.ToDateTime(
420                         year, month, day,
421                         hour, minute, second, milliseconds);
422         }
423                                         
424         /// <summary>
425         /// Constructor that sets the
426         /// Gregorian calendar type (
427         /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
428         /// </summary>
429         /// <param name="type">The parameter specifies the Gregorian 
430         /// calendar type.
431         /// </param>
432         public GregorianCalendar(GregorianCalendarTypes type) {
433                 CalendarType = type;
434                 M_AbbrEraNames = new string[] {"C.E."};
435                 M_EraNames = new string[] {"Common Era"};
436                 if (M_TwoDigitYearMax == 99)
437                         M_TwoDigitYearMax = 2029;
438         }
439         
440         /// <summary>
441         /// Default constructor. Sets the Gregorian calendar type to 
442         /// <see
443         /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
444         /// </summary>
445         public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
446 } // class GregorianCalendar
447         
448 } // namespace System.Globalization