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 four eras.</para>
39 /// <list type="table">
41 /// <term>era number</term>
42 /// <term>Gregorian start date</term>
43 /// <term>Gregorian end date</term>
47 /// <term>September 8, 1868</term>
48 /// <term>July 29, 1912</term>
52 /// <term>July 30, 1912</term>
53 /// <term>December 24, 1926</term>
57 /// <term>December 25, 1926</term>
58 /// <term>January 7, 1989</term>
62 /// <term>January 8, 1989</term>
63 /// <term>present</term>
66 /// <para>The implementation uses the
67 /// <see cref="N:CalendricalCalculations"/> namespace.
71 [MonoTODO ("Fix serialization compatibility with MS.NET")]
72 public class JapaneseCalendar : Calendar {
74 /// Static protected field storing the
75 /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
77 internal static readonly CCGregorianEraHandler M_EraHandler;
80 /// Static constructor, who creates and initializes
81 /// <see cref="F:M_EraHandler"/>.
83 static JapaneseCalendar() {
84 M_EraHandler = new CCGregorianEraHandler();
85 M_EraHandler.appendEra(1,
86 CCGregorianCalendar.fixed_from_dmy(8, 9, 1868),
87 CCGregorianCalendar.fixed_from_dmy(29, 7, 1912));
88 M_EraHandler.appendEra(2,
89 CCGregorianCalendar.fixed_from_dmy(30, 7, 1912),
90 CCGregorianCalendar.fixed_from_dmy(24, 12, 1926));
91 M_EraHandler.appendEra(3,
92 CCGregorianCalendar.fixed_from_dmy(25, 12, 1926),
93 CCGregorianCalendar.fixed_from_dmy(7, 1, 1989));
94 M_EraHandler.appendEra(4,
95 CCGregorianCalendar.fixed_from_dmy(8, 1, 1989));
99 /// Default constructor.
101 public JapaneseCalendar() {
102 M_AbbrEraNames = new string[] { "M", "T", "S", "H" };
103 M_EraNames = new string[] { "Meiji", "Taisho", "Showa",
108 /// <value>Overridden. Gives the eras supported by the
109 /// calendar as an array of integers.
111 public override int[] Eras {
113 return (int[])M_EraHandler.Eras.Clone();
117 int twoDigitYearMax = 99;
119 public override int TwoDigitYearMax
122 return twoDigitYearMax;
125 M_ArgumentInRange ("value", value, 100, M_MaxYear);
127 twoDigitYearMax = value;
132 /// A protected member checking a
133 /// <see cref="T:System.DateTime"/> value.
135 /// <param name="time">The
136 /// <see cref="T:System.DateTime"/>
139 /// <exception cref="T:System.ArgumentOutOfRangeException">
140 /// The exception is thrown if the
141 /// <see cref="T:System.DateTime"/> parameter is outside all
144 internal void M_CheckDateTime(DateTime time) {
145 M_EraHandler.CheckDateTime(time);
149 /// A protected method checking the era number.
151 /// <param name="era">The era number as reference. It is set
152 /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
153 /// <exception name="T:System.ArgumentException">
154 /// The exception is thrown if the era is not supported by the class.
156 internal void M_CheckEra(ref int era) {
157 if (era == CurrentEra)
159 if (!M_EraHandler.ValidEra(era))
160 throw new ArgumentException("Era value was not valid.");
164 /// A protected method checking calendar year and the era number.
166 /// <param name="year">An integer representing the calendar year.
168 /// <param name="era">The era number as reference.</param>
169 /// <exception name="T:System.ArgumentException">
170 /// The exception is thrown if the era is not supported by the class.
172 /// <exception cref="T:System.ArgumentOutOfRangeException">
173 /// The exception is thrown if the calendar year is outside of
174 /// the supported range.
176 internal int M_CheckYEG(int year, ref int era) {
178 return M_EraHandler.GregorianYear(year, era);
182 /// Checks whether the year is the era is valid, if era = CurrentEra
183 /// the right value is set.
185 /// <param name="year">The year to check.</param>
186 /// <param name="era">The era to check.</Param>
187 /// <exception cref="T:ArgumentOutOfRangeException">
188 /// The exception will be thrown, if the year is not valid.
190 internal override void M_CheckYE(int year, ref int era) {
191 M_CheckYEG(year, ref era);
195 /// A protected method checking the calendar year, month, and
198 /// <param name="year">An integer representing the calendar year.
200 /// <param name="month">An integer giving the calendar month.
202 /// <param name="era">The era number as reference.</param>
203 /// <exception name="T:System.ArgumentException">
204 /// The exception is thrown if the era is not supported by the class.
206 /// <exception cref="T:System.ArgumentOutOfRangeException">
207 /// The exception is thrown if the calendar year or month is
208 /// outside of the supported range.
210 internal int M_CheckYMEG(int year, int month, ref int era) {
211 int gregorianYear = M_CheckYEG(year, ref era);
212 if (month < 1 || month > 12)
213 throw new ArgumentOutOfRangeException("month",
214 "Month must be between one and twelve.");
215 return gregorianYear;
219 /// A protected method checking the calendar day, month, and year
220 /// and the era number.
222 /// <param name="year">An integer representing the calendar year.
224 /// <param name="month">An integer giving the calendar month.
226 /// <param name="day">An integer giving the calendar day.
228 /// <param name="era">The era number as reference.</param>
229 /// <exception name="T:System.ArgumentException">
230 /// The exception is thrown if the era is not supported by the class.
232 /// <exception cref="T:System.ArgumentOutOfRangeException">
233 /// The exception is thrown if the calendar year, month, or day is
234 /// outside of the supported range.
236 internal int M_CheckYMDEG(int year, int month, int day, ref int era)
238 int gregorianYear = M_CheckYMEG(year, month, ref era);
239 M_ArgumentInRange("day", day, 1, GetDaysInMonth(year, month, era));
240 return gregorianYear;
245 // Ifdefed out because this is not on the .NET Framewokr.
248 /// Overridden. Adds days to a given date.
250 /// <param name="time">The
251 /// <see cref="T:System.DateTime"/> to which to add
254 /// <param name="days">The number of days to add.</param>
255 /// <returns>A new <see cref="T:System.DateTime"/> value, that
256 /// results from adding <paramref name="days"/> to the specified
257 /// DateTime.</returns>
258 /// <exception cref="T:System.ArgumentOutOfRangeException">
259 /// The exception is thrown if the
260 /// <see cref="T:System.DateTime"/> return value is outside all
263 public override DateTime AddDays(DateTime time, int days) {
264 DateTime t = base.AddDays(time, days);
270 /// Overridden. Adds hours to a given date.
272 /// <param name="time">The
273 /// <see cref="T:System.DateTime"/> to which to add
276 /// <param name="hours">The number of hours to add.</param>
277 /// <returns>A new <see cref="T:System.DateTime"/> value, that
278 /// results from adding <paramref name="hours"/> to the specified
279 /// DateTime.</returns>
280 /// <exception cref="T:System.ArgumentOutOfRangeException">
281 /// The exception is thrown if the
282 /// <see cref="T:System.DateTime"/> return value is outside all
285 public override DateTime AddHours(DateTime time, int hours) {
286 DateTime t = base.AddHours(time, hours);
292 /// Overridden. Adds milliseconds to a given date.
294 /// <param name="time">The
295 /// <see cref="T:System.DateTime"/> to which to add
298 /// <param name="milliseconds">The number of milliseconds given as
299 /// double to add. Keep in mind the 100 nanosecond resolution of
300 /// <see cref="T:System.DateTime"/>.
302 /// <returns>A new <see cref="T:System.DateTime"/> value, that
303 /// results from adding <paramref name="milliseconds"/> 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 AddMilliseconds(DateTime time,
313 DateTime t = base.AddMilliseconds(time, milliseconds);
319 /// Overridden. Adds minutes to a given date.
321 /// <param name="time">The
322 /// <see cref="T:System.DateTime"/> to which to add
325 /// <param name="minutes">The number of minutes to add.</param>
326 /// <returns>A new <see cref="T:System.DateTime"/> value, that
327 /// results from adding <paramref name="minutes"/> to the specified
328 /// DateTime.</returns>
329 /// <exception cref="T:System.ArgumentOutOfRangeException">
330 /// The exception is thrown if the
331 /// <see cref="T:System.DateTime"/> return value is outside all
334 public override DateTime AddMinutes(DateTime time, int minutes) {
335 DateTime t = base.AddMinutes(time, minutes);
341 /// Overridden. Adds seconds to a given date.
343 /// <param name="time">The
344 /// <see cref="T:System.DateTime"/> to which to add
347 /// <param name="seconds">The number of seconds to add.</param>
348 /// <returns>A new <see cref="T:System.DateTime"/> value, that
349 /// results from adding <paramref name="seconds"/> to the specified
350 /// DateTime.</returns>
351 /// <exception cref="T:System.ArgumentOutOfRangeException">
352 /// The exception is thrown if the
353 /// <see cref="T:System.DateTime"/> return value is outside all
356 public override DateTime AddSeconds(DateTime time, int seconds) {
357 DateTime t = base.AddSeconds(time, seconds);
364 /// Overridden. Adds weeks to a given date.
366 /// <param name="time">The
367 /// <see cref="T:System.DateTime"/> to which to add
370 /// <param name="weeks">The number of weeks to add.</param>
371 /// <returns>A new <see cref="T:System.DateTime"/> value, that
372 /// results from adding <paramref name="weeks"/> to the specified
373 /// DateTime.</returns>
374 /// <exception cref="T:System.ArgumentOutOfRangeException">
375 /// The exception is thrown if the
376 /// <see cref="T:System.DateTime"/> return value is outside all
379 public override DateTime AddWeeks(DateTime time, int weeks) {
380 DateTime t = base.AddWeeks(time, weeks);
386 /// Overridden. Gives the hour of the specified time.
388 /// <param name="time">The
389 /// <see cref="T:System.DateTime"/> that specifies the
392 /// <returns>An integer that gives the hour of the specified time,
393 /// starting with 0.</returns>
394 /// <exception cref="T:System.ArgumentOutOfRangeException">
395 /// The exception is thrown if the
396 /// <see cref="T:System.DateTime"/> parameter is outside all
399 public override int GetHour(DateTime time) {
400 M_CheckDateTime(time);
401 return base.GetHour(time);
405 /// Overridden. Gives the milliseconds in the current second
406 /// of the specified time.
408 /// <param name="time">The
409 /// <see cref="T:System.DateTime"/> that specifies the
412 /// <returns>An integer that gives the milliseconds in the seconds
413 /// of the specified time, starting with 0.</returns>
414 /// <exception cref="T:System.ArgumentOutOfRangeException">
415 /// The exception is thrown if the
416 /// <see cref="T:System.DateTime"/> parameter is outside all
419 public override double GetMilliseconds(DateTime time) {
420 M_CheckDateTime(time);
421 return base.GetMilliseconds(time);
425 /// Overridden. Gives the minute of the specified time.
427 /// <param name="time">The
428 /// <see cref="T:System.DateTime"/> that specifies the
431 /// <returns>An integer that gives the minute of the specified time,
432 /// starting with 0.</returns>
433 /// <exception cref="T:System.ArgumentOutOfRangeException">
434 /// The exception is thrown if the
435 /// <see cref="T:System.DateTime"/> parameter is outside all
438 public override int GetMinute(DateTime time) {
439 M_CheckDateTime(time);
440 return base.GetMinute(time);
444 /// Overridden. Gives the second of the specified time.
446 /// <param name="time">The
447 /// <see cref="T:System.DateTime"/> that specifies the
450 /// <returns>An integer that gives the second of the specified time,
451 /// starting with 0.</returns>
452 /// <exception cref="T:System.ArgumentOutOfRangeException">
453 /// The exception is thrown if the
454 /// <see cref="T:System.DateTime"/> parameter is outside all
457 public override int GetSecond(DateTime time) {
458 M_CheckDateTime(time);
459 return base.GetMinute(time);
464 /// Overrideden. Adds months to a given date.
466 /// <param name="time">The
467 /// <see cref="T:System.DateTime"/> to which to add
470 /// <param name="months">The number of months to add.</param>
471 /// <returns>A new <see cref="T:System.DateTime"/> value, that
472 /// results from adding <paramref name="months"/> to the specified
473 /// DateTime.</returns>
474 /// <exception cref="T:System.ArgumentOutOfRangeException">
475 /// The exception is thrown if
476 /// <see cref="T:System.DateTime"/> return value is outside all
479 public override DateTime AddMonths(DateTime time, int months) {
480 DateTime t = CCGregorianCalendar.AddMonths(time, months);
486 /// Overridden. Adds years to a given date.
488 /// <param name="time">The
489 /// <see cref="T:System.DateTime"/> to which to add
492 /// <param name="years">The number of years to add.</param>
493 /// <returns>A new <see cref="T:System.DateTime"/> value, that
494 /// results from adding <paramref name="years"/> to the specified
495 /// DateTime.</returns>
496 /// <exception cref="T:System.ArgumentOutOfRangeException">
497 /// The exception is thrown if
498 /// <see cref="T:System.DateTime"/> return value is outside all
501 public override DateTime AddYears(DateTime time, int years) {
502 DateTime t = CCGregorianCalendar.AddYears(time, years);
508 /// Overriden. Gets the day of the month from
509 /// <paramref name="time"/>.
511 /// <param name="time">The
512 /// <see cref="T:System.DateTime"/> that specifies a
515 /// <returns>An integer giving the day of months, starting with 1.
517 /// <exception cref="T:System.ArgumentOutOfRangeException">
518 /// The exception is thrown if the
519 /// <see cref="T:System.DateTime"/> parameter is outside all
522 public override int GetDayOfMonth(DateTime time) {
523 M_CheckDateTime(time);
524 return CCGregorianCalendar.GetDayOfMonth(time);
528 /// Overriden. Gets the day of the week from the specified date.
530 /// <param name="time">The
531 /// <see cref="T:System.DateTime"/> that specifies a
534 /// <returns>An integer giving the day of months, starting with 1.
536 /// <exception cref="T:System.ArgumentOutOfRangeException">
537 /// The exception is thrown if the
538 /// <see cref="T:System.DateTime"/> parameter is outside all
541 public override DayOfWeek GetDayOfWeek(DateTime time) {
542 M_CheckDateTime(time);
543 int rd = CCFixed.FromDateTime(time);
544 return (DayOfWeek)CCFixed.day_of_week(rd);
548 /// Overridden. Gives the number of the day in the year.
550 /// <param name="time">The
551 /// <see cref="T:System.DateTime"/> that specifies a
554 /// <returns>An integer representing the day of the year,
555 /// starting with 1.</returns>
556 /// <exception cref="T:System.ArgumentOutOfRangeException">
557 /// The exception is thrown if the
558 /// <see cref="T:System.DateTime"/> parameter is outside all
561 public override int GetDayOfYear(DateTime time) {
562 M_CheckDateTime(time);
563 return CCGregorianCalendar.GetDayOfYear(time);
567 /// Overridden. Gives the number of days in the specified month
568 /// of the given year and era.
570 /// <param name="year">An integer that gives the year.
572 /// <param name="month">An integer that gives the month, starting
574 /// <param name="era">An integer that gives the era of the specified
576 /// <returns>An integer that gives the number of days of the
577 /// specified month.</returns>
578 /// <exception cref="T:System.ArgumentOutOfRangeException">
579 /// The exception is thrown, if <paramref name="month"/>,
580 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
581 /// the allowed range.
583 public override int GetDaysInMonth(int year, int month, int era) {
584 int gregorianYear = M_CheckYMEG(year, month, ref era);
585 return CCGregorianCalendar.GetDaysInMonth(gregorianYear, month);
589 /// Overridden. Gives the number of days of the specified
590 /// year of the given era.
592 /// <param name="year">An integer that specifies the year.
594 /// <param name="era">An ineger that specifies the era.
596 /// <returns>An integer that gives the number of days of the
597 /// specified year.</returns>
598 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
599 /// The exception is thrown, if
600 /// <paramref name="year"/> or <paramref name="era"/> are outside the
603 public override int GetDaysInYear(int year, int era) {
604 int gregorianYear = M_CheckYEG(year, ref era);
605 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
610 /// Overridden. Gives the era of the specified date.
612 /// <param name="time">The
613 /// <see cref="T:System.DateTime"/> that specifies a
616 /// <returns>An integer representing the era of the calendar.
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 GetEra(DateTime time) {
624 // M_CheckDateTime not needed, because EraYear does the
626 int rd = CCFixed.FromDateTime(time);
628 M_EraHandler.EraYear(out era, rd);
633 /// Overridden. Gives the number of the month of the specified
636 /// <param name="time">The
637 /// <see cref="T:System.DateTime"/> that specifies a
640 /// <returns>An integer representing the month,
641 /// starting with 1.</returns>
642 /// <exception cref="T:System.ArgumentOutOfRangeException">
643 /// The exception is thrown if the
644 /// <see cref="T:System.DateTime"/> parameter is outside all
647 public override int GetMonth(DateTime time) {
648 M_CheckDateTime(time);
649 return CCGregorianCalendar.GetMonth(time);
653 /// Overridden. Gives the number of months in the specified year
656 /// <param name="year">An integer that specifies the year.
658 /// <param name="era">An integer that specifies the era.
660 /// <returns>An integer that gives the number of the months in the
661 /// specified year.</returns>
662 /// <exception cref="T:System.ArgumentOutOfRangeException">
663 /// The exception is thrown, if the year or the era are not valid.
665 public override int GetMonthsInYear(int year, int era) {
666 M_CheckYE(year, ref era);
671 /// Overridden. Gives the number of the year of the specified
674 /// <param name="time">The
675 /// <see cref="T:System.DateTime"/> that specifies a
678 /// <returns>An integer representing the year,
679 /// starting with 1.</returns>
680 /// <exception cref="T:System.ArgumentOutOfRangeException">
681 /// The exception is thrown if the
682 /// <see cref="T:System.DateTime"/> parameter is outside all
685 public override int GetYear(DateTime time) {
686 // M_CheckDateTime not needed, because EraYeat does the
688 int rd = CCFixed.FromDateTime(time);
690 return M_EraHandler.EraYear(out era, rd);
694 /// Overridden. Tells whether the given day
697 /// <param name="year">An integer that specifies the year in the
700 /// <param name="month">An integer that specifies the month.
702 /// <param name="day">An integer that specifies the day.
704 /// <param name="era">An integer that specifies the era.
706 /// <returns>A boolean that tells whether the given day is a leap
709 /// <exception cref="T:System.ArgumentOutOfRangeException">
710 /// The exception is thrown, if the year, month, day, or era is not
713 public override bool IsLeapDay(int year, int month, int day, int era)
715 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
716 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
720 /// Overridden. Tells whether the given month
723 /// <param name="year">An integer that specifies the year in the
726 /// <param name="month">An integer that specifies the month.
728 /// <param name="era">An integer that specifies the era.
730 /// <returns>A boolean that tells whether the given month is a leap
733 /// <exception cref="T:System.ArgumentOutOfRangeException">
734 /// The exception is thrown, if the year, month, or era is not
737 public override bool IsLeapMonth(int year, int month, int era) {
738 M_CheckYMEG(year, month, ref era);
743 /// Overridden. Tells whether the given year
746 /// <param name="year">An integer that specifies the year in the
749 /// <param name="era">An integer that specifies the era.
751 /// <returns>A boolean that tells whether the given year is a leap
754 /// <exception cref="T:System.ArgumentOutOfRangeException">
755 /// The exception is thrown, if the year or era is not
758 public override bool IsLeapYear(int year, int era) {
759 int gregorianYear = M_CheckYEG(year, ref era);
760 return CCGregorianCalendar.is_leap_year(gregorianYear);
764 /// Overridden. Creates the
765 /// <see cref="T:System.DateTime"/> from the parameters.
767 /// <param name="year">An integer that gives the year in the
768 /// <paramref name="era"/>.
770 /// <param name="month">An integer that specifies the month.
772 /// <param name="day">An integer that specifies the day.
774 /// <param name="hour">An integer that specifies the hour.
776 /// <param name="minute">An integer that specifies the minute.
778 /// <param name="second">An integer that gives the second.
780 /// <param name="milliseconds">An integer that gives the
783 /// <param name="era">An integer that specifies the era.
786 /// <see cref="T:system.DateTime"/> representig the date and time.
788 /// <exception cref="T:System.ArgumentOutOfRangeException">
789 /// The exception is thrown, if at least one of the parameters
792 public override DateTime ToDateTime(int year, int month, int day,
793 int hour, int minute, int second, int milliseconds,
796 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
797 M_CheckHMSM(hour, minute, second, milliseconds);
798 return CCGregorianCalendar.ToDateTime(
799 gregorianYear, month, day,
800 hour, minute, second, milliseconds);
805 /// This functions returns simply the year for the Japanese calendar.
807 /// <param name="year">An integer that gives the year.
809 /// <returns>The same argument as the year.
811 /// <exception cref="T:System.ArgumentOutOfRangeException">
812 /// The exception is thrown if the year is negative or the resulting
815 public override int ToFourDigitYear(int year) {
817 throw new ArgumentOutOfRangeException(
818 "year", "Non-negative number required.");
819 int era = CurrentEra;
820 M_CheckYE(year, ref era);
823 } // class JapaneseCalendar
825 } // namespace System.Globalization