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

  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.     using System;
  18.     using System.Text;
  19.    
  20.     ////////////////////////////////////////////////////////////////////////////
  21.     //
  22.     // Rules for the Hebrew calendar:
  23.     // - The Hebrew calendar is both a Lunar (months) and Solar (years)
  24.     // calendar, but allows for a week of seven days.
  25.     // - Days begin at sunset.
  26.     // - Leap Years occur in the 3, 6, 8, 11, 14, 17, & 19th years of a
  27.     // 19-year cycle. Year = leap iff ((7y+1) mod 19 < 7).
  28.     // - There are 12 months in a common year and 13 months in a leap year.
  29.     // - In a common year, the 6th month, Adar, has 29 days. In a leap
  30.     // year, the 6th month, Adar I, has 30 days and the leap month,
  31.     // Adar II, has 29 days.
  32.     // - Common years have 353-355 days. Leap years have 383-385 days.
  33.     // - The Hebrew new year (Rosh HaShanah) begins on the 1st of Tishri,
  34.     // the 7th month in the list below.
  35.     // - The new year may not begin on Sunday, Wednesday, or Friday.
  36.     // - If the new year would fall on a Tuesday and the conjunction of
  37.     // the following year were at midday or later, the new year is
  38.     // delayed until Thursday.
  39.     // - If the new year would fall on a Monday after a leap year, the
  40.     // new year is delayed until Tuesday.
  41.     // - The length of the 8th and 9th months vary from year to year,
  42.     // depending on the overall length of the year.
  43.     // - The length of a year is determined by the dates of the new
  44.     // years (Tishri 1) preceding and following the year in question.
  45.     // - The 2th month is long (30 days) if the year has 355 or 385 days.
  46.     // - The 3th month is short (29 days) if the year has 353 or 383 days.
  47.     // - The Hebrew months are:
  48.     // 1. Tishri (30 days)
  49.     // 2. Heshvan (29 or 30 days)
  50.     // 3. Kislev (29 or 30 days)
  51.     // 4. Teveth (29 days)
  52.     // 5. Shevat (30 days)
  53.     // 6. Adar I (30 days)
  54.     // 7. Adar {II} (29 days, this only exists if that year is a leap year)
  55.     // 8. Nisan (30 days)
  56.     // 9. Iyyar (29 days)
  57.     // 10. Sivan (30 days)
  58.     // 11. Tammuz (29 days)
  59.     // 12. Av (30 days)
  60.     // 13. Elul (29 days)
  61.     //
  62.     ////////////////////////////////////////////////////////////////////////////
  63. /*
  64.     **  Calendar support range:
  65.     **      Calendar    Minimum    Maximum
  66.     **      ==========  ==========  ==========
  67.     **      Gregorian  1583/01/01  2239/09/29
  68.     **      Hebrew      5343/04/07  5999/13/29
  69.     */   
  70.    
  71.     // Includes CHebrew implemetation;i.e All the code necessary for converting
  72.     // Gregorian to Hebrew Lunar from 1583 to 2239.
  73.    
  74.    
  75.     [Serializable()]
  76.     [System.Runtime.InteropServices.ComVisible(true)]
  77.     public class HebrewCalendar : Calendar
  78.     {
  79.        
  80.        
  81.         public static readonly int HebrewEra = 1;
  82.        
  83.         internal const int DatePartYear = 0;
  84.         internal const int DatePartDayOfYear = 1;
  85.         internal const int DatePartMonth = 2;
  86.         internal const int DatePartDay = 3;
  87.         internal const int DatePartDayOfWeek = 4;
  88.        
  89.         //
  90.         // Hebrew Translation Table.
  91.         //
  92.         // This table is used to get the following Hebrew calendar information for a
  93.         // given Gregorian year:
  94.         // 1. The day of the Hebrew month corresponding to Gregorian January 1st
  95.         // for a given Gregorian year.
  96.         // 2. The month of the Hebrew month corresponding to Gregorian January 1st
  97.         // for a given Gregorian year.
  98.         // The information is not directly in the table. Instead, the info is decoded
  99.         // by special values (numbers above 29 and below 1).
  100.         // 3. The type of the Hebrew year for a given Gregorian year.
  101.         //
  102.        
  103. /*
  104.             More notes:
  105.             This table includes 2 numbers for each year.
  106.             The offset into the table determines the year. (offset 0 is Gregorian year 1500)
  107.             1st number determines the day of the Hebrew month coresponeds to January 1st.
  108.             2nd number determines the type of the Hebrew year. (the type determines how
  109.             many days are there in the year.)
  110.             normal years : 1 = 353 days  2 = 354 days  3 = 355 days.
  111.             Leap years  : 4 = 383        5  384        6 = 385 days.
  112.             A 99 means the year is not supported for translation.
  113.             for convenience the table was defined for 750 year,
  114.             but only 640 years are supported. (from 1583 to 2239)
  115.             the years before 1582 (starting of Georgian calander)
  116.             and after 2239, are filled with 99.
  117.             Greogrian January 1st falls usually in Tevet (4th month). Tevet has always 29 days.
  118.             That's why, there no nead to specify the lunar month in the table.
  119.             There are exceptions, these are coded by giving numbers above 29 and below 1.
  120.             Actual decoding is takenig place whenever fetching information from the table.
  121.             The function for decoding is in GetLunarMonthDay().
  122.             Example:
  123.                 The data for 2000 - 2005 A.D. is:
  124.                     23,6,6,1,17,2,27,6,7,3,        // 2000 - 2004
  125.                 For year 2000, we know it has a Hebrew year type 6, which means it has 385 days.
  126.                 And 1/1/2000 A.D. is Hebrew year 5760, 23rd day of 4th month.
  127.         */       
  128.        
  129.         //
  130.         // Jewish Era in use today is dated from the supposed year of the
  131.         // Creation with its beginning in 3761 B.C.
  132.         //
  133.        
  134.         // The Hebrew year of Gregorian 1st year AD.
  135.         // 0001/01/01 AD is Hebrew 3760/01/01
  136.         private const int HebrewYearOf1AD = 3760;
  137.        
  138.         // The first Gregorian year in m_HebrewTable.
  139.         private const int FirstGregorianTableYear = 1583;
  140.         // == Hebrew Year 5343
  141.         // The last Gregorian year in m_HebrewTable.
  142.         private const int LastGregorianTableYear = 2239;
  143.         // == Hebrew Year 5999
  144.         private const int TABLESIZE = (LastGregorianTableYear - FirstGregorianTableYear);
  145.        
  146.         private const int m_minHebrewYear = HebrewYearOf1AD + FirstGregorianTableYear;
  147.         // == 5343
  148.         private const int m_maxHebrewYear = HebrewYearOf1AD + LastGregorianTableYear;
  149.         // == 5999
  150.         private static readonly int[] m_HebrewTable = {7, 3, 17, 3, 0, 4, 11, 2, 21, 6,
  151.         1, 3, 13, 2, 25, 4, 5, 3, 16, 2,
  152.         27, 6, 9, 1, 20, 2, 0, 6, 11, 3,
  153.         23, 4, 4, 2, 14, 3, 27, 4, 8, 2,
  154.         18, 3, 28, 6, 11, 1, 22, 5, 2, 3,
  155.         12, 3, 25, 4, 6, 2, 16, 3, 26, 6,
  156.         8, 2, 20, 1, 0, 6, 11, 2, 24, 4,
  157.         4, 3, 15, 2, 25, 6, 8, 1, 19, 2,
  158.         29, 6, 9, 3, 22, 4, 3, 2, 13, 3,
  159.         25, 4, 6, 3, 17, 2, 27, 6, 7, 3,
  160.         19, 2, 31, 4, 11, 3, 23, 4, 5, 2,
  161.         15, 3, 25, 6, 6, 2, 19, 1, 29, 6,
  162.         10, 2, 22, 4, 3, 3, 14, 2, 24, 6,
  163.         6, 1, 17, 3, 28, 5, 8, 3, 20, 1,
  164.         32, 5, 12, 3, 22, 6, 4, 1, 16, 2,
  165.         26, 6, 6, 3, 17, 2, 0, 4, 10, 3,
  166.         22, 4, 3, 2, 14, 3, 24, 6, 5, 2,
  167.         17, 1, 28, 6, 9, 2, 19, 3, 31, 4,
  168.         13, 2, 23, 6, 3, 3, 15, 1, 27, 5,
  169.         7, 3, 17, 3, 29, 4, 11, 2, 21, 6,
  170.         3, 1, 14, 2, 25, 6, 5, 3, 16, 2,
  171.         28, 4, 9, 3, 20, 2, 0, 6, 12, 1,
  172.         23, 6, 4, 2, 14, 3, 26, 4, 8, 2,
  173.         18, 3, 0, 4, 10, 3, 21, 5, 1, 3,
  174.         13, 1, 24, 5, 5, 3, 15, 3, 27, 4,
  175.         8, 2, 19, 3, 29, 6, 10, 2, 22, 4,
  176.         3, 3, 14, 2, 26, 4, 6, 3, 18, 2,
  177.         28, 6, 10, 1, 20, 6, 2, 2, 12, 3,
  178.         24, 4, 5, 2, 16, 3, 28, 4, 8, 3,
  179.         19, 2, 0, 6, 12, 1, 23, 5, 3, 3,
  180.         14, 3, 26, 4, 7, 2, 17, 3, 28, 6,
  181.         9, 2, 21, 4, 1, 3, 13, 2, 25, 4,
  182.         5, 3, 16, 2, 27, 6, 9, 1, 19, 3,
  183.         0, 5, 11, 3, 23, 4, 4, 2, 14, 3,
  184.         25, 6, 7, 1, 18, 2, 28, 6, 9, 3,
  185.         21, 4, 2, 2, 12, 3, 25, 4, 6, 2,
  186.         16, 3, 26, 6, 8, 2, 20, 1, 0, 6,
  187.         11, 2, 22, 6, 4, 1, 15, 2, 25, 6,
  188.         6, 3, 18, 1, 29, 5, 9, 3, 22, 4,
  189.         2, 3, 13, 2, 23, 6, 4, 3, 15, 2,
  190.         27, 4, 7, 3, 19, 2, 31, 4, 11, 3,
  191.         21, 6, 3, 2, 15, 1, 25, 6, 6, 2,
  192.         17, 3, 29, 4, 10, 2, 20, 6, 3, 1,
  193.         13, 3, 24, 5, 4, 3, 16, 1, 27, 5,
  194.         7, 3, 17, 3, 0, 4, 11, 2, 21, 6,
  195.         1, 3, 13, 2, 25, 4, 5, 3, 16, 2,
  196.         29, 4, 9, 3, 19, 6, 30, 2, 13, 1,
  197.         23, 6, 4, 2, 14, 3, 27, 4, 8, 2,
  198.         18, 3, 0, 4, 11, 3, 22, 5, 2, 3,
  199.         14, 1, 26, 5, 6, 3, 16, 3, 28, 4,
  200.         10, 2, 20, 6, 30, 3, 11, 2, 24, 4,
  201.         4, 3, 15, 2, 25, 6, 8, 1, 19, 2,
  202.         29, 6, 9, 3, 22, 4, 3, 2, 13, 3,
  203.         25, 4, 7, 2, 17, 3, 27, 6, 9, 1,
  204.         21, 5, 1, 3, 11, 3, 23, 4, 5, 2,
  205.         15, 3, 25, 6, 6, 2, 19, 1, 29, 6,
  206.         10, 2, 22, 4, 3, 3, 14, 2, 24, 6,
  207.         6, 1, 18, 2, 28, 6, 8, 3, 20, 4,
  208.         2, 2, 12, 3, 24, 4, 4, 3, 16, 2,
  209.         26, 6, 6, 3, 17, 2, 0, 4, 10, 3,
  210.         22, 4, 3, 2, 14, 3, 24, 6, 5, 2,
  211.         17, 1, 28, 6, 9, 2, 21, 4, 1, 3,
  212.         13, 2, 23, 6, 5, 1, 15, 3, 27, 5,
  213.         7, 3, 19, 1, 0, 5, 10, 3, 22, 4,
  214.         2, 3, 13, 2, 24, 6, 4, 3, 15, 2,
  215.         27, 4, 8, 3, 20, 4, 1, 2, 11, 3,
  216.         22, 6, 3, 2, 15, 1, 25, 6, 7, 2,
  217.         17, 3, 29, 4, 10, 2, 21, 6, 1, 3,
  218.         13, 1, 24, 5, 5, 3, 15, 3, 27, 4,
  219.         8, 2, 19, 6, 1, 1, 12, 2, 22, 6,
  220.         3, 3, 14, 2, 26, 4, 6, 3, 18, 2,
  221.         28, 6, 10, 1, 20, 6, 2, 2, 12, 3,
  222.         24, 4, 5, 2, 16, 3, 28, 4, 9, 2,
  223.         19, 6, 30, 3, 12, 1, 23, 5, 3, 3,
  224.         14, 3, 26, 4, 7, 2, 17, 3, 28, 6,
  225.         9, 2, 21, 4, 1, 3, 13, 2, 25, 4,
  226.         5, 3, 16, 2, 27, 6, 9, 1, 19, 6,
  227.         30, 2, 11, 3, 23, 4, 4, 2, 14, 3,
  228.         27, 4, 7, 3, 18, 2, 28, 6, 11, 1,
  229.         22, 5, 2, 3, 12, 3, 25, 4, 6, 2,
  230.         16, 3, 26, 6, 8, 2, 20, 4, 30, 3,
  231.         11, 2, 24, 4, 4, 3, 15, 2, 25, 6,
  232.         8, 1, 18, 3, 29, 5, 9, 3, 22, 4,
  233.         3, 2, 13, 3, 23, 6, 6, 1, 17, 2,
  234.         27, 6, 7, 3, 20, 4, 1, 2, 11, 3,
  235.         23, 4, 5, 2, 15, 3, 25, 6, 6, 2,
  236.         19, 1, 29, 6, 10, 2, 20, 6, 3, 1,
  237.         14, 2, 24, 6, 4, 3, 17, 1, 28, 5,
  238.         8, 3, 20, 4, 1, 3, 12, 2, 22, 6,
  239.         2, 3, 14, 2, 26, 4, 6, 3, 17, 2,
  240.         0, 4, 10, 3, 20, 6, 1, 2, 14, 1,
  241.         24, 6, 5, 2, 15, 3, 28, 4, 9, 2,
  242.         19, 6, 1, 1, 12, 3, 23, 5, 3, 3,
  243.         15, 1, 27, 5, 7, 3, 17, 3, 29, 4,
  244.         11, 2, 21, 6, 1, 3, 12, 2, 25, 4,
  245.         5, 3, 16, 2, 28, 4, 9, 3, 19, 6,
  246.         30, 2, 12, 1, 23, 6, 4, 2, 14, 3,
  247.         26, 4, 8, 2, 18, 3, 0, 4, 10, 3,
  248.         22, 5, 2, 3, 14, 1, 25, 5, 6, 3,
  249.         16, 3, 28, 4, 9, 2, 20, 6, 30, 3,
  250.         11, 2, 23, 4, 4, 3, 15, 2, 27, 4,
  251.         7, 3, 19, 2, 29, 6, 11, 1, 21, 6,
  252.         3, 2, 13, 3, 25, 4, 6, 2, 17, 3,
  253.         27, 6, 9, 1, 20, 5, 30, 3, 10, 3,
  254.         22, 4, 3, 2, 14, 3, 24, 6, 5, 2,
  255.         17, 1, 28, 6, 9, 2, 21, 4, 1, 3,
  256.         13, 2, 23, 6, 5, 1, 16, 2, 27, 6,
  257.         7, 3, 19, 4, 30, 2, 11, 3, 23, 4,
  258.         3, 3, 14, 2, 25, 6, 5, 3, 16, 2,
  259.         28, 4, 9, 3, 21, 4, 2, 2, 12, 3,
  260.         23, 6, 4, 2, 16, 1, 26, 6, 8, 2,
  261.         20, 4, 30, 3, 11, 2, 22, 6, 4, 1,
  262.         14, 3, 25, 5, 6, 3, 18, 1, 29, 5,
  263.         9, 3, 22, 4, 2, 3, 13, 2, 23, 6,
  264.         4, 3, 15, 2, 27, 4, 7, 3, 20, 4,
  265.         1, 2, 11, 3, 21, 6, 3, 2, 15, 1,
  266.         25, 6, 6, 2, 17, 3, 29, 4, 10, 2,
  267.         20, 6, 3, 1, 13, 3, 24, 5, 4, 3,
  268.         17, 1, 28, 5, 8, 3, 18, 6, 1, 1,
  269.         12, 2, 22, 6, 2, 3, 14, 2, 26, 4,
  270.         6, 3, 17, 2, 28, 6, 10, 1, 20, 6,
  271.         1, 2, 12, 3, 24, 4, 5, 2, 15, 3,
  272.         28, 4, 9, 2, 19, 6, 33, 3, 12, 1,
  273.         23, 5, 3, 3, 13, 3, 25, 4, 6, 2,
  274.         16, 3, 26, 6, 8, 2, 20, 4, 30, 3,
  275.         11, 2, 24, 4, 4, 3, 15, 2, 25, 6,
  276.         8, 1, 18, 6, 33, 2, 9, 3, 22, 4,
  277.         3, 2, 13, 3, 25, 4, 6, 3, 17, 2,
  278.         27, 6, 9, 1, 21, 5, 1, 3, 11, 3,
  279.         23, 4, 5, 2, 15, 3, 25, 6, 6, 2,
  280.         19, 4, 33, 3, 10, 2, 22, 4, 3, 3,
  281.             // 1583-1584 (Hebrew year: 5343 - 5344)
  282.             // 1585-1589
  283.             // 1590-1594
  284.             // 1595-1599
  285.             // 1600
  286.             // 1605
  287.             // 1610
  288.             // 1615
  289.             // 1620
  290.             // 1625
  291.             // 1630
  292.             // 1635
  293.             // 1640
  294.             // 1645
  295.             // 1650
  296.             // 1655
  297.             // 1660
  298.             // 1665
  299.             // 1670
  300.             // 1675
  301.             // 1680
  302.             // 1685
  303.             // 1690
  304.             // 1695
  305.             // 1700
  306.             // 1705
  307.             // 1710
  308.             // 1715
  309.             // 1720
  310.             // 1725
  311.             // 1730
  312.             // 1735
  313.             // 1740
  314.             // 1745
  315.             // 1750
  316.             // 1755
  317.             // 1760
  318.             // 1765
  319.             // 1770
  320.             // 1775
  321.             // 1780
  322.             // 1785
  323.             // 1790
  324.             // 1795
  325.             // 1800
  326.             // 1805
  327.             // 1810
  328.             // 1815
  329.             // 1820
  330.             // 1825
  331.             // 1830
  332.             // 1835
  333.             // 1840
  334.             // 1845
  335.             // 1850
  336.             // 1855
  337.             // 1860
  338.             // 1865
  339.             // 1870
  340.             // 1875
  341.             // 1880
  342.             // 1885
  343.             // 1890
  344.             // 1895
  345.             // 1900
  346.             // 1905
  347.             // 1910
  348.             // 1915
  349.             // 1920
  350.             // 1925
  351.             // 1930
  352.             // 1935
  353.             // 1940
  354.             // 1945
  355.             // 1950
  356.             // 1955
  357.             // 1960
  358.             // 1965
  359.             // 1970
  360.             // 1975
  361.             // 1980
  362.             // 1985
  363.             // 1990
  364.             // 1995
  365.             // 2000 - 2004
  366.             // 2005 - 2009
  367.             // 2010
  368.             // 2015
  369.             // 2020
  370.             // 2025
  371.             // 2030
  372.             // 2035
  373.             // 2040
  374.             // 2045
  375.             // 2050
  376.             // 2055
  377.             // 2060
  378.             // 2065
  379.             // 2070
  380.             // 2075
  381.             // 2080
  382.             // 2085
  383.             // 2090
  384.             // 2095
  385.             // 2100
  386.             // 2105
  387.             // 2110
  388.             // 2115
  389.             // 2120
  390.             // 2125
  391.             // 2130
  392.             // 2135
  393.             // 2140
  394.             // 2145
  395.             // 2150
  396.             // 2155
  397.             // 2160
  398.             // 2165
  399.             // 2170
  400.             // 2175
  401.             // 2180
  402.             // 2185
  403.             // 2190
  404.             // 2195
  405.             // 2200
  406.             // 2205
  407.             // 2210
  408.             // 2215
  409.             // 2220
  410.             // 2225
  411.             // 2230
  412.             // 2235
  413.             // 2240 (Hebrew year: 6000)
  414.         14, 2, 24, 6, 6, 1};
  415.        
  416.         //
  417.         // The lunar calendar has 6 different variations of month lengths
  418.         // within a year.
  419.         //
  420.         private static readonly int[,] m_lunarMonthLen = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  421.         0, 0, 0, 0}, {0, 30, 29, 29, 29, 30, 29, 30, 29, 30,
  422.         29, 30, 29, 0}, {0, 30, 29, 30, 29, 30, 29, 30, 29, 30,
  423.         29, 30, 29, 0}, {0, 30, 30, 30, 29, 30, 29, 30, 29, 30,
  424.         29, 30, 29, 0}, {0, 30, 29, 29, 29, 30, 30, 29, 30, 29,
  425.         30, 29, 30, 29}, {0, 30, 29, 30, 29, 30, 30, 29, 30, 29,
  426.         30, 29, 30, 29}, {0, 30, 30, 30, 29, 30, 30, 29, 30, 29,
  427.             // 3 common year variations
  428.             // 3 leap year variations
  429.         30, 29, 30, 29}};
  430.        
  431.         //internal static Calendar m_defaultInstance;
  432.        
  433.         static internal readonly DateTime calendarMinValue = new DateTime(1583, 1, 1);
  434.         // Gregorian 2239/9/29 = Hebrew 5999/13/29 (last day in Hebrew year 5999).
  435.         // We can only format/parse Hebrew numbers up to 999, so we limit the max range to Hebrew year 5999.
  436.         static internal readonly DateTime calendarMaxValue = new DateTime((new DateTime(2239, 9, 29, 23, 59, 59, 999)).Ticks + 9999);
  437.        
  438.        
  439.        
  440.         public override DateTime MinSupportedDateTime {
  441.             get { return (calendarMinValue); }
  442.         }
  443.        
  444.        
  445.        
  446.         public override DateTime MaxSupportedDateTime {
  447.             get { return (calendarMaxValue); }
  448.         }
  449.        
  450.        
  451.         // Return the type of the Hebrew calendar.
  452.         //
  453.        
  454.        
  455.         public override CalendarAlgorithmType AlgorithmType {
  456.             get { return CalendarAlgorithmType.LunisolarCalendar; }
  457.         }
  458.        
  459. /*=================================GetDefaultInstance==========================
  460.         **Action: Internal method to provide a default intance of HebrewCalendar.  Used by NLS+ implementation
  461.         **      and other calendars.
  462.         **Returns:
  463.         **Arguments:
  464.         **Exceptions:
  465.         ============================================================================*/       
  466.        
  467. /*
  468.         internal static Calendar GetDefaultInstance() {
  469.             if (m_defaultInstance == null) {
  470.                 m_defaultInstance = new HebrewCalendar();
  471.             }
  472.             return (m_defaultInstance);
  473.         }
  474.         */       
  475.        
  476.        
  477.         // Construct an instance of gregorian calendar.
  478.        
  479.         public HebrewCalendar()
  480.         {
  481.         }
  482.        
  483.         internal override int ID {
  484.             get { return (CAL_HEBREW); }
  485.         }
  486.        
  487.        
  488. /*=================================CheckHebrewYearValue==========================
  489.         **Action: Check if the Hebrew year value is supported in this class.
  490.         **Returns:  None.
  491.         **Arguments: y  Hebrew year value
  492.         **          ear Hebrew era value
  493.         **Exceptions: ArgumentOutOfRange_Range if the year value is not supported.
  494.         **Note:
  495.         **  We use a table for the Hebrew calendar calculation, so the year supported is limited.
  496.         ============================================================================*/       
  497.        
  498.         private void CheckHebrewYearValue(int y, int era, string varName)
  499.         {
  500.             CheckEraRange(era);
  501.             if (y > m_maxHebrewYear || y < m_minHebrewYear) {
  502.                 throw new ArgumentOutOfRangeException(varName, String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), m_minHebrewYear, m_maxHebrewYear));
  503.             }
  504.         }
  505.        
  506. /*=================================CheckHebrewMonthValue==========================
  507.         **Action: Check if the Hebrew month value is valid.
  508.         **Returns:  None.
  509.         **Arguments: year  Hebrew year value
  510.         **          month Hebrew month value
  511.         **Exceptions: ArgumentOutOfRange_Range if the month value is not valid.
  512.         **Note:
  513.         **  Call CheckHebrewYearValue() before calling this to verify the year value is supported.
  514.         ============================================================================*/       
  515.        
  516.         private void CheckHebrewMonthValue(int year, int month, int era)
  517.         {
  518.             int monthsInYear = GetMonthsInYear(year, era);
  519.             if (month < 1 || month > monthsInYear) {
  520.                 throw new ArgumentOutOfRangeException("month", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, monthsInYear));
  521.             }
  522.         }
  523.        
  524. /*=================================CheckHebrewDayValue==========================
  525.         **Action: Check if the Hebrew day value is valid.
  526.         **Returns:  None.
  527.         **Arguments: year  Hebrew year value
  528.         **          month Hebrew month value
  529.         **          day    Hebrew day value.
  530.         **Exceptions: ArgumentOutOfRange_Range if the day value is not valid.
  531.         **Note:
  532.         **  Call CheckHebrewYearValue()/CheckHebrewMonthValue() before calling this to verify the year/month values are valid.
  533.         ============================================================================*/       
  534.        
  535.         private void CheckHebrewDayValue(int year, int month, int day, int era)
  536.         {
  537.             int daysInMonth = GetDaysInMonth(year, month, era);
  538.             if (day < 1 || day > daysInMonth) {
  539.                 throw new ArgumentOutOfRangeException("day", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 1, daysInMonth));
  540.             }
  541.         }
  542.        
  543.         internal void CheckEraRange(int era)
  544.         {
  545.             if (era != CurrentEra && era != HebrewEra) {
  546.                 throw new ArgumentOutOfRangeException("era", Environment.GetResourceString("ArgumentOutOfRange_InvalidEraValue"));
  547.             }
  548.         }
  549.        
  550.         private void CheckTicksRange(long ticks)
  551.         {
  552.             if (ticks < calendarMinValue.Ticks || ticks > calendarMaxValue.Ticks) {
  553.                     // Print out the date in Gregorian using InvariantCulture since the DateTime is based on GreograinCalendar.
  554.                 throw new ArgumentOutOfRangeException("time", String.Format(CultureInfo.InvariantCulture, Environment.GetResourceString("ArgumentOutOfRange_CalendarRange"), calendarMinValue, calendarMaxValue));
  555.             }
  556.         }
  557.        
  558.         internal int GetResult(__DateBuffer result, int part)
  559.         {
  560.             switch (part) {
  561.                 case DatePartYear:
  562.                     return (result.year);
  563.                 case DatePartMonth:
  564.                     return (result.month);
  565.                 case DatePartDay:
  566.                     return (result.day);
  567.             }
  568.            
  569.             throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DateTimeParsing"));
  570.         }
  571.        
  572. /*=================================GetLunarMonthDay==========================
  573.         **Action: Using the Hebrew table (m_HebrewTable) to get the Hebrew month/day value for Gregorian January 1st
  574.         ** in a given Gregorian year.
  575.         ** Greogrian January 1st falls usually in Tevet (4th month). Tevet has always 29 days.
  576.         **    That's why, there no nead to specify the lunar month in the table.  There are exceptions, and these
  577.         **    are coded by giving numbers above 29 and below 1.
  578.         **    Actual decoding is takenig place in the switch statement below.
  579.         **Returns:
  580.         **    The Hebrew year type. The value is from 1 to 6.
  581.         **    normal years : 1 = 353 days  2 = 354 days  3 = 355 days.
  582.         **    Leap years  : 4 = 383        5  384        6 = 385 days.
  583.         **Arguments:
  584.         **      gregorianYear  The year value in Gregorian calendar.  The value should be between 1500 and 2239.
  585.         **      lunarDate      Object to take the result of the Hebrew year/month/day.
  586.         **Exceptions:
  587.         ============================================================================*/       
  588.        
  589.         internal int GetLunarMonthDay(int gregorianYear, __DateBuffer lunarDate)
  590.         {
  591.             //
  592.             // Get the offset into the m_lunarMonthLen array and the lunar day
  593.             // for January 1st.
  594.             //
  595.             int index = gregorianYear - FirstGregorianTableYear;
  596.             if (index < 0 || index > TABLESIZE) {
  597.                 throw new ArgumentOutOfRangeException("gregorianYear");
  598.             }
  599.            
  600.             index *= 2;
  601.             lunarDate.day = m_HebrewTable[index];
  602.            
  603.             // Get the type of the year. The value is from 1 to 6
  604.             int LunarYearType = m_HebrewTable[index + 1];
  605.            
  606.             //
  607.             // Get the Lunar Month.
  608.             //
  609.             switch (lunarDate.day) {
  610.                 case (0):
  611.                     // 1/1 is on Shvat 1
  612.                     lunarDate.month = 5;
  613.                     lunarDate.day = 1;
  614.                     break;
  615.                 case (30):
  616.                     // 1/1 is on Kislev 30
  617.                     lunarDate.month = 3;
  618.                     break;
  619.                 case (31):
  620.                     // 1/1 is on Shvat 2
  621.                     lunarDate.month = 5;
  622.                     lunarDate.day = 2;
  623.                     break;
  624.                 case (32):
  625.                     // 1/1 is on Shvat 3
  626.                     lunarDate.month = 5;
  627.                     lunarDate.day = 3;
  628.                     break;
  629.                 case (33):
  630.                     // 1/1 is on Kislev 29
  631.                     lunarDate.month = 3;
  632.                     lunarDate.day = 29;
  633.                     break;
  634.                 default:
  635.                     // 1/1 is on Tevet (This is the general case)
  636.                     lunarDate.month = 4;
  637.                     break;
  638.             }
  639.             return (LunarYearType);
  640.         }
  641.        
  642.         // Returns a given date part of this DateTime. This method is used
  643.         // to compute the year, day-of-year, month, or day part.
  644.        
  645.         internal virtual int GetDatePart(long ticks, int part)
  646.         {
  647.             // The Gregorian year, month, day value for ticks.
  648.             int gregorianYear;
  649.             int gregorianMonth;
  650.             int gregorianDay;
  651.             int hebrewYearType;
  652.             // lunar year type
  653.             long AbsoluteDate;
  654.             // absolute date - absolute date 1/1/1600
  655.             //
  656.             // Make sure we have a valid Gregorian date that will fit into our
  657.             // Hebrew conversion limits.
  658.             //
  659.             CheckTicksRange(ticks);
  660.            
  661.             DateTime time = new DateTime(ticks);
  662.            
  663.             //
  664.             // Save the Gregorian date values.
  665.             //
  666.             gregorianYear = time.Year;
  667.             gregorianMonth = time.Month;
  668.             gregorianDay = time.Day;
  669.            
  670.             __DateBuffer lunarDate = new __DateBuffer();
  671.             // lunar month and day for Jan 1
  672.             // From the table looking-up value of m_HebrewTable[index] (stored in lunarDate.day), we get the the
  673.             // lunar month and lunar day where the Gregorian date 1/1 falls.
  674.             lunarDate.year = gregorianYear + HebrewYearOf1AD;
  675.             hebrewYearType = GetLunarMonthDay(gregorianYear, lunarDate);
  676.            
  677.             // This is the buffer used to store the result Hebrew date.
  678.             __DateBuffer result = new __DateBuffer();
  679.            
  680.             //
  681.             // Store the values for the start of the new year - 1/1.
  682.             //
  683.             result.year = lunarDate.year;
  684.             result.month = lunarDate.month;
  685.             result.day = lunarDate.day;
  686.            
  687.             //
  688.             // Get the absolute date from 1/1/1600.
  689.             //
  690.             AbsoluteDate = GregorianCalendar.GetAbsoluteDate(gregorianYear, gregorianMonth, gregorianDay);
  691.            
  692.             //
  693.             // If the requested date was 1/1, then we're done.
  694.             //
  695.             if ((gregorianMonth == 1) && (gregorianDay == 1)) {
  696.                 return (GetResult(result, part));
  697.             }
  698.            
  699.             //
  700.             // Calculate the number of days between 1/1 and the requested date.
  701.             //
  702.             long NumDays;
  703.             // number of days since 1/1
  704.             NumDays = AbsoluteDate - GregorianCalendar.GetAbsoluteDate(gregorianYear, 1, 1);
  705.            
  706.             //
  707.             // If the requested date is within the current lunar month, then
  708.             // we're done.
  709.             //
  710.             if ((NumDays + (long)lunarDate.day) <= (long)(m_lunarMonthLen[hebrewYearType, lunarDate.month])) {
  711.                 result.day += (int)NumDays;
  712.                 return (GetResult(result, part));
  713.             }
  714.            
  715.             //
  716.             // Adjust for the current partial month.
  717.             //
  718.             result.month++;
  719.             result.day = 1;
  720.            
  721.             //
  722.             // Adjust the Lunar Month and Year (if necessary) based on the number
  723.             // of days between 1/1 and the requested date.
  724.             //
  725.             // Assumes Jan 1 can never translate to the last Lunar month, which
  726.             // is true.
  727.             //
  728.             NumDays -= (long)(m_lunarMonthLen[hebrewYearType, lunarDate.month] - lunarDate.day);
  729.             BCLDebug.Assert(NumDays >= 1, "NumDays >= 1");
  730.            
  731.             // If NumDays is 1, then we are done. Otherwise, find the correct Hebrew month
  732.             // and day.
  733.             if (NumDays > 1) {
  734.                 //
  735.                 // See if we're on the correct Lunar month.
  736.                 //
  737.                 while (NumDays > (long)(m_lunarMonthLen[hebrewYearType, result.month])) {
  738.                     //
  739.                     // Adjust the number of days and move to the next month.
  740.                     //
  741.                     NumDays -= (long)(m_lunarMonthLen[hebrewYearType, result.month++]);
  742.                    
  743.                     //
  744.                     // See if we need to adjust the Year.
  745.                     // Must handle both 12 and 13 month years.
  746.                     //
  747.                     if ((result.month > 13) || (m_lunarMonthLen[hebrewYearType, result.month] == 0)) {
  748.                         //
  749.                         // Adjust the Year.
  750.                         //
  751.                         result.year++;
  752.                         hebrewYearType = m_HebrewTable[(gregorianYear + 1 - FirstGregorianTableYear) * 2 + 1];
  753.                        
  754.                         //
  755.                         // Adjust the Month.
  756.                         //
  757.                         result.month = 1;
  758.                     }
  759.                 }
  760.                 //
  761.                 // Found the right Lunar month.
  762.                 //
  763.                 result.day += (int)(NumDays - 1);
  764.             }
  765.             return (GetResult(result, part));
  766.         }
  767.        
  768.         // Returns the DateTime resulting from adding the given number of
  769.         // months to the specified DateTime. The result is computed by incrementing
  770.         // (or decrementing) the year and month parts of the specified DateTime by
  771.         // value months, and, if required, adjusting the day part of the
  772.         // resulting date downwards to the last day of the resulting month in the
  773.         // resulting year. The time-of-day part of the result is the same as the
  774.         // time-of-day part of the specified DateTime.
  775.         //
  776.         // In more precise terms, considering the specified DateTime to be of the
  777.         // form y / m / d + t, where y is the
  778.         // year, m is the month, d is the day, and t is the
  779.         // time-of-day, the result is y1 / m1 / d1 + t,
  780.         // where y1 and m1 are computed by adding value months
  781.         // to y and m, and d1 is the largest value less than
  782.         // or equal to d that denotes a valid day in month m1 of year
  783.         // y1.
  784.         //
  785.        
  786.         public override DateTime AddMonths(DateTime time, int months)
  787.         {
  788.             try {
  789.                 int y = GetDatePart(time.Ticks, DatePartYear);
  790.                 int m = GetDatePart(time.Ticks, DatePartMonth);
  791.                 int d = GetDatePart(time.Ticks, DatePartDay);
  792.                
  793.                
  794.                 int monthsInYear;
  795.                 int i;
  796.                 if (months >= 0) {
  797.                     i = m + months;
  798.                     while (i > (monthsInYear = GetMonthsInYear(y, CurrentEra))) {
  799.                         y++;
  800.                         i -= monthsInYear;
  801.                     }
  802.                 }
  803.                 else {
  804.                     if ((i = m + months) <= 0) {
  805.                         months = -months;
  806.                         months -= m;
  807.                         y--;
  808.                        
  809.                         while (months > (monthsInYear = GetMonthsInYear(y, CurrentEra))) {
  810.                             y--;
  811.                             months -= monthsInYear;
  812.                         }
  813.                         monthsInYear = GetMonthsInYear(y, CurrentEra);
  814.                         i = monthsInYear - months;
  815.                     }
  816.                 }
  817.                
  818.                 int days = GetDaysInMonth(y, i);
  819.                 if (d > days) {
  820.                     d = days;
  821.                 }
  822.                 return (new DateTime(ToDateTime(y, i, d, 0, 0, 0, 0).Ticks + (time.Ticks % TicksPerDay)));
  823.             }
  824.             // We expect ArgumentException and ArgumentOutOfRangeException (which is subclass of ArgumentException)
  825.             // If exception is thrown in the calls above, we are out of the supported range of this calendar.
  826.             catch (ArgumentException) {
  827.                 throw new ArgumentOutOfRangeException("months", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_AddValue")));
  828.             }
  829.         }
  830.        
  831.         // Returns the DateTime resulting from adding the given number of
  832.         // years to the specified DateTime. The result is computed by incrementing
  833.         // (or decrementing) the year part of the specified DateTime by value
  834.         // years. If the month and day of the specified DateTime is 2/29, and if the
  835.         // resulting year is not a leap year, the month and day of the resulting
  836.         // DateTime becomes 2/28. Otherwise, the month, day, and time-of-day
  837.         // parts of the result are the same as those of the specified DateTime.
  838.         //
  839.        
  840.         public override DateTime AddYears(DateTime time, int years)
  841.         {
  842.             int y = GetDatePart(time.Ticks, DatePartYear);
  843.             int m = GetDatePart(time.Ticks, DatePartMonth);
  844.             int d = GetDatePart(time.Ticks, DatePartDay);
  845.            
  846.             y += years;
  847.             CheckHebrewYearValue(y, Calendar.CurrentEra, "years");
  848.            
  849.             int months = GetMonthsInYear(y, CurrentEra);
  850.             if (m > months) {
  851.                 m = months;
  852.             }
  853.            
  854.             int days = GetDaysInMonth(y, m);
  855.             if (d > days) {
  856.                 d = days;
  857.             }
  858.            
  859.             long ticks = ToDateTime(y, m, d, 0, 0, 0, 0).Ticks + (time.Ticks % TicksPerDay);
  860.             Calendar.CheckAddResult(ticks, MinSupportedDateTime, MaxSupportedDateTime);
  861.             return (new DateTime(ticks));
  862.         }
  863.        
  864.         // Returns the day-of-month part of the specified DateTime. The returned
  865.         // value is an integer between 1 and 31.
  866.         //
  867.        
  868.         public override int GetDayOfMonth(DateTime time)
  869.         {
  870.             return (GetDatePart(time.Ticks, DatePartDay));
  871.         }
  872.        
  873.         // Returns the day-of-week part of the specified DateTime. The returned value
  874.         // is an integer between 0 and 6, where 0 indicates Sunday, 1 indicates
  875.         // Monday, 2 indicates Tuesday, 3 indicates Wednesday, 4 indicates
  876.         // Thursday, 5 indicates Friday, and 6 indicates Saturday.
  877.         //
  878.        
  879.         public override DayOfWeek GetDayOfWeek(DateTime time)
  880.         {
  881.             // If we calculate back, the Hebrew day of week for Gregorian 0001/1/1 is Monday (1).
  882.             // Therfore, the fomula is:
  883.             return ((DayOfWeek)((int)(time.Ticks / TicksPerDay + 1) % 7));
  884.         }
  885.        
  886.         internal int GetHebrewYearType(int year, int era)
  887.         {
  888.             CheckHebrewYearValue(year, era, "year");
  889.             // The m_HebrewTable is indexed by Gregorian year and starts from FirstGregorianYear.
  890.             // So we need to convert year (Hebrew year value) to Gregorian Year below.
  891.             return (m_HebrewTable[(year - HebrewYearOf1AD - FirstGregorianTableYear) * 2 + 1]);
  892.         }
  893.        
  894.         // Returns the day-of-year part of the specified DateTime. The returned value
  895.         // is an integer between 1 and 366.
  896.         //
  897.        
  898.         public override int GetDayOfYear(DateTime time)
  899.         {
  900.             // Get Hebrew year value of the specified time.
  901.             int year = GetYear(time);
  902.             DateTime beginOfYearDate = ToDateTime(year, 1, 1, 0, 0, 0, 0, CurrentEra);
  903.             return ((int)((time.Ticks - beginOfYearDate.Ticks) / TicksPerDay) + 1);
  904.         }
  905.        
  906.         // Returns the number of days in the month given by the year and
  907.         // month arguments.
  908.         //
  909.        
  910.         public override int GetDaysInMonth(int year, int month, int era)
  911.         {
  912.             CheckEraRange(era);
  913.             int hebrewYearType = GetHebrewYearType(year, era);
  914.             CheckHebrewMonthValue(year, month, era);
  915.            
  916.             BCLDebug.Assert(hebrewYearType >= 1 && hebrewYearType <= 6, "hebrewYearType should be from 1 to 6, but now hebrewYearType = " + hebrewYearType + " for hebrew year " + year);
  917.             int monthDays = m_lunarMonthLen[hebrewYearType, month];
  918.             if (monthDays == 0) {
  919.                 throw new ArgumentOutOfRangeException("month", Environment.GetResourceString("ArgumentOutOfRange_Month"));
  920.             }
  921.             return (monthDays);
  922.         }
  923.        
  924.         // Returns the number of days in the year given by the year argument for the current era.
  925.         //
  926.        
  927.         public override int GetDaysInYear(int year, int era)
  928.         {
  929.             CheckEraRange(era);
  930.             // normal years : 1 = 353 days 2 = 354 days 3 = 355 days.
  931.             // Leap years : 4 = 383 5 384 6 = 385 days.
  932.            
  933.             // LunarYearType is from 1 to 6
  934.             int LunarYearType = GetHebrewYearType(year, era);
  935.             if (LunarYearType < 4) {
  936.                 // common year: LunarYearType = 1, 2, 3
  937.                 return (352 + LunarYearType);
  938.             }
  939.             return (382 + (LunarYearType - 3));
  940.         }
  941.        
  942.         // Returns the era for the specified DateTime value.
  943.        
  944.         public override int GetEra(DateTime time)
  945.         {
  946.            
  947.             return (HebrewEra);
  948.         }
  949.        
  950.        
  951.         public override int[] Eras {
  952.             get { return (new int[] {HebrewEra}); }
  953.         }
  954.        
  955.         // Returns the month part of the specified DateTime. The returned value is an
  956.         // integer between 1 and 12.
  957.         //
  958.        
  959.         public override int GetMonth(DateTime time)
  960.         {
  961.             return (GetDatePart(time.Ticks, DatePartMonth));
  962.         }
  963.        
  964.         // Returns the number of months in the specified year and era.
  965.        
  966.         public override int GetMonthsInYear(int year, int era)
  967.         {
  968.             return (IsLeapYear(year, era) ? 13 : 12);
  969.         }
  970.        
  971.         // Returns the year part of the specified DateTime. The returned value is an
  972.         // integer between 1 and 9999.
  973.         //
  974.        
  975.         public override int GetYear(DateTime time)
  976.         {
  977.             return (GetDatePart(time.Ticks, DatePartYear));
  978.         }
  979.        
  980.         // Checks whether a given day in the specified era is a leap day. This method returns true if
  981.         // the date is a leap day, or false if not.
  982.         //
  983.        
  984.         public override bool IsLeapDay(int year, int month, int day, int era)
  985.         {
  986.             if (IsLeapMonth(year, month, era)) {
  987.                 // Every day in a leap month is a leap day.
  988.                 CheckHebrewDayValue(year, month, day, era);
  989.                 return (true);
  990.             }
  991.             else if (IsLeapYear(year, Calendar.CurrentEra)) {
  992.                 // There is an additional day in the 6th month in the leap year (the extra day is the 30th day in the 6th month),
  993.                 // so we should return true for 6/30 if that's in a leap year.
  994.                 if (month == 6 && day == 30) {
  995.                     return (true);
  996.                 }
  997.             }
  998.             CheckHebrewDayValue(year, month, day, era);
  999.             return (false);
  1000.         }
  1001.        
  1002.         // Returns the leap month in a calendar year of the specified era. This method returns 0
  1003.         // if this calendar does not have leap month, or this year is not a leap year.
  1004.         //
  1005.        
  1006.        
  1007.         public override int GetLeapMonth(int year, int era)
  1008.         {
  1009.             // Year/era values are checked in IsLeapYear().
  1010.             if (IsLeapYear(year, era)) {
  1011.                 // The 7th month in a leap year is a leap month.
  1012.                 return (7);
  1013.             }
  1014.             return (0);
  1015.         }
  1016.        
  1017.         // Checks whether a given month in the specified era is a leap month. This method returns true if
  1018.         // month is a leap month, or false if not.
  1019.         //
  1020.        
  1021.         public override bool IsLeapMonth(int year, int month, int era)
  1022.         {
  1023.             // Year/era values are checked in IsLeapYear().
  1024.             bool isLeapYear = IsLeapYear(year, era);
  1025.             CheckHebrewMonthValue(year, month, era);
  1026.             // The 7th month in a leap year is a leap month.
  1027.             if (isLeapYear) {
  1028.                 if (month == 7) {
  1029.                     return (true);
  1030.                 }
  1031.             }
  1032.             return (false);
  1033.         }
  1034.        
  1035.         // Checks whether a given year in the specified era is a leap year. This method returns true if
  1036.         // year is a leap year, or false if not.
  1037.         //
  1038.        
  1039.         public override bool IsLeapYear(int year, int era)
  1040.         {
  1041.             CheckHebrewYearValue(year, era, "year");
  1042.             return (((7 * (long)year + 1) % 19) < 7);
  1043.         }
  1044.        
  1045.         // (month1, day1) - (month2, day2)
  1046.         int GetDayDifference(int lunarYearType, int month1, int day1, int month2, int day2)
  1047.         {
  1048.             if (month1 == month2) {
  1049.                 return (day1 - day2);
  1050.             }
  1051.            
  1052.             // Make sure that (month1, day1) < (month2, day2)
  1053.             bool swap = (month1 > month2);
  1054.             if (swap) {
  1055.                 // (month1, day1) < (month2, day2). Swap the values.
  1056.                 // The result will be a negative number.
  1057.                 int tempMonth;
  1058.                 int tempDay;
  1059.                 tempMonth = month1;
  1060.                 tempDay = day1;
  1061.                 month1 = month2;
  1062.                 day1 = day2;
  1063.                 month2 = tempMonth;
  1064.                 day2 = tempDay;
  1065.             }
  1066.            
  1067.             // Get the number of days from (month1,day1) to (month1, end of month1)
  1068.             int days = m_lunarMonthLen[lunarYearType, month1] - day1;
  1069.            
  1070.             // Move to next month.
  1071.             month1++;
  1072.            
  1073.             // Add up the days.
  1074.             while (month1 < month2) {
  1075.                 days += m_lunarMonthLen[lunarYearType, month1++];
  1076.             }
  1077.             days += day2;
  1078.            
  1079.             return (swap ? days : -days);
  1080.         }
  1081.        
  1082. /*=================================HebrewToGregorian==========================
  1083.         **Action: Convert Hebrew date to Gregorian date.
  1084.         **Returns:
  1085.         **Arguments:
  1086.         **Exceptions:
  1087.         **  The algorithm is like this:
  1088.         **      The hebrew year has an offset to the Gregorian year, so we can guess the Gregorian year for
  1089.         **      the specified Hebrew year.  That is, GreogrianYear = HebrewYear - FirstHebrewYearOf1AD.
  1090.         **
  1091.         **      From the Gregorian year and m_HebrewTable, we can get the Hebrew month/day value
  1092.         **      of the Gregorian date January 1st.  Let's call this month/day value [hebrewDateForJan1]
  1093.         **
  1094.         **      If the requested Hebrew month/day is less than [hebrewDateForJan1], we know the result
  1095.         **      Gregorian date falls in previous year.  So we decrease the Gregorian year value, and
  1096.         **      retrieve the Hebrew month/day value of the Gregorian date january 1st again.
  1097.         **
  1098.         **      Now, we get the answer of the Gregorian year.
  1099.         **
  1100.         **      The next step is to get the number of days between the requested Hebrew month/day
  1101.         **      and [hebrewDateForJan1].  When we get that, we can create the DateTime by adding/subtracting
  1102.         **      the ticks value of the number of days.
  1103.         **
  1104.         ============================================================================*/       
  1105.        
  1106.        
  1107.         DateTime HebrewToGregorian(int hebrewYear, int hebrewMonth, int hebrewDay, int hour, int minute, int second, int millisecond)
  1108.         {
  1109.             // Get the rough Gregorian year for the specified hebrewYear.
  1110.             //
  1111.             int gregorianYear = hebrewYear - HebrewYearOf1AD;
  1112.            
  1113.             __DateBuffer hebrewDateOfJan1 = new __DateBuffer();
  1114.             // year value is unused.
  1115.             int lunarYearType = GetLunarMonthDay(gregorianYear, hebrewDateOfJan1);
  1116.            
  1117.             if ((hebrewMonth == hebrewDateOfJan1.month) && (hebrewDay == hebrewDateOfJan1.day)) {
  1118.                 return (new DateTime(gregorianYear, 1, 1, hour, minute, second, millisecond));
  1119.             }
  1120.            
  1121.             int days = GetDayDifference(lunarYearType, hebrewMonth, hebrewDay, hebrewDateOfJan1.month, hebrewDateOfJan1.day);
  1122.            
  1123.             DateTime gregorianNewYear = new DateTime(gregorianYear, 1, 1);
  1124.             return (new DateTime(gregorianNewYear.Ticks + days * TicksPerDay + TimeToTicks(hour, minute, second, millisecond)));
  1125.         }
  1126.        
  1127.         // Returns the date and time converted to a DateTime value. Throws an exception if the n-tuple is invalid.
  1128.         //
  1129.        
  1130.         public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
  1131.         {
  1132.             CheckHebrewYearValue(year, era, "year");
  1133.             CheckHebrewMonthValue(year, month, era);
  1134.             CheckHebrewDayValue(year, month, day, era);
  1135.             DateTime dt = HebrewToGregorian(year, month, day, hour, minute, second, millisecond);
  1136.             CheckTicksRange(dt.Ticks);
  1137.             return (dt);
  1138.         }
  1139.        
  1140.         private const int DEFAULT_TWO_DIGIT_YEAR_MAX = 5790;
  1141.        
  1142.        
  1143.         public override int TwoDigitYearMax {
  1144.             get {
  1145.                 if (twoDigitYearMax == -1) {
  1146.                     twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DEFAULT_TWO_DIGIT_YEAR_MAX);
  1147.                 }
  1148.                 return (twoDigitYearMax);
  1149.             }
  1150.            
  1151.             set {
  1152.                 VerifyWritable();
  1153.                 if (value == 99) {
  1154.                     // Do nothing here. Year 99 is allowed so that TwoDitYearMax is disabled.
  1155.                 }
  1156.                 else {
  1157.                     CheckHebrewYearValue(value, HebrewEra, "value");
  1158.                 }
  1159.                 twoDigitYearMax = value;
  1160.             }
  1161.         }
  1162.        
  1163.        
  1164.         public override int ToFourDigitYear(int year)
  1165.         {
  1166.             if (year < 100) {
  1167.                 return (base.ToFourDigitYear(year));
  1168.             }
  1169.            
  1170.             if (year > m_maxHebrewYear || year < m_minHebrewYear) {
  1171.                 throw new ArgumentOutOfRangeException("year", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), m_minHebrewYear, m_maxHebrewYear));
  1172.             }
  1173.             return (year);
  1174.         }
  1175.        
  1176.         internal class __DateBuffer
  1177.         {
  1178.             internal int year;
  1179.             internal int month;
  1180.             internal int day;
  1181.         }
  1182.        
  1183.     }
  1184.    
  1185. }

Developer Fusion