1 // ThaiBuddhistCalendar.cs
3 // (C) Ulrich Kunitz 2002
6 namespace System.Globalization {
11 /// This is the ThaiBudhist calendar. It differs from the Gegorian calendar
12 /// only in the year counting.
15 /// <para>The implementation uses the
16 /// <see cref="N:CalendricalCalculations"/> namespace.
20 public class ThaiBuddhistCalendar : Calendar {
22 /// Static protected field storing the
23 /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
25 internal static readonly CCGregorianEraHandler M_EraHandler;
28 /// The standard era for this calendar.
30 public const int ThaiBuddhistEra = 1;
33 /// Static constructor, who creates and initializes
34 /// <see cref="F:M_EraHandler"/>.
36 static ThaiBuddhistCalendar() {
37 M_EraHandler = new CCGregorianEraHandler();
38 M_EraHandler.appendEra(ThaiBuddhistEra,
39 CCGregorianCalendar.fixed_from_dmy(1, 1, -542));
43 /// Default constructor.
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;
52 /// <value>Overridden. Gives the eras supported by the
53 /// calendar as an array of integers.
55 public override int[] Eras {
57 return (int[])M_EraHandler.Eras.Clone();
62 /// A protected method checking the era number.
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.
69 internal 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.");
77 /// A protected method checking calendar year and the era number.
79 /// <param name="year">An integer representing the calendar year.
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.
85 /// <exception cref="T:System.ArgumentOutOfRangeException">
86 /// The exception is thrown if the calendar year is outside of
87 /// the supported range.
89 internal int M_CheckYEG(int year, ref int era) {
91 return M_EraHandler.GregorianYear(year, era);
95 /// Checks whether the year is the era is valid, if era = CurrentEra
96 /// the right value is set.
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.
103 internal override void M_CheckYE(int year, ref int era) {
104 M_CheckYEG(year, ref era);
108 /// A protected method checking the calendar year, month, and
111 /// <param name="year">An integer representing the calendar year.
113 /// <param name="month">An integer giving the calendar month.
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.
119 /// <exception cref="T:System.ArgumentOutOfRangeException">
120 /// The exception is thrown if the calendar year or month is
121 /// outside of the supported range.
123 internal 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;
132 /// A protected method checking the calendar day, month, and year
133 /// and the era number.
135 /// <param name="year">An integer representing the calendar year.
137 /// <param name="month">An integer giving the calendar month.
139 /// <param name="day">An integer giving the calendar day.
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.
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.
149 internal int M_CheckYMDEG(int year, int month, int day, ref int era)
151 int gregorianYear = M_CheckYMEG(year, month, ref era);
152 M_ArgumentInRange("day", day, 1,
153 GetDaysInMonth(year, month, era));
154 return gregorianYear;
158 /// Overrideden. Adds months to a given date.
160 /// <param name="time">The
161 /// <see cref="T:System.DateTime"/> to which to add
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);
173 /// Overridden. Adds years to a given date.
175 /// <param name="time">The
176 /// <see cref="T:System.DateTime"/> to which to add
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);
188 /// Overriden. Gets the day of the month from
189 /// <paramref name="time"/>.
191 /// <param name="time">The
192 /// <see cref="T:System.DateTime"/> that specifies a
195 /// <returns>An integer giving the day of months, starting with 1.
197 public override int GetDayOfMonth(DateTime time) {
198 return CCGregorianCalendar.GetDayOfMonth(time);
202 /// Overriden. Gets the day of the week from the specified date.
204 /// <param name="time">The
205 /// <see cref="T:System.DateTime"/> that specifies a
208 /// <returns>An integer giving the day of months, starting with 1.
210 public override DayOfWeek GetDayOfWeek(DateTime time) {
211 int rd = CCFixed.FromDateTime(time);
212 return (DayOfWeek)CCFixed.day_of_week(rd);
216 /// Overridden. Gives the number of the day in the year.
218 /// <param name="time">The
219 /// <see cref="T:System.DateTime"/> that specifies a
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);
229 /// Overridden. Gives the number of days in the specified month
230 /// of the given year and era.
232 /// <param name="year">An integer that gives the year.
234 /// <param name="month">An integer that gives the month, starting
236 /// <param name="era">An integer that gives the era of the specified
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.
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);
251 /// Overridden. Gives the number of days of the specified
252 /// year of the given era.
254 /// <param name="year">An integer that specifies the year.
256 /// <param name="era">An ineger that specifies the era.
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
265 public override int GetDaysInYear(int year, int era) {
266 int gregorianYear = M_CheckYEG(year, ref era);
267 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
271 /// Overridden. Gives the era of the specified date.
273 /// <param name="time">The
274 /// <see cref="T:System.DateTime"/> that specifies a
277 /// <returns>An integer representing the era of the calendar.
279 public override int GetEra(DateTime time) {
280 int rd = CCFixed.FromDateTime(time);
282 M_EraHandler.EraYear(out era, rd);
287 /// Overridden. Gives the number of the month of the specified
290 /// <param name="time">The
291 /// <see cref="T:System.DateTime"/> that specifies a
294 /// <returns>An integer representing the month,
295 /// starting with 1.</returns>
296 public override int GetMonth(DateTime time) {
297 return CCGregorianCalendar.GetMonth(time);
301 /// Overridden. Gives the number of months in the specified year
304 /// <param name="year">An integer that specifies the year.
306 /// <param name="era">An integer that specifies the era.
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.
313 public override int GetMonthsInYear(int year, int era) {
314 M_CheckYE(year, ref era);
319 /// Overridden. Gives the number of the year of the specified
322 /// <param name="time">The
323 /// <see cref="T:System.DateTime"/> that specifies a
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);
331 return M_EraHandler.EraYear(out era, rd);
335 /// Overridden. Tells whether the given day
338 /// <param name="year">An integer that specifies the year in the
341 /// <param name="month">An integer that specifies the month.
343 /// <param name="day">An integer that specifies the day.
345 /// <param name="era">An integer that specifies the era.
347 /// <returns>A boolean that tells whether the given day is a leap
350 /// <exception cref="T:System.ArgumentOutOfRangeException">
351 /// The exception is thrown, if the year, month, day, or era is not
354 public override bool IsLeapDay(int year, int month, int day, int era)
356 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
357 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
361 /// Overridden. Tells whether the given month
364 /// <param name="year">An integer that specifies the year in the
367 /// <param name="month">An integer that specifies the month.
369 /// <param name="era">An integer that specifies the era.
371 /// <returns>A boolean that tells whether the given month is a leap
374 /// <exception cref="T:System.ArgumentOutOfRangeException">
375 /// The exception is thrown, if the year, month, or era is not
378 public override bool IsLeapMonth(int year, int month, int era) {
379 M_CheckYMEG(year, month, ref era);
384 /// Overridden. Tells whether the given year
387 /// <param name="year">An integer that specifies the year in the
390 /// <param name="era">An integer that specifies the era.
392 /// <returns>A boolean that tells whether the given year is a leap
395 /// <exception cref="T:System.ArgumentOutOfRangeException">
396 /// The exception is thrown, if the year or era is not
399 public override bool IsLeapYear(int year, int era) {
400 int gregorianYear = M_CheckYEG(year, ref era);
401 return CCGregorianCalendar.is_leap_year(gregorianYear);
405 /// Overridden. Creates the
406 /// <see cref="T:System.DateTime"/> from the parameters.
408 /// <param name="year">An integer that gives the year in the
409 /// <paramref name="era"/>.
411 /// <param name="month">An integer that specifies the month.
413 /// <param name="day">An integer that specifies the day.
415 /// <param name="hour">An integer that specifies the hour.
417 /// <param name="minute">An integer that specifies the minute.
419 /// <param name="second">An integer that gives the second.
421 /// <param name="milliseconds">An integer that gives the
424 /// <param name="era">An integer that specifies the era.
427 /// <see cref="T:system.DateTime"/> representig the date and time.
429 /// <exception cref="T:System.ArgumentOutOfRangeException">
430 /// The exception is thrown, if at least one of the parameters
433 public override DateTime ToDateTime(int year, int month, int day,
434 int hour, int minute, int second, int milliseconds,
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);
443 } // class ThaiBuddhistCalendar
445 } // namespace System.Globalization