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 {
34 /// This is the Japanese calendar. It differs from the Gregorian calendar
35 /// only in the years.
38 /// <para>The Japanese calendar support a single era starting at January 1,
40 /// <para>The implementation uses the
41 /// <see cref="N:CalendricalCalculations"/> namespace.
45 [MonoTODO ("Fix serialization compatibility with MS.NET")]
46 public class TaiwanCalendar : Calendar {
48 /// Static protected field storing the
49 /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
51 internal static readonly CCGregorianEraHandler M_EraHandler;
54 /// Static constructor, who creates and initializes
55 /// <see cref="F:M_EraHandler"/>.
57 static TaiwanCalendar() {
58 M_EraHandler = new CCGregorianEraHandler();
59 M_EraHandler.appendEra(1,
60 CCGregorianCalendar.fixed_from_dmy(1, 1, 1912));
64 /// Default constructor.
66 public TaiwanCalendar() {
67 M_AbbrEraNames = new string[] {"T.C.E."};
68 M_EraNames = new string[] {"Taiwan current era"};
71 /// <value>Overridden. Gives the eras supported by the
72 /// calendar as an array of integers.
74 public override int[] Eras {
76 return (int[])M_EraHandler.Eras.Clone();
80 int twoDigitYearMax = 99;
82 public override int TwoDigitYearMax
85 return twoDigitYearMax;
88 M_ArgumentInRange ("value", value, 100, M_MaxYear);
90 twoDigitYearMax = value;
95 /// A protected member checking a
96 /// <see cref="T:System.DateTime"/> value.
98 /// <param name="time">The
99 /// <see cref="T:System.DateTime"/>
102 /// <exception cref="T:System.ArgumentOutOfRangeException">
103 /// The exception is thrown if the
104 /// <see cref="T:System.DateTime"/> parameter is outside all
107 internal void M_CheckDateTime(DateTime time) {
108 M_EraHandler.CheckDateTime(time);
112 /// A protected method checking the era number.
114 /// <param name="era">The era number as reference. It is set
115 /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
116 /// <exception name="T:System.ArgumentException">
117 /// The exception is thrown if the era is not supported by the class.
119 internal void M_CheckEra(ref int era) {
120 if (era == CurrentEra)
122 if (!M_EraHandler.ValidEra(era))
123 throw new ArgumentException("Era value was not valid.");
127 /// A protected method checking calendar year and the era number.
129 /// <param name="year">An integer representing the calendar year.
131 /// <param name="era">The era number as reference.</param>
132 /// <exception name="T:System.ArgumentException">
133 /// The exception is thrown if the era is not supported by the class.
135 /// <exception cref="T:System.ArgumentOutOfRangeException">
136 /// The exception is thrown if the calendar year is outside of
137 /// the supported range.
139 internal int M_CheckYEG(int year, ref int era) {
141 return M_EraHandler.GregorianYear(year, era);
145 /// Checks whether the year is the era is valid, if era = CurrentEra
146 /// the right value is set.
148 /// <param name="year">The year to check.</param>
149 /// <param name="era">The era to check.</Param>
150 /// <exception cref="T:ArgumentOutOfRangeException">
151 /// The exception will be thrown, if the year is not valid.
153 internal override void M_CheckYE(int year, ref int era) {
154 M_CheckYEG(year, ref era);
158 /// A protected method checking the calendar year, month, and
161 /// <param name="year">An integer representing the calendar year.
163 /// <param name="month">An integer giving the calendar month.
165 /// <param name="era">The era number as reference.</param>
166 /// <exception name="T:System.ArgumentException">
167 /// The exception is thrown if the era is not supported by the class.
169 /// <exception cref="T:System.ArgumentOutOfRangeException">
170 /// The exception is thrown if the calendar year or month is
171 /// outside of the supported range.
173 internal int M_CheckYMEG(int year, int month, ref int era) {
174 int gregorianYear = M_CheckYEG(year, ref era);
175 if (month < 1 || month > 12)
176 throw new ArgumentOutOfRangeException("month",
177 "Month must be between one and twelve.");
178 return gregorianYear;
182 /// A protected method checking the calendar day, month, and year
183 /// and the era number.
185 /// <param name="year">An integer representing the calendar year.
187 /// <param name="month">An integer giving the calendar month.
189 /// <param name="day">An integer giving the calendar day.
191 /// <param name="era">The era number as reference.</param>
192 /// <exception name="T:System.ArgumentException">
193 /// The exception is thrown if the era is not supported by the class.
195 /// <exception cref="T:System.ArgumentOutOfRangeException">
196 /// The exception is thrown if the calendar year, month, or day is
197 /// outside of the supported range.
199 internal int M_CheckYMDEG(int year, int month, int day, ref int era)
201 int gregorianYear = M_CheckYMEG(year, month, ref era);
202 M_ArgumentInRange("day", day, 1,
203 GetDaysInMonth(year, month, era));
204 return gregorianYear;
209 // Ifdefed out because this is not on the .NET Framework
212 /// Overridden. Adds days to a given date.
214 /// <param name="time">The
215 /// <see cref="T:System.DateTime"/> to which to add
218 /// <param name="days">The number of days to add.</param>
219 /// <returns>A new <see cref="T:System.DateTime"/> value, that
220 /// results from adding <paramref name="days"/> to the specified
221 /// DateTime.</returns>
222 /// <exception cref="T:System.ArgumentOutOfRangeException">
223 /// The exception is thrown if the
224 /// <see cref="T:System.DateTime"/> return value is outside all
227 public override DateTime AddDays(DateTime time, int days) {
228 DateTime t = base.AddDays(time, days);
234 /// Overridden. Adds hours to a given date.
236 /// <param name="time">The
237 /// <see cref="T:System.DateTime"/> to which to add
240 /// <param name="hours">The number of hours to add.</param>
241 /// <returns>A new <see cref="T:System.DateTime"/> value, that
242 /// results from adding <paramref name="hours"/> to the specified
243 /// DateTime.</returns>
244 /// <exception cref="T:System.ArgumentOutOfRangeException">
245 /// The exception is thrown if the
246 /// <see cref="T:System.DateTime"/> return value is outside all
249 public override DateTime AddHours(DateTime time, int hours) {
250 DateTime t = base.AddHours(time, hours);
256 /// Overridden. Adds milliseconds to a given date.
258 /// <param name="time">The
259 /// <see cref="T:System.DateTime"/> to which to add
262 /// <param name="milliseconds">The number of milliseconds given as
263 /// double to add. Keep in mind the 100 nanosecond resolution of
264 /// <see cref="T:System.DateTime"/>.
266 /// <returns>A new <see cref="T:System.DateTime"/> value, that
267 /// results from adding <paramref name="milliseconds"/> to the specified
268 /// DateTime.</returns>
269 /// <exception cref="T:System.ArgumentOutOfRangeException">
270 /// The exception is thrown if the
271 /// <see cref="T:System.DateTime"/> return value is outside all
274 public override DateTime AddMilliseconds(DateTime time,
277 DateTime t = base.AddMilliseconds(time, milliseconds);
283 /// Overridden. Adds minutes to a given date.
285 /// <param name="time">The
286 /// <see cref="T:System.DateTime"/> to which to add
289 /// <param name="minutes">The number of minutes to add.</param>
290 /// <returns>A new <see cref="T:System.DateTime"/> value, that
291 /// results from adding <paramref name="minutes"/> to the specified
292 /// DateTime.</returns>
293 /// <exception cref="T:System.ArgumentOutOfRangeException">
294 /// The exception is thrown if the
295 /// <see cref="T:System.DateTime"/> return value is outside all
298 public override DateTime AddMinutes(DateTime time, int minutes) {
299 DateTime t = base.AddMinutes(time, minutes);
305 /// Overridden. Adds seconds to a given date.
307 /// <param name="time">The
308 /// <see cref="T:System.DateTime"/> to which to add
311 /// <param name="seconds">The number of seconds to add.</param>
312 /// <returns>A new <see cref="T:System.DateTime"/> value, that
313 /// results from adding <paramref name="seconds"/> to the specified
314 /// DateTime.</returns>
315 /// <exception cref="T:System.ArgumentOutOfRangeException">
316 /// The exception is thrown if the
317 /// <see cref="T:System.DateTime"/> return value is outside all
320 public override DateTime AddSeconds(DateTime time, int seconds) {
321 DateTime t = base.AddSeconds(time, seconds);
328 /// Overridden. Adds weeks to a given date.
330 /// <param name="time">The
331 /// <see cref="T:System.DateTime"/> to which to add
334 /// <param name="weeks">The number of weeks to add.</param>
335 /// <returns>A new <see cref="T:System.DateTime"/> value, that
336 /// results from adding <paramref name="weeks"/> to the specified
337 /// DateTime.</returns>
338 /// <exception cref="T:System.ArgumentOutOfRangeException">
339 /// The exception is thrown if the
340 /// <see cref="T:System.DateTime"/> return value is outside all
343 public override DateTime AddWeeks(DateTime time, int weeks) {
344 DateTime t = base.AddWeeks(time, weeks);
350 /// Overridden. Gives the hour of the specified time.
352 /// <param name="time">The
353 /// <see cref="T:System.DateTime"/> that specifies the
356 /// <returns>An integer that gives the hour of the specified time,
357 /// starting with 0.</returns>
358 /// <exception cref="T:System.ArgumentOutOfRangeException">
359 /// The exception is thrown if the
360 /// <see cref="T:System.DateTime"/> parameter is outside all
363 public override int GetHour(DateTime time) {
364 M_CheckDateTime(time);
365 return base.GetHour(time);
369 /// Overridden. Gives the milliseconds in the current second
370 /// of the specified time.
372 /// <param name="time">The
373 /// <see cref="T:System.DateTime"/> that specifies the
376 /// <returns>An integer that gives the milliseconds in the seconds
377 /// of the specified time, starting with 0.</returns>
378 /// <exception cref="T:System.ArgumentOutOfRangeException">
379 /// The exception is thrown if the
380 /// <see cref="T:System.DateTime"/> parameter is outside all
383 public override double GetMilliseconds(DateTime time) {
384 M_CheckDateTime(time);
385 return base.GetMilliseconds(time);
389 /// Overridden. Gives the minute of the specified time.
391 /// <param name="time">The
392 /// <see cref="T:System.DateTime"/> that specifies the
395 /// <returns>An integer that gives the minute of the specified time,
396 /// starting with 0.</returns>
397 /// <exception cref="T:System.ArgumentOutOfRangeException">
398 /// The exception is thrown if the
399 /// <see cref="T:System.DateTime"/> parameter is outside all
402 public override int GetMinute(DateTime time) {
403 M_CheckDateTime(time);
404 return base.GetMinute(time);
408 /// Overridden. Gives the second of the specified time.
410 /// <param name="time">The
411 /// <see cref="T:System.DateTime"/> that specifies the
414 /// <returns>An integer that gives the second of the specified time,
415 /// starting with 0.</returns>
416 /// <exception cref="T:System.ArgumentOutOfRangeException">
417 /// The exception is thrown if the
418 /// <see cref="T:System.DateTime"/> parameter is outside all
421 public override int GetSecond(DateTime time) {
422 M_CheckDateTime(time);
423 return base.GetMinute(time);
428 /// Overrideden. Adds months to a given date.
430 /// <param name="time">The
431 /// <see cref="T:System.DateTime"/> to which to add
434 /// <param name="months">The number of months to add.</param>
435 /// <returns>A new <see cref="T:System.DateTime"/> value, that
436 /// results from adding <paramref name="months"/> to the specified
437 /// DateTime.</returns>
438 /// <exception cref="T:System.ArgumentOutOfRangeException">
439 /// The exception is thrown if
440 /// <see cref="T:System.DateTime"/> return value is outside all
443 public override DateTime AddMonths(DateTime time, int months) {
444 DateTime t = CCGregorianCalendar.AddMonths(time, months);
450 /// Overridden. Adds years to a given date.
452 /// <param name="time">The
453 /// <see cref="T:System.DateTime"/> to which to add
456 /// <param name="years">The number of years to add.</param>
457 /// <returns>A new <see cref="T:System.DateTime"/> value, that
458 /// results from adding <paramref name="years"/> to the specified
459 /// DateTime.</returns>
460 /// <exception cref="T:System.ArgumentOutOfRangeException">
461 /// The exception is thrown if
462 /// <see cref="T:System.DateTime"/> return value is outside all
465 public override DateTime AddYears(DateTime time, int years) {
466 DateTime t = CCGregorianCalendar.AddYears(time, years);
472 /// Overriden. Gets the day of the month from
473 /// <paramref name="time"/>.
475 /// <param name="time">The
476 /// <see cref="T:System.DateTime"/> that specifies a
479 /// <returns>An integer giving the day of months, starting with 1.
481 /// <exception cref="T:System.ArgumentOutOfRangeException">
482 /// The exception is thrown if the
483 /// <see cref="T:System.DateTime"/> parameter is outside all
486 public override int GetDayOfMonth(DateTime time) {
487 M_CheckDateTime(time);
488 return CCGregorianCalendar.GetDayOfMonth(time);
492 /// Overriden. Gets the day of the week from the specified date.
494 /// <param name="time">The
495 /// <see cref="T:System.DateTime"/> that specifies a
498 /// <returns>An integer giving the day of months, starting with 1.
500 /// <exception cref="T:System.ArgumentOutOfRangeException">
501 /// The exception is thrown if the
502 /// <see cref="T:System.DateTime"/> parameter is outside all
505 public override DayOfWeek GetDayOfWeek(DateTime time) {
506 M_CheckDateTime(time);
507 int rd = CCFixed.FromDateTime(time);
508 return (DayOfWeek)CCFixed.day_of_week(rd);
512 /// Overridden. Gives the number of the day in the year.
514 /// <param name="time">The
515 /// <see cref="T:System.DateTime"/> that specifies a
518 /// <returns>An integer representing the day of the year,
519 /// starting with 1.</returns>
520 /// <exception cref="T:System.ArgumentOutOfRangeException">
521 /// The exception is thrown if the
522 /// <see cref="T:System.DateTime"/> parameter is outside all
525 public override int GetDayOfYear(DateTime time) {
526 M_CheckDateTime(time);
527 return CCGregorianCalendar.GetDayOfYear(time);
531 /// Overridden. Gives the number of days in the specified month
532 /// of the given year and era.
534 /// <param name="year">An integer that gives the year.
536 /// <param name="month">An integer that gives the month, starting
538 /// <param name="era">An integer that gives the era of the specified
540 /// <returns>An integer that gives the number of days of the
541 /// specified month.</returns>
542 /// <exception cref="T:System.ArgumentOutOfRangeException">
543 /// The exception is thrown, if <paramref name="month"/>,
544 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
545 /// the allowed range.
547 public override int GetDaysInMonth(int year, int month, int era) {
548 int gregorianYear = M_CheckYMEG(year, month, ref era);
549 return CCGregorianCalendar.GetDaysInMonth(gregorianYear, month);
553 /// Overridden. Gives the number of days of the specified
554 /// year of the given era.
556 /// <param name="year">An integer that specifies the year.
558 /// <param name="era">An ineger that specifies the era.
560 /// <returns>An integer that gives the number of days of the
561 /// specified year.</returns>
562 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
563 /// The exception is thrown, if
564 /// <paramref name="year"/> or <paramref name="era"/> are outside the
567 public override int GetDaysInYear(int year, int era) {
568 int gregorianYear = M_CheckYEG(year, ref era);
569 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
574 /// Overridden. Gives the era of the specified date.
576 /// <param name="time">The
577 /// <see cref="T:System.DateTime"/> that specifies a
580 /// <returns>An integer representing the era of the calendar.
582 /// <exception cref="T:System.ArgumentOutOfRangeException">
583 /// The exception is thrown if the
584 /// <see cref="T:System.DateTime"/> parameter is outside all
587 public override int GetEra(DateTime time) {
588 // M_CheckDateTime not needed, because EraYear does the
590 int rd = CCFixed.FromDateTime(time);
592 M_EraHandler.EraYear(out era, rd);
597 /// Overridden. Gives the number of the month of the specified
600 /// <param name="time">The
601 /// <see cref="T:System.DateTime"/> that specifies a
604 /// <returns>An integer representing the month,
605 /// starting with 1.</returns>
606 /// <exception cref="T:System.ArgumentOutOfRangeException">
607 /// The exception is thrown if the
608 /// <see cref="T:System.DateTime"/> parameter is outside all
611 public override int GetMonth(DateTime time) {
612 M_CheckDateTime(time);
613 return CCGregorianCalendar.GetMonth(time);
617 /// Overridden. Gives the number of months in the specified year
620 /// <param name="year">An integer that specifies the year.
622 /// <param name="era">An integer that specifies the era.
624 /// <returns>An integer that gives the number of the months in the
625 /// specified year.</returns>
626 /// <exception cref="T:System.ArgumentOutOfRangeException">
627 /// The exception is thrown, if the year or the era are not valid.
629 public override int GetMonthsInYear(int year, int era) {
630 M_CheckYEG(year, ref era);
635 /// Overridden. Gives the number of the year of the specified
638 /// <param name="time">The
639 /// <see cref="T:System.DateTime"/> that specifies a
642 /// <returns>An integer representing the year,
643 /// starting with 1.</returns>
644 /// <exception cref="T:System.ArgumentOutOfRangeException">
645 /// The exception is thrown if the
646 /// <see cref="T:System.DateTime"/> parameter is outside all
649 public override int GetYear(DateTime time) {
650 // M_CheckDateTime not needed, because EraYeat does the
652 int rd = CCFixed.FromDateTime(time);
654 return M_EraHandler.EraYear(out era, rd);
658 /// Overridden. Tells whether the given day
661 /// <param name="year">An integer that specifies the year in the
664 /// <param name="month">An integer that specifies the month.
666 /// <param name="day">An integer that specifies the day.
668 /// <param name="era">An integer that specifies the era.
670 /// <returns>A boolean that tells whether the given day is a leap
673 /// <exception cref="T:System.ArgumentOutOfRangeException">
674 /// The exception is thrown, if the year, month, day, or era is not
677 public override bool IsLeapDay(int year, int month, int day, int era)
679 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
680 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
684 /// Overridden. Tells whether the given month
687 /// <param name="year">An integer that specifies the year in the
690 /// <param name="month">An integer that specifies the month.
692 /// <param name="era">An integer that specifies the era.
694 /// <returns>A boolean that tells whether the given month is a leap
697 /// <exception cref="T:System.ArgumentOutOfRangeException">
698 /// The exception is thrown, if the year, month, or era is not
701 public override bool IsLeapMonth(int year, int month, int era) {
702 M_CheckYMEG(year, month, ref era);
707 /// Overridden. Tells whether the given year
710 /// <param name="year">An integer that specifies the year in the
713 /// <param name="era">An integer that specifies the era.
715 /// <returns>A boolean that tells whether the given year is a leap
718 /// <exception cref="T:System.ArgumentOutOfRangeException">
719 /// The exception is thrown, if the year or era is not
722 public override bool IsLeapYear(int year, int era) {
723 int gregorianYear = M_CheckYEG(year, ref era);
724 return CCGregorianCalendar.is_leap_year(gregorianYear);
728 /// Overridden. Creates the
729 /// <see cref="T:System.DateTime"/> from the parameters.
731 /// <param name="year">An integer that gives the year in the
732 /// <paramref name="era"/>.
734 /// <param name="month">An integer that specifies the month.
736 /// <param name="day">An integer that specifies the day.
738 /// <param name="hour">An integer that specifies the hour.
740 /// <param name="minute">An integer that specifies the minute.
742 /// <param name="second">An integer that gives the second.
744 /// <param name="milliseconds">An integer that gives the
747 /// <param name="era">An integer that specifies the era.
750 /// <see cref="T:system.DateTime"/> representig the date and time.
752 /// <exception cref="T:System.ArgumentOutOfRangeException">
753 /// The exception is thrown, if at least one of the parameters
756 public override DateTime ToDateTime(int year, int month, int day,
757 int hour, int minute, int second, int milliseconds,
760 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
761 M_CheckHMSM(hour, minute, second, milliseconds);
762 return CCGregorianCalendar.ToDateTime(
763 gregorianYear, month, day,
764 hour, minute, second, milliseconds);
768 /// This functions returns simply the year for the Taiwan calendar.
770 /// <param name="year">An integer that gives the year.
772 /// <returns>The same argument as the year.
774 /// <exception cref="T:System.ArgumentOutOfRangeException">
775 /// The exception is thrown if the year is negative or the resulting
778 public override int ToFourDigitYear(int year) {
780 throw new ArgumentOutOfRangeException(
781 "year", "Non-negative number required.");
782 int era = CurrentEra;
783 M_CheckYE(year, ref era);
786 } // class TaiwanCalendar
788 } // namespace System.Globalization