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