1 // System.Globalization.KoreanCalendar.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 Korean calendar. It differs from the Gegorian calendar only
36 /// in the year counting.
39 /// <para>The implementation uses the
40 /// <see cref="N:CalendricalCalculations"/> namespace.
45 [MonoLimitation ("Serialization format not compatible with .NET")]
46 public class KoreanCalendar
: Calendar
{
48 /// Static protected field storing the
49 /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
51 internal static readonly CCGregorianEraHandler M_EraHandler
;
54 /// The standard era for the <see cref="T:KoreanCalendar"/>.
56 public const int KoreanEra
= 1;
59 /// Static constructor, who creates and initializes
60 /// <see cref="F:M_EraHandler"/>.
62 static KoreanCalendar() {
63 M_EraHandler
= new CCGregorianEraHandler();
64 M_EraHandler
.appendEra(KoreanEra
,
65 CCGregorianCalendar
.fixed_from_dmy(1, 1, -2332));
69 /// Default constructor.
71 public KoreanCalendar() {
72 M_AbbrEraNames
= new string[] {"K.C.E."}
;
73 M_EraNames
= new string[] {"Korean Current Era"}
;
74 if (twoDigitYearMax
== 99)
75 twoDigitYearMax
= 4362;
78 /// <value>Overridden. Gives the eras supported by the
79 /// calendar as an array of integers.
81 public override int[] Eras
{
83 return (int[])M_EraHandler
.Eras
.Clone();
87 public override int TwoDigitYearMax
90 return twoDigitYearMax
;
94 M_ArgumentInRange ("value", value, 100, M_MaxYear
);
96 twoDigitYearMax
= value;
101 /// A protected method checking the era number.
103 /// <param name="era">The era number as reference. It is set
104 /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
105 /// <exception name="T:System.ArgumentException">
106 /// The exception is thrown if the era is not supported by the class.
108 internal void M_CheckEra(ref int era
) {
109 if (era
== CurrentEra
)
111 if (!M_EraHandler
.ValidEra(era
))
112 throw new ArgumentException("Era value was not valid.");
116 /// A protected method checking calendar year and the era number.
118 /// <param name="year">An integer representing the calendar year.
120 /// <param name="era">The era number as reference.</param>
121 /// <exception name="T:System.ArgumentException">
122 /// The exception is thrown if the era is not supported by the class.
124 /// <exception cref="T:System.ArgumentOutOfRangeException">
125 /// The exception is thrown if the calendar year is outside of
126 /// the supported range.
128 internal int M_CheckYEG(int year
, ref int era
) {
130 return M_EraHandler
.GregorianYear(year
, era
);
134 /// Checks whether the year is the era is valid, if era = CurrentEra
135 /// the right value is set.
137 /// <param name="year">The year to check.</param>
138 /// <param name="era">The era to check.</Param>
139 /// <exception cref="T:ArgumentOutOfRangeException">
140 /// The exception will be thrown, if the year is not valid.
142 internal override void M_CheckYE(int year
, ref int era
) {
143 M_CheckYEG(year
, ref era
);
147 /// A protected method checking the calendar year, month, and
150 /// <param name="year">An integer representing the calendar year.
152 /// <param name="month">An integer giving the calendar month.
154 /// <param name="era">The era number as reference.</param>
155 /// <exception name="T:System.ArgumentException">
156 /// The exception is thrown if the era is not supported by the class.
158 /// <exception cref="T:System.ArgumentOutOfRangeException">
159 /// The exception is thrown if the calendar year or month is
160 /// outside of the supported range.
162 internal int M_CheckYMEG(int year
, int month
, ref int era
) {
163 int gregorianYear
= M_CheckYEG(year
, ref era
);
164 if (month
< 1 || month
> 12)
165 throw new ArgumentOutOfRangeException("month",
166 "Month must be between one and twelve.");
167 return gregorianYear
;
171 /// A protected method checking the calendar day, month, and year
172 /// and the era number.
174 /// <param name="year">An integer representing the calendar year.
176 /// <param name="month">An integer giving the calendar month.
178 /// <param name="day">An integer giving the calendar day.
180 /// <param name="era">The era number as reference.</param>
181 /// <exception name="T:System.ArgumentException">
182 /// The exception is thrown if the era is not supported by the class.
184 /// <exception cref="T:System.ArgumentOutOfRangeException">
185 /// The exception is thrown if the calendar year, month, or day is
186 /// outside of the supported range.
188 internal int M_CheckYMDEG(int year
, int month
, int day
, ref int era
)
190 int gregorianYear
= M_CheckYMEG(year
, month
, ref era
);
191 M_ArgumentInRange("day", day
, 1,
192 GetDaysInMonth(year
, month
, era
));
193 return gregorianYear
;
197 /// Overrideden. Adds months to a given date.
199 /// <param name="time">The
200 /// <see cref="T:System.DateTime"/> to which to add
203 /// <param name="months">The number of months to add.</param>
204 /// <returns>A new <see cref="T:System.DateTime"/> value, that
205 /// results from adding <paramref name="months"/> to the specified
206 /// DateTime.</returns>
207 public override DateTime
AddMonths(DateTime time
, int months
) {
208 return CCGregorianCalendar
.AddMonths(time
, months
);
212 /// Overridden. Adds years to a given date.
214 /// <param name="time">The
215 /// <see cref="T:System.DateTime"/> to which to add
218 /// <param name="years">The number of years to add.</param>
219 /// <returns>A new <see cref="T:System.DateTime"/> value, that
220 /// results from adding <paramref name="years"/> to the specified
221 /// DateTime.</returns>
222 public override DateTime
AddYears(DateTime time
, int years
) {
223 return CCGregorianCalendar
.AddYears(time
, years
);
227 /// Overriden. Gets the day of the month from
228 /// <paramref name="time"/>.
230 /// <param name="time">The
231 /// <see cref="T:System.DateTime"/> that specifies a
234 /// <returns>An integer giving the day of months, starting with 1.
236 public override int GetDayOfMonth(DateTime time
) {
237 return CCGregorianCalendar
.GetDayOfMonth(time
);
241 /// Overriden. Gets the day of the week from the specified date.
243 /// <param name="time">The
244 /// <see cref="T:System.DateTime"/> that specifies a
247 /// <returns>An integer giving the day of months, starting with 1.
249 public override DayOfWeek
GetDayOfWeek(DateTime time
) {
250 int rd
= CCFixed
.FromDateTime(time
);
251 return (DayOfWeek
)CCFixed
.day_of_week(rd
);
255 /// Overridden. Gives the number of the day in the year.
257 /// <param name="time">The
258 /// <see cref="T:System.DateTime"/> that specifies a
261 /// <returns>An integer representing the day of the year,
262 /// starting with 1.</returns>
263 public override int GetDayOfYear(DateTime time
) {
264 return CCGregorianCalendar
.GetDayOfYear(time
);
268 /// Overridden. Gives the number of days in the specified month
269 /// of the given year and era.
271 /// <param name="year">An integer that gives the year.
273 /// <param name="month">An integer that gives the month, starting
275 /// <param name="era">An integer that gives the era of the specified
277 /// <returns>An integer that gives the number of days of the
278 /// specified month.</returns>
279 /// <exception cref="T:System.ArgumentOutOfRangeException">
280 /// The exception is thrown, if <paramref name="month"/>,
281 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
282 /// the allowed range.
284 public override int GetDaysInMonth(int year
, int month
, int era
) {
285 int gregorianYear
= M_CheckYMEG(year
, month
, ref era
);
286 return CCGregorianCalendar
.GetDaysInMonth(gregorianYear
, month
);
290 /// Overridden. Gives the number of days of the specified
291 /// year of the given era.
293 /// <param name="year">An integer that specifies the year.
295 /// <param name="era">An ineger that specifies the era.
297 /// <returns>An integer that gives the number of days of the
298 /// specified year.</returns>
299 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
300 /// The exception is thrown, if
301 /// <paramref name="year"/> or <paramref name="era"/> are outside the
304 public override int GetDaysInYear(int year
, int era
) {
305 int gregorianYear
= M_CheckYEG(year
, ref era
);
306 return CCGregorianCalendar
.GetDaysInYear(gregorianYear
);
310 /// Overridden. Gives the era of the specified date.
312 /// <param name="time">The
313 /// <see cref="T:System.DateTime"/> that specifies a
316 /// <returns>An integer representing the era of the calendar.
318 public override int GetEra(DateTime time
) {
319 int rd
= CCFixed
.FromDateTime(time
);
321 M_EraHandler
.EraYear(out era
, rd
);
326 public override int GetLeapMonth (int year
, int era
)
332 /// Overridden. Gives the number of the month of the specified
335 /// <param name="time">The
336 /// <see cref="T:System.DateTime"/> that specifies a
339 /// <returns>An integer representing the month,
340 /// starting with 1.</returns>
341 public override int GetMonth(DateTime time
) {
342 return CCGregorianCalendar
.GetMonth(time
);
346 /// Overridden. Gives the number of months in the specified year
349 /// <param name="year">An integer that specifies the year.
351 /// <param name="era">An integer that specifies the era.
353 /// <returns>An integer that gives the number of the months in the
354 /// specified year.</returns>
355 /// <exception cref="T:System.ArgumentOutOfRangeException">
356 /// The exception is thrown, if the year or the era are not valid.
358 public override int GetMonthsInYear(int year
, int era
) {
359 M_CheckYEG(year
, ref era
);
364 public override int GetWeekOfYear (DateTime time
, CalendarWeekRule rule
, DayOfWeek firstDayOfWeek
)
366 return base.GetWeekOfYear (time
, rule
, firstDayOfWeek
);
370 /// Overridden. Gives the number of the year of the specified
373 /// <param name="time">The
374 /// <see cref="T:System.DateTime"/> that specifies a
377 /// <returns>An integer representing the year,
378 /// starting with 1.</returns>
379 public override int GetYear(DateTime time
) {
380 int rd
= CCFixed
.FromDateTime(time
);
382 return M_EraHandler
.EraYear(out era
, rd
);
386 /// Overridden. Tells whether the given day
389 /// <param name="year">An integer that specifies the year in the
392 /// <param name="month">An integer that specifies the month.
394 /// <param name="day">An integer that specifies the day.
396 /// <param name="era">An integer that specifies the era.
398 /// <returns>A boolean that tells whether the given day is a leap
401 /// <exception cref="T:System.ArgumentOutOfRangeException">
402 /// The exception is thrown, if the year, month, day, or era is not
405 public override bool IsLeapDay(int year
, int month
, int day
, int era
)
407 int gregorianYear
= M_CheckYMDEG(year
, month
, day
, ref era
);
408 return CCGregorianCalendar
.IsLeapDay(gregorianYear
, month
, day
);
412 /// Overridden. Tells whether the given month
415 /// <param name="year">An integer that specifies the year in the
418 /// <param name="month">An integer that specifies the month.
420 /// <param name="era">An integer that specifies the era.
422 /// <returns>A boolean that tells whether the given month is a leap
425 /// <exception cref="T:System.ArgumentOutOfRangeException">
426 /// The exception is thrown, if the year, month, or era is not
429 public override bool IsLeapMonth(int year
, int month
, int era
) {
430 M_CheckYMEG(year
, month
, ref era
);
435 /// Overridden. Tells whether the given year
438 /// <param name="year">An integer that specifies the year in the
441 /// <param name="era">An integer that specifies the era.
443 /// <returns>A boolean that tells whether the given year is a leap
446 /// <exception cref="T:System.ArgumentOutOfRangeException">
447 /// The exception is thrown, if the year or era is not
450 public override bool IsLeapYear(int year
, int era
) {
451 int gregorianYear
= M_CheckYEG(year
, ref era
);
452 return CCGregorianCalendar
.is_leap_year(gregorianYear
);
456 /// Overridden. Creates the
457 /// <see cref="T:System.DateTime"/> from the parameters.
459 /// <param name="year">An integer that gives the year in the
460 /// <paramref name="era"/>.
462 /// <param name="month">An integer that specifies the month.
464 /// <param name="day">An integer that specifies the day.
466 /// <param name="hour">An integer that specifies the hour.
468 /// <param name="minute">An integer that specifies the minute.
470 /// <param name="second">An integer that gives the second.
472 /// <param name="milliseconds">An integer that gives the
475 /// <param name="era">An integer that specifies the era.
478 /// <see cref="T:system.DateTime"/> representig the date and time.
480 /// <exception cref="T:System.ArgumentOutOfRangeException">
481 /// The exception is thrown, if at least one of the parameters
484 public override DateTime
ToDateTime(int year
, int month
, int day
,
485 int hour
, int minute
, int second
, int millisecond
,
488 int gregorianYear
= M_CheckYMDEG(year
, month
, day
, ref era
);
489 M_CheckHMSM(hour
, minute
, second
, millisecond
);
490 return CCGregorianCalendar
.ToDateTime(gregorianYear
,
491 month
, day
, hour
, minute
, second
, millisecond
);
494 public override int ToFourDigitYear(int year
)
496 return base.ToFourDigitYear (year
);
501 public override CalendarAlgorithmType AlgorithmType
{
503 return CalendarAlgorithmType
.SolarCalendar
;
508 static DateTime KoreanMin
= new DateTime (1, 1, 1, 0, 0, 0);
509 static DateTime KoreanMax
= new DateTime (9999, 12, 31, 11, 59, 59);
512 public override DateTime MinSupportedDateTime
{
519 public override DateTime MaxSupportedDateTime
{
525 } // class KoreanCalendar
527 } // namespace System.Globalization