3 // (C) Ulrich Kunitz 2002
6 namespace System.Globalization {
11 /// This is the Japanese calendar. It differs from the Gregorian calendar
12 /// only in the years.
15 /// <para>The Japanese calendar support four eras.</para>
16 /// <list type="table">
18 /// <term>era number</term>
19 /// <term>Gregorian start date</term>
20 /// <term>Gregorian end date</term>
24 /// <term>September 8, 1868</term>
25 /// <term>July 29, 1912</term>
29 /// <term>July 30, 1912</term>
30 /// <term>December 24, 1926</term>
34 /// <term>December 25, 1926</term>
35 /// <term>January 7, 1989</term>
39 /// <term>January 8, 1989</term>
40 /// <term>present</term>
43 /// <para>The implementation uses the
44 /// <see cref="N:CalendricalCalculations"/> namespace.
48 public class JapaneseCalendar : 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 JapaneseCalendar() {
60 M_EraHandler = new CCGregorianEraHandler();
61 M_EraHandler.appendEra(1,
62 CCGregorianCalendar.fixed_from_dmy(8, 9, 1868),
63 CCGregorianCalendar.fixed_from_dmy(29, 7, 1912));
64 M_EraHandler.appendEra(2,
65 CCGregorianCalendar.fixed_from_dmy(30, 7, 1912),
66 CCGregorianCalendar.fixed_from_dmy(24, 12, 1926));
67 M_EraHandler.appendEra(3,
68 CCGregorianCalendar.fixed_from_dmy(25, 12, 1926),
69 CCGregorianCalendar.fixed_from_dmy(7, 1, 1989));
70 M_EraHandler.appendEra(4,
71 CCGregorianCalendar.fixed_from_dmy(8, 1, 1989));
75 /// Default constructor.
77 public JapaneseCalendar() {
78 M_AbbrEraNames = new string[] { "M", "T", "S", "H" };
79 M_EraNames = new string[] { "Meiji", "Taisho", "Showa",
84 /// <value>Overridden. Gives the eras supported by the
85 /// calendar as an array of integers.
87 public override int[] Eras {
89 return (int[])M_EraHandler.Eras.Clone();
93 int twoDigitYearMax = 99;
95 public override int TwoDigitYearMax
98 return twoDigitYearMax;
101 M_ArgumentInRange ("value", value, 100, M_MaxYear);
103 twoDigitYearMax = value;
108 /// A protected member checking a
109 /// <see cref="T:System.DateTime"/> value.
111 /// <param name="time">The
112 /// <see cref="T:System.DateTime"/>
115 /// <exception cref="T:System.ArgumentOutOfRangeException">
116 /// The exception is thrown if the
117 /// <see cref="T:System.DateTime"/> parameter is outside all
120 internal void M_CheckDateTime(DateTime time) {
121 M_EraHandler.CheckDateTime(time);
125 /// A protected method checking the era number.
127 /// <param name="era">The era number as reference. It is set
128 /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
129 /// <exception name="T:System.ArgumentException">
130 /// The exception is thrown if the era is not supported by the class.
132 internal void M_CheckEra(ref int era) {
133 if (era == CurrentEra)
135 if (!M_EraHandler.ValidEra(era))
136 throw new ArgumentException("Era value was not valid.");
140 /// A protected method checking calendar year and the era number.
142 /// <param name="year">An integer representing the calendar year.
144 /// <param name="era">The era number as reference.</param>
145 /// <exception name="T:System.ArgumentException">
146 /// The exception is thrown if the era is not supported by the class.
148 /// <exception cref="T:System.ArgumentOutOfRangeException">
149 /// The exception is thrown if the calendar year is outside of
150 /// the supported range.
152 internal int M_CheckYEG(int year, ref int era) {
154 return M_EraHandler.GregorianYear(year, era);
158 /// Checks whether the year is the era is valid, if era = CurrentEra
159 /// the right value is set.
161 /// <param name="year">The year to check.</param>
162 /// <param name="era">The era to check.</Param>
163 /// <exception cref="T:ArgumentOutOfRangeException">
164 /// The exception will be thrown, if the year is not valid.
166 internal override void M_CheckYE(int year, ref int era) {
167 M_CheckYEG(year, ref era);
171 /// A protected method checking the calendar year, month, and
174 /// <param name="year">An integer representing the calendar year.
176 /// <param name="month">An integer giving the calendar month.
178 /// <param name="era">The era number as reference.</param>
179 /// <exception name="T:System.ArgumentException">
180 /// The exception is thrown if the era is not supported by the class.
182 /// <exception cref="T:System.ArgumentOutOfRangeException">
183 /// The exception is thrown if the calendar year or month is
184 /// outside of the supported range.
186 internal int M_CheckYMEG(int year, int month, ref int era) {
187 int gregorianYear = M_CheckYEG(year, ref era);
188 if (month < 1 || month > 12)
189 throw new ArgumentOutOfRangeException("month",
190 "Month must be between one and twelve.");
191 return gregorianYear;
195 /// A protected method checking the calendar day, month, and year
196 /// and the era number.
198 /// <param name="year">An integer representing the calendar year.
200 /// <param name="month">An integer giving the calendar month.
202 /// <param name="day">An integer giving the calendar day.
204 /// <param name="era">The era number as reference.</param>
205 /// <exception name="T:System.ArgumentException">
206 /// The exception is thrown if the era is not supported by the class.
208 /// <exception cref="T:System.ArgumentOutOfRangeException">
209 /// The exception is thrown if the calendar year, month, or day is
210 /// outside of the supported range.
212 internal int M_CheckYMDEG(int year, int month, int day, ref int era)
214 int gregorianYear = M_CheckYMEG(year, month, ref era);
215 M_ArgumentInRange("day", day, 1, GetDaysInMonth(year, month, era));
216 return gregorianYear;
221 // Ifdefed out because this is not on the .NET Framewokr.
224 /// Overridden. Adds days to a given date.
226 /// <param name="time">The
227 /// <see cref="T:System.DateTime"/> to which to add
230 /// <param name="days">The number of days to add.</param>
231 /// <returns>A new <see cref="T:System.DateTime"/> value, that
232 /// results from adding <paramref name="days"/> to the specified
233 /// DateTime.</returns>
234 /// <exception cref="T:System.ArgumentOutOfRangeException">
235 /// The exception is thrown if the
236 /// <see cref="T:System.DateTime"/> return value is outside all
239 public override DateTime AddDays(DateTime time, int days) {
240 DateTime t = base.AddDays(time, days);
246 /// Overridden. Adds hours to a given date.
248 /// <param name="time">The
249 /// <see cref="T:System.DateTime"/> to which to add
252 /// <param name="hours">The number of hours to add.</param>
253 /// <returns>A new <see cref="T:System.DateTime"/> value, that
254 /// results from adding <paramref name="hours"/> to the specified
255 /// DateTime.</returns>
256 /// <exception cref="T:System.ArgumentOutOfRangeException">
257 /// The exception is thrown if the
258 /// <see cref="T:System.DateTime"/> return value is outside all
261 public override DateTime AddHours(DateTime time, int hours) {
262 DateTime t = base.AddHours(time, hours);
268 /// Overridden. Adds milliseconds to a given date.
270 /// <param name="time">The
271 /// <see cref="T:System.DateTime"/> to which to add
274 /// <param name="milliseconds">The number of milliseconds given as
275 /// double to add. Keep in mind the 100 nanosecond resolution of
276 /// <see cref="T:System.DateTime"/>.
278 /// <returns>A new <see cref="T:System.DateTime"/> value, that
279 /// results from adding <paramref name="milliseconds"/> to the specified
280 /// DateTime.</returns>
281 /// <exception cref="T:System.ArgumentOutOfRangeException">
282 /// The exception is thrown if the
283 /// <see cref="T:System.DateTime"/> return value is outside all
286 public override DateTime AddMilliseconds(DateTime time,
289 DateTime t = base.AddMilliseconds(time, milliseconds);
295 /// Overridden. Adds minutes to a given date.
297 /// <param name="time">The
298 /// <see cref="T:System.DateTime"/> to which to add
301 /// <param name="minutes">The number of minutes to add.</param>
302 /// <returns>A new <see cref="T:System.DateTime"/> value, that
303 /// results from adding <paramref name="minutes"/> to the specified
304 /// DateTime.</returns>
305 /// <exception cref="T:System.ArgumentOutOfRangeException">
306 /// The exception is thrown if the
307 /// <see cref="T:System.DateTime"/> return value is outside all
310 public override DateTime AddMinutes(DateTime time, int minutes) {
311 DateTime t = base.AddMinutes(time, minutes);
317 /// Overridden. Adds seconds to a given date.
319 /// <param name="time">The
320 /// <see cref="T:System.DateTime"/> to which to add
323 /// <param name="seconds">The number of seconds to add.</param>
324 /// <returns>A new <see cref="T:System.DateTime"/> value, that
325 /// results from adding <paramref name="seconds"/> to the specified
326 /// DateTime.</returns>
327 /// <exception cref="T:System.ArgumentOutOfRangeException">
328 /// The exception is thrown if the
329 /// <see cref="T:System.DateTime"/> return value is outside all
332 public override DateTime AddSeconds(DateTime time, int seconds) {
333 DateTime t = base.AddSeconds(time, seconds);
340 /// Overridden. Adds weeks to a given date.
342 /// <param name="time">The
343 /// <see cref="T:System.DateTime"/> to which to add
346 /// <param name="weeks">The number of weeks to add.</param>
347 /// <returns>A new <see cref="T:System.DateTime"/> value, that
348 /// results from adding <paramref name="weeks"/> to the specified
349 /// DateTime.</returns>
350 /// <exception cref="T:System.ArgumentOutOfRangeException">
351 /// The exception is thrown if the
352 /// <see cref="T:System.DateTime"/> return value is outside all
355 public override DateTime AddWeeks(DateTime time, int weeks) {
356 DateTime t = base.AddWeeks(time, weeks);
362 /// Overridden. Gives the hour of the specified time.
364 /// <param name="time">The
365 /// <see cref="T:System.DateTime"/> that specifies the
368 /// <returns>An integer that gives the hour of the specified time,
369 /// starting with 0.</returns>
370 /// <exception cref="T:System.ArgumentOutOfRangeException">
371 /// The exception is thrown if the
372 /// <see cref="T:System.DateTime"/> parameter is outside all
375 public override int GetHour(DateTime time) {
376 M_CheckDateTime(time);
377 return base.GetHour(time);
381 /// Overridden. Gives the milliseconds in the current second
382 /// of the specified time.
384 /// <param name="time">The
385 /// <see cref="T:System.DateTime"/> that specifies the
388 /// <returns>An integer that gives the milliseconds in the seconds
389 /// of the specified time, starting with 0.</returns>
390 /// <exception cref="T:System.ArgumentOutOfRangeException">
391 /// The exception is thrown if the
392 /// <see cref="T:System.DateTime"/> parameter is outside all
395 public override double GetMilliseconds(DateTime time) {
396 M_CheckDateTime(time);
397 return base.GetMilliseconds(time);
401 /// Overridden. Gives the minute of the specified time.
403 /// <param name="time">The
404 /// <see cref="T:System.DateTime"/> that specifies the
407 /// <returns>An integer that gives the minute of the specified time,
408 /// starting with 0.</returns>
409 /// <exception cref="T:System.ArgumentOutOfRangeException">
410 /// The exception is thrown if the
411 /// <see cref="T:System.DateTime"/> parameter is outside all
414 public override int GetMinute(DateTime time) {
415 M_CheckDateTime(time);
416 return base.GetMinute(time);
420 /// Overridden. Gives the second of the specified time.
422 /// <param name="time">The
423 /// <see cref="T:System.DateTime"/> that specifies the
426 /// <returns>An integer that gives the second of the specified time,
427 /// starting with 0.</returns>
428 /// <exception cref="T:System.ArgumentOutOfRangeException">
429 /// The exception is thrown if the
430 /// <see cref="T:System.DateTime"/> parameter is outside all
433 public override int GetSecond(DateTime time) {
434 M_CheckDateTime(time);
435 return base.GetMinute(time);
440 /// Overrideden. Adds months to a given date.
442 /// <param name="time">The
443 /// <see cref="T:System.DateTime"/> to which to add
446 /// <param name="months">The number of months to add.</param>
447 /// <returns>A new <see cref="T:System.DateTime"/> value, that
448 /// results from adding <paramref name="months"/> to the specified
449 /// DateTime.</returns>
450 /// <exception cref="T:System.ArgumentOutOfRangeException">
451 /// The exception is thrown if
452 /// <see cref="T:System.DateTime"/> return value is outside all
455 public override DateTime AddMonths(DateTime time, int months) {
456 DateTime t = CCGregorianCalendar.AddMonths(time, months);
462 /// Overridden. Adds years to a given date.
464 /// <param name="time">The
465 /// <see cref="T:System.DateTime"/> to which to add
468 /// <param name="years">The number of years to add.</param>
469 /// <returns>A new <see cref="T:System.DateTime"/> value, that
470 /// results from adding <paramref name="years"/> to the specified
471 /// DateTime.</returns>
472 /// <exception cref="T:System.ArgumentOutOfRangeException">
473 /// The exception is thrown if
474 /// <see cref="T:System.DateTime"/> return value is outside all
477 public override DateTime AddYears(DateTime time, int years) {
478 DateTime t = CCGregorianCalendar.AddYears(time, years);
484 /// Overriden. Gets the day of the month from
485 /// <paramref name="time"/>.
487 /// <param name="time">The
488 /// <see cref="T:System.DateTime"/> that specifies a
491 /// <returns>An integer giving the day of months, starting with 1.
493 /// <exception cref="T:System.ArgumentOutOfRangeException">
494 /// The exception is thrown if the
495 /// <see cref="T:System.DateTime"/> parameter is outside all
498 public override int GetDayOfMonth(DateTime time) {
499 M_CheckDateTime(time);
500 return CCGregorianCalendar.GetDayOfMonth(time);
504 /// Overriden. Gets the day of the week from the specified date.
506 /// <param name="time">The
507 /// <see cref="T:System.DateTime"/> that specifies a
510 /// <returns>An integer giving the day of months, starting with 1.
512 /// <exception cref="T:System.ArgumentOutOfRangeException">
513 /// The exception is thrown if the
514 /// <see cref="T:System.DateTime"/> parameter is outside all
517 public override DayOfWeek GetDayOfWeek(DateTime time) {
518 M_CheckDateTime(time);
519 int rd = CCFixed.FromDateTime(time);
520 return (DayOfWeek)CCFixed.day_of_week(rd);
524 /// Overridden. Gives the number of the day in the year.
526 /// <param name="time">The
527 /// <see cref="T:System.DateTime"/> that specifies a
530 /// <returns>An integer representing the day of the year,
531 /// starting with 1.</returns>
532 /// <exception cref="T:System.ArgumentOutOfRangeException">
533 /// The exception is thrown if the
534 /// <see cref="T:System.DateTime"/> parameter is outside all
537 public override int GetDayOfYear(DateTime time) {
538 M_CheckDateTime(time);
539 return CCGregorianCalendar.GetDayOfYear(time);
543 /// Overridden. Gives the number of days in the specified month
544 /// of the given year and era.
546 /// <param name="year">An integer that gives the year.
548 /// <param name="month">An integer that gives the month, starting
550 /// <param name="era">An integer that gives the era of the specified
552 /// <returns>An integer that gives the number of days of the
553 /// specified month.</returns>
554 /// <exception cref="T:System.ArgumentOutOfRangeException">
555 /// The exception is thrown, if <paramref name="month"/>,
556 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
557 /// the allowed range.
559 public override int GetDaysInMonth(int year, int month, int era) {
560 int gregorianYear = M_CheckYMEG(year, month, ref era);
561 return CCGregorianCalendar.GetDaysInMonth(gregorianYear, month);
565 /// Overridden. Gives the number of days of the specified
566 /// year of the given era.
568 /// <param name="year">An integer that specifies the year.
570 /// <param name="era">An ineger that specifies the era.
572 /// <returns>An integer that gives the number of days of the
573 /// specified year.</returns>
574 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
575 /// The exception is thrown, if
576 /// <paramref name="year"/> or <paramref name="era"/> are outside the
579 public override int GetDaysInYear(int year, int era) {
580 int gregorianYear = M_CheckYEG(year, ref era);
581 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
586 /// Overridden. Gives the era of the specified date.
588 /// <param name="time">The
589 /// <see cref="T:System.DateTime"/> that specifies a
592 /// <returns>An integer representing the era of the calendar.
594 /// <exception cref="T:System.ArgumentOutOfRangeException">
595 /// The exception is thrown if the
596 /// <see cref="T:System.DateTime"/> parameter is outside all
599 public override int GetEra(DateTime time) {
600 // M_CheckDateTime not needed, because EraYear does the
602 int rd = CCFixed.FromDateTime(time);
604 M_EraHandler.EraYear(out era, rd);
609 /// Overridden. Gives the number of the month of the specified
612 /// <param name="time">The
613 /// <see cref="T:System.DateTime"/> that specifies a
616 /// <returns>An integer representing the month,
617 /// starting with 1.</returns>
618 /// <exception cref="T:System.ArgumentOutOfRangeException">
619 /// The exception is thrown if the
620 /// <see cref="T:System.DateTime"/> parameter is outside all
623 public override int GetMonth(DateTime time) {
624 M_CheckDateTime(time);
625 return CCGregorianCalendar.GetMonth(time);
629 /// Overridden. Gives the number of months in the specified year
632 /// <param name="year">An integer that specifies the year.
634 /// <param name="era">An integer that specifies the era.
636 /// <returns>An integer that gives the number of the months in the
637 /// specified year.</returns>
638 /// <exception cref="T:System.ArgumentOutOfRangeException">
639 /// The exception is thrown, if the year or the era are not valid.
641 public override int GetMonthsInYear(int year, int era) {
642 M_CheckYE(year, ref era);
647 /// Overridden. Gives the number of the year of the specified
650 /// <param name="time">The
651 /// <see cref="T:System.DateTime"/> that specifies a
654 /// <returns>An integer representing the year,
655 /// starting with 1.</returns>
656 /// <exception cref="T:System.ArgumentOutOfRangeException">
657 /// The exception is thrown if the
658 /// <see cref="T:System.DateTime"/> parameter is outside all
661 public override int GetYear(DateTime time) {
662 // M_CheckDateTime not needed, because EraYeat does the
664 int rd = CCFixed.FromDateTime(time);
666 return M_EraHandler.EraYear(out era, rd);
670 /// Overridden. Tells whether the given day
673 /// <param name="year">An integer that specifies the year in the
676 /// <param name="month">An integer that specifies the month.
678 /// <param name="day">An integer that specifies the day.
680 /// <param name="era">An integer that specifies the era.
682 /// <returns>A boolean that tells whether the given day is a leap
685 /// <exception cref="T:System.ArgumentOutOfRangeException">
686 /// The exception is thrown, if the year, month, day, or era is not
689 public override bool IsLeapDay(int year, int month, int day, int era)
691 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
692 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
696 /// Overridden. Tells whether the given month
699 /// <param name="year">An integer that specifies the year in the
702 /// <param name="month">An integer that specifies the month.
704 /// <param name="era">An integer that specifies the era.
706 /// <returns>A boolean that tells whether the given month is a leap
709 /// <exception cref="T:System.ArgumentOutOfRangeException">
710 /// The exception is thrown, if the year, month, or era is not
713 public override bool IsLeapMonth(int year, int month, int era) {
714 M_CheckYMEG(year, month, ref era);
719 /// Overridden. Tells whether the given year
722 /// <param name="year">An integer that specifies the year in the
725 /// <param name="era">An integer that specifies the era.
727 /// <returns>A boolean that tells whether the given year is a leap
730 /// <exception cref="T:System.ArgumentOutOfRangeException">
731 /// The exception is thrown, if the year or era is not
734 public override bool IsLeapYear(int year, int era) {
735 int gregorianYear = M_CheckYEG(year, ref era);
736 return CCGregorianCalendar.is_leap_year(gregorianYear);
740 /// Overridden. Creates the
741 /// <see cref="T:System.DateTime"/> from the parameters.
743 /// <param name="year">An integer that gives the year in the
744 /// <paramref name="era"/>.
746 /// <param name="month">An integer that specifies the month.
748 /// <param name="day">An integer that specifies the day.
750 /// <param name="hour">An integer that specifies the hour.
752 /// <param name="minute">An integer that specifies the minute.
754 /// <param name="second">An integer that gives the second.
756 /// <param name="milliseconds">An integer that gives the
759 /// <param name="era">An integer that specifies the era.
762 /// <see cref="T:system.DateTime"/> representig the date and time.
764 /// <exception cref="T:System.ArgumentOutOfRangeException">
765 /// The exception is thrown, if at least one of the parameters
768 public override DateTime ToDateTime(int year, int month, int day,
769 int hour, int minute, int second, int milliseconds,
772 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
773 M_CheckHMSM(hour, minute, second, milliseconds);
774 return CCGregorianCalendar.ToDateTime(
775 gregorianYear, month, day,
776 hour, minute, second, milliseconds);
781 /// This functions returns simply the year for the Japanese calendar.
783 /// <param name="year">An integer that gives the year.
785 /// <returns>The same argument as the year.
787 /// <exception cref="T:System.ArgumentOutOfRangeException">
788 /// The exception is thrown if the year is negative or the resulting
791 public override int ToFourDigitYear(int year) {
793 throw new ArgumentOutOfRangeException(
794 "year", "Non-negative number required.");
795 int era = CurrentEra;
796 M_CheckYE(year, ref era);
799 } // class JapaneseCalendar
801 } // namespace System.Globalization