[bcl] Updates referencesource to 4.7.1
[mono-project.git] / mcs / class / referencesource / mscorlib / system / globalization / taiwancalendar.cs
blobe0027b5b6d0434ad377370953b98ff66f64ecf38
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 namespace System.Globalization {
8 using System;
9 using System.Diagnostics.CodeAnalysis;
10 using System.Diagnostics.Contracts;
11 /*
14 ** Taiwan calendar is based on the Gregorian calendar. And the year is an offset to Gregorian calendar.
15 ** That is,
16 ** Taiwan year = Gregorian year - 1911. So 1912/01/01 A.D. is Taiwan 1/01/01
18 ** Calendar support range:
19 ** Calendar Minimum Maximum
20 ** ========== ========== ==========
21 ** Gregorian 1912/01/01 9999/12/31
22 ** Taiwan 01/01/01 8088/12/31
23 ============================================================================*/
25 [System.Runtime.InteropServices.ComVisible(true)]
26 [Serializable] public class TaiwanCalendar: Calendar {
28 // The era value for the current era.
31 // Since
32 // Gregorian Year = Era Year + yearOffset
33 // When Gregorian Year 1912 is year 1, so that
34 // 1912 = 1 + yearOffset
35 // So yearOffset = 1911
36 //m_EraInfo[0] = new EraInfo(1, new DateTime(1912, 1, 1).Ticks, 1911, 1, GregorianCalendar.MaxYear - 1911);
38 // Initialize our era info.
39 static internal EraInfo[] taiwanEraInfo = new EraInfo[] {
40 new EraInfo( 1, 1912, 1, 1, 1911, 1, GregorianCalendar.MaxYear - 1911) // era #, start year/month/day, yearOffset, minEraYear
43 internal static volatile Calendar s_defaultInstance;
45 internal GregorianCalendarHelper helper;
47 /*=================================GetDefaultInstance==========================
48 **Action: Internal method to provide a default intance of TaiwanCalendar. Used by NLS+ implementation
49 ** and other calendars.
50 **Returns:
51 **Arguments:
52 **Exceptions:
53 ============================================================================*/
55 internal static Calendar GetDefaultInstance() {
56 if (s_defaultInstance == null) {
57 s_defaultInstance = new TaiwanCalendar();
59 return (s_defaultInstance);
62 internal static readonly DateTime calendarMinValue = new DateTime(1912, 1, 1);
65 [System.Runtime.InteropServices.ComVisible(false)]
66 public override DateTime MinSupportedDateTime
68 get
70 return (calendarMinValue);
74 [System.Runtime.InteropServices.ComVisible(false)]
75 public override DateTime MaxSupportedDateTime
77 get
79 return (DateTime.MaxValue);
83 // Return the type of the Taiwan calendar.
86 [System.Runtime.InteropServices.ComVisible(false)]
87 public override CalendarAlgorithmType AlgorithmType
89 get
91 return CalendarAlgorithmType.SolarCalendar;
96 public TaiwanCalendar() {
97 try {
98 new CultureInfo("zh-TW");
99 } catch (ArgumentException e) {
100 throw new TypeInitializationException(this.GetType().FullName, e);
102 helper = new GregorianCalendarHelper(this, taiwanEraInfo);
105 internal override int ID {
106 get {
107 return (CAL_TAIWAN);
112 public override DateTime AddMonths(DateTime time, int months) {
113 return (helper.AddMonths(time, months));
117 public override DateTime AddYears(DateTime time, int years) {
118 return (helper.AddYears(time, years));
122 public override int GetDaysInMonth(int year, int month, int era) {
123 return (helper.GetDaysInMonth(year, month, era));
127 public override int GetDaysInYear(int year, int era) {
128 return (helper.GetDaysInYear(year, era));
132 public override int GetDayOfMonth(DateTime time) {
133 return (helper.GetDayOfMonth(time));
137 public override DayOfWeek GetDayOfWeek(DateTime time) {
138 return (helper.GetDayOfWeek(time));
142 public override int GetDayOfYear(DateTime time)
144 return (helper.GetDayOfYear(time));
148 public override int GetMonthsInYear(int year, int era) {
149 return (helper.GetMonthsInYear(year, era));
153 [SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems.
154 [System.Runtime.InteropServices.ComVisible(false)]
155 public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
157 return (helper.GetWeekOfYear(time, rule, firstDayOfWeek));
161 public override int GetEra(DateTime time) {
162 return (helper.GetEra(time));
165 public override int GetMonth(DateTime time) {
166 return (helper.GetMonth(time));
170 public override int GetYear(DateTime time) {
171 return (helper.GetYear(time));
175 public override bool IsLeapDay(int year, int month, int day, int era)
177 return (helper.IsLeapDay(year, month, day, era));
181 public override bool IsLeapYear(int year, int era) {
182 return (helper.IsLeapYear(year, era));
185 // Returns the leap month in a calendar year of the specified era. This method returns 0
186 // if this calendar does not have leap month, or this year is not a leap year.
189 [System.Runtime.InteropServices.ComVisible(false)]
190 public override int GetLeapMonth(int year, int era)
192 return (helper.GetLeapMonth(year, era));
196 public override bool IsLeapMonth(int year, int month, int era) {
197 return (helper.IsLeapMonth(year, month, era));
201 public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era) {
202 return (helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era));
206 public override int[] Eras {
207 get {
208 return (helper.Eras);
212 private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 99;
214 public override int TwoDigitYearMax {
215 get {
216 if (twoDigitYearMax == -1) {
217 twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
219 return (twoDigitYearMax);
222 set {
223 VerifyWritable();
224 if (value < 99 || value > helper.MaxYear)
226 throw new ArgumentOutOfRangeException(
227 "year",
228 String.Format(
229 CultureInfo.CurrentCulture,
230 Environment.GetResourceString("ArgumentOutOfRange_Range"),
232 helper.MaxYear));
235 twoDigitYearMax = value;
239 // For Taiwan calendar, four digit year is not used.
240 // Therefore, for any two digit number, we just return the original number.
242 public override int ToFourDigitYear(int year) {
243 if (year <= 0) {
244 throw new ArgumentOutOfRangeException("year",
245 Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
247 Contract.EndContractBlock();
249 if (year > helper.MaxYear) {
250 throw new ArgumentOutOfRangeException(
251 "year",
252 String.Format(
253 CultureInfo.CurrentCulture,
254 Environment.GetResourceString("ArgumentOutOfRange_Range"),
256 helper.MaxYear));
258 return (year);