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 ("Serialization format not compatible with .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
;
126 M_ArgumentInRange ("value", value, 100, M_MaxYear
);
128 twoDigitYearMax
= value;
133 /// A protected member checking a
134 /// <see cref="T:System.DateTime"/> value.
136 /// <param name="time">The
137 /// <see cref="T:System.DateTime"/>
140 /// <exception cref="T:System.ArgumentOutOfRangeException">
141 /// The exception is thrown if the
142 /// <see cref="T:System.DateTime"/> parameter is outside all
145 internal void M_CheckDateTime(DateTime time
) {
146 M_EraHandler
.CheckDateTime(time
);
150 /// A protected method checking the era number.
152 /// <param name="era">The era number as reference. It is set
153 /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
154 /// <exception name="T:System.ArgumentException">
155 /// The exception is thrown if the era is not supported by the class.
157 internal void M_CheckEra(ref int era
) {
158 if (era
== CurrentEra
)
160 if (!M_EraHandler
.ValidEra(era
))
161 throw new ArgumentException("Era value was not valid.");
165 /// A protected method checking calendar year and the era number.
167 /// <param name="year">An integer representing the calendar year.
169 /// <param name="era">The era number as reference.</param>
170 /// <exception name="T:System.ArgumentException">
171 /// The exception is thrown if the era is not supported by the class.
173 /// <exception cref="T:System.ArgumentOutOfRangeException">
174 /// The exception is thrown if the calendar year is outside of
175 /// the supported range.
177 internal int M_CheckYEG(int year
, ref int era
) {
179 return M_EraHandler
.GregorianYear(year
, era
);
183 /// Checks whether the year is the era is valid, if era = CurrentEra
184 /// the right value is set.
186 /// <param name="year">The year to check.</param>
187 /// <param name="era">The era to check.</Param>
188 /// <exception cref="T:ArgumentOutOfRangeException">
189 /// The exception will be thrown, if the year is not valid.
191 internal override void M_CheckYE(int year
, ref int era
) {
192 M_CheckYEG(year
, ref era
);
196 /// A protected method checking the calendar year, month, and
199 /// <param name="year">An integer representing the calendar year.
201 /// <param name="month">An integer giving the calendar month.
203 /// <param name="era">The era number as reference.</param>
204 /// <exception name="T:System.ArgumentException">
205 /// The exception is thrown if the era is not supported by the class.
207 /// <exception cref="T:System.ArgumentOutOfRangeException">
208 /// The exception is thrown if the calendar year or month is
209 /// outside of the supported range.
211 internal int M_CheckYMEG(int year
, int month
, ref int era
) {
212 int gregorianYear
= M_CheckYEG(year
, ref era
);
213 if (month
< 1 || month
> 12)
214 throw new ArgumentOutOfRangeException("month",
215 "Month must be between one and twelve.");
216 return gregorianYear
;
220 /// A protected method checking the calendar day, month, and year
221 /// and the era number.
223 /// <param name="year">An integer representing the calendar year.
225 /// <param name="month">An integer giving the calendar month.
227 /// <param name="day">An integer giving the calendar day.
229 /// <param name="era">The era number as reference.</param>
230 /// <exception name="T:System.ArgumentException">
231 /// The exception is thrown if the era is not supported by the class.
233 /// <exception cref="T:System.ArgumentOutOfRangeException">
234 /// The exception is thrown if the calendar year, month, or day is
235 /// outside of the supported range.
237 internal int M_CheckYMDEG(int year
, int month
, int day
, ref int era
)
239 int gregorianYear
= M_CheckYMEG(year
, month
, ref era
);
240 M_ArgumentInRange("day", day
, 1, GetDaysInMonth(year
, month
, era
));
241 return gregorianYear
;
246 // Ifdefed out because this is not on the .NET Framewokr.
249 /// Overridden. Adds days to a given date.
251 /// <param name="time">The
252 /// <see cref="T:System.DateTime"/> to which to add
255 /// <param name="days">The number of days to add.</param>
256 /// <returns>A new <see cref="T:System.DateTime"/> value, that
257 /// results from adding <paramref name="days"/> to the specified
258 /// DateTime.</returns>
259 /// <exception cref="T:System.ArgumentOutOfRangeException">
260 /// The exception is thrown if the
261 /// <see cref="T:System.DateTime"/> return value is outside all
264 public override DateTime
AddDays(DateTime time
, int days
) {
265 DateTime t
= base.AddDays(time
, days
);
271 /// Overridden. Adds hours to a given date.
273 /// <param name="time">The
274 /// <see cref="T:System.DateTime"/> to which to add
277 /// <param name="hours">The number of hours to add.</param>
278 /// <returns>A new <see cref="T:System.DateTime"/> value, that
279 /// results from adding <paramref name="hours"/> 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
AddHours(DateTime time
, int hours
) {
287 DateTime t
= base.AddHours(time
, hours
);
293 /// Overridden. Adds milliseconds to a given date.
295 /// <param name="time">The
296 /// <see cref="T:System.DateTime"/> to which to add
299 /// <param name="milliseconds">The number of milliseconds given as
300 /// double to add. Keep in mind the 100 nanosecond resolution of
301 /// <see cref="T:System.DateTime"/>.
303 /// <returns>A new <see cref="T:System.DateTime"/> value, that
304 /// results from adding <paramref name="milliseconds"/> to the specified
305 /// DateTime.</returns>
306 /// <exception cref="T:System.ArgumentOutOfRangeException">
307 /// The exception is thrown if the
308 /// <see cref="T:System.DateTime"/> return value is outside all
311 public override DateTime
AddMilliseconds(DateTime time
,
314 DateTime t
= base.AddMilliseconds(time
, milliseconds
);
320 /// Overridden. Adds minutes to a given date.
322 /// <param name="time">The
323 /// <see cref="T:System.DateTime"/> to which to add
326 /// <param name="minutes">The number of minutes to add.</param>
327 /// <returns>A new <see cref="T:System.DateTime"/> value, that
328 /// results from adding <paramref name="minutes"/> to the specified
329 /// DateTime.</returns>
330 /// <exception cref="T:System.ArgumentOutOfRangeException">
331 /// The exception is thrown if the
332 /// <see cref="T:System.DateTime"/> return value is outside all
335 public override DateTime
AddMinutes(DateTime time
, int minutes
) {
336 DateTime t
= base.AddMinutes(time
, minutes
);
342 /// Overridden. Adds seconds to a given date.
344 /// <param name="time">The
345 /// <see cref="T:System.DateTime"/> to which to add
348 /// <param name="seconds">The number of seconds to add.</param>
349 /// <returns>A new <see cref="T:System.DateTime"/> value, that
350 /// results from adding <paramref name="seconds"/> to the specified
351 /// DateTime.</returns>
352 /// <exception cref="T:System.ArgumentOutOfRangeException">
353 /// The exception is thrown if the
354 /// <see cref="T:System.DateTime"/> return value is outside all
357 public override DateTime
AddSeconds(DateTime time
, int seconds
) {
358 DateTime t
= base.AddSeconds(time
, seconds
);
365 /// Overridden. Adds weeks to a given date.
367 /// <param name="time">The
368 /// <see cref="T:System.DateTime"/> to which to add
371 /// <param name="weeks">The number of weeks to add.</param>
372 /// <returns>A new <see cref="T:System.DateTime"/> value, that
373 /// results from adding <paramref name="weeks"/> to the specified
374 /// DateTime.</returns>
375 /// <exception cref="T:System.ArgumentOutOfRangeException">
376 /// The exception is thrown if the
377 /// <see cref="T:System.DateTime"/> return value is outside all
380 public override DateTime
AddWeeks(DateTime time
, int weeks
) {
381 DateTime t
= base.AddWeeks(time
, weeks
);
387 /// Overridden. Gives the hour of the specified time.
389 /// <param name="time">The
390 /// <see cref="T:System.DateTime"/> that specifies the
393 /// <returns>An integer that gives the hour of the specified time,
394 /// starting with 0.</returns>
395 /// <exception cref="T:System.ArgumentOutOfRangeException">
396 /// The exception is thrown if the
397 /// <see cref="T:System.DateTime"/> parameter is outside all
400 public override int GetHour(DateTime time
) {
401 M_CheckDateTime(time
);
402 return base.GetHour(time
);
406 /// Overridden. Gives the milliseconds in the current second
407 /// of the specified time.
409 /// <param name="time">The
410 /// <see cref="T:System.DateTime"/> that specifies the
413 /// <returns>An integer that gives the milliseconds in the seconds
414 /// of the specified time, starting with 0.</returns>
415 /// <exception cref="T:System.ArgumentOutOfRangeException">
416 /// The exception is thrown if the
417 /// <see cref="T:System.DateTime"/> parameter is outside all
420 public override double GetMilliseconds(DateTime time
) {
421 M_CheckDateTime(time
);
422 return base.GetMilliseconds(time
);
426 /// Overridden. Gives the minute of the specified time.
428 /// <param name="time">The
429 /// <see cref="T:System.DateTime"/> that specifies the
432 /// <returns>An integer that gives the minute of the specified time,
433 /// starting with 0.</returns>
434 /// <exception cref="T:System.ArgumentOutOfRangeException">
435 /// The exception is thrown if the
436 /// <see cref="T:System.DateTime"/> parameter is outside all
439 public override int GetMinute(DateTime time
) {
440 M_CheckDateTime(time
);
441 return base.GetMinute(time
);
445 /// Overridden. Gives the second of the specified time.
447 /// <param name="time">The
448 /// <see cref="T:System.DateTime"/> that specifies the
451 /// <returns>An integer that gives the second of the specified time,
452 /// starting with 0.</returns>
453 /// <exception cref="T:System.ArgumentOutOfRangeException">
454 /// The exception is thrown if the
455 /// <see cref="T:System.DateTime"/> parameter is outside all
458 public override int GetSecond(DateTime time
) {
459 M_CheckDateTime(time
);
460 return base.GetMinute(time
);
465 /// Overrideden. Adds months to a given date.
467 /// <param name="time">The
468 /// <see cref="T:System.DateTime"/> to which to add
471 /// <param name="months">The number of months to add.</param>
472 /// <returns>A new <see cref="T:System.DateTime"/> value, that
473 /// results from adding <paramref name="months"/> to the specified
474 /// DateTime.</returns>
475 /// <exception cref="T:System.ArgumentOutOfRangeException">
476 /// The exception is thrown if
477 /// <see cref="T:System.DateTime"/> return value is outside all
480 public override DateTime
AddMonths(DateTime time
, int months
) {
481 DateTime t
= CCGregorianCalendar
.AddMonths(time
, months
);
487 /// Overridden. Adds years to a given date.
489 /// <param name="time">The
490 /// <see cref="T:System.DateTime"/> to which to add
493 /// <param name="years">The number of years to add.</param>
494 /// <returns>A new <see cref="T:System.DateTime"/> value, that
495 /// results from adding <paramref name="years"/> to the specified
496 /// DateTime.</returns>
497 /// <exception cref="T:System.ArgumentOutOfRangeException">
498 /// The exception is thrown if
499 /// <see cref="T:System.DateTime"/> return value is outside all
502 public override DateTime
AddYears(DateTime time
, int years
) {
503 DateTime t
= CCGregorianCalendar
.AddYears(time
, years
);
509 /// Overriden. Gets the day of the month from
510 /// <paramref name="time"/>.
512 /// <param name="time">The
513 /// <see cref="T:System.DateTime"/> that specifies a
516 /// <returns>An integer giving the day of months, starting with 1.
518 /// <exception cref="T:System.ArgumentOutOfRangeException">
519 /// The exception is thrown if the
520 /// <see cref="T:System.DateTime"/> parameter is outside all
523 public override int GetDayOfMonth(DateTime time
) {
524 M_CheckDateTime(time
);
525 return CCGregorianCalendar
.GetDayOfMonth(time
);
529 /// Overriden. Gets the day of the week from the specified date.
531 /// <param name="time">The
532 /// <see cref="T:System.DateTime"/> that specifies a
535 /// <returns>An integer giving the day of months, starting with 1.
537 /// <exception cref="T:System.ArgumentOutOfRangeException">
538 /// The exception is thrown if the
539 /// <see cref="T:System.DateTime"/> parameter is outside all
542 public override DayOfWeek
GetDayOfWeek(DateTime time
) {
543 M_CheckDateTime(time
);
544 int rd
= CCFixed
.FromDateTime(time
);
545 return (DayOfWeek
)CCFixed
.day_of_week(rd
);
549 /// Overridden. Gives the number of the day in the year.
551 /// <param name="time">The
552 /// <see cref="T:System.DateTime"/> that specifies a
555 /// <returns>An integer representing the day of the year,
556 /// starting with 1.</returns>
557 /// <exception cref="T:System.ArgumentOutOfRangeException">
558 /// The exception is thrown if the
559 /// <see cref="T:System.DateTime"/> parameter is outside all
562 public override int GetDayOfYear(DateTime time
) {
563 M_CheckDateTime(time
);
564 return CCGregorianCalendar
.GetDayOfYear(time
);
568 /// Overridden. Gives the number of days in the specified month
569 /// of the given year and era.
571 /// <param name="year">An integer that gives the year.
573 /// <param name="month">An integer that gives the month, starting
575 /// <param name="era">An integer that gives the era of the specified
577 /// <returns>An integer that gives the number of days of the
578 /// specified month.</returns>
579 /// <exception cref="T:System.ArgumentOutOfRangeException">
580 /// The exception is thrown, if <paramref name="month"/>,
581 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
582 /// the allowed range.
584 public override int GetDaysInMonth(int year
, int month
, int era
) {
585 int gregorianYear
= M_CheckYMEG(year
, month
, ref era
);
586 return CCGregorianCalendar
.GetDaysInMonth(gregorianYear
, month
);
590 /// Overridden. Gives the number of days of the specified
591 /// year of the given era.
593 /// <param name="year">An integer that specifies the year.
595 /// <param name="era">An ineger that specifies the era.
597 /// <returns>An integer that gives the number of days of the
598 /// specified year.</returns>
599 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
600 /// The exception is thrown, if
601 /// <paramref name="year"/> or <paramref name="era"/> are outside the
604 public override int GetDaysInYear(int year
, int era
) {
605 int gregorianYear
= M_CheckYEG(year
, ref era
);
606 return CCGregorianCalendar
.GetDaysInYear(gregorianYear
);
611 /// Overridden. Gives the era of the specified date.
613 /// <param name="time">The
614 /// <see cref="T:System.DateTime"/> that specifies a
617 /// <returns>An integer representing the era of the calendar.
619 /// <exception cref="T:System.ArgumentOutOfRangeException">
620 /// The exception is thrown if the
621 /// <see cref="T:System.DateTime"/> parameter is outside all
624 public override int GetEra(DateTime time
) {
625 // M_CheckDateTime not needed, because EraYear does the
627 int rd
= CCFixed
.FromDateTime(time
);
629 M_EraHandler
.EraYear(out era
, rd
);
634 /// Overridden. Gives the number of the month of the specified
637 /// <param name="time">The
638 /// <see cref="T:System.DateTime"/> that specifies a
641 /// <returns>An integer representing the month,
642 /// starting with 1.</returns>
643 /// <exception cref="T:System.ArgumentOutOfRangeException">
644 /// The exception is thrown if the
645 /// <see cref="T:System.DateTime"/> parameter is outside all
648 public override int GetMonth(DateTime time
) {
649 M_CheckDateTime(time
);
650 return CCGregorianCalendar
.GetMonth(time
);
654 /// Overridden. Gives the number of months in the specified year
657 /// <param name="year">An integer that specifies the year.
659 /// <param name="era">An integer that specifies the era.
661 /// <returns>An integer that gives the number of the months in the
662 /// specified year.</returns>
663 /// <exception cref="T:System.ArgumentOutOfRangeException">
664 /// The exception is thrown, if the year or the era are not valid.
666 public override int GetMonthsInYear(int year
, int era
) {
667 M_CheckYE(year
, ref era
);
672 /// Overridden. Gives the number of the year of the specified
675 /// <param name="time">The
676 /// <see cref="T:System.DateTime"/> that specifies a
679 /// <returns>An integer representing the year,
680 /// starting with 1.</returns>
681 /// <exception cref="T:System.ArgumentOutOfRangeException">
682 /// The exception is thrown if the
683 /// <see cref="T:System.DateTime"/> parameter is outside all
686 public override int GetYear(DateTime time
) {
687 // M_CheckDateTime not needed, because EraYeat does the
689 int rd
= CCFixed
.FromDateTime(time
);
691 return M_EraHandler
.EraYear(out era
, rd
);
695 /// Overridden. Tells whether the given day
698 /// <param name="year">An integer that specifies the year in the
701 /// <param name="month">An integer that specifies the month.
703 /// <param name="day">An integer that specifies the day.
705 /// <param name="era">An integer that specifies the era.
707 /// <returns>A boolean that tells whether the given day is a leap
710 /// <exception cref="T:System.ArgumentOutOfRangeException">
711 /// The exception is thrown, if the year, month, day, or era is not
714 public override bool IsLeapDay(int year
, int month
, int day
, int era
)
716 int gregorianYear
= M_CheckYMDEG(year
, month
, day
, ref era
);
717 return CCGregorianCalendar
.IsLeapDay(gregorianYear
, month
, day
);
721 /// Overridden. Tells whether the given month
724 /// <param name="year">An integer that specifies the year in the
727 /// <param name="month">An integer that specifies the month.
729 /// <param name="era">An integer that specifies the era.
731 /// <returns>A boolean that tells whether the given month is a leap
734 /// <exception cref="T:System.ArgumentOutOfRangeException">
735 /// The exception is thrown, if the year, month, or era is not
738 public override bool IsLeapMonth(int year
, int month
, int era
) {
739 M_CheckYMEG(year
, month
, ref era
);
744 /// Overridden. Tells whether the given year
747 /// <param name="year">An integer that specifies the year in the
750 /// <param name="era">An integer that specifies the era.
752 /// <returns>A boolean that tells whether the given year is a leap
755 /// <exception cref="T:System.ArgumentOutOfRangeException">
756 /// The exception is thrown, if the year or era is not
759 public override bool IsLeapYear(int year
, int era
) {
760 int gregorianYear
= M_CheckYEG(year
, ref era
);
761 return CCGregorianCalendar
.is_leap_year(gregorianYear
);
765 /// Overridden. Creates the
766 /// <see cref="T:System.DateTime"/> from the parameters.
768 /// <param name="year">An integer that gives the year in the
769 /// <paramref name="era"/>.
771 /// <param name="month">An integer that specifies the month.
773 /// <param name="day">An integer that specifies the day.
775 /// <param name="hour">An integer that specifies the hour.
777 /// <param name="minute">An integer that specifies the minute.
779 /// <param name="second">An integer that gives the second.
781 /// <param name="milliseconds">An integer that gives the
784 /// <param name="era">An integer that specifies the era.
787 /// <see cref="T:system.DateTime"/> representig the date and time.
789 /// <exception cref="T:System.ArgumentOutOfRangeException">
790 /// The exception is thrown, if at least one of the parameters
793 public override DateTime
ToDateTime(int year
, int month
, int day
,
794 int hour
, int minute
, int second
, int milliseconds
,
797 int gregorianYear
= M_CheckYMDEG(year
, month
, day
, ref era
);
798 M_CheckHMSM(hour
, minute
, second
, milliseconds
);
799 return CCGregorianCalendar
.ToDateTime(
800 gregorianYear
, month
, day
,
801 hour
, minute
, second
, milliseconds
);
806 /// This functions returns simply the year for the Japanese calendar.
808 /// <param name="year">An integer that gives the year.
810 /// <returns>The same argument as the year.
812 /// <exception cref="T:System.ArgumentOutOfRangeException">
813 /// The exception is thrown if the year is negative or the resulting
816 public override int ToFourDigitYear(int year
) {
818 throw new ArgumentOutOfRangeException(
819 "year", "Non-negative number required.");
820 int era
= CurrentEra
;
821 M_CheckYE(year
, ref era
);
826 public override CalendarAlgorithmType AlgorithmType
{
828 return CalendarAlgorithmType
.SolarCalendar
;
832 static DateTime JapanMin
= new DateTime (1868, 9, 8, 0, 0, 0);
833 static DateTime JapanMax
= new DateTime (9999, 12, 31, 11, 59, 59);
835 public override DateTime MinSupportedDateTime
{
841 public override DateTime MaxSupportedDateTime
{
848 } // class JapaneseCalendar
850 } // namespace System.Globalization