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

  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. /*=================================JapaneseCalendar==========================
  21.     **
  22.     ** JapaneseCalendar is based on Gregorian calendar.  The month and day values are the same as
  23.     ** Gregorian calendar.  However, the year value is an offset to the Gregorian
  24.     ** year based on the era.
  25.     **
  26.     ** This system is adopted by Emperor Meiji in 1868. The year value is counted based on the reign of an emperor,
  27.     ** and the era begins on the day an emperor ascends the throne and continues until his death.
  28.     ** The era changes at 12:00AM.
  29.     **
  30.     ** For example, the current era is Heisei.  It started on 1989/1/8 A.D.  Therefore, Gregorian year 1989 is also Heisei 1st.
  31.     ** 1989/1/8 A.D. is also Heisei 1st 1/8.
  32.     **
  33.     ** Any date in the year during which era is changed can be reckoned in either era.  For example,
  34.     ** 1989/1/1 can be 1/1 Heisei 1st year or 1/1 Showa 64th year.
  35.     **
  36.     ** Note:
  37.     **  The DateTime can be represented by the JapaneseCalendar are limited to two factors:
  38.     **      1. The min value and max value of DateTime class.
  39.     **      2. The available era information.
  40.     **
  41.     **  Calendar support range:
  42.     **      Calendar    Minimum    Maximum
  43.     **      ==========  ==========  ==========
  44.     **      Gregorian  1868/09/08  9999/12/31
  45.     **      Japanese    Meiji 01/01 Heisei 8011/12/31
  46.     ============================================================================*/   
  47.    
  48.    
  49.     [Serializable()]
  50.     [System.Runtime.InteropServices.ComVisible(true)]
  51.     public class JapaneseCalendar : Calendar
  52.     {
  53.         static internal readonly DateTime calendarMinValue = new DateTime(1868, 9, 8);
  54.        
  55.        
  56.         [System.Runtime.InteropServices.ComVisible(false)]
  57.         public override DateTime MinSupportedDateTime {
  58.             get { return (calendarMinValue); }
  59.         }
  60.        
  61.         [System.Runtime.InteropServices.ComVisible(false)]
  62.         public override DateTime MaxSupportedDateTime {
  63.             get { return (DateTime.MaxValue); }
  64.         }
  65.        
  66.         // Return the type of the Japanese calendar.
  67.         //
  68.        
  69.         [System.Runtime.InteropServices.ComVisible(false)]
  70.         public override CalendarAlgorithmType AlgorithmType {
  71.             get { return CalendarAlgorithmType.SolarCalendar; }
  72.         }
  73.        
  74.         // m_EraInfo must be listed in reverse chronological order. The most recent era
  75.         // should be the first element.
  76.         // That is, m_EraInfo[0] contains the most recent era.
  77.         static internal EraInfo[] m_EraInfo = GregorianCalendarHelper.InitEraInfo(Calendar.CAL_JAPAN);
  78.         // The era value of the current era.
  79.        
  80.         static internal Calendar m_defaultInstance;
  81.         internal GregorianCalendarHelper helper;
  82.        
  83. /*=================================GetDefaultInstance==========================
  84.         **Action: Internal method to provide a default intance of JapaneseCalendar.  Used by NLS+ implementation
  85.         **      and other calendars.
  86.         **Returns:
  87.         **Arguments:
  88.         **Exceptions:
  89.         ============================================================================*/       
  90.        
  91.         static internal Calendar GetDefaultInstance()
  92.         {
  93.             if (m_defaultInstance == null) {
  94.                 m_defaultInstance = new JapaneseCalendar();
  95.             }
  96.             return (m_defaultInstance);
  97.         }
  98.        
  99.        
  100.         public JapaneseCalendar()
  101.         {
  102.             helper = new GregorianCalendarHelper(this, m_EraInfo);
  103.         }
  104.        
  105.         internal override int ID {
  106.             get { return (CAL_JAPAN); }
  107.         }
  108.        
  109.        
  110.         public override DateTime AddMonths(DateTime time, int months)
  111.         {
  112.             return (helper.AddMonths(time, months));
  113.         }
  114.        
  115.        
  116.         public override DateTime AddYears(DateTime time, int years)
  117.         {
  118.             return (helper.AddYears(time, years));
  119.         }
  120.        
  121. /*=================================GetDaysInMonth==========================
  122.         **Action: Returns the number of days in the month given by the year and month arguments.
  123.         **Returns: The number of days in the given month.
  124.         **Arguments:
  125.         **      year The year in Japanese calendar.
  126.         **      month The month
  127.         **      era    The Japanese era value.
  128.         **Exceptions
  129.         **  ArgumentException  If month is less than 1 or greater * than 12.
  130.         ============================================================================*/       
  131.        
  132.        
  133.         public override int GetDaysInMonth(int year, int month, int era)
  134.         {
  135.             return (helper.GetDaysInMonth(year, month, era));
  136.         }
  137.        
  138.        
  139.         public override int GetDaysInYear(int year, int era)
  140.         {
  141.             return (helper.GetDaysInYear(year, era));
  142.         }
  143.        
  144.        
  145.         public override int GetDayOfMonth(DateTime time)
  146.         {
  147.             return (helper.GetDayOfMonth(time));
  148.         }
  149.        
  150.        
  151.         public override DayOfWeek GetDayOfWeek(DateTime time)
  152.         {
  153.             return (helper.GetDayOfWeek(time));
  154.         }
  155.        
  156.        
  157.         public override int GetDayOfYear(DateTime time)
  158.         {
  159.             return (helper.GetDayOfYear(time));
  160.         }
  161.        
  162.        
  163.         public override int GetMonthsInYear(int year, int era)
  164.         {
  165.             return (helper.GetMonthsInYear(year, era));
  166.         }
  167.        
  168.        
  169.         [System.Runtime.InteropServices.ComVisible(false)]
  170.         public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
  171.         {
  172.             return (helper.GetWeekOfYear(time, rule, firstDayOfWeek));
  173.         }
  174.        
  175. /*=================================GetEra==========================
  176.         **Action: Get the era value of the specified time.
  177.         **Returns: The era value for the specified time.
  178.         **Arguments:
  179.         **      time the specified date time.
  180.         **Exceptions: ArgumentOutOfRangeException if time is out of the valid era ranges.
  181.         ============================================================================*/       
  182.        
  183.        
  184.         public override int GetEra(DateTime time)
  185.         {
  186.             return (helper.GetEra(time));
  187.         }
  188.        
  189.        
  190.         public override int GetMonth(DateTime time)
  191.         {
  192.             return (helper.GetMonth(time));
  193.         }
  194.        
  195.        
  196.         public override int GetYear(DateTime time)
  197.         {
  198.             return (helper.GetYear(time));
  199.         }
  200.        
  201.        
  202.         public override bool IsLeapDay(int year, int month, int day, int era)
  203.         {
  204.             return (helper.IsLeapDay(year, month, day, era));
  205.         }
  206.        
  207.        
  208.         public override bool IsLeapYear(int year, int era)
  209.         {
  210.             return (helper.IsLeapYear(year, era));
  211.         }
  212.        
  213.         // Returns the leap month in a calendar year of the specified era. This method returns 0
  214.         // if this calendar does not have leap month, or this year is not a leap year.
  215.         //
  216.        
  217.         [System.Runtime.InteropServices.ComVisible(false)]
  218.         public override int GetLeapMonth(int year, int era)
  219.         {
  220.             return (helper.GetLeapMonth(year, era));
  221.         }
  222.        
  223.        
  224.         public override bool IsLeapMonth(int year, int month, int era)
  225.         {
  226.             return (helper.IsLeapMonth(year, month, era));
  227.         }
  228.        
  229.        
  230.         public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  231.         {
  232.             return (helper.ToDateTime(year, month, day, hour, minute, second, millisecond, era));
  233.         }
  234.        
  235.         // For Japanese calendar, four digit year is not used. Few emperors will live for more than one hundred years.
  236.         // Therefore, for any two digit number, we just return the original number.
  237.        
  238.         public override int ToFourDigitYear(int year)
  239.         {
  240.             if (year <= 0) {
  241.                 throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
  242.             }
  243.            
  244.             if (year > helper.MaxYear) {
  245.                 throw new ArgumentOutOfRangeException("year", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, helper.MaxYear));
  246.             }
  247.             return (year);
  248.         }
  249.        
  250.        
  251.         public override int[] Eras {
  252.             get { return (helper.Eras); }
  253.         }
  254.        
  255.         private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 99;
  256.        
  257.         internal override bool IsValidYear(int year, int era)
  258.         {
  259.             return helper.IsValidYear(year, era);
  260.         }
  261.        
  262.         public override int TwoDigitYearMax {
  263.             get {
  264.                 if (twoDigitYearMax == -1) {
  265.                     twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
  266.                 }
  267.                 return (twoDigitYearMax);
  268.             }
  269.            
  270.             set {
  271.                 VerifyWritable();
  272.                 if (value < 99 || value > helper.MaxYear) {
  273.                     throw new ArgumentOutOfRangeException("year", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 99, helper.MaxYear));
  274.                 }
  275.                 twoDigitYearMax = value;
  276.             }
  277.         }
  278.     }
  279. }

Developer Fusion