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