2003-05-11 Ben Maurer <bmaurer@users.sourceforge.net>
[mono.git] / mcs / class / corlib / System.Globalization / KoreanCalendar.cs
1 // KoreanCalendar.cs
2 //
3 // (C) Ulrich Kunitz 2002
4 //
5
6 namespace System.Globalization {
7
8 using System;
9
10 /// <summary>
11 /// This is the Korean calendar. It differs from the Gegorian calendar only
12 /// in the year counting.
13 /// </summary>
14 /// <remarks>
15 /// <para>The implementation uses the
16 /// <see cref="N:CalendricalCalculations"/> namespace.
17 /// </para>
18 /// </remarks>
19 [Serializable]
20 public class KoreanCalendar : Calendar {
21         /// <summary>
22         /// Static protected field storing the
23         /// <see cref="T:CalendricalCalculations.GregorianEraHandler"/>.
24         /// </summary>
25         internal static readonly CCGregorianEraHandler M_EraHandler;
26
27         /// <variable>
28         /// The standard era for the <see cref="T:KoreanCalendar"/>.
29         /// </variable>
30         public const int KoreanEra = 1;
31
32         /// <summary>
33         /// Static constructor, who creates and initializes
34         /// <see cref="F:M_EraHandler"/>.
35         /// </summary>
36         static KoreanCalendar() {
37                 M_EraHandler = new CCGregorianEraHandler();
38                 M_EraHandler.appendEra(KoreanEra,
39                         CCGregorianCalendar.fixed_from_dmy(1, 1, -2332));
40         }
41
42         /// <summary>
43         /// Default constructor.
44         /// </summary>
45         public KoreanCalendar() {
46                 M_AbbrEraNames = new string[] {"K.C.E."};
47                 M_EraNames = new string[] {"Korean Current Era"};
48                 if (M_TwoDigitYearMax == 99)
49                         M_TwoDigitYearMax = 4362;
50         }
51
52         /// <value>Overridden. Gives the eras supported by the
53         /// calendar as an array of integers.
54         /// </value>
55         public override int[] Eras {
56                 get {
57                         return (int[])M_EraHandler.Eras.Clone();
58                 }
59         }
60
61         int twoDigitYearMax = 4362;
62
63         public override int TwoDigitYearMax 
64         {
65                 get {
66                         return twoDigitYearMax;
67                 }
68                 set {
69                         M_ArgumentInRange ("value", value, 100, M_MaxYear);
70
71                         twoDigitYearMax = value;
72                 }
73         }
74         
75         /// <summary>
76         /// A protected method checking the era number.
77         /// </summary>
78         /// <param name="era">The era number as reference. It is set
79         /// to <see cref="F:CurrentEra"/>, if the input value is 0.</param>
80         /// <exception name="T:System.ArgumentException">
81         /// The exception is thrown if the era is not supported by the class.
82         /// </exception>
83         internal void M_CheckEra(ref int era) {
84                 if (era == CurrentEra)
85                         era = KoreanEra;
86                 if (!M_EraHandler.ValidEra(era))
87                         throw new ArgumentException("Era value was not valid.");
88         }
89
90         /// <summary>
91         /// A protected method checking calendar year and the era number.
92         /// </summary>
93         /// <param name="year">An integer representing the calendar year.
94         /// </param>
95         /// <param name="era">The era number as reference.</param>
96         /// <exception name="T:System.ArgumentException">
97         /// The exception is thrown if the era is not supported by the class.
98         /// </exception>
99         /// <exception cref="T:System.ArgumentOutOfRangeException">
100         /// The exception is thrown if the calendar year is outside of
101         /// the supported range.
102         /// </exception>
103         internal int M_CheckYEG(int year, ref int era) {
104                 M_CheckEra(ref era);
105                 return M_EraHandler.GregorianYear(year, era);
106         }
107
108         /// <summary>
109         /// Checks whether the year is the era is valid, if era = CurrentEra
110         /// the right value is set.
111         /// </summary>
112         /// <param name="year">The year to check.</param>
113         /// <param name="era">The era to check.</Param>
114         /// <exception cref="T:ArgumentOutOfRangeException">
115         /// The exception will be thrown, if the year is not valid.
116         /// </exception>
117         internal override void M_CheckYE(int year, ref int era) {
118                 M_CheckYEG(year, ref era);
119         }
120
121         /// <summary>
122         /// A protected method checking the calendar year, month, and
123         /// era number.
124         /// </summary>
125         /// <param name="year">An integer representing the calendar year.
126         /// </param>
127         /// <param name="month">An integer giving the calendar month.
128         /// </param>
129         /// <param name="era">The era number as reference.</param>
130         /// <exception name="T:System.ArgumentException">
131         /// The exception is thrown if the era is not supported by the class.
132         /// </exception>
133         /// <exception cref="T:System.ArgumentOutOfRangeException">
134         /// The exception is thrown if the calendar year or month is
135         /// outside of the supported range.
136         /// </exception>
137         internal int M_CheckYMEG(int year, int month, ref int era) {
138                 int gregorianYear = M_CheckYEG(year, ref era);
139                 if (month < 1 || month > 12)
140                         throw new ArgumentOutOfRangeException("month",
141                                 "Month must be between one and twelve.");
142                 return gregorianYear;
143         }
144
145         /// <summary>
146         /// A protected method checking the calendar day, month, and year
147         /// and the era number.
148         /// </summary>
149         /// <param name="year">An integer representing the calendar year.
150         /// </param>
151         /// <param name="month">An integer giving the calendar month.
152         /// </param>
153         /// <param name="day">An integer giving the calendar day.
154         /// </param>
155         /// <param name="era">The era number as reference.</param>
156         /// <exception name="T:System.ArgumentException">
157         /// The exception is thrown if the era is not supported by the class.
158         /// </exception>
159         /// <exception cref="T:System.ArgumentOutOfRangeException">
160         /// The exception is thrown if the calendar year, month, or day is
161         /// outside of the supported range.
162         /// </exception>
163         internal int M_CheckYMDEG(int year, int month, int day, ref int era)
164         {
165                 int gregorianYear = M_CheckYMEG(year, month, ref era);
166                 M_ArgumentInRange("day", day, 1,
167                         GetDaysInMonth(year, month, era));
168                 return gregorianYear;
169         }
170
171         /// <summary>
172         /// Overrideden. Adds months to a given date.
173         /// </summary>
174         /// <param name="time">The
175         /// <see cref="T:System.DateTime"/> to which to add
176         /// months.
177         /// </param>
178         /// <param name="months">The number of months to add.</param>
179         /// <returns>A new <see cref="T:System.DateTime"/> value, that
180         /// results from adding <paramref name="months"/> to the specified
181         /// DateTime.</returns>
182         public override DateTime AddMonths(DateTime time, int months) {
183                 return CCGregorianCalendar.AddMonths(time, months);
184         }
185
186         /// <summary>
187         /// Overridden. Adds years to a given date.
188         /// </summary>
189         /// <param name="time">The
190         /// <see cref="T:System.DateTime"/> to which to add
191         /// years.
192         /// </param>
193         /// <param name="years">The number of years to add.</param>
194         /// <returns>A new <see cref="T:System.DateTime"/> value, that
195         /// results from adding <paramref name="years"/> to the specified
196         /// DateTime.</returns>
197         public override DateTime AddYears(DateTime time, int years) {
198                 return CCGregorianCalendar.AddYears(time, years);
199         }
200                 
201         /// <summary>
202         /// Overriden. Gets the day of the month from
203         /// <paramref name="time"/>.
204         /// </summary>
205         /// <param name="time">The
206         /// <see cref="T:System.DateTime"/> that specifies a
207         /// date.
208         /// </param>
209         /// <returns>An integer giving the day of months, starting with 1.
210         /// </returns>
211         public override int GetDayOfMonth(DateTime time) {
212                 return CCGregorianCalendar.GetDayOfMonth(time);
213         }
214
215         /// <summary>
216         /// Overriden. Gets the day of the week from the specified date.
217         /// </summary>
218         /// <param name="time">The
219         /// <see cref="T:System.DateTime"/> that specifies a
220         /// date.
221         /// </param>
222         /// <returns>An integer giving the day of months, starting with 1.
223         /// </returns>
224         public override DayOfWeek GetDayOfWeek(DateTime time) {
225                 int rd = CCFixed.FromDateTime(time);
226                 return (DayOfWeek)CCFixed.day_of_week(rd);
227         }
228
229         /// <summary>
230         /// Overridden. Gives the number of the day in the year.
231         /// </summary>
232         /// <param name="time">The
233         /// <see cref="T:System.DateTime"/> that specifies a
234         /// date.
235         /// </param>
236         /// <returns>An integer representing the day of the year,
237         /// starting with 1.</returns>
238         public override int GetDayOfYear(DateTime time) {
239                 return CCGregorianCalendar.GetDayOfYear(time);
240         }
241
242         /// <summary>
243         /// Overridden. Gives the number of days in the specified month
244         /// of the given year and era.
245         /// </summary>
246         /// <param name="year">An integer that gives the year.
247         /// </param>
248         /// <param name="month">An integer that gives the month, starting
249         /// with 1.</param>
250         /// <param name="era">An integer that gives the era of the specified
251         /// year.</param>
252         /// <returns>An integer that gives the number of days of the
253         /// specified month.</returns>
254         /// <exception cref="T:System.ArgumentOutOfRangeException">
255         /// The exception is thrown, if <paramref name="month"/>,
256         /// <paramref name="year"/> ,or <paramref name="era"/> is outside
257         /// the allowed range.
258         /// </exception>
259         public override int GetDaysInMonth(int year, int month, int era) {
260                 int gregorianYear = M_CheckYMEG(year, month, ref era);
261                 return CCGregorianCalendar.GetDaysInMonth(gregorianYear, month);
262         }
263
264         /// <summary>
265         /// Overridden. Gives the number of days of the specified
266         /// year of the given era. 
267         /// </summary>
268         /// <param name="year">An integer that specifies the year. 
269         /// </param>
270         /// <param name="era">An ineger that specifies the era.
271         /// </param>
272         /// <returns>An integer that gives the number of days of the
273         /// specified year.</returns>
274         /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
275         /// The exception is thrown, if
276         /// <paramref name="year"/> or <paramref name="era"/> are outside the
277         /// allowed range.
278         /// </exception>
279         public override int GetDaysInYear(int year, int era) {
280                 int gregorianYear = M_CheckYEG(year, ref era);
281                 return CCGregorianCalendar.GetDaysInYear(gregorianYear);
282         }
283
284         /// <summary>
285         /// Overridden. Gives the era of the specified date.
286         /// </summary>
287         /// <param name="time">The
288         /// <see cref="T:System.DateTime"/> that specifies a
289         /// date.
290         /// </param>
291         /// <returns>An integer representing the era of the calendar.
292         /// </returns>
293         public override int GetEra(DateTime time) {
294                 int rd = CCFixed.FromDateTime(time);
295                 int era;
296                 M_EraHandler.EraYear(out era, rd);
297                 return era;
298         }
299
300         /// <summary>
301         /// Overridden. Gives the number of the month of the specified
302         /// date.
303         /// </summary>
304         /// <param name="time">The
305         /// <see cref="T:System.DateTime"/> that specifies a
306         /// date.
307         /// </param>
308         /// <returns>An integer representing the month, 
309         /// starting with 1.</returns>
310         public override int GetMonth(DateTime time) {
311                 return CCGregorianCalendar.GetMonth(time);
312         }
313
314         /// <summary>
315         /// Overridden. Gives the number of months in the specified year 
316         /// and era.
317         /// </summary>
318         /// <param name="year">An integer that specifies the year.
319         /// </param>
320         /// <param name="era">An integer that specifies the era.
321         /// </param>
322         /// <returns>An integer that gives the number of the months in the
323         /// specified year.</returns>
324         /// <exception cref="T:System.ArgumentOutOfRangeException">
325         /// The exception is thrown, if the year or the era are not valid.
326         /// </exception>
327         public override int GetMonthsInYear(int year, int era) {
328                 M_CheckYEG(year, ref era);
329                 return 12;
330         }
331
332         /// <summary>
333         /// Overridden. Gives the number of the year of the specified
334         /// date.
335         /// </summary>
336         /// <param name="time">The
337         /// <see cref="T:System.DateTime"/> that specifies a
338         /// date.
339         /// </param>
340         /// <returns>An integer representing the year, 
341         /// starting with 1.</returns>
342         public override int GetYear(DateTime time) {
343                 int rd = CCFixed.FromDateTime(time);
344                 int era;
345                 return M_EraHandler.EraYear(out era, rd);
346         }
347
348         /// <summary>
349         /// Overridden. Tells whether the given day 
350         /// is a leap day.
351         /// </summary>
352         /// <param name="year">An integer that specifies the year in the
353         /// given era.
354         /// </param>
355         /// <param name="month">An integer that specifies the month.
356         /// </param>
357         /// <param name="day">An integer that specifies the day.
358         /// </param>
359         /// <param name="era">An integer that specifies the era.
360         /// </param>
361         /// <returns>A boolean that tells whether the given day is a leap
362         /// day.
363         /// </returns>
364         /// <exception cref="T:System.ArgumentOutOfRangeException">
365         /// The exception is thrown, if the year, month, day, or era is not
366         /// valid.
367         /// </exception>
368         public override bool IsLeapDay(int year, int month, int day, int era)
369         {
370                 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
371                 return CCGregorianCalendar.IsLeapDay(gregorianYear, month, day);
372         }
373
374         /// <summary>
375         /// Overridden. Tells whether the given month 
376         /// is a leap month.
377         /// </summary>
378         /// <param name="year">An integer that specifies the year in the
379         /// given era.
380         /// </param>
381         /// <param name="month">An integer that specifies the month.
382         /// </param>
383         /// <param name="era">An integer that specifies the era.
384         /// </param>
385         /// <returns>A boolean that tells whether the given month is a leap
386         /// month.
387         /// </returns>
388         /// <exception cref="T:System.ArgumentOutOfRangeException">
389         /// The exception is thrown, if the year, month, or era is not
390         /// valid.
391         /// </exception>
392         public override bool IsLeapMonth(int year, int month, int era) {
393                 M_CheckYMEG(year, month, ref era);
394                 return false;
395         }
396
397         /// <summary>
398         /// Overridden. Tells whether the given year
399         /// is a leap year.
400         /// </summary>
401         /// <param name="year">An integer that specifies the year in the
402         /// given era.
403         /// </param>
404         /// <param name="era">An integer that specifies the era.
405         /// </param>
406         /// <returns>A boolean that tells whether the given year is a leap
407         /// year.
408         /// </returns>
409         /// <exception cref="T:System.ArgumentOutOfRangeException">
410         /// The exception is thrown, if the year or era is not
411         /// valid.
412         /// </exception>
413         public override bool IsLeapYear(int year, int era) {
414                 int gregorianYear = M_CheckYEG(year, ref era);
415                 return CCGregorianCalendar.is_leap_year(gregorianYear);
416         }
417
418         /// <summary>
419         /// Overridden. Creates the
420         /// <see cref="T:System.DateTime"/> from the parameters.
421         /// </summary>
422         /// <param name="year">An integer that gives the year in the
423         /// <paramref name="era"/>.
424         /// </param>
425         /// <param name="month">An integer that specifies the month.
426         /// </param>
427         /// <param name="day">An integer that specifies the day.
428         /// </param>
429         /// <param name="hour">An integer that specifies the hour.
430         /// </param>
431         /// <param name="minute">An integer that specifies the minute.
432         /// </param>
433         /// <param name="second">An integer that gives the second.
434         /// </param>
435         /// <param name="milliseconds">An integer that gives the
436         /// milliseconds.
437         /// </param>
438         /// <param name="era">An integer that specifies the era.
439         /// </param>
440         /// <returns>A
441         /// <see cref="T:system.DateTime"/> representig the date and time.
442         /// </returns>
443         /// <exception cref="T:System.ArgumentOutOfRangeException">
444         /// The exception is thrown, if at least one of the parameters
445         /// is out of range.
446         /// </exception>
447         public override DateTime ToDateTime(int year, int month, int day,
448                 int hour, int minute, int second, int milliseconds,
449                 int era)
450         {
451                 int gregorianYear = M_CheckYMDEG(year, month, day, ref era);
452                 M_CheckHMSM(hour, minute, second, milliseconds);
453                 return CCGregorianCalendar.ToDateTime(gregorianYear,
454                         month, day, hour, minute, second, milliseconds);
455         }
456
457         [MonoTODO]
458         public override int ToFourDigitYear(int year)
459         {
460                 throw new NotImplementedException();
461         }
462         
463 } // class KoreanCalendar
464         
465 } // namespace System.Globalization