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;
6 using System.Diagnostics.CodeAnalysis;
7 
8 namespace System.Globalization
9 {
10     /*=================================TaiwanCalendar==========================
11     **
12     ** Taiwan calendar is based on the Gregorian calendar.  And the year is an offset to Gregorian calendar.
13     ** That is,
14     **      Taiwan year = Gregorian year - 1911.  So 1912/01/01 A.D. is Taiwan 1/01/01
15     **
16     **  Calendar support range:
17     **      Calendar    Minimum     Maximum
18     **      ==========  ==========  ==========
19     **      Gregorian   1912/01/01  9999/12/31
20     **      Taiwan      01/01/01    8088/12/31
21     ============================================================================*/
22 
23     public class TaiwanCalendar : Calendar
24     {
25         //
26         // The era value for the current era.
27         //
28 
29         // Since
30         //    Gregorian Year = Era Year + yearOffset
31         // When Gregorian Year 1912 is year 1, so that
32         //    1912 = 1 + yearOffset
33         //  So yearOffset = 1911
34         //m_EraInfo[0] = new EraInfo(1, new DateTime(1912, 1, 1).Ticks, 1911, 1, GregorianCalendar.MaxYear - 1911);
35 
36         // Initialize our era info.
37         internal static EraInfo[] taiwanEraInfo = new EraInfo[] {
38             new EraInfo( 1, 1912, 1, 1, 1911, 1, GregorianCalendar.MaxYear - 1911)    // era #, start year/month/day, yearOffset, minEraYear
39         };
40 
41         internal static volatile Calendar s_defaultInstance;
42 
43         internal GregorianCalendarHelper helper;
44 
45         /*=================================GetDefaultInstance==========================
46         **Action: Internal method to provide a default intance of TaiwanCalendar.  Used by NLS+ implementation
47         **       and other calendars.
48         **Returns:
49         **Arguments:
50         **Exceptions:
51         ============================================================================*/
52 
GetDefaultInstance()53         internal static Calendar GetDefaultInstance()
54         {
55             if (s_defaultInstance == null)
56             {
57                 s_defaultInstance = new TaiwanCalendar();
58             }
59             return (s_defaultInstance);
60         }
61 
62         internal static readonly DateTime calendarMinValue = new DateTime(1912, 1, 1);
63 
64 
65         public override DateTime MinSupportedDateTime
66         {
67             get
68             {
69                 return (calendarMinValue);
70             }
71         }
72 
73         public override DateTime MaxSupportedDateTime
74         {
75             get
76             {
77                 return (DateTime.MaxValue);
78             }
79         }
80 
81         public override CalendarAlgorithmType AlgorithmType
82         {
83             get
84             {
85                 return CalendarAlgorithmType.SolarCalendar;
86             }
87         }
88 
89         // Return the type of the Taiwan calendar.
90         //
91 
TaiwanCalendar()92         public TaiwanCalendar()
93         {
94             try
95             {
96                 new CultureInfo("zh-TW");
97             }
98             catch (ArgumentException e)
99             {
100                 throw new TypeInitializationException(this.GetType().ToString(), e);
101             }
102             helper = new GregorianCalendarHelper(this, taiwanEraInfo);
103         }
104 
105         internal override CalendarId ID
106         {
107             get
108             {
109                 return CalendarId.TAIWAN;
110             }
111         }
112 
113 
AddMonths(DateTime time, int months)114         public override DateTime AddMonths(DateTime time, int months)
115         {
116             return (helper.AddMonths(time, months));
117         }
118 
119 
AddYears(DateTime time, int years)120         public override DateTime AddYears(DateTime time, int years)
121         {
122             return (helper.AddYears(time, years));
123         }
124 
125 
GetDaysInMonth(int year, int month, int era)126         public override int GetDaysInMonth(int year, int month, int era)
127         {
128             return (helper.GetDaysInMonth(year, month, era));
129         }
130 
131 
GetDaysInYear(int year, int era)132         public override int GetDaysInYear(int year, int era)
133         {
134             return (helper.GetDaysInYear(year, era));
135         }
136 
137 
GetDayOfMonth(DateTime time)138         public override int GetDayOfMonth(DateTime time)
139         {
140             return (helper.GetDayOfMonth(time));
141         }
142 
143 
GetDayOfWeek(DateTime time)144         public override DayOfWeek GetDayOfWeek(DateTime time)
145         {
146             return (helper.GetDayOfWeek(time));
147         }
148 
149 
GetDayOfYear(DateTime time)150         public override int GetDayOfYear(DateTime time)
151         {
152             return (helper.GetDayOfYear(time));
153         }
154 
155 
GetMonthsInYear(int year, int era)156         public override int GetMonthsInYear(int year, int era)
157         {
158             return (helper.GetMonthsInYear(year, era));
159         }
160 
161 
GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)162         public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
163         {
164             return (helper.GetWeekOfYear(time, rule, firstDayOfWeek));
165         }
166 
167 
GetEra(DateTime time)168         public override int GetEra(DateTime time)
169         {
170             return (helper.GetEra(time));
171         }
172 
GetMonth(DateTime time)173         public override int GetMonth(DateTime time)
174         {
175             return (helper.GetMonth(time));
176         }
177 
178 
GetYear(DateTime time)179         public override int GetYear(DateTime time)
180         {
181             return (helper.GetYear(time));
182         }
183 
184 
IsLeapDay(int year, int month, int day, int era)185         public override bool IsLeapDay(int year, int month, int day, int era)
186         {
187             return (helper.IsLeapDay(year, month, day, era));
188         }
189 
190 
IsLeapYear(int year, int era)191         public override bool IsLeapYear(int year, int era)
192         {
193             return (helper.IsLeapYear(year, era));
194         }
195 
196         // Returns  the leap month in a calendar year of the specified era. This method returns 0
197         // if this calendar does not have leap month, or this year is not a leap year.
198         //
199 
GetLeapMonth(int year, int era)200         public override int GetLeapMonth(int year, int era)
201         {
202             return (helper.GetLeapMonth(year, era));
203         }
204 
205 
IsLeapMonth(int year, int month, int era)206         public override bool IsLeapMonth(int year, int month, int era)
207         {
208             return (helper.IsLeapMonth(year, month, era));
209         }
210 
211 
ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)212         public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
213         {
214             return (helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era));
215         }
216 
217 
218         public override int[] Eras
219         {
220             get
221             {
222                 return (helper.Eras);
223             }
224         }
225 
226         private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 99;
227 
228         public override int TwoDigitYearMax
229         {
230             get
231             {
232                 if (twoDigitYearMax == -1)
233                 {
234                     twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
235                 }
236                 return (twoDigitYearMax);
237             }
238 
239             set
240             {
241                 VerifyWritable();
242                 if (value < 99 || value > helper.MaxYear)
243                 {
244                     throw new ArgumentOutOfRangeException(
245                                 "year",
246                                 String.Format(
247                                     CultureInfo.CurrentCulture,
248                                     SR.ArgumentOutOfRange_Range,
249                                     99,
250                                     helper.MaxYear));
251                 }
252                 twoDigitYearMax = value;
253             }
254         }
255 
256         // For Taiwan calendar, four digit year is not used.
257         // Therefore, for any two digit number, we just return the original number.
258 
ToFourDigitYear(int year)259         public override int ToFourDigitYear(int year)
260         {
261             if (year <= 0)
262             {
263                 throw new ArgumentOutOfRangeException(nameof(year),
264                     SR.ArgumentOutOfRange_NeedPosNum);
265             }
266 
267             if (year > helper.MaxYear)
268             {
269                 throw new ArgumentOutOfRangeException(
270                             nameof(year),
271                             String.Format(
272                                 CultureInfo.CurrentCulture,
273                                 SR.ArgumentOutOfRange_Range,
274                                 1,
275                                 helper.MaxYear));
276             }
277             return (year);
278         }
279     }
280 }
281 
282