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