1 // System.Globalization.TaiwanCalendar.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 Japanese calendar. It differs from the Gregorian calendar
36 /// only in the years.
39 /// <para>The Japanese calendar support a single era starting at January 1,
41 /// <para>The implementation uses the
42 /// <see cref="N:CalendricalCalculations"/> namespace.
45 [System.Runtime.InteropServices.ComVisible(true)]
47 [MonoLimitation ("Serialization format not compatible with.NET")]
48 public class TaiwanCalendar : Calendar {
50 /// Static protected field storing the
51 /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
53 internal static readonly CCGregorianEraHandler M_EraHandler;
56 /// Static constructor, who creates and initializes
57 /// <see cref="F:M_EraHandler"/>.
59 static TaiwanCalendar() {
60 M_EraHandler = new CCGregorianEraHandler();
61 M_EraHandler.appendEra(1,
62 CCGregorianCalendar.fixed_from_dmy(1, 1, 1912));
66 /// Default constructor.
68 public TaiwanCalendar() {
69 M_AbbrEraNames = new string[] {"T.C.E."};
70 M_EraNames = new string[] {"Taiwan current era"};
73 /// <value>Overridden. Gives the eras supported by the
74 /// calendar as an array of integers.
76 public override int[] Eras {
78 return (int[])M_EraHandler.Eras.Clone();
82 public override int TwoDigitYearMax
85 return twoDigitYearMax;
89 M_ArgumentInRange ("value", value, 100, M_MaxYear);
91 twoDigitYearMax = value;
96 /// A protected member checking a
97 /// <see cref="T:System.DateTime"/> value.
99 /// <param name="time">The
100 /// <see cref="T:System.DateTime"/>
103 /// <exception cref="T:System.ArgumentOutOfRangeException">
104 /// The exception is thrown if the
105 /// <see cref="T:System.DateTime"/> parameter is outside all
108 internal void M_CheckDateTime(DateTime time) {
109 M_EraHandler.CheckDateTime(time);
113 /// A protected method checking the era number.
115 /// <param name="era">The era number as reference. It is set
116 /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
117 /// <exception name="T:System.ArgumentException">
118 /// The exception is thrown if the era is not supported by the class.
120 internal void M_CheckEra(ref int era) {
121 if (era == CurrentEra)
123 if (!M_EraHandler.ValidEra(era))
124 throw new ArgumentException("Era value was not valid.");
128 /// A protected method checking calendar year and the era number.
130 /// <param name="year">An integer representing the calendar year.
132 /// <param name="era">The era number as reference.</param>
133 /// <exception name="T:System.ArgumentException">
134 /// The exception is thrown if the era is not supported by the class.
136 /// <exception cref="T:System.ArgumentOutOfRangeException">
137 /// The exception is thrown if the calendar year is outside of
138 /// the supported range.
140 internal int M_CheckYEG(int year, ref int era) {
142 return M_EraHandler.GregorianYear(year, era);
146 /// Checks whether the year is the era is valid, if era = CurrentEra
147 /// the right value is set.
149 /// <param name="year">The year to check.</param>
150 /// <param name="era">The era to check.</Param>
151 /// <exception cref="T:ArgumentOutOfRangeException">
152 /// The exception will be thrown, if the year is not valid.
154 internal override void M_CheckYE(int year, ref int era) {
155 M_CheckYEG(year, ref era);
159 /// A protected method checking the calendar year, month, and
162 /// <param name="year">An integer representing the calendar year.
164 /// <param name="month">An integer giving the calendar month.
166 /// <param name="era">The era number as reference.</param>
167 /// <exception name="T:System.ArgumentException">
168 /// The exception is thrown if the era is not supported by the class.
170 /// <exception cref="T:System.ArgumentOutOfRangeException">
171 /// The exception is thrown if the calendar year or month is
172 /// outside of the supported range.
174 internal int M_CheckYMEG(int year, int month, ref int era) {
175 int gregorianYear = M_CheckYEG(year, ref era);
176 if (month < 1 || month > 12)
177 throw new ArgumentOutOfRangeException("month",
178 "Month must be between one and twelve.");
179 return gregorianYear;
183 /// A protected method checking the calendar day, month, and year
184 /// and the era number.
186 /// <param name="year">An integer representing the calendar year.
188 /// <param name="month">An integer giving the calendar month.
190 /// <param name="day">An integer giving the calendar day.
192 /// <param name="era">The era number as reference.</param>
193 /// <exception name="T:System.ArgumentException">
194 /// The exception is thrown if the era is not supported by the class.
196 /// <exception cref="T:System.ArgumentOutOfRangeException">
197 /// The exception is thrown if the calendar year, month, or day is
198 /// outside of the supported range.
200 internal int M_CheckYMDEG(int year, int month, int day, ref int era)
202 int gregorianYear = M_CheckYMEG(year, month, ref era);
203 M_ArgumentInRange("day", day, 1,
204 GetDaysInMonth(year, month, era));
205 return gregorianYear;
209 /// Overrideden. Adds months to a given date.
211 /// <param name="time">The
212 /// <see cref="T:System.DateTime"/> to which to add
215 /// <param name="months">The number of months to add.</param>
216 /// <returns>A new <see cref="T:System.DateTime"/> value, that
217 /// results from adding <paramref name="months"/> to the specified
218 /// DateTime.</returns>
219 /// <exception cref="T:System.ArgumentOutOfRangeException">
220 /// The exception is thrown if
221 /// <see cref="T:System.DateTime"/> return value is outside all
224 public override DateTime AddMonths(DateTime time, int months) {
225 DateTime t = CCGregorianCalendar.AddMonths(time, months);
231 /// Overridden. Adds years to a given date.
233 /// <param name="time">The
234 /// <see cref="T:System.DateTime"/> to which to add
237 /// <param name="years">The number of years to add.</param>
238 /// <returns>A new <see cref="T:System.DateTime"/> value, that
239 /// results from adding <paramref name="years"/> to the specified
240 /// DateTime.</returns>
241 /// <exception cref="T:System.ArgumentOutOfRangeException">
242 /// The exception is thrown if
243 /// <see cref="T:System.DateTime"/> return value is outside all
246 public override DateTime AddYears(DateTime time, int years) {
247 DateTime t = CCGregorianCalendar.AddYears(time, years);
253 /// Overriden. Gets the day of the month from
254 /// <paramref name="time"/>.
256 /// <param name="time">The
257 /// <see cref="T:System.DateTime"/> that specifies a
260 /// <returns>An integer giving the day of months, starting with 1.
262 /// <exception cref="T:System.ArgumentOutOfRangeException">
263 /// The exception is thrown if the
264 /// <see cref="T:System.DateTime"/> parameter is outside all
267 public override int GetDayOfMonth(DateTime time) {
268 M_CheckDateTime(time);
269 return CCGregorianCalendar.GetDayOfMonth(time);
273 /// Overriden. Gets the day of the week from the specified date.
275 /// <param name="time">The
276 /// <see cref="T:System.DateTime"/> that specifies a
279 /// <returns>An integer giving the day of months, starting with 1.
281 /// <exception cref="T:System.ArgumentOutOfRangeException">
282 /// The exception is thrown if the
283 /// <see cref="T:System.DateTime"/> parameter is outside all
286 public override DayOfWeek GetDayOfWeek(DateTime time) {
287 M_CheckDateTime(time);
288 int rd = CCFixed.FromDateTime(time);
289 return (DayOfWeek)CCFixed.day_of_week(rd);
293 /// Overridden. Gives the number of the day in the year.
295 /// <param name="time">The
296 /// <see cref="T:System.DateTime"/> that specifies a
299 /// <returns>An integer representing the day of the year,
300 /// starting with 1.</returns>
301 /// <exception cref="T:System.ArgumentOutOfRangeException">
302 /// The exception is thrown if the
303 /// <see cref="T:System.DateTime"/> parameter is outside all
306 public override int GetDayOfYear(DateTime time) {
307 M_CheckDateTime(time);
308 return CCGregorianCalendar.GetDayOfYear(time);
312 /// Overridden. Gives the number of days in the specified month
313 /// of the given year and era.
315 /// <param name="year">An integer that gives the year.
317 /// <param name="month">An integer that gives the month, starting
319 /// <param name="era">An integer that gives the era of the specified
321 /// <returns>An integer that gives the number of days of the
322 /// specified month.</returns>
323 /// <exception cref="T:System.ArgumentOutOfRangeException">
324 /// The exception is thrown, if <paramref name="month"/>,
325 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
326 /// the allowed range.
328 public override int GetDaysInMonth(int year, int month, int era) {
329 int gregorianYear = M_CheckYMEG(year, month, ref era);
330 return CCGregorianCalendar.GetDaysInMonth(gregorianYear, month);
334 /// Overridden. Gives the number of days of the specified
335 /// year of the given era.
337 /// <param name="year">An integer that specifies the year.
339 /// <param name="era">An ineger that specifies the era.
341 /// <returns>An integer that gives the number of days of the
342 /// specified year.</returns>
343 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
344 /// The exception is thrown, if
345 /// <paramref name="year"/> or <paramref name="era"/> are outside the
348 public override int GetDaysInYear(int year, int era) {
349 int gregorianYear = M_CheckYEG(year, ref era);
350 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
355 /// Overridden. Gives the era of the specified date.
357 /// <param name="time">The
358 /// <see cref="T:System.DateTime"/> that specifies a
361 /// <returns>An integer representing the era of the calendar.
363 /// <exception cref="T:System.ArgumentOutOfRangeException">
364 /// The exception is thrown if the
365 /// <see cref="T:System.DateTime"/> parameter is outside all
368 public override int GetEra(DateTime time) {
369 // M_CheckDateTime not needed, because EraYear does the
371 int rd = CCFixed.FromDateTime(time);
373 M_EraHandler.EraYear(out era, rd);
378 public override int GetLeapMonth (int year, int era)
384 /// Overridden. Gives the number of the month of the specified
387 /// <param name="time">The
388 /// <see cref="T:System.DateTime"/> that specifies a
391 /// <returns>An integer representing the month,
392 /// starting with 1.</returns>
393 /// <exception cref="T:System.ArgumentOutOfRangeException">
394 /// The exception is thrown if the
395 /// <see cref="T:System.DateTime"/> parameter is outside all
398 public override int GetMonth(DateTime time) {
399 M_CheckDateTime(time);
400 return CCGregorianCalendar.GetMonth(time);
404 /// Overridden. Gives the number of months in the specified year
407 /// <param name="year">An integer that specifies the year.
409 /// <param name="era">An integer that specifies the era.
411 /// <returns>An integer that gives the number of the months in the
412 /// specified year.</returns>
413 /// <exception cref="T:System.ArgumentOutOfRangeException">
414 /// The exception is thrown, if the year or the era are not valid.
416 public override int GetMonthsInYear(int year, int era) {
417 M_CheckYEG(year, ref era);
422 public override int GetWeekOfYear (DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
424 return base.GetWeekOfYear (time, rule, firstDayOfWeek);
428 /// Overridden. Gives the number of the year of the specified
431 /// <param name="time">The
432 /// <see cref="T:System.DateTime"/> that specifies a
435 /// <returns>An integer representing the year,
436 /// starting with 1.</returns>
437 /// <exception cref="T:System.ArgumentOutOfRangeException">
438 /// The exception is thrown if the
439 /// <see cref="T:System.DateTime"/> parameter is outside all
442 public override int GetYear(DateTime time) {
443 // M_CheckDateTime not needed, because EraYeat does the
445 int rd = CCFixed.FromDateTime(time);
447 return M_EraHandler.EraYear(out era, rd);
451 /// Overridden. Tells whether the given day
454 /// <param name="year">An integer that specifies the year in the
457 /// <param name="month">An integer that specifies the month.
459 /// <param name="day">An integer that specifies the day.
461 /// <param name="era">An integer that specifies the era.
463 /// <returns>A boolean that tells whether the given day is a leap
466 /// <exception cref="T:System.ArgumentOutOfRangeException">
467 /// The exception is thrown, if the year, month, day, or era is not
470 public override bool IsLeapDay(int year, int month, int day, int era)
472 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
473 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
477 /// Overridden. Tells whether the given month
480 /// <param name="year">An integer that specifies the year in the
483 /// <param name="month">An integer that specifies the month.
485 /// <param name="era">An integer that specifies the era.
487 /// <returns>A boolean that tells whether the given month is a leap
490 /// <exception cref="T:System.ArgumentOutOfRangeException">
491 /// The exception is thrown, if the year, month, or era is not
494 public override bool IsLeapMonth(int year, int month, int era) {
495 M_CheckYMEG(year, month, ref era);
500 /// Overridden. Tells whether the given year
503 /// <param name="year">An integer that specifies the year in the
506 /// <param name="era">An integer that specifies the era.
508 /// <returns>A boolean that tells whether the given year is a leap
511 /// <exception cref="T:System.ArgumentOutOfRangeException">
512 /// The exception is thrown, if the year or era is not
515 public override bool IsLeapYear(int year, int era) {
516 int gregorianYear = M_CheckYEG(year, ref era);
517 return CCGregorianCalendar.is_leap_year(gregorianYear);
521 /// Overridden. Creates the
522 /// <see cref="T:System.DateTime"/> from the parameters.
524 /// <param name="year">An integer that gives the year in the
525 /// <paramref name="era"/>.
527 /// <param name="month">An integer that specifies the month.
529 /// <param name="day">An integer that specifies the day.
531 /// <param name="hour">An integer that specifies the hour.
533 /// <param name="minute">An integer that specifies the minute.
535 /// <param name="second">An integer that gives the second.
537 /// <param name="milliseconds">An integer that gives the
540 /// <param name="era">An integer that specifies the era.
543 /// <see cref="T:system.DateTime"/> representig the date and time.
545 /// <exception cref="T:System.ArgumentOutOfRangeException">
546 /// The exception is thrown, if at least one of the parameters
549 public override DateTime ToDateTime(int year, int month, int day,
550 int hour, int minute, int second, int millisecond,
553 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
554 M_CheckHMSM(hour, minute, second, millisecond);
555 return CCGregorianCalendar.ToDateTime(
556 gregorianYear, month, day,
557 hour, minute, second, millisecond);
561 /// This functions returns simply the year for the Taiwan calendar.
563 /// <param name="year">An integer that gives the year.
565 /// <returns>The same argument as the year.
567 /// <exception cref="T:System.ArgumentOutOfRangeException">
568 /// The exception is thrown if the year is negative or the resulting
571 public override int ToFourDigitYear(int year) {
573 throw new ArgumentOutOfRangeException(
574 "year", "Non-negative number required.");
575 int era = CurrentEra;
576 M_CheckYE(year, ref era);
582 public override CalendarAlgorithmType AlgorithmType {
584 return CalendarAlgorithmType.SolarCalendar;
589 static DateTime TaiwanMin = new DateTime (1912, 1, 1, 0, 0, 0);
590 static DateTime TaiwanMax = new DateTime (9999, 12, 31, 11, 59, 59);
593 public override DateTime MinSupportedDateTime {
600 public override DateTime MaxSupportedDateTime {
605 } // class TaiwanCalendar
607 } // namespace System.Globalization