1 // ThaiBuddhistCalendar.cs
3 // (C) Ulrich Kunitz 2002
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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 {
32 using System.Runtime.InteropServices;
35 /// This is the ThaiBudhist calendar. It differs from the Gegorian calendar
36 /// only in the year counting.
39 /// <para>The implementation uses the
40 /// <see cref="N:CalendricalCalculations"/> namespace.
47 [MonoTODO ("Serialization format not compatible with.NET")]
48 public class ThaiBuddhistCalendar : Calendar {
50 /// Static protected field storing the
51 /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
53 internal static readonly CCGregorianEraHandler M_EraHandler;
56 /// The standard era for this calendar.
58 public const int ThaiBuddhistEra = 1;
61 /// Static constructor, who creates and initializes
62 /// <see cref="F:M_EraHandler"/>.
64 static ThaiBuddhistCalendar() {
65 M_EraHandler = new CCGregorianEraHandler();
66 M_EraHandler.appendEra(ThaiBuddhistEra,
67 CCGregorianCalendar.fixed_from_dmy(1, 1, -542));
71 /// Default constructor.
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;
80 /// <value>Overridden. Gives the eras supported by the
81 /// calendar as an array of integers.
83 public override int[] Eras {
85 return (int[])M_EraHandler.Eras.Clone();
89 public override int TwoDigitYearMax
92 return twoDigitYearMax;
96 M_ArgumentInRange ("value", value, 100, M_MaxYear);
98 twoDigitYearMax = value;
103 /// A protected method checking the era number.
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.
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.");
118 /// A protected method checking calendar year and the era number.
120 /// <param name="year">An integer representing the calendar year.
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.
126 /// <exception cref="T:System.ArgumentOutOfRangeException">
127 /// The exception is thrown if the calendar year is outside of
128 /// the supported range.
130 internal int M_CheckYEG(int year, ref int era) {
132 return M_EraHandler.GregorianYear(year, era);
136 /// Checks whether the year is the era is valid, if era = CurrentEra
137 /// the right value is set.
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.
144 internal override void M_CheckYE(int year, ref int era) {
145 M_CheckYEG(year, ref era);
149 /// A protected method checking the calendar year, month, and
152 /// <param name="year">An integer representing the calendar year.
154 /// <param name="month">An integer giving the calendar month.
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.
160 /// <exception cref="T:System.ArgumentOutOfRangeException">
161 /// The exception is thrown if the calendar year or month is
162 /// outside of the supported range.
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;
173 /// A protected method checking the calendar day, month, and year
174 /// and the era number.
176 /// <param name="year">An integer representing the calendar year.
178 /// <param name="month">An integer giving the calendar month.
180 /// <param name="day">An integer giving the calendar day.
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.
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.
190 internal int M_CheckYMDEG(int year, int month, int day, ref int era)
192 int gregorianYear = M_CheckYMEG(year, month, ref era);
193 M_ArgumentInRange("day", day, 1,
194 GetDaysInMonth(year, month, era));
195 return gregorianYear;
199 /// Overrideden. Adds months to a given date.
201 /// <param name="time">The
202 /// <see cref="T:System.DateTime"/> to which to add
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);
214 /// Overridden. Adds years to a given date.
216 /// <param name="time">The
217 /// <see cref="T:System.DateTime"/> to which to add
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);
229 /// Overriden. Gets the day of the month from
230 /// <paramref name="time"/>.
232 /// <param name="time">The
233 /// <see cref="T:System.DateTime"/> that specifies a
236 /// <returns>An integer giving the day of months, starting with 1.
238 public override int GetDayOfMonth(DateTime time) {
239 return CCGregorianCalendar.GetDayOfMonth(time);
243 /// Overriden. Gets the day of the week from the specified date.
245 /// <param name="time">The
246 /// <see cref="T:System.DateTime"/> that specifies a
249 /// <returns>An integer giving the day of months, starting with 1.
251 public override DayOfWeek GetDayOfWeek(DateTime time) {
252 int rd = CCFixed.FromDateTime(time);
253 return (DayOfWeek)CCFixed.day_of_week(rd);
257 /// Overridden. Gives the number of the day in the year.
259 /// <param name="time">The
260 /// <see cref="T:System.DateTime"/> that specifies a
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);
270 /// Overridden. Gives the number of days in the specified month
271 /// of the given year and era.
273 /// <param name="year">An integer that gives the year.
275 /// <param name="month">An integer that gives the month, starting
277 /// <param name="era">An integer that gives the era of the specified
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.
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);
292 /// Overridden. Gives the number of days of the specified
293 /// year of the given era.
295 /// <param name="year">An integer that specifies the year.
297 /// <param name="era">An ineger that specifies the era.
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
306 public override int GetDaysInYear(int year, int era) {
307 int gregorianYear = M_CheckYEG(year, ref era);
308 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
312 /// Overridden. Gives the era of the specified date.
314 /// <param name="time">The
315 /// <see cref="T:System.DateTime"/> that specifies a
318 /// <returns>An integer representing the era of the calendar.
320 public override int GetEra(DateTime time) {
321 int rd = CCFixed.FromDateTime(time);
323 M_EraHandler.EraYear(out era, rd);
329 public override int GetLeapMonth (int year, int era)
336 /// Overridden. Gives the number of the month of the specified
339 /// <param name="time">The
340 /// <see cref="T:System.DateTime"/> that specifies a
343 /// <returns>An integer representing the month,
344 /// starting with 1.</returns>
345 public override int GetMonth(DateTime time) {
346 return CCGregorianCalendar.GetMonth(time);
350 /// Overridden. Gives the number of months in the specified year
353 /// <param name="year">An integer that specifies the year.
355 /// <param name="era">An integer that specifies the era.
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.
362 public override int GetMonthsInYear(int year, int era) {
363 M_CheckYE(year, ref era);
369 public override int GetWeekOfYear (DateTime date, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
371 return base.GetWeekOfYear (date, rule, firstDayOfWeek);
376 /// Overridden. Gives the number of the year of the specified
379 /// <param name="time">The
380 /// <see cref="T:System.DateTime"/> that specifies a
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);
388 return M_EraHandler.EraYear(out era, rd);
392 /// Overridden. Tells whether the given day
395 /// <param name="year">An integer that specifies the year in the
398 /// <param name="month">An integer that specifies the month.
400 /// <param name="day">An integer that specifies the day.
402 /// <param name="era">An integer that specifies the era.
404 /// <returns>A boolean that tells whether the given day is a leap
407 /// <exception cref="T:System.ArgumentOutOfRangeException">
408 /// The exception is thrown, if the year, month, day, or era is not
411 public override bool IsLeapDay(int year, int month, int day, int era)
413 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
414 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
418 /// Overridden. Tells whether the given month
421 /// <param name="year">An integer that specifies the year in the
424 /// <param name="month">An integer that specifies the month.
426 /// <param name="era">An integer that specifies the era.
428 /// <returns>A boolean that tells whether the given month is a leap
431 /// <exception cref="T:System.ArgumentOutOfRangeException">
432 /// The exception is thrown, if the year, month, or era is not
435 public override bool IsLeapMonth(int year, int month, int era) {
436 M_CheckYMEG(year, month, ref era);
441 /// Overridden. Tells whether the given year
444 /// <param name="year">An integer that specifies the year in the
447 /// <param name="era">An integer that specifies the era.
449 /// <returns>A boolean that tells whether the given year is a leap
452 /// <exception cref="T:System.ArgumentOutOfRangeException">
453 /// The exception is thrown, if the year or era is not
456 public override bool IsLeapYear(int year, int era) {
457 int gregorianYear = M_CheckYEG(year, ref era);
458 return CCGregorianCalendar.is_leap_year(gregorianYear);
462 /// Overridden. Creates the
463 /// <see cref="T:System.DateTime"/> from the parameters.
465 /// <param name="year">An integer that gives the year in the
466 /// <paramref name="era"/>.
468 /// <param name="month">An integer that specifies the month.
470 /// <param name="day">An integer that specifies the day.
472 /// <param name="hour">An integer that specifies the hour.
474 /// <param name="minute">An integer that specifies the minute.
476 /// <param name="second">An integer that gives the second.
478 /// <param name="milliseconds">An integer that gives the
481 /// <param name="era">An integer that specifies the era.
484 /// <see cref="T:system.DateTime"/> representig the date and time.
486 /// <exception cref="T:System.ArgumentOutOfRangeException">
487 /// The exception is thrown, if at least one of the parameters
490 public override DateTime ToDateTime(int year, int month, int day,
491 int hour, int minute, int second, int milliseconds,
494 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
495 M_CheckHMSM(hour, minute, second, milliseconds);
496 return CCGregorianCalendar.ToDateTime(
497 gregorianYear, month, day,
498 hour, minute, second, milliseconds);
501 public override int ToFourDigitYear(int year)
503 return base.ToFourDigitYear (year);
508 public override CalendarAlgorithmType AlgorithmType {
510 return CalendarAlgorithmType.SolarCalendar;
514 static DateTime ThaiMin = new DateTime (1, 1, 1, 0, 0, 0);
515 static DateTime ThaiMax = new DateTime (9999, 12, 31, 11, 59, 59);
518 public override DateTime MinSupportedDateTime {
525 public override DateTime MaxSupportedDateTime {
531 } // class ThaiBuddhistCalendar
533 } // namespace System.Globalization