1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Diagnostics.CodeAnalysis;
6 
7 namespace System.Globalization
8 {
9     /*=================================KoreanCalendar==========================
10     **
11     ** Korean calendar is based on the Gregorian calendar.  And the year is an offset to Gregorian calendar.
12     ** That is,
13     **      Korean year = Gregorian year + 2333.  So 2000/01/01 A.D. is Korean 4333/01/01
14     **
15     ** 0001/1/1 A.D. is Korean year 2334.
16     **
17     **  Calendar support range:
18     **      Calendar    Minimum     Maximum
19     **      ==========  ==========  ==========
20     **      Gregorian   0001/01/01   9999/12/31
21     **      Korean      2334/01/01  12332/12/31
22     ============================================================================*/
23 
24 
25     public class KoreanCalendar : Calendar
26     {
27         //
28         // The era value for the current era.
29         //
30 
31         public const int KoreanEra = 1;
32 
33         // Since
34         //    Gregorian Year = Era Year + yearOffset
35         // Gregorian Year 1 is Korean year 2334, so that
36         //    1 = 2334 + yearOffset
37         //  So yearOffset = -2333
38         // Gregorian year 2001 is Korean year 4334.
39 
40         //m_EraInfo[0] = new EraInfo(1, new DateTime(1, 1, 1).Ticks, -2333, 2334, GregorianCalendar.MaxYear + 2333);
41 
42         // Initialize our era info.
43         internal static EraInfo[] koreanEraInfo = new EraInfo[] {
44             new EraInfo( 1, 1, 1, 1, -2333, 2334, GregorianCalendar.MaxYear + 2333)   // era #, start year/month/day, yearOffset, minEraYear
45         };
46 
47         internal GregorianCalendarHelper helper;
48 
49 
50         public override DateTime MinSupportedDateTime
51         {
52             get
53             {
54                 return (DateTime.MinValue);
55             }
56         }
57 
58         public override DateTime MaxSupportedDateTime
59         {
60             get
61             {
62                 return (DateTime.MaxValue);
63             }
64         }
65 
66         public override CalendarAlgorithmType AlgorithmType
67         {
68             get
69             {
70                 return CalendarAlgorithmType.SolarCalendar;
71             }
72         }
73 
KoreanCalendar()74         public KoreanCalendar()
75         {
76             try
77             {
78                 new CultureInfo("ko-KR");
79             }
80             catch (ArgumentException e)
81             {
82                 throw new TypeInitializationException(this.GetType().ToString(), e);
83             }
84             helper = new GregorianCalendarHelper(this, koreanEraInfo);
85         }
86 
87         internal override CalendarId ID
88         {
89             get
90             {
91                 return CalendarId.KOREA;
92             }
93         }
94 
95 
AddMonths(DateTime time, int months)96         public override DateTime AddMonths(DateTime time, int months)
97         {
98             return (helper.AddMonths(time, months));
99         }
100 
101 
AddYears(DateTime time, int years)102         public override DateTime AddYears(DateTime time, int years)
103         {
104             return (helper.AddYears(time, years));
105         }
106 
107         /*=================================GetDaysInMonth==========================
108         **Action: Returns the number of days in the month given by the year and month arguments.
109         **Returns: The number of days in the given month.
110         **Arguments:
111         **      year The year in Korean calendar.
112         **      month The month
113         **      era     The Japanese era value.
114         **Exceptions
115         **  ArgumentException  If month is less than 1 or greater * than 12.
116         ============================================================================*/
117 
118 
GetDaysInMonth(int year, int month, int era)119         public override int GetDaysInMonth(int year, int month, int era)
120         {
121             return (helper.GetDaysInMonth(year, month, era));
122         }
123 
124 
GetDaysInYear(int year, int era)125         public override int GetDaysInYear(int year, int era)
126         {
127             return (helper.GetDaysInYear(year, era));
128         }
129 
130 
GetDayOfMonth(DateTime time)131         public override int GetDayOfMonth(DateTime time)
132         {
133             return (helper.GetDayOfMonth(time));
134         }
135 
136 
GetDayOfWeek(DateTime time)137         public override DayOfWeek GetDayOfWeek(DateTime time)
138         {
139             return (helper.GetDayOfWeek(time));
140         }
141 
142 
GetDayOfYear(DateTime time)143         public override int GetDayOfYear(DateTime time)
144         {
145             return (helper.GetDayOfYear(time));
146         }
147 
148 
GetMonthsInYear(int year, int era)149         public override int GetMonthsInYear(int year, int era)
150         {
151             return (helper.GetMonthsInYear(year, era));
152         }
153 
154 
GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)155         public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
156         {
157             return (helper.GetWeekOfYear(time, rule, firstDayOfWeek));
158         }
159 
160 
GetEra(DateTime time)161         public override int GetEra(DateTime time)
162         {
163             return (helper.GetEra(time));
164         }
165 
GetMonth(DateTime time)166         public override int GetMonth(DateTime time)
167         {
168             return (helper.GetMonth(time));
169         }
170 
171 
GetYear(DateTime time)172         public override int GetYear(DateTime time)
173         {
174             return (helper.GetYear(time));
175         }
176 
177 
IsLeapDay(int year, int month, int day, int era)178         public override bool IsLeapDay(int year, int month, int day, int era)
179         {
180             return (helper.IsLeapDay(year, month, day, era));
181         }
182 
183 
IsLeapYear(int year, int era)184         public override bool IsLeapYear(int year, int era)
185         {
186             return (helper.IsLeapYear(year, era));
187         }
188 
189         // Returns  the leap month in a calendar year of the specified era. This method returns 0
190         // if this calendar does not have leap month, or this year is not a leap year.
191         //
192 
GetLeapMonth(int year, int era)193         public override int GetLeapMonth(int year, int era)
194         {
195             return (helper.GetLeapMonth(year, era));
196         }
197 
198 
IsLeapMonth(int year, int month, int era)199         public override bool IsLeapMonth(int year, int month, int era)
200         {
201             return (helper.IsLeapMonth(year, month, era));
202         }
203 
204 
ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)205         public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
206         {
207             return (helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era));
208         }
209 
210 
211         public override int[] Eras
212         {
213             get
214             {
215                 return (helper.Eras);
216             }
217         }
218 
219         private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 4362;
220 
221 
222         public override int TwoDigitYearMax
223         {
224             get
225             {
226                 if (twoDigitYearMax == -1)
227                 {
228                     twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
229                 }
230                 return (twoDigitYearMax);
231             }
232 
233             set
234             {
235                 VerifyWritable();
236                 if (value < 99 || value > helper.MaxYear)
237                 {
238                     throw new ArgumentOutOfRangeException(
239                                 "year",
240                                 String.Format(
241                                     CultureInfo.CurrentCulture,
242                                     SR.ArgumentOutOfRange_Range,
243                                     99,
244                                     helper.MaxYear));
245                 }
246                 twoDigitYearMax = value;
247             }
248         }
249 
250 
ToFourDigitYear(int year)251         public override int ToFourDigitYear(int year)
252         {
253             if (year < 0)
254             {
255                 throw new ArgumentOutOfRangeException(nameof(year),
256                     SR.ArgumentOutOfRange_NeedNonNegNum);
257             }
258 
259             return (helper.ToFourDigitYear(year, this.TwoDigitYearMax));
260         }
261     }
262 }
263