merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[mono.git] / mcs / class / corlib / System.Globalization / KoreanCalendar.cs
1 // KoreanCalendar.cs
2 //
3 // (C) Ulrich Kunitz 2002
4 //
5
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 namespace System.Globalization {
30
31 using System;
32
33 /// <summary>
34 /// This is the Korean calendar. It differs from the Gegorian calendar only
35 /// in the year counting.
36 /// </summary>
37 /// <remarks>
38 /// <para>The implementation uses the
39 /// <see cref="N:CalendricalCalculations"/> namespace.
40 /// </para>
41 /// </remarks>
42 [Serializable]
43 [MonoTODO ("Fix serialization compatibility with MS.NET")]
44 public class KoreanCalendar : Calendar {
45         /// <summary>
46         /// Static protected field storing the
47         /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
48         /// </summary>
49         internal static readonly CCGregorianEraHandler M_EraHandler;
50
51         /// <variable>
52         /// The standard era for the <see cref="T:KoreanCalendar"/>.
53         /// </variable>
54         public const int KoreanEra = 1;
55
56         /// <summary>
57         /// Static constructor, who creates and initializes
58         /// <see cref="F:M_EraHandler"/>.
59         /// </summary>
60         static KoreanCalendar() {
61                 M_EraHandler = new CCGregorianEraHandler();
62                 M_EraHandler.appendEra(KoreanEra,
63                         CCGregorianCalendar.fixed_from_dmy(1, 1, -2332));
64         }
65
66         /// <summary>
67         /// Default constructor.
68         /// </summary>
69         public KoreanCalendar() {
70                 M_AbbrEraNames = new string[] {"K.C.E."};
71                 M_EraNames = new string[] {"Korean Current Era"};
72                 if (M_TwoDigitYearMax == 99)
73                         M_TwoDigitYearMax = 4362;
74         }
75
76         /// <value>Overridden. Gives the eras supported by the
77         /// calendar as an array of integers.
78         /// </value>
79         public override int[] Eras {
80                 get {
81                         return (int[])M_EraHandler.Eras.Clone();
82                 }
83         }
84
85         int twoDigitYearMax = 4362;
86
87         public override int TwoDigitYearMax 
88         {
89                 get {
90                         return twoDigitYearMax;
91                 }
92                 set {
93                         CheckReadOnly ();
94                         M_ArgumentInRange ("value", value, 100, M_MaxYear);
95
96                         twoDigitYearMax = value;
97                 }
98         }
99         
100         /// <summary>
101         /// A protected method checking the era number.
102         /// </summary>
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.
107         /// </exception>
108         internal void M_CheckEra(ref int era) {
109                 if (era == CurrentEra)
110                         era = KoreanEra;
111                 if (!M_EraHandler.ValidEra(era))
112                         throw new ArgumentException("Era value was not valid.");
113         }
114
115         /// <summary>
116         /// A protected method checking calendar year and the era number.
117         /// </summary>
118         /// <param name="year">An integer representing the calendar year.
119         /// </param>
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.
123         /// </exception>
124         /// <exception cref="T:System.ArgumentOutOfRangeException">
125         /// The exception is thrown if the calendar year is outside of
126         /// the supported range.
127         /// </exception>
128         internal int M_CheckYEG(int year, ref int era) {
129                 M_CheckEra(ref era);
130                 return M_EraHandler.GregorianYear(year, era);
131         }
132
133         /// <summary>
134         /// Checks whether the year is the era is valid, if era = CurrentEra
135         /// the right value is set.
136         /// </summary>
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.
141         /// </exception>
142         internal override void M_CheckYE(int year, ref int era) {
143                 M_CheckYEG(year, ref era);
144         }
145
146         /// <summary>
147         /// A protected method checking the calendar year, month, and
148         /// era number.
149         /// </summary>
150         /// <param name="year">An integer representing the calendar year.
151         /// </param>
152         /// <param name="month">An integer giving the calendar month.
153         /// </param>
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.
157         /// </exception>
158         /// <exception cref="T:System.ArgumentOutOfRangeException">
159         /// The exception is thrown if the calendar year or month is
160         /// outside of the supported range.
161         /// </exception>
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;
168         }
169
170         /// <summary>
171         /// A protected method checking the calendar day, month, and year
172         /// and the era number.
173         /// </summary>
174         /// <param name="year">An integer representing the calendar year.
175         /// </param>
176         /// <param name="month">An integer giving the calendar month.
177         /// </param>
178         /// <param name="day">An integer giving the calendar day.
179         /// </param>
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.
183         /// </exception>
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.
187         /// </exception>
188         internal int M_CheckYMDEG(int year, int month, int day, ref int era)
189         {
190                 int gregorianYear = M_CheckYMEG(year, month, ref era);
191                 M_ArgumentInRange("day", day, 1,
192                         GetDaysInMonth(year, month, era));
193                 return gregorianYear;
194         }
195
196         /// <summary>
197         /// Overrideden. Adds months to a given date.
198         /// </summary>
199         /// <param name="time">The
200         /// <see cref="T:System.DateTime"/> to which to add
201         /// months.
202         /// </param>
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);
209         }
210
211         /// <summary>
212         /// Overridden. Adds years to a given date.
213         /// </summary>
214         /// <param name="time">The
215         /// <see cref="T:System.DateTime"/> to which to add
216         /// years.
217         /// </param>
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);
224         }
225                 
226         /// <summary>
227         /// Overriden. Gets the day of the month from
228         /// <paramref name="time"/>.
229         /// </summary>
230         /// <param name="time">The
231         /// <see cref="T:System.DateTime"/> that specifies a
232         /// date.
233         /// </param>
234         /// <returns>An integer giving the day of months, starting with 1.
235         /// </returns>
236         public override int GetDayOfMonth(DateTime time) {
237                 return CCGregorianCalendar.GetDayOfMonth(time);
238         }
239
240         /// <summary>
241         /// Overriden. Gets the day of the week from the specified date.
242         /// </summary>
243         /// <param name="time">The
244         /// <see cref="T:System.DateTime"/> that specifies a
245         /// date.
246         /// </param>
247         /// <returns>An integer giving the day of months, starting with 1.
248         /// </returns>
249         public override DayOfWeek GetDayOfWeek(DateTime time) {
250                 int rd = CCFixed.FromDateTime(time);
251                 return (DayOfWeek)CCFixed.day_of_week(rd);
252         }
253
254         /// <summary>
255         /// Overridden. Gives the number of the day in the year.
256         /// </summary>
257         /// <param name="time">The
258         /// <see cref="T:System.DateTime"/> that specifies a
259         /// date.
260         /// </param>
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);
265         }
266
267         /// <summary>
268         /// Overridden. Gives the number of days in the specified month
269         /// of the given year and era.
270         /// </summary>
271         /// <param name="year">An integer that gives the year.
272         /// </param>
273         /// <param name="month">An integer that gives the month, starting
274         /// with 1.</param>
275         /// <param name="era">An integer that gives the era of the specified
276         /// year.</param>
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.
283         /// </exception>
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);
287         }
288
289         /// <summary>
290         /// Overridden. Gives the number of days of the specified
291         /// year of the given era. 
292         /// </summary>
293         /// <param name="year">An integer that specifies the year. 
294         /// </param>
295         /// <param name="era">An ineger that specifies the era.
296         /// </param>
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
302         /// allowed range.
303         /// </exception>
304         public override int GetDaysInYear(int year, int era) {
305                 int gregorianYear = M_CheckYEG(year, ref era);
306                 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
307         }
308
309         /// <summary>
310         /// Overridden. Gives the era of the specified date.
311         /// </summary>
312         /// <param name="time">The
313         /// <see cref="T:System.DateTime"/> that specifies a
314         /// date.
315         /// </param>
316         /// <returns>An integer representing the era of the calendar.
317         /// </returns>
318         public override int GetEra(DateTime time) {
319                 int rd = CCFixed.FromDateTime(time);
320                 int era;
321                 M_EraHandler.EraYear(out era, rd);
322                 return era;
323         }
324
325         /// <summary>
326         /// Overridden. Gives the number of the month of the specified
327         /// date.
328         /// </summary>
329         /// <param name="time">The
330         /// <see cref="T:System.DateTime"/> that specifies a
331         /// date.
332         /// </param>
333         /// <returns>An integer representing the month, 
334         /// starting with 1.</returns>
335         public override int GetMonth(DateTime time) {
336                 return CCGregorianCalendar.GetMonth(time);
337         }
338
339         /// <summary>
340         /// Overridden. Gives the number of months in the specified year 
341         /// and era.
342         /// </summary>
343         /// <param name="year">An integer that specifies the year.
344         /// </param>
345         /// <param name="era">An integer that specifies the era.
346         /// </param>
347         /// <returns>An integer that gives the number of the months in the
348         /// specified year.</returns>
349         /// <exception cref="T:System.ArgumentOutOfRangeException">
350         /// The exception is thrown, if the year or the era are not valid.
351         /// </exception>
352         public override int GetMonthsInYear(int year, int era) {
353                 M_CheckYEG(year, ref era);
354                 return 12;
355         }
356
357         /// <summary>
358         /// Overridden. Gives the number of the year of the specified
359         /// date.
360         /// </summary>
361         /// <param name="time">The
362         /// <see cref="T:System.DateTime"/> that specifies a
363         /// date.
364         /// </param>
365         /// <returns>An integer representing the year, 
366         /// starting with 1.</returns>
367         public override int GetYear(DateTime time) {
368                 int rd = CCFixed.FromDateTime(time);
369                 int era;
370                 return M_EraHandler.EraYear(out era, rd);
371         }
372
373         /// <summary>
374         /// Overridden. Tells whether the given day 
375         /// is a leap day.
376         /// </summary>
377         /// <param name="year">An integer that specifies the year in the
378         /// given era.
379         /// </param>
380         /// <param name="month">An integer that specifies the month.
381         /// </param>
382         /// <param name="day">An integer that specifies the day.
383         /// </param>
384         /// <param name="era">An integer that specifies the era.
385         /// </param>
386         /// <returns>A boolean that tells whether the given day is a leap
387         /// day.
388         /// </returns>
389         /// <exception cref="T:System.ArgumentOutOfRangeException">
390         /// The exception is thrown, if the year, month, day, or era is not
391         /// valid.
392         /// </exception>
393         public override bool IsLeapDay(int year, int month, int day, int era)
394         {
395                 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
396                 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
397         }
398
399         /// <summary>
400         /// Overridden. Tells whether the given month 
401         /// is a leap month.
402         /// </summary>
403         /// <param name="year">An integer that specifies the year in the
404         /// given era.
405         /// </param>
406         /// <param name="month">An integer that specifies the month.
407         /// </param>
408         /// <param name="era">An integer that specifies the era.
409         /// </param>
410         /// <returns>A boolean that tells whether the given month is a leap
411         /// month.
412         /// </returns>
413         /// <exception cref="T:System.ArgumentOutOfRangeException">
414         /// The exception is thrown, if the year, month, or era is not
415         /// valid.
416         /// </exception>
417         public override bool IsLeapMonth(int year, int month, int era) {
418                 M_CheckYMEG(year, month, ref era);
419                 return false;
420         }
421
422         /// <summary>
423         /// Overridden. Tells whether the given year
424         /// is a leap year.
425         /// </summary>
426         /// <param name="year">An integer that specifies the year in the
427         /// given era.
428         /// </param>
429         /// <param name="era">An integer that specifies the era.
430         /// </param>
431         /// <returns>A boolean that tells whether the given year is a leap
432         /// year.
433         /// </returns>
434         /// <exception cref="T:System.ArgumentOutOfRangeException">
435         /// The exception is thrown, if the year or era is not
436         /// valid.
437         /// </exception>
438         public override bool IsLeapYear(int year, int era) {
439                 int gregorianYear = M_CheckYEG(year, ref era);
440                 return CCGregorianCalendar.is_leap_year(gregorianYear);
441         }
442
443         /// <summary>
444         /// Overridden. Creates the
445         /// <see cref="T:System.DateTime"/> from the parameters.
446         /// </summary>
447         /// <param name="year">An integer that gives the year in the
448         /// <paramref name="era"/>.
449         /// </param>
450         /// <param name="month">An integer that specifies the month.
451         /// </param>
452         /// <param name="day">An integer that specifies the day.
453         /// </param>
454         /// <param name="hour">An integer that specifies the hour.
455         /// </param>
456         /// <param name="minute">An integer that specifies the minute.
457         /// </param>
458         /// <param name="second">An integer that gives the second.
459         /// </param>
460         /// <param name="milliseconds">An integer that gives the
461         /// milliseconds.
462         /// </param>
463         /// <param name="era">An integer that specifies the era.
464         /// </param>
465         /// <returns>A
466         /// <see cref="T:system.DateTime"/> representig the date and time.
467         /// </returns>
468         /// <exception cref="T:System.ArgumentOutOfRangeException">
469         /// The exception is thrown, if at least one of the parameters
470         /// is out of range.
471         /// </exception>
472         public override DateTime ToDateTime(int year, int month, int day,
473                 int hour, int minute, int second, int milliseconds,
474                 int era)
475         {
476                 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
477                 M_CheckHMSM(hour, minute, second, milliseconds);
478                 return CCGregorianCalendar.ToDateTime(gregorianYear,
479                         month, day, hour, minute, second, milliseconds);
480         }
481
482         [MonoTODO]
483         public override int ToFourDigitYear(int year)
484         {
485                 throw new NotImplementedException();
486         }
487         
488 #if NET_2_0
489         public override CalendarAlgorithmType AlgorithmType {
490                 get {
491                         return CalendarAlgorithmType.SolarCalendar;
492                 }
493         }
494
495         static DateTime KoreanMin = new DateTime (1, 1, 1, 0, 0, 0);
496         static DateTime KoreanMax = new DateTime (9999, 12, 31, 11, 59, 59);
497                 
498         public override DateTime MinSupportedDateTime {
499                 get {
500                         return KoreanMin;
501                 }
502         }
503
504         public override DateTime MaxSupportedDateTime {
505                 get {
506                         return KoreanMax;
507                 }
508         }
509 #endif
510         
511 } // class KoreanCalendar
512         
513 } // namespace System.Globalization