1 // System.Globalization.JapaneseCalendar.cs
3 // (C) Ulrich Kunitz 2002
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace System.Globalization {
32 using System.Runtime.InteropServices;
35 /// This is the Japanese calendar. It differs from the Gregorian calendar
36 /// only in the years.
39 /// <para>The Japanese calendar support four eras.</para>
40 /// <list type="table">
42 /// <term>era number</term>
43 /// <term>Gregorian start date</term>
44 /// <term>Gregorian end date</term>
48 /// <term>September 8, 1868</term>
49 /// <term>July 29, 1912</term>
53 /// <term>July 30, 1912</term>
54 /// <term>December 24, 1926</term>
58 /// <term>December 25, 1926</term>
59 /// <term>January 7, 1989</term>
63 /// <term>January 8, 1989</term>
64 /// <term>present</term>
67 /// <para>The implementation uses the
68 /// <see cref="N:CalendricalCalculations"/> namespace.
75 [MonoTODO ("Serialization format not compatible with .NET")]
76 public class JapaneseCalendar : Calendar {
78 /// Static protected field storing the
79 /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
81 internal static readonly CCGregorianEraHandler M_EraHandler;
84 /// Static constructor, who creates and initializes
85 /// <see cref="F:M_EraHandler"/>.
87 static JapaneseCalendar() {
88 M_EraHandler = new CCGregorianEraHandler();
89 M_EraHandler.appendEra(1,
90 CCGregorianCalendar.fixed_from_dmy(8, 9, 1868),
91 CCGregorianCalendar.fixed_from_dmy(29, 7, 1912));
92 M_EraHandler.appendEra(2,
93 CCGregorianCalendar.fixed_from_dmy(30, 7, 1912),
94 CCGregorianCalendar.fixed_from_dmy(24, 12, 1926));
95 M_EraHandler.appendEra(3,
96 CCGregorianCalendar.fixed_from_dmy(25, 12, 1926),
97 CCGregorianCalendar.fixed_from_dmy(7, 1, 1989));
98 M_EraHandler.appendEra(4,
99 CCGregorianCalendar.fixed_from_dmy(8, 1, 1989));
103 /// Default constructor.
105 public JapaneseCalendar() {
106 M_AbbrEraNames = new string[] { "M", "T", "S", "H" };
107 M_EraNames = new string[] { "Meiji", "Taisho", "Showa",
112 /// <value>Overridden. Gives the eras supported by the
113 /// calendar as an array of integers.
115 public override int[] Eras {
117 return (int[])M_EraHandler.Eras.Clone();
121 public override int TwoDigitYearMax
124 return twoDigitYearMax;
128 M_ArgumentInRange ("value", value, 100, M_MaxYear);
130 twoDigitYearMax = value;
135 /// A protected member checking a
136 /// <see cref="T:System.DateTime"/> value.
138 /// <param name="time">The
139 /// <see cref="T:System.DateTime"/>
142 /// <exception cref="T:System.ArgumentOutOfRangeException">
143 /// The exception is thrown if the
144 /// <see cref="T:System.DateTime"/> parameter is outside all
147 internal void M_CheckDateTime(DateTime time) {
148 M_EraHandler.CheckDateTime(time);
152 /// A protected method checking the era number.
154 /// <param name="era">The era number as reference. It is set
155 /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
156 /// <exception name="T:System.ArgumentException">
157 /// The exception is thrown if the era is not supported by the class.
159 internal void M_CheckEra(ref int era) {
160 if (era == CurrentEra)
162 if (!M_EraHandler.ValidEra(era))
163 throw new ArgumentException("Era value was not valid.");
167 /// A protected method checking calendar year and the era number.
169 /// <param name="year">An integer representing the calendar year.
171 /// <param name="era">The era number as reference.</param>
172 /// <exception name="T:System.ArgumentException">
173 /// The exception is thrown if the era is not supported by the class.
175 /// <exception cref="T:System.ArgumentOutOfRangeException">
176 /// The exception is thrown if the calendar year is outside of
177 /// the supported range.
179 internal int M_CheckYEG(int year, ref int era) {
181 return M_EraHandler.GregorianYear(year, era);
185 /// Checks whether the year is the era is valid, if era = CurrentEra
186 /// the right value is set.
188 /// <param name="year">The year to check.</param>
189 /// <param name="era">The era to check.</Param>
190 /// <exception cref="T:ArgumentOutOfRangeException">
191 /// The exception will be thrown, if the year is not valid.
193 internal override void M_CheckYE(int year, ref int era) {
194 M_CheckYEG(year, ref era);
198 /// A protected method checking the calendar year, month, and
201 /// <param name="year">An integer representing the calendar year.
203 /// <param name="month">An integer giving the calendar month.
205 /// <param name="era">The era number as reference.</param>
206 /// <exception name="T:System.ArgumentException">
207 /// The exception is thrown if the era is not supported by the class.
209 /// <exception cref="T:System.ArgumentOutOfRangeException">
210 /// The exception is thrown if the calendar year or month is
211 /// outside of the supported range.
213 internal int M_CheckYMEG(int year, int month, ref int era) {
214 int gregorianYear = M_CheckYEG(year, ref era);
215 if (month < 1 || month > 12)
216 throw new ArgumentOutOfRangeException("month",
217 "Month must be between one and twelve.");
218 return gregorianYear;
222 /// A protected method checking the calendar day, month, and year
223 /// and the era number.
225 /// <param name="year">An integer representing the calendar year.
227 /// <param name="month">An integer giving the calendar month.
229 /// <param name="day">An integer giving the calendar day.
231 /// <param name="era">The era number as reference.</param>
232 /// <exception name="T:System.ArgumentException">
233 /// The exception is thrown if the era is not supported by the class.
235 /// <exception cref="T:System.ArgumentOutOfRangeException">
236 /// The exception is thrown if the calendar year, month, or day is
237 /// outside of the supported range.
239 internal int M_CheckYMDEG(int year, int month, int day, ref int era)
241 int gregorianYear = M_CheckYMEG(year, month, ref era);
242 M_ArgumentInRange("day", day, 1, GetDaysInMonth(year, month, era));
243 return gregorianYear;
247 /// Overrideden. Adds months to a given date.
249 /// <param name="time">The
250 /// <see cref="T:System.DateTime"/> to which to add
253 /// <param name="months">The number of months to add.</param>
254 /// <returns>A new <see cref="T:System.DateTime"/> value, that
255 /// results from adding <paramref name="months"/> to the specified
256 /// DateTime.</returns>
257 /// <exception cref="T:System.ArgumentOutOfRangeException">
258 /// The exception is thrown if
259 /// <see cref="T:System.DateTime"/> return value is outside all
262 public override DateTime AddMonths(DateTime time, int months) {
263 DateTime t = CCGregorianCalendar.AddMonths(time, months);
269 /// Overridden. Adds years to a given date.
271 /// <param name="time">The
272 /// <see cref="T:System.DateTime"/> to which to add
275 /// <param name="years">The number of years to add.</param>
276 /// <returns>A new <see cref="T:System.DateTime"/> value, that
277 /// results from adding <paramref name="years"/> to the specified
278 /// DateTime.</returns>
279 /// <exception cref="T:System.ArgumentOutOfRangeException">
280 /// The exception is thrown if
281 /// <see cref="T:System.DateTime"/> return value is outside all
284 public override DateTime AddYears(DateTime time, int years) {
285 DateTime t = CCGregorianCalendar.AddYears(time, years);
291 /// Overriden. Gets the day of the month from
292 /// <paramref name="time"/>.
294 /// <param name="time">The
295 /// <see cref="T:System.DateTime"/> that specifies a
298 /// <returns>An integer giving the day of months, starting with 1.
300 /// <exception cref="T:System.ArgumentOutOfRangeException">
301 /// The exception is thrown if the
302 /// <see cref="T:System.DateTime"/> parameter is outside all
305 public override int GetDayOfMonth(DateTime time) {
306 M_CheckDateTime(time);
307 return CCGregorianCalendar.GetDayOfMonth(time);
311 /// Overriden. Gets the day of the week from the specified date.
313 /// <param name="time">The
314 /// <see cref="T:System.DateTime"/> that specifies a
317 /// <returns>An integer giving the day of months, starting with 1.
319 /// <exception cref="T:System.ArgumentOutOfRangeException">
320 /// The exception is thrown if the
321 /// <see cref="T:System.DateTime"/> parameter is outside all
324 public override DayOfWeek GetDayOfWeek(DateTime time) {
325 M_CheckDateTime(time);
326 int rd = CCFixed.FromDateTime(time);
327 return (DayOfWeek)CCFixed.day_of_week(rd);
331 /// Overridden. Gives the number of the day in the year.
333 /// <param name="time">The
334 /// <see cref="T:System.DateTime"/> that specifies a
337 /// <returns>An integer representing the day of the year,
338 /// starting with 1.</returns>
339 /// <exception cref="T:System.ArgumentOutOfRangeException">
340 /// The exception is thrown if the
341 /// <see cref="T:System.DateTime"/> parameter is outside all
344 public override int GetDayOfYear(DateTime time) {
345 M_CheckDateTime(time);
346 return CCGregorianCalendar.GetDayOfYear(time);
350 /// Overridden. Gives the number of days in the specified month
351 /// of the given year and era.
353 /// <param name="year">An integer that gives the year.
355 /// <param name="month">An integer that gives the month, starting
357 /// <param name="era">An integer that gives the era of the specified
359 /// <returns>An integer that gives the number of days of the
360 /// specified month.</returns>
361 /// <exception cref="T:System.ArgumentOutOfRangeException">
362 /// The exception is thrown, if <paramref name="month"/>,
363 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
364 /// the allowed range.
366 public override int GetDaysInMonth(int year, int month, int era) {
367 int gregorianYear = M_CheckYMEG(year, month, ref era);
368 return CCGregorianCalendar.GetDaysInMonth(gregorianYear, month);
372 /// Overridden. Gives the number of days of the specified
373 /// year of the given era.
375 /// <param name="year">An integer that specifies the year.
377 /// <param name="era">An ineger that specifies the era.
379 /// <returns>An integer that gives the number of days of the
380 /// specified year.</returns>
381 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
382 /// The exception is thrown, if
383 /// <paramref name="year"/> or <paramref name="era"/> are outside the
386 public override int GetDaysInYear(int year, int era) {
387 int gregorianYear = M_CheckYEG(year, ref era);
388 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
393 /// Overridden. Gives the era of the specified date.
395 /// <param name="time">The
396 /// <see cref="T:System.DateTime"/> that specifies a
399 /// <returns>An integer representing the era of the calendar.
401 /// <exception cref="T:System.ArgumentOutOfRangeException">
402 /// The exception is thrown if the
403 /// <see cref="T:System.DateTime"/> parameter is outside all
406 public override int GetEra(DateTime time) {
407 // M_CheckDateTime not needed, because EraYear does the
409 int rd = CCFixed.FromDateTime(time);
411 M_EraHandler.EraYear(out era, rd);
417 public override int GetLeapMonth (int year, int era)
424 /// Overridden. Gives the number of the month of the specified
427 /// <param name="time">The
428 /// <see cref="T:System.DateTime"/> that specifies a
431 /// <returns>An integer representing the month,
432 /// starting with 1.</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 GetMonth(DateTime time) {
439 M_CheckDateTime(time);
440 return CCGregorianCalendar.GetMonth(time);
444 /// Overridden. Gives the number of months in the specified year
447 /// <param name="year">An integer that specifies the year.
449 /// <param name="era">An integer that specifies the era.
451 /// <returns>An integer that gives the number of the months in the
452 /// specified year.</returns>
453 /// <exception cref="T:System.ArgumentOutOfRangeException">
454 /// The exception is thrown, if the year or the era are not valid.
456 public override int GetMonthsInYear(int year, int era) {
457 M_CheckYE(year, ref era);
463 public override int GetWeekOfYear (DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
465 return base.GetWeekOfYear (time, rule, firstDayOfWeek);
470 /// Overridden. Gives the number of the year of the specified
473 /// <param name="time">The
474 /// <see cref="T:System.DateTime"/> that specifies a
477 /// <returns>An integer representing the year,
478 /// starting with 1.</returns>
479 /// <exception cref="T:System.ArgumentOutOfRangeException">
480 /// The exception is thrown if the
481 /// <see cref="T:System.DateTime"/> parameter is outside all
484 public override int GetYear(DateTime time) {
485 // M_CheckDateTime not needed, because EraYeat does the
487 int rd = CCFixed.FromDateTime(time);
489 return M_EraHandler.EraYear(out era, rd);
493 /// Overridden. Tells whether the given day
496 /// <param name="year">An integer that specifies the year in the
499 /// <param name="month">An integer that specifies the month.
501 /// <param name="day">An integer that specifies the day.
503 /// <param name="era">An integer that specifies the era.
505 /// <returns>A boolean that tells whether the given day is a leap
508 /// <exception cref="T:System.ArgumentOutOfRangeException">
509 /// The exception is thrown, if the year, month, day, or era is not
512 public override bool IsLeapDay(int year, int month, int day, int era)
514 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
515 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
519 /// Overridden. Tells whether the given month
522 /// <param name="year">An integer that specifies the year in the
525 /// <param name="month">An integer that specifies the month.
527 /// <param name="era">An integer that specifies the era.
529 /// <returns>A boolean that tells whether the given month is a leap
532 /// <exception cref="T:System.ArgumentOutOfRangeException">
533 /// The exception is thrown, if the year, month, or era is not
536 public override bool IsLeapMonth(int year, int month, int era) {
537 M_CheckYMEG(year, month, ref era);
542 /// Overridden. Tells whether the given year
545 /// <param name="year">An integer that specifies the year in the
548 /// <param name="era">An integer that specifies the era.
550 /// <returns>A boolean that tells whether the given year is a leap
553 /// <exception cref="T:System.ArgumentOutOfRangeException">
554 /// The exception is thrown, if the year or era is not
557 public override bool IsLeapYear(int year, int era) {
558 int gregorianYear = M_CheckYEG(year, ref era);
559 return CCGregorianCalendar.is_leap_year(gregorianYear);
563 /// Overridden. Creates the
564 /// <see cref="T:System.DateTime"/> from the parameters.
566 /// <param name="year">An integer that gives the year in the
567 /// <paramref name="era"/>.
569 /// <param name="month">An integer that specifies the month.
571 /// <param name="day">An integer that specifies the day.
573 /// <param name="hour">An integer that specifies the hour.
575 /// <param name="minute">An integer that specifies the minute.
577 /// <param name="second">An integer that gives the second.
579 /// <param name="milliseconds">An integer that gives the
582 /// <param name="era">An integer that specifies the era.
585 /// <see cref="T:system.DateTime"/> representig the date and time.
587 /// <exception cref="T:System.ArgumentOutOfRangeException">
588 /// The exception is thrown, if at least one of the parameters
591 public override DateTime ToDateTime(int year, int month, int day,
592 int hour, int minute, int second, int millisecond,
595 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
596 M_CheckHMSM(hour, minute, second, millisecond);
597 return CCGregorianCalendar.ToDateTime(
598 gregorianYear, month, day,
599 hour, minute, second, millisecond);
604 /// This functions returns simply the year for the Japanese calendar.
606 /// <param name="year">An integer that gives the year.
608 /// <returns>The same argument as the year.
610 /// <exception cref="T:System.ArgumentOutOfRangeException">
611 /// The exception is thrown if the year is negative or the resulting
614 public override int ToFourDigitYear(int year) {
616 throw new ArgumentOutOfRangeException(
617 "year", "Non-negative number required.");
618 int era = CurrentEra;
619 M_CheckYE(year, ref era);
625 public override CalendarAlgorithmType AlgorithmType {
627 return CalendarAlgorithmType.SolarCalendar;
631 static DateTime JapanMin = new DateTime (1868, 9, 8, 0, 0, 0);
632 static DateTime JapanMax = new DateTime (9999, 12, 31, 11, 59, 59);
635 public override DateTime MinSupportedDateTime {
642 public override DateTime MaxSupportedDateTime {
649 } // class JapaneseCalendar
651 } // namespace System.Globalization