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