The Labs \ Source Viewer \ SSCLI \ System.Globalization \ TaiwanCalendar

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace System.Globalization
  16. {
  17.    
  18.     using System;
  19.    
  20. /*=================================TaiwanCalendar==========================
  21.     **
  22.     ** Taiwan calendar is based on the Gregorian calendar.  And the year is an offset to Gregorian calendar.
  23.     ** That is,
  24.     **      Taiwan year = Gregorian year - 1911.  So 1912/01/01 A.D. is Taiwan 1/01/01
  25.     **
  26.     **  Calendar support range:
  27.     **      Calendar    Minimum    Maximum
  28.     **      ==========  ==========  ==========
  29.     **      Gregorian  1912/01/01  9999/12/31
  30.     **      Taiwan      01/01/01    8088/12/31
  31.     ============================================================================*/   
  32.    
  33.    
  34.     [System.Runtime.InteropServices.ComVisible(true)]
  35.     [Serializable()]
  36.     public class TaiwanCalendar : Calendar
  37.     {
  38.         //
  39.         // The era value for the current era.
  40.         //
  41.        
  42.         // Since
  43.         // Gregorian Year = Era Year + yearOffset
  44.         // When Gregorian Year 1912 is year 1, so that
  45.         // 1912 = 1 + yearOffset
  46.         // So yearOffset = 1911
  47.         //m_EraInfo[0] = new EraInfo(1, new DateTime(1912, 1, 1).Ticks, 1911, 1, GregorianCalendar.MaxYear - 1911);
  48.         static internal EraInfo[] m_EraInfo = GregorianCalendarHelper.InitEraInfo(Calendar.CAL_TAIWAN);
  49.        
  50.         static internal Calendar m_defaultInstance;
  51.        
  52.         internal GregorianCalendarHelper helper;
  53.        
  54. /*=================================GetDefaultInstance==========================
  55.         **Action: Internal method to provide a default intance of TaiwanCalendar.  Used by NLS+ implementation
  56.         **      and other calendars.
  57.         **Returns:
  58.         **Arguments:
  59.         **Exceptions:
  60.         ============================================================================*/       
  61.        
  62.         static internal Calendar GetDefaultInstance()
  63.         {
  64.             if (m_defaultInstance == null) {
  65.                 m_defaultInstance = new TaiwanCalendar();
  66.             }
  67.             return (m_defaultInstance);
  68.         }
  69.        
  70.         static internal readonly DateTime calendarMinValue = new DateTime(1912, 1, 1);
  71.        
  72.        
  73.         [System.Runtime.InteropServices.ComVisible(false)]
  74.         public override DateTime MinSupportedDateTime {
  75.             get { return (calendarMinValue); }
  76.         }
  77.        
  78.         [System.Runtime.InteropServices.ComVisible(false)]
  79.         public override DateTime MaxSupportedDateTime {
  80.             get { return (DateTime.MaxValue); }
  81.         }
  82.        
  83.         // Return the type of the Taiwan calendar.
  84.         //
  85.        
  86.         [System.Runtime.InteropServices.ComVisible(false)]
  87.         public override CalendarAlgorithmType AlgorithmType {
  88.             get { return CalendarAlgorithmType.SolarCalendar; }
  89.         }
  90.        
  91.        
  92.         public TaiwanCalendar()
  93.         {
  94.             helper = new GregorianCalendarHelper(this, m_EraInfo);
  95.         }
  96.        
  97.         internal override int ID {
  98.             get { return (CAL_TAIWAN); }
  99.         }
  100.        
  101.        
  102.         public override DateTime AddMonths(DateTime time, int months)
  103.         {
  104.             return (helper.AddMonths(time, months));
  105.         }
  106.        
  107.        
  108.         public override DateTime AddYears(DateTime time, int years)
  109.         {
  110.             return (helper.AddYears(time, years));
  111.         }
  112.        
  113.        
  114.         public override int GetDaysInMonth(int year, int month, int era)
  115.         {
  116.             return (helper.GetDaysInMonth(year, month, era));
  117.         }
  118.        
  119.        
  120.         public override int GetDaysInYear(int year, int era)
  121.         {
  122.             return (helper.GetDaysInYear(year, era));
  123.         }
  124.        
  125.        
  126.         public override int GetDayOfMonth(DateTime time)
  127.         {
  128.             return (helper.GetDayOfMonth(time));
  129.         }
  130.        
  131.        
  132.         public override DayOfWeek GetDayOfWeek(DateTime time)
  133.         {
  134.             return (helper.GetDayOfWeek(time));
  135.         }
  136.        
  137.        
  138.         public override int GetDayOfYear(DateTime time)
  139.         {
  140.             return (helper.GetDayOfYear(time));
  141.         }
  142.        
  143.        
  144.         public override int GetMonthsInYear(int year, int era)
  145.         {
  146.             return (helper.GetMonthsInYear(year, era));
  147.         }
  148.        
  149.        
  150.         [System.Runtime.InteropServices.ComVisible(false)]
  151.         public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
  152.         {
  153.             return (helper.GetWeekOfYear(time, rule, firstDayOfWeek));
  154.         }
  155.        
  156.        
  157.         public override int GetEra(DateTime time)
  158.         {
  159.             return (helper.GetEra(time));
  160.         }
  161.        
  162.         public override int GetMonth(DateTime time)
  163.         {
  164.             return (helper.GetMonth(time));
  165.         }
  166.        
  167.        
  168.         public override int GetYear(DateTime time)
  169.         {
  170.             return (helper.GetYear(time));
  171.         }
  172.        
  173.        
  174.         public override bool IsLeapDay(int year, int month, int day, int era)
  175.         {
  176.             return (helper.IsLeapDay(year, month, day, era));
  177.         }
  178.        
  179.        
  180.         public override bool IsLeapYear(int year, int era)
  181.         {
  182.             return (helper.IsLeapYear(year, era));
  183.         }
  184.        
  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.
  187.         //
  188.        
  189.         [System.Runtime.InteropServices.ComVisible(false)]
  190.         public override int GetLeapMonth(int year, int era)
  191.         {
  192.             return (helper.GetLeapMonth(year, era));
  193.         }
  194.        
  195.        
  196.         public override bool IsLeapMonth(int year, int month, int era)
  197.         {
  198.             return (helper.IsLeapMonth(year, month, era));
  199.         }
  200.        
  201.        
  202.         public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  203.         {
  204.             return (helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era));
  205.         }
  206.        
  207.        
  208.         public override int[] Eras {
  209.             get { return (helper.Eras); }
  210.         }
  211.        
  212.         private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 99;
  213.        
  214.         public override int TwoDigitYearMax {
  215.             get {
  216.                 if (twoDigitYearMax == -1) {
  217.                     twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
  218.                 }
  219.                 return (twoDigitYearMax);
  220.             }
  221.            
  222.             set {
  223.                 VerifyWritable();
  224.                 if (value < 99 || value > helper.MaxYear) {
  225.                     throw new ArgumentOutOfRangeException("year", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 99, helper.MaxYear));
  226.                    
  227.                 }
  228.                 twoDigitYearMax = value;
  229.             }
  230.         }
  231.        
  232.         // For Taiwan calendar, four digit year is not used.
  233.         // Therefore, for any two digit number, we just return the original number.
  234.        
  235.         public override int ToFourDigitYear(int year)
  236.         {
  237.             if (year <= 0) {
  238.                 throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
  239.             }
  240.            
  241.             if (year > helper.MaxYear) {
  242.                 throw new ArgumentOutOfRangeException("year", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, helper.MaxYear));
  243.             }
  244.             return (year);
  245.         }
  246.     }
  247. }

Developer Fusion