| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Globalization |
| | | 7 | | { |
| | | 8 | | /// <remarks> |
| | | 9 | | /// Rules for the Hebrew calendar: |
| | | 10 | | /// - The Hebrew calendar is both a Lunar (months) and Solar (years) |
| | | 11 | | /// calendar, but allows for a week of seven days. |
| | | 12 | | /// - Days begin at sunset. |
| | | 13 | | /// - Leap Years occur in the 3, 6, 8, 11, 14, 17, & 19th years of a |
| | | 14 | | /// 19-year cycle. Year = leap iff ((7y+1) mod 19 < 7). |
| | | 15 | | /// - There are 12 months in a common year and 13 months in a leap year. |
| | | 16 | | /// - In a common year, the 6th month, Adar, has 29 days. In a leap |
| | | 17 | | /// year, the 6th month, Adar I, has 30 days and the leap month, |
| | | 18 | | /// Adar II, has 29 days. |
| | | 19 | | /// - Common years have 353-355 days. Leap years have 383-385 days. |
| | | 20 | | /// - The Hebrew new year (Rosh HaShanah) begins on the 1st of Tishri, |
| | | 21 | | /// the 7th month in the list below. |
| | | 22 | | /// - The new year may not begin on Sunday, Wednesday, or Friday. |
| | | 23 | | /// - If the new year would fall on a Tuesday and the conjunction of |
| | | 24 | | /// the following year were at midday or later, the new year is |
| | | 25 | | /// delayed until Thursday. |
| | | 26 | | /// - If the new year would fall on a Monday after a leap year, the |
| | | 27 | | /// new year is delayed until Tuesday. |
| | | 28 | | /// - The length of the 8th and 9th months vary from year to year, |
| | | 29 | | /// depending on the overall length of the year. |
| | | 30 | | /// - The length of a year is determined by the dates of the new |
| | | 31 | | /// years (Tishri 1) preceding and following the year in question. |
| | | 32 | | /// - The 2th month is long (30 days) if the year has 355 or 385 days. |
| | | 33 | | /// - The 3th month is short (29 days) if the year has 353 or 383 days. |
| | | 34 | | /// - The Hebrew months are: |
| | | 35 | | /// 1. Tishri (30 days) |
| | | 36 | | /// 2. Heshvan (29 or 30 days) |
| | | 37 | | /// 3. Kislev (29 or 30 days) |
| | | 38 | | /// 4. Teveth (29 days) |
| | | 39 | | /// 5. Shevat (30 days) |
| | | 40 | | /// 6. Adar I (30 days) |
| | | 41 | | /// 7. Adar {II} (29 days, this only exists if that year is a leap year) |
| | | 42 | | /// 8. Nisan (30 days) |
| | | 43 | | /// 9. Iyyar (29 days) |
| | | 44 | | /// 10. Sivan (30 days) |
| | | 45 | | /// 11. Tammuz (29 days) |
| | | 46 | | /// 12. Av (30 days) |
| | | 47 | | /// 13. Elul (29 days) |
| | | 48 | | /// Calendar support range: |
| | | 49 | | /// Calendar Minimum Maximum |
| | | 50 | | /// ========== ========== ========== |
| | | 51 | | /// Gregorian 1583/01/01 2239/09/29 |
| | | 52 | | /// Hebrew 5343/04/07 5999/13/29 |
| | | 53 | | /// |
| | | 54 | | /// Includes CHebrew implementation;i.e All the code necessary for converting |
| | | 55 | | /// Gregorian to Hebrew Lunar from 1583 to 2239. |
| | | 56 | | /// </remarks> |
| | | 57 | | public class HebrewCalendar : Calendar |
| | | 58 | | { |
| | 0 | 59 | | public static readonly int HebrewEra = 1; |
| | | 60 | | |
| | | 61 | | private const int DatePartYear = 0; |
| | | 62 | | private const int DatePartMonth = 2; |
| | | 63 | | private const int DatePartDay = 3; |
| | | 64 | | |
| | | 65 | | // Hebrew Translation Table. |
| | | 66 | | // |
| | | 67 | | // This table is used to get the following Hebrew calendar information for a |
| | | 68 | | // given Gregorian year: |
| | | 69 | | // 1. The day of the Hebrew month corresponding to Gregorian January 1st |
| | | 70 | | // for a given Gregorian year. |
| | | 71 | | // 2. The month of the Hebrew month corresponding to Gregorian January 1st |
| | | 72 | | // for a given Gregorian year. |
| | | 73 | | // The information is not directly in the table. Instead, the info is decoded |
| | | 74 | | // by special values (numbers above 29 and below 1). |
| | | 75 | | // 3. The type of the Hebrew year for a given Gregorian year. |
| | | 76 | | // |
| | | 77 | | // More notes: |
| | | 78 | | // This table includes 2 numbers for each year. |
| | | 79 | | // The offset into the table determines the year. (offset 0 is Gregorian year 1500) |
| | | 80 | | // 1st number determines the day of the Hebrew month coresponeds to January 1st. |
| | | 81 | | // 2nd number determines the type of the Hebrew year. (the type determines how |
| | | 82 | | // many days are there in the year.) |
| | | 83 | | // |
| | | 84 | | // normal years : 1 = 353 days 2 = 354 days 3 = 355 days. |
| | | 85 | | // Leap years : 4 = 383 5 384 6 = 385 days. |
| | | 86 | | // |
| | | 87 | | // A 99 means the year is not supported for translation. |
| | | 88 | | // for convenience the table was defined for 750 year, |
| | | 89 | | // but only 640 years are supported. (from 1583 to 2239) |
| | | 90 | | // the years before 1582 (starting of Georgian calendar) |
| | | 91 | | // and after 2239, are filled with 99. |
| | | 92 | | // |
| | | 93 | | // Greogrian January 1st falls usually in Tevet (4th month). Tevet has always 29 days. |
| | | 94 | | // That's why, there no nead to specify the lunar month in the table. |
| | | 95 | | // There are exceptions, these are coded by giving numbers above 29 and below 1. |
| | | 96 | | // Actual decoding is takenig place whenever fetching information from the table. |
| | | 97 | | // The function for decoding is in GetLunarMonthDay(). |
| | | 98 | | // |
| | | 99 | | // Example: |
| | | 100 | | // The data for 2000 - 2005 A.D. is: |
| | | 101 | | // 23,6,6,1,17,2,27,6,7,3, // 2000 - 2004 |
| | | 102 | | // |
| | | 103 | | // For year 2000, we know it has a Hebrew year type 6, which means it has 385 days. |
| | | 104 | | // And 1/1/2000 A.D. is Hebrew year 5760, 23rd day of 4th month. |
| | | 105 | | // |
| | | 106 | | // Jewish Era in use today is dated from the supposed year of the |
| | | 107 | | // Creation with its beginning in 3761 B.C. |
| | | 108 | | // |
| | | 109 | | // The Hebrew year of Gregorian 1st year AD. |
| | | 110 | | // 0001/01/01 AD is Hebrew 3760/01/01 |
| | | 111 | | private const int HebrewYearOf1AD = 3760; |
| | | 112 | | |
| | | 113 | | private const int FirstGregorianTableYear = 1583; // == Hebrew Year 5343 |
| | | 114 | | private const int LastGregorianTableYear = 2239; // == Hebrew Year 5999 |
| | | 115 | | |
| | | 116 | | private const int TableSize = (LastGregorianTableYear - FirstGregorianTableYear); |
| | | 117 | | |
| | | 118 | | private const int MinHebrewYear = HebrewYearOf1AD + FirstGregorianTableYear; // == 5343 |
| | | 119 | | private const int MaxHebrewYear = HebrewYearOf1AD + LastGregorianTableYear; // == 5999 |
| | | 120 | | |
| | | 121 | | private static ReadOnlySpan<byte> HebrewTable => |
| | 0 | 122 | | [ |
| | 0 | 123 | | 7, 3, 17, 3, // 1583-1584 (Hebrew year: 5343 - 5344) |
| | 0 | 124 | | 0, 4, 11, 2, 21, 6, 1, 3, 13, 2, // 1585-1589 |
| | 0 | 125 | | 25, 4, 5, 3, 16, 2, 27, 6, 9, 1, // 1590-1594 |
| | 0 | 126 | | 20, 2, 0, 6, 11, 3, 23, 4, 4, 2, // 1595-1599 |
| | 0 | 127 | | 14, 3, 27, 4, 8, 2, 18, 3, 28, 6, // 1600 |
| | 0 | 128 | | 11, 1, 22, 5, 2, 3, 12, 3, 25, 4, // 1605 |
| | 0 | 129 | | 6, 2, 16, 3, 26, 6, 8, 2, 20, 1, // 1610 |
| | 0 | 130 | | 0, 6, 11, 2, 24, 4, 4, 3, 15, 2, // 1615 |
| | 0 | 131 | | 25, 6, 8, 1, 19, 2, 29, 6, 9, 3, // 1620 |
| | 0 | 132 | | 22, 4, 3, 2, 13, 3, 25, 4, 6, 3, // 1625 |
| | 0 | 133 | | 17, 2, 27, 6, 7, 3, 19, 2, 31, 4, // 1630 |
| | 0 | 134 | | 11, 3, 23, 4, 5, 2, 15, 3, 25, 6, // 1635 |
| | 0 | 135 | | 6, 2, 19, 1, 29, 6, 10, 2, 22, 4, // 1640 |
| | 0 | 136 | | 3, 3, 14, 2, 24, 6, 6, 1, 17, 3, // 1645 |
| | 0 | 137 | | 28, 5, 8, 3, 20, 1, 32, 5, 12, 3, // 1650 |
| | 0 | 138 | | 22, 6, 4, 1, 16, 2, 26, 6, 6, 3, // 1655 |
| | 0 | 139 | | 17, 2, 0, 4, 10, 3, 22, 4, 3, 2, // 1660 |
| | 0 | 140 | | 14, 3, 24, 6, 5, 2, 17, 1, 28, 6, // 1665 |
| | 0 | 141 | | 9, 2, 19, 3, 31, 4, 13, 2, 23, 6, // 1670 |
| | 0 | 142 | | 3, 3, 15, 1, 27, 5, 7, 3, 17, 3, // 1675 |
| | 0 | 143 | | 29, 4, 11, 2, 21, 6, 3, 1, 14, 2, // 1680 |
| | 0 | 144 | | 25, 6, 5, 3, 16, 2, 28, 4, 9, 3, // 1685 |
| | 0 | 145 | | 20, 2, 0, 6, 12, 1, 23, 6, 4, 2, // 1690 |
| | 0 | 146 | | 14, 3, 26, 4, 8, 2, 18, 3, 0, 4, // 1695 |
| | 0 | 147 | | 10, 3, 21, 5, 1, 3, 13, 1, 24, 5, // 1700 |
| | 0 | 148 | | 5, 3, 15, 3, 27, 4, 8, 2, 19, 3, // 1705 |
| | 0 | 149 | | 29, 6, 10, 2, 22, 4, 3, 3, 14, 2, // 1710 |
| | 0 | 150 | | 26, 4, 6, 3, 18, 2, 28, 6, 10, 1, // 1715 |
| | 0 | 151 | | 20, 6, 2, 2, 12, 3, 24, 4, 5, 2, // 1720 |
| | 0 | 152 | | 16, 3, 28, 4, 8, 3, 19, 2, 0, 6, // 1725 |
| | 0 | 153 | | 12, 1, 23, 5, 3, 3, 14, 3, 26, 4, // 1730 |
| | 0 | 154 | | 7, 2, 17, 3, 28, 6, 9, 2, 21, 4, // 1735 |
| | 0 | 155 | | 1, 3, 13, 2, 25, 4, 5, 3, 16, 2, // 1740 |
| | 0 | 156 | | 27, 6, 9, 1, 19, 3, 0, 5, 11, 3, // 1745 |
| | 0 | 157 | | 23, 4, 4, 2, 14, 3, 25, 6, 7, 1, // 1750 |
| | 0 | 158 | | 18, 2, 28, 6, 9, 3, 21, 4, 2, 2, // 1755 |
| | 0 | 159 | | 12, 3, 25, 4, 6, 2, 16, 3, 26, 6, // 1760 |
| | 0 | 160 | | 8, 2, 20, 1, 0, 6, 11, 2, 22, 6, // 1765 |
| | 0 | 161 | | 4, 1, 15, 2, 25, 6, 6, 3, 18, 1, // 1770 |
| | 0 | 162 | | 29, 5, 9, 3, 22, 4, 2, 3, 13, 2, // 1775 |
| | 0 | 163 | | 23, 6, 4, 3, 15, 2, 27, 4, 7, 3, // 1780 |
| | 0 | 164 | | 19, 2, 31, 4, 11, 3, 21, 6, 3, 2, // 1785 |
| | 0 | 165 | | 15, 1, 25, 6, 6, 2, 17, 3, 29, 4, // 1790 |
| | 0 | 166 | | 10, 2, 20, 6, 3, 1, 13, 3, 24, 5, // 1795 |
| | 0 | 167 | | 4, 3, 16, 1, 27, 5, 7, 3, 17, 3, // 1800 |
| | 0 | 168 | | 0, 4, 11, 2, 21, 6, 1, 3, 13, 2, // 1805 |
| | 0 | 169 | | 25, 4, 5, 3, 16, 2, 29, 4, 9, 3, // 1810 |
| | 0 | 170 | | 19, 6, 30, 2, 13, 1, 23, 6, 4, 2, // 1815 |
| | 0 | 171 | | 14, 3, 27, 4, 8, 2, 18, 3, 0, 4, // 1820 |
| | 0 | 172 | | 11, 3, 22, 5, 2, 3, 14, 1, 26, 5, // 1825 |
| | 0 | 173 | | 6, 3, 16, 3, 28, 4, 10, 2, 20, 6, // 1830 |
| | 0 | 174 | | 30, 3, 11, 2, 24, 4, 4, 3, 15, 2, // 1835 |
| | 0 | 175 | | 25, 6, 8, 1, 19, 2, 29, 6, 9, 3, // 1840 |
| | 0 | 176 | | 22, 4, 3, 2, 13, 3, 25, 4, 7, 2, // 1845 |
| | 0 | 177 | | 17, 3, 27, 6, 9, 1, 21, 5, 1, 3, // 1850 |
| | 0 | 178 | | 11, 3, 23, 4, 5, 2, 15, 3, 25, 6, // 1855 |
| | 0 | 179 | | 6, 2, 19, 1, 29, 6, 10, 2, 22, 4, // 1860 |
| | 0 | 180 | | 3, 3, 14, 2, 24, 6, 6, 1, 18, 2, // 1865 |
| | 0 | 181 | | 28, 6, 8, 3, 20, 4, 2, 2, 12, 3, // 1870 |
| | 0 | 182 | | 24, 4, 4, 3, 16, 2, 26, 6, 6, 3, // 1875 |
| | 0 | 183 | | 17, 2, 0, 4, 10, 3, 22, 4, 3, 2, // 1880 |
| | 0 | 184 | | 14, 3, 24, 6, 5, 2, 17, 1, 28, 6, // 1885 |
| | 0 | 185 | | 9, 2, 21, 4, 1, 3, 13, 2, 23, 6, // 1890 |
| | 0 | 186 | | 5, 1, 15, 3, 27, 5, 7, 3, 19, 1, // 1895 |
| | 0 | 187 | | 0, 5, 10, 3, 22, 4, 2, 3, 13, 2, // 1900 |
| | 0 | 188 | | 24, 6, 4, 3, 15, 2, 27, 4, 8, 3, // 1905 |
| | 0 | 189 | | 20, 4, 1, 2, 11, 3, 22, 6, 3, 2, // 1910 |
| | 0 | 190 | | 15, 1, 25, 6, 7, 2, 17, 3, 29, 4, // 1915 |
| | 0 | 191 | | 10, 2, 21, 6, 1, 3, 13, 1, 24, 5, // 1920 |
| | 0 | 192 | | 5, 3, 15, 3, 27, 4, 8, 2, 19, 6, // 1925 |
| | 0 | 193 | | 1, 1, 12, 2, 22, 6, 3, 3, 14, 2, // 1930 |
| | 0 | 194 | | 26, 4, 6, 3, 18, 2, 28, 6, 10, 1, // 1935 |
| | 0 | 195 | | 20, 6, 2, 2, 12, 3, 24, 4, 5, 2, // 1940 |
| | 0 | 196 | | 16, 3, 28, 4, 9, 2, 19, 6, 30, 3, // 1945 |
| | 0 | 197 | | 12, 1, 23, 5, 3, 3, 14, 3, 26, 4, // 1950 |
| | 0 | 198 | | 7, 2, 17, 3, 28, 6, 9, 2, 21, 4, // 1955 |
| | 0 | 199 | | 1, 3, 13, 2, 25, 4, 5, 3, 16, 2, // 1960 |
| | 0 | 200 | | 27, 6, 9, 1, 19, 6, 30, 2, 11, 3, // 1965 |
| | 0 | 201 | | 23, 4, 4, 2, 14, 3, 27, 4, 7, 3, // 1970 |
| | 0 | 202 | | 18, 2, 28, 6, 11, 1, 22, 5, 2, 3, // 1975 |
| | 0 | 203 | | 12, 3, 25, 4, 6, 2, 16, 3, 26, 6, // 1980 |
| | 0 | 204 | | 8, 2, 20, 4, 30, 3, 11, 2, 24, 4, // 1985 |
| | 0 | 205 | | 4, 3, 15, 2, 25, 6, 8, 1, 18, 3, // 1990 |
| | 0 | 206 | | 29, 5, 9, 3, 22, 4, 3, 2, 13, 3, // 1995 |
| | 0 | 207 | | 23, 6, 6, 1, 17, 2, 27, 6, 7, 3, // 2000 - 2004 |
| | 0 | 208 | | 20, 4, 1, 2, 11, 3, 23, 4, 5, 2, // 2005 - 2009 |
| | 0 | 209 | | 15, 3, 25, 6, 6, 2, 19, 1, 29, 6, // 2010 |
| | 0 | 210 | | 10, 2, 20, 6, 3, 1, 14, 2, 24, 6, // 2015 |
| | 0 | 211 | | 4, 3, 17, 1, 28, 5, 8, 3, 20, 4, // 2020 |
| | 0 | 212 | | 1, 3, 12, 2, 22, 6, 2, 3, 14, 2, // 2025 |
| | 0 | 213 | | 26, 4, 6, 3, 17, 2, 0, 4, 10, 3, // 2030 |
| | 0 | 214 | | 20, 6, 1, 2, 14, 1, 24, 6, 5, 2, // 2035 |
| | 0 | 215 | | 15, 3, 28, 4, 9, 2, 19, 6, 1, 1, // 2040 |
| | 0 | 216 | | 12, 3, 23, 5, 3, 3, 15, 1, 27, 5, // 2045 |
| | 0 | 217 | | 7, 3, 17, 3, 29, 4, 11, 2, 21, 6, // 2050 |
| | 0 | 218 | | 1, 3, 12, 2, 25, 4, 5, 3, 16, 2, // 2055 |
| | 0 | 219 | | 28, 4, 9, 3, 19, 6, 30, 2, 12, 1, // 2060 |
| | 0 | 220 | | 23, 6, 4, 2, 14, 3, 26, 4, 8, 2, // 2065 |
| | 0 | 221 | | 18, 3, 0, 4, 10, 3, 22, 5, 2, 3, // 2070 |
| | 0 | 222 | | 14, 1, 25, 5, 6, 3, 16, 3, 28, 4, // 2075 |
| | 0 | 223 | | 9, 2, 20, 6, 30, 3, 11, 2, 23, 4, // 2080 |
| | 0 | 224 | | 4, 3, 15, 2, 27, 4, 7, 3, 19, 2, // 2085 |
| | 0 | 225 | | 29, 6, 11, 1, 21, 6, 3, 2, 13, 3, // 2090 |
| | 0 | 226 | | 25, 4, 6, 2, 17, 3, 27, 6, 9, 1, // 2095 |
| | 0 | 227 | | 20, 5, 30, 3, 10, 3, 22, 4, 3, 2, // 2100 |
| | 0 | 228 | | 14, 3, 24, 6, 5, 2, 17, 1, 28, 6, // 2105 |
| | 0 | 229 | | 9, 2, 21, 4, 1, 3, 13, 2, 23, 6, // 2110 |
| | 0 | 230 | | 5, 1, 16, 2, 27, 6, 7, 3, 19, 4, // 2115 |
| | 0 | 231 | | 30, 2, 11, 3, 23, 4, 3, 3, 14, 2, // 2120 |
| | 0 | 232 | | 25, 6, 5, 3, 16, 2, 28, 4, 9, 3, // 2125 |
| | 0 | 233 | | 21, 4, 2, 2, 12, 3, 23, 6, 4, 2, // 2130 |
| | 0 | 234 | | 16, 1, 26, 6, 8, 2, 20, 4, 30, 3, // 2135 |
| | 0 | 235 | | 11, 2, 22, 6, 4, 1, 14, 3, 25, 5, // 2140 |
| | 0 | 236 | | 6, 3, 18, 1, 29, 5, 9, 3, 22, 4, // 2145 |
| | 0 | 237 | | 2, 3, 13, 2, 23, 6, 4, 3, 15, 2, // 2150 |
| | 0 | 238 | | 27, 4, 7, 3, 20, 4, 1, 2, 11, 3, // 2155 |
| | 0 | 239 | | 21, 6, 3, 2, 15, 1, 25, 6, 6, 2, // 2160 |
| | 0 | 240 | | 17, 3, 29, 4, 10, 2, 20, 6, 3, 1, // 2165 |
| | 0 | 241 | | 13, 3, 24, 5, 4, 3, 17, 1, 28, 5, // 2170 |
| | 0 | 242 | | 8, 3, 18, 6, 1, 1, 12, 2, 22, 6, // 2175 |
| | 0 | 243 | | 2, 3, 14, 2, 26, 4, 6, 3, 17, 2, // 2180 |
| | 0 | 244 | | 28, 6, 10, 1, 20, 6, 1, 2, 12, 3, // 2185 |
| | 0 | 245 | | 24, 4, 5, 2, 15, 3, 28, 4, 9, 2, // 2190 |
| | 0 | 246 | | 19, 6, 33, 3, 12, 1, 23, 5, 3, 3, // 2195 |
| | 0 | 247 | | 13, 3, 25, 4, 6, 2, 16, 3, 26, 6, // 2200 |
| | 0 | 248 | | 8, 2, 20, 4, 30, 3, 11, 2, 24, 4, // 2205 |
| | 0 | 249 | | 4, 3, 15, 2, 25, 6, 8, 1, 18, 6, // 2210 |
| | 0 | 250 | | 33, 2, 9, 3, 22, 4, 3, 2, 13, 3, // 2215 |
| | 0 | 251 | | 25, 4, 6, 3, 17, 2, 27, 6, 9, 1, // 2220 |
| | 0 | 252 | | 21, 5, 1, 3, 11, 3, 23, 4, 5, 2, // 2225 |
| | 0 | 253 | | 15, 3, 25, 6, 6, 2, 19, 4, 33, 3, // 2230 |
| | 0 | 254 | | 10, 2, 22, 4, 3, 3, 14, 2, 24, 6, // 2235 |
| | 0 | 255 | | 6, 1 // 2240 (Hebrew year: 6000) |
| | 0 | 256 | | ]; |
| | | 257 | | |
| | | 258 | | private const int MaxMonthPlusOne = 14; |
| | | 259 | | |
| | | 260 | | // The lunar calendar has 6 different variations of month lengths |
| | | 261 | | // within a year. |
| | | 262 | | private static ReadOnlySpan<byte> LunarMonthLen => |
| | 0 | 263 | | [ |
| | 0 | 264 | | 0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0, |
| | 0 | 265 | | 0, 30, 29, 29, 29, 30, 29, 30, 29, 30, 29, 30, 29, 0, // 3 common year variations |
| | 0 | 266 | | 0, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 0, |
| | 0 | 267 | | 0, 30, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 0, |
| | 0 | 268 | | 0, 30, 29, 29, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, // 3 leap year variations |
| | 0 | 269 | | 0, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, |
| | 0 | 270 | | 0, 30, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29 |
| | 0 | 271 | | ]; |
| | | 272 | | |
| | 0 | 273 | | private static readonly DateTime s_calendarMinValue = new DateTime(1583, 1, 1); |
| | | 274 | | |
| | | 275 | | // Gregorian 2239/9/29 = Hebrew 5999/13/29 (last day in Hebrew year 5999). |
| | | 276 | | // We can only format/parse Hebrew numbers up to 999, so we limit the max range to Hebrew year 5999. |
| | 0 | 277 | | private static readonly DateTime s_calendarMaxValue = new DateTime((new DateTime(2239, 9, 29, 23, 59, 59, 999)). |
| | | 278 | | |
| | 0 | 279 | | public override DateTime MinSupportedDateTime => s_calendarMinValue; |
| | | 280 | | |
| | 0 | 281 | | public override DateTime MaxSupportedDateTime => s_calendarMaxValue; |
| | | 282 | | |
| | 0 | 283 | | public override CalendarAlgorithmType AlgorithmType => CalendarAlgorithmType.LunisolarCalendar; |
| | | 284 | | |
| | 0 | 285 | | public HebrewCalendar() |
| | | 286 | | { |
| | 0 | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | internal override CalendarId ID => CalendarId.HEBREW; |
| | | 290 | | |
| | | 291 | | private static void CheckHebrewYearValue(int y, int era, string varName) |
| | | 292 | | { |
| | 0 | 293 | | CheckEraRange(era); |
| | 0 | 294 | | if (y > MaxHebrewYear || y < MinHebrewYear) |
| | | 295 | | { |
| | 0 | 296 | | throw new ArgumentOutOfRangeException( |
| | 0 | 297 | | varName, |
| | 0 | 298 | | y, |
| | 0 | 299 | | SR.Format(SR.ArgumentOutOfRange_Range, MinHebrewYear, MaxHebrewYear)); |
| | | 300 | | } |
| | 0 | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Check if the Hebrew month value is valid. |
| | | 305 | | /// </summary> |
| | | 306 | | /// <remarks> |
| | | 307 | | /// Call CheckHebrewYearValue() before calling this to verify the year value is supported. |
| | | 308 | | /// </remarks> |
| | | 309 | | private void CheckHebrewMonthValue(int year, int month, int era) |
| | | 310 | | { |
| | 0 | 311 | | int monthsInYear = GetMonthsInYear(year, era); |
| | 0 | 312 | | if (month < 1 || month > monthsInYear) |
| | | 313 | | { |
| | 0 | 314 | | throw new ArgumentOutOfRangeException( |
| | 0 | 315 | | nameof(month), |
| | 0 | 316 | | month, |
| | 0 | 317 | | SR.Format(SR.ArgumentOutOfRange_Range, 1, monthsInYear)); |
| | | 318 | | } |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Check if the Hebrew day value is valid. |
| | | 323 | | /// </summary> |
| | | 324 | | /// <remarks> |
| | | 325 | | /// Call CheckHebrewYearValue()/CheckHebrewMonthValue() before calling this to verify the year/month values are |
| | | 326 | | /// </remarks> |
| | | 327 | | private void CheckHebrewDayValue(int year, int month, int day, int era) |
| | | 328 | | { |
| | 0 | 329 | | int daysInMonth = GetDaysInMonth(year, month, era); |
| | 0 | 330 | | if (day < 1 || day > daysInMonth) |
| | | 331 | | { |
| | 0 | 332 | | throw new ArgumentOutOfRangeException( |
| | 0 | 333 | | nameof(day), |
| | 0 | 334 | | day, |
| | 0 | 335 | | SR.Format(SR.ArgumentOutOfRange_Range, 1, daysInMonth)); |
| | | 336 | | } |
| | 0 | 337 | | } |
| | | 338 | | |
| | | 339 | | private static void CheckEraRange(int era) |
| | | 340 | | { |
| | 0 | 341 | | if (era != CurrentEra && era != HebrewEra) |
| | | 342 | | { |
| | 0 | 343 | | throw new ArgumentOutOfRangeException(nameof(era), era, SR.ArgumentOutOfRange_InvalidEraValue); |
| | | 344 | | } |
| | 0 | 345 | | } |
| | | 346 | | |
| | | 347 | | private static void CheckTicksRange(long ticks) |
| | | 348 | | { |
| | 0 | 349 | | if (ticks < s_calendarMinValue.Ticks || ticks > s_calendarMaxValue.Ticks) |
| | | 350 | | { |
| | 0 | 351 | | throw new ArgumentOutOfRangeException( |
| | 0 | 352 | | "time", |
| | 0 | 353 | | ticks, |
| | 0 | 354 | | // Print out the date in Gregorian using InvariantCulture since the DateTime is based on GreograinCa |
| | 0 | 355 | | SR.Format( |
| | 0 | 356 | | CultureInfo.InvariantCulture, |
| | 0 | 357 | | SR.ArgumentOutOfRange_CalendarRange, |
| | 0 | 358 | | s_calendarMinValue, |
| | 0 | 359 | | s_calendarMaxValue)); |
| | | 360 | | } |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | private static int GetResult(DateBuffer result, int part) |
| | | 364 | | { |
| | 0 | 365 | | return part switch |
| | 0 | 366 | | { |
| | 0 | 367 | | DatePartYear => result.year, |
| | 0 | 368 | | DatePartMonth => result.month, |
| | 0 | 369 | | DatePartDay => result.day, |
| | 0 | 370 | | _ => throw new InvalidOperationException(SR.InvalidOperation_DateTimeParsing), |
| | 0 | 371 | | }; |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | /// <summary> |
| | | 375 | | /// Using the Hebrew table (HebrewTable) to get the Hebrew month/day value for Gregorian January 1st |
| | | 376 | | /// in a given Gregorian year. |
| | | 377 | | /// Greogrian January 1st falls usually in Tevet (4th month). Tevet has always 29 days. |
| | | 378 | | /// That's why, there no nead to specify the lunar month in the table. There are exceptions, and these |
| | | 379 | | /// are coded by giving numbers above 29 and below 1. |
| | | 380 | | /// Actual decoding is takenig place in the switch statement below. |
| | | 381 | | /// </summary> |
| | | 382 | | /// <returns> |
| | | 383 | | /// The Hebrew year type. The value is from 1 to 6. |
| | | 384 | | /// normal years : 1 = 353 days 2 = 354 days 3 = 355 days. |
| | | 385 | | /// Leap years : 4 = 383 5 384 6 = 385 days. |
| | | 386 | | /// </returns> |
| | | 387 | | internal static int GetLunarMonthDay(int gregorianYear, DateBuffer lunarDate) |
| | | 388 | | { |
| | | 389 | | // Get the offset into the LunarMonthLen array and the lunar day |
| | | 390 | | // for January 1st. |
| | 0 | 391 | | int index = gregorianYear - FirstGregorianTableYear; |
| | 0 | 392 | | ArgumentOutOfRangeException.ThrowIfNegative(index, nameof(gregorianYear)); |
| | 0 | 393 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(index, TableSize, nameof(gregorianYear)); |
| | | 394 | | |
| | 0 | 395 | | index *= 2; |
| | 0 | 396 | | lunarDate.day = HebrewTable[index]; |
| | | 397 | | |
| | | 398 | | // Get the type of the year. The value is from 1 to 6 |
| | 0 | 399 | | int lunarYearType = HebrewTable[index + 1]; |
| | | 400 | | |
| | | 401 | | // Get the Lunar Month. |
| | 0 | 402 | | switch (lunarDate.day) |
| | | 403 | | { |
| | | 404 | | case 0: // 1/1 is on Shvat 1 |
| | 0 | 405 | | lunarDate.month = 5; |
| | 0 | 406 | | lunarDate.day = 1; |
| | 0 | 407 | | break; |
| | | 408 | | case 30: // 1/1 is on Kislev 30 |
| | 0 | 409 | | lunarDate.month = 3; |
| | 0 | 410 | | break; |
| | | 411 | | case 31: // 1/1 is on Shvat 2 |
| | 0 | 412 | | lunarDate.month = 5; |
| | 0 | 413 | | lunarDate.day = 2; |
| | 0 | 414 | | break; |
| | | 415 | | case 32: // 1/1 is on Shvat 3 |
| | 0 | 416 | | lunarDate.month = 5; |
| | 0 | 417 | | lunarDate.day = 3; |
| | 0 | 418 | | break; |
| | | 419 | | case 33: // 1/1 is on Kislev 29 |
| | 0 | 420 | | lunarDate.month = 3; |
| | 0 | 421 | | lunarDate.day = 29; |
| | 0 | 422 | | break; |
| | | 423 | | default: // 1/1 is on Tevet (This is the general case) |
| | 0 | 424 | | lunarDate.month = 4; |
| | | 425 | | break; |
| | | 426 | | } |
| | | 427 | | |
| | 0 | 428 | | return lunarYearType; |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Returns a given date part of this DateTime. This method is used |
| | | 433 | | /// to compute the year, day-of-year, month, or day part. |
| | | 434 | | /// </summary> |
| | | 435 | | private static int GetDatePart(long ticks, int part) |
| | | 436 | | { |
| | | 437 | | // The Gregorian year, month, day value for ticks. |
| | | 438 | | int hebrewYearType; // lunar year type |
| | | 439 | | long AbsoluteDate; // absolute date - absolute date 1/1/1600 |
| | | 440 | | |
| | | 441 | | // Make sure we have a valid Gregorian date that will fit into our |
| | | 442 | | // Hebrew conversion limits. |
| | 0 | 443 | | CheckTicksRange(ticks); |
| | | 444 | | |
| | 0 | 445 | | DateTime time = new DateTime(ticks); |
| | | 446 | | |
| | | 447 | | // Save the Gregorian date values. |
| | 0 | 448 | | time.GetDate(out int gregorianYear, out int gregorianMonth, out int gregorianDay); |
| | | 449 | | |
| | 0 | 450 | | DateBuffer lunarDate = new DateBuffer(); // lunar month and day for Jan 1 |
| | | 451 | | |
| | | 452 | | // From the table looking-up value of HebrewTable[index] (stored in lunarDate.day), we get the |
| | | 453 | | // lunar month and lunar day where the Gregorian date 1/1 falls. |
| | 0 | 454 | | lunarDate.year = gregorianYear + HebrewYearOf1AD; |
| | 0 | 455 | | hebrewYearType = GetLunarMonthDay(gregorianYear, lunarDate); |
| | | 456 | | |
| | | 457 | | // This is the buffer used to store the result Hebrew date. |
| | 0 | 458 | | DateBuffer result = new DateBuffer(); |
| | | 459 | | |
| | | 460 | | // Store the values for the start of the new year - 1/1. |
| | 0 | 461 | | result.year = lunarDate.year; |
| | 0 | 462 | | result.month = lunarDate.month; |
| | 0 | 463 | | result.day = lunarDate.day; |
| | | 464 | | |
| | | 465 | | // Get the absolute date from 1/1/1600. |
| | 0 | 466 | | AbsoluteDate = GregorianCalendar.GetAbsoluteDate(gregorianYear, gregorianMonth, gregorianDay); |
| | | 467 | | |
| | | 468 | | // If the requested date was 1/1, then we're done. |
| | 0 | 469 | | if ((gregorianMonth == 1) && (gregorianDay == 1)) |
| | | 470 | | { |
| | 0 | 471 | | return GetResult(result, part); |
| | | 472 | | } |
| | | 473 | | |
| | | 474 | | // Calculate the number of days between 1/1 and the requested date. |
| | 0 | 475 | | long numDays = AbsoluteDate - GregorianCalendar.GetAbsoluteDate(gregorianYear, 1, 1); |
| | | 476 | | |
| | | 477 | | // If the requested date is within the current lunar month, then |
| | | 478 | | // we're done. |
| | 0 | 479 | | if ((numDays + (long)lunarDate.day) <= (long)(LunarMonthLen[hebrewYearType * MaxMonthPlusOne + lunarDate.mon |
| | | 480 | | { |
| | 0 | 481 | | result.day += (int)numDays; |
| | 0 | 482 | | return GetResult(result, part); |
| | | 483 | | } |
| | | 484 | | |
| | | 485 | | // Adjust for the current partial month. |
| | 0 | 486 | | result.month++; |
| | 0 | 487 | | result.day = 1; |
| | | 488 | | |
| | | 489 | | // Adjust the Lunar Month and Year (if necessary) based on the number |
| | | 490 | | // of days between 1/1 and the requested date. |
| | | 491 | | // Assumes Jan 1 can never translate to the last Lunar month, which |
| | | 492 | | // is true. |
| | 0 | 493 | | numDays -= (long)(LunarMonthLen[hebrewYearType * MaxMonthPlusOne + lunarDate.month] - lunarDate.day); |
| | 0 | 494 | | Debug.Assert(numDays >= 1, "NumDays >= 1"); |
| | | 495 | | |
| | | 496 | | // If NumDays is 1, then we are done. Otherwise, find the correct Hebrew month |
| | | 497 | | // and day. |
| | 0 | 498 | | if (numDays > 1) |
| | | 499 | | { |
| | | 500 | | // See if we're on the correct Lunar month. |
| | 0 | 501 | | while (numDays > (long)(LunarMonthLen[hebrewYearType * MaxMonthPlusOne + result.month])) |
| | | 502 | | { |
| | | 503 | | // Adjust the number of days and move to the next month. |
| | 0 | 504 | | numDays -= (long)(LunarMonthLen[hebrewYearType * MaxMonthPlusOne + result.month++]); |
| | | 505 | | |
| | | 506 | | // See if we need to adjust the Year. |
| | | 507 | | // Must handle both 12 and 13 month years. |
| | 0 | 508 | | if ((result.month > 13) || (LunarMonthLen[hebrewYearType * MaxMonthPlusOne + result.month] == 0)) |
| | | 509 | | { |
| | | 510 | | // Adjust the Year. |
| | 0 | 511 | | result.year++; |
| | 0 | 512 | | hebrewYearType = HebrewTable[(gregorianYear + 1 - FirstGregorianTableYear) * 2 + 1]; |
| | | 513 | | |
| | | 514 | | // Adjust the Month. |
| | 0 | 515 | | result.month = 1; |
| | | 516 | | } |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | // Found the right Lunar month. |
| | 0 | 520 | | result.day += (int)(numDays - 1); |
| | | 521 | | } |
| | | 522 | | |
| | 0 | 523 | | return GetResult(result, part); |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | public override DateTime AddMonths(DateTime time, int months) |
| | | 527 | | { |
| | | 528 | | try |
| | | 529 | | { |
| | 0 | 530 | | int y = GetDatePart(time.Ticks, DatePartYear); |
| | 0 | 531 | | int m = GetDatePart(time.Ticks, DatePartMonth); |
| | 0 | 532 | | int d = GetDatePart(time.Ticks, DatePartDay); |
| | | 533 | | |
| | | 534 | | int monthsInYear; |
| | | 535 | | int i; |
| | 0 | 536 | | if (months >= 0) |
| | | 537 | | { |
| | 0 | 538 | | i = m + months; |
| | 0 | 539 | | while (i > (monthsInYear = GetMonthsInYear(y, CurrentEra))) |
| | | 540 | | { |
| | 0 | 541 | | y++; |
| | 0 | 542 | | i -= monthsInYear; |
| | | 543 | | } |
| | | 544 | | } |
| | | 545 | | else |
| | | 546 | | { |
| | 0 | 547 | | if ((i = m + months) <= 0) |
| | | 548 | | { |
| | 0 | 549 | | months = -months; |
| | 0 | 550 | | months -= m; |
| | 0 | 551 | | y--; |
| | | 552 | | |
| | 0 | 553 | | while (months > (monthsInYear = GetMonthsInYear(y, CurrentEra))) |
| | | 554 | | { |
| | 0 | 555 | | y--; |
| | 0 | 556 | | months -= monthsInYear; |
| | | 557 | | } |
| | 0 | 558 | | monthsInYear = GetMonthsInYear(y, CurrentEra); |
| | 0 | 559 | | i = monthsInYear - months; |
| | | 560 | | } |
| | | 561 | | } |
| | | 562 | | |
| | 0 | 563 | | int days = GetDaysInMonth(y, i); |
| | 0 | 564 | | if (d > days) |
| | | 565 | | { |
| | 0 | 566 | | d = days; |
| | | 567 | | } |
| | | 568 | | |
| | 0 | 569 | | return new DateTime(ToDateTime(y, i, d, 0, 0, 0, 0).Ticks + (time.Ticks % TimeSpan.TicksPerDay)); |
| | | 570 | | } |
| | | 571 | | // We expect ArgumentException and ArgumentOutOfRangeException (which is subclass of ArgumentException) |
| | | 572 | | // If exception is thrown in the calls above, we are out of the supported range of this calendar. |
| | 0 | 573 | | catch (ArgumentException) |
| | | 574 | | { |
| | 0 | 575 | | throw new ArgumentOutOfRangeException(nameof(months), months, SR.ArgumentOutOfRange_AddValue); |
| | | 576 | | } |
| | 0 | 577 | | } |
| | | 578 | | |
| | | 579 | | public override DateTime AddYears(DateTime time, int years) |
| | | 580 | | { |
| | 0 | 581 | | int y = GetDatePart(time.Ticks, DatePartYear); |
| | 0 | 582 | | int m = GetDatePart(time.Ticks, DatePartMonth); |
| | 0 | 583 | | int d = GetDatePart(time.Ticks, DatePartDay); |
| | | 584 | | |
| | 0 | 585 | | y += years; |
| | 0 | 586 | | CheckHebrewYearValue(y, CurrentEra, nameof(years)); |
| | | 587 | | |
| | 0 | 588 | | int months = GetMonthsInYear(y, CurrentEra); |
| | 0 | 589 | | if (m > months) |
| | | 590 | | { |
| | 0 | 591 | | m = months; |
| | | 592 | | } |
| | | 593 | | |
| | 0 | 594 | | int days = GetDaysInMonth(y, m); |
| | 0 | 595 | | if (d > days) |
| | | 596 | | { |
| | 0 | 597 | | d = days; |
| | | 598 | | } |
| | | 599 | | |
| | 0 | 600 | | long ticks = ToDateTime(y, m, d, 0, 0, 0, 0).Ticks + (time.Ticks % TimeSpan.TicksPerDay); |
| | 0 | 601 | | CheckAddResult(ticks, MinSupportedDateTime, MaxSupportedDateTime); |
| | 0 | 602 | | return new DateTime(ticks); |
| | | 603 | | } |
| | | 604 | | |
| | | 605 | | public override int GetDayOfMonth(DateTime time) |
| | | 606 | | { |
| | 0 | 607 | | return GetDatePart(time.Ticks, DatePartDay); |
| | | 608 | | } |
| | | 609 | | |
| | | 610 | | public override DayOfWeek GetDayOfWeek(DateTime time) |
| | | 611 | | { |
| | | 612 | | // If we calculate back, the Hebrew day of week for Gregorian 0001/1/1 is Monday (1). |
| | 0 | 613 | | return time.DayOfWeek; |
| | | 614 | | } |
| | | 615 | | |
| | | 616 | | internal static int GetHebrewYearType(int year, int era) |
| | | 617 | | { |
| | 0 | 618 | | CheckHebrewYearValue(year, era, nameof(year)); |
| | | 619 | | // The HebrewTable is indexed by Gregorian year and starts from FirstGregorianYear. |
| | | 620 | | // So we need to convert year (Hebrew year value) to Gregorian Year below. |
| | 0 | 621 | | return HebrewTable[(year - HebrewYearOf1AD - FirstGregorianTableYear) * 2 + 1]; |
| | | 622 | | } |
| | | 623 | | |
| | | 624 | | public override int GetDayOfYear(DateTime time) |
| | | 625 | | { |
| | | 626 | | // Get Hebrew year value of the specified time. |
| | 0 | 627 | | int year = GetYear(time); |
| | | 628 | | DateTime beginOfYearDate; |
| | 0 | 629 | | if (year == 5343) |
| | | 630 | | { |
| | | 631 | | // Gregorian 1583/01/01 corresponds to Hebrew 5343/04/07 (MinSupportedDateTime) |
| | | 632 | | // To figure out the Gregorian date associated with Hebrew 5343/01/01, we need to |
| | | 633 | | // count the days from 5343/01/01 to 5343/04/07 and subtract that from Gregorian |
| | | 634 | | // 1583/01/01. |
| | | 635 | | // 1. Tishri (30 days) |
| | | 636 | | // 2. Heshvan (30 days since 5343 has 355 days) |
| | | 637 | | // 3. Kislev (30 days since 5343 has 355 days) |
| | | 638 | | // 96 days to get from 5343/01/01 to 5343/04/07 |
| | | 639 | | // Gregorian 1583/01/01 - 96 days = 1582/9/27 |
| | | 640 | | |
| | | 641 | | // the beginning of Hebrew year 5343 corresponds to Gregorian September 27, 1582. |
| | 0 | 642 | | beginOfYearDate = new DateTime(1582, 9, 27); |
| | | 643 | | } |
| | | 644 | | else |
| | | 645 | | { |
| | | 646 | | // following line will fail when year is 5343 (first supported year) |
| | 0 | 647 | | beginOfYearDate = ToDateTime(year, 1, 1, 0, 0, 0, 0, CurrentEra); |
| | | 648 | | } |
| | | 649 | | |
| | 0 | 650 | | return (int)((time.Ticks - beginOfYearDate.Ticks) / TimeSpan.TicksPerDay) + 1; |
| | | 651 | | } |
| | | 652 | | |
| | | 653 | | public override int GetDaysInMonth(int year, int month, int era) |
| | | 654 | | { |
| | 0 | 655 | | CheckEraRange(era); |
| | 0 | 656 | | int hebrewYearType = GetHebrewYearType(year, era); |
| | 0 | 657 | | CheckHebrewMonthValue(year, month, era); |
| | | 658 | | |
| | 0 | 659 | | Debug.Assert(hebrewYearType >= 1 && hebrewYearType <= 6, |
| | 0 | 660 | | $"hebrewYearType should be from 1 to 6, but now hebrewYearType = {hebrewYearType} for hebrew year {year |
| | 0 | 661 | | int monthDays = LunarMonthLen[hebrewYearType * MaxMonthPlusOne + month]; |
| | 0 | 662 | | if (monthDays == 0) |
| | | 663 | | { |
| | 0 | 664 | | ThrowHelper.ThrowArgumentOutOfRange_Month(month); |
| | | 665 | | } |
| | | 666 | | |
| | 0 | 667 | | return monthDays; |
| | | 668 | | } |
| | | 669 | | |
| | | 670 | | public override int GetDaysInYear(int year, int era) |
| | | 671 | | { |
| | 0 | 672 | | CheckEraRange(era); |
| | | 673 | | // normal years : 1 = 353 days 2 = 354 days 3 = 355 days. |
| | | 674 | | // Leap years : 4 = 383 5 384 6 = 385 days. |
| | | 675 | | |
| | | 676 | | // LunarYearType is from 1 to 6 |
| | 0 | 677 | | int LunarYearType = GetHebrewYearType(year, era); |
| | 0 | 678 | | if (LunarYearType < 4) |
| | | 679 | | { |
| | | 680 | | // common year: LunarYearType = 1, 2, 3 |
| | 0 | 681 | | return 352 + LunarYearType; |
| | | 682 | | } |
| | | 683 | | |
| | 0 | 684 | | return 382 + (LunarYearType - 3); |
| | | 685 | | } |
| | | 686 | | |
| | 0 | 687 | | public override int GetEra(DateTime time) => HebrewEra; |
| | | 688 | | |
| | 0 | 689 | | public override int[] Eras => [HebrewEra]; |
| | | 690 | | |
| | | 691 | | public override int GetMonth(DateTime time) |
| | | 692 | | { |
| | 0 | 693 | | return GetDatePart(time.Ticks, DatePartMonth); |
| | | 694 | | } |
| | | 695 | | |
| | | 696 | | public override int GetMonthsInYear(int year, int era) |
| | | 697 | | { |
| | 0 | 698 | | return IsLeapYear(year, era) ? 13 : 12; |
| | | 699 | | } |
| | | 700 | | |
| | | 701 | | public override int GetYear(DateTime time) |
| | | 702 | | { |
| | 0 | 703 | | return GetDatePart(time.Ticks, DatePartYear); |
| | | 704 | | } |
| | | 705 | | |
| | | 706 | | public override bool IsLeapDay(int year, int month, int day, int era) |
| | | 707 | | { |
| | 0 | 708 | | if (IsLeapMonth(year, month, era)) |
| | | 709 | | { |
| | | 710 | | // Every day in a leap month is a leap day. |
| | 0 | 711 | | CheckHebrewDayValue(year, month, day, era); |
| | 0 | 712 | | return true; |
| | | 713 | | } |
| | 0 | 714 | | else if (IsLeapYear(year, CurrentEra)) |
| | | 715 | | { |
| | | 716 | | // There is an additional day in the 6th month in the leap year (the extra day is the 30th day in the 6t |
| | | 717 | | // so we should return true for 6/30 if that's in a leap year. |
| | 0 | 718 | | if (month == 6 && day == 30) |
| | | 719 | | { |
| | 0 | 720 | | return true; |
| | | 721 | | } |
| | | 722 | | } |
| | | 723 | | |
| | 0 | 724 | | CheckHebrewDayValue(year, month, day, era); |
| | 0 | 725 | | return false; |
| | | 726 | | } |
| | | 727 | | |
| | | 728 | | public override int GetLeapMonth(int year, int era) |
| | | 729 | | { |
| | | 730 | | // Year/era values are checked in IsLeapYear(). |
| | 0 | 731 | | if (IsLeapYear(year, era)) |
| | | 732 | | { |
| | | 733 | | // The 7th month in a leap year is a leap month. |
| | 0 | 734 | | return 7; |
| | | 735 | | } |
| | 0 | 736 | | return 0; |
| | | 737 | | } |
| | | 738 | | |
| | | 739 | | public override bool IsLeapMonth(int year, int month, int era) |
| | | 740 | | { |
| | | 741 | | // Year/era values are checked in IsLeapYear(). |
| | 0 | 742 | | bool isLeapYear = IsLeapYear(year, era); |
| | 0 | 743 | | CheckHebrewMonthValue(year, month, era); |
| | | 744 | | |
| | | 745 | | // The 7th month in a leap year is a leap month. |
| | 0 | 746 | | return isLeapYear && month == 7; |
| | | 747 | | } |
| | | 748 | | |
| | | 749 | | public override bool IsLeapYear(int year, int era) |
| | | 750 | | { |
| | 0 | 751 | | CheckHebrewYearValue(year, era, nameof(year)); |
| | 0 | 752 | | return ((7 * (long)year + 1) % 19) < 7; |
| | | 753 | | } |
| | | 754 | | |
| | | 755 | | /// <summary> |
| | | 756 | | /// (month1, day1) - (month2, day2) |
| | | 757 | | /// </summary> |
| | | 758 | | private static int GetDayDifference(int lunarYearType, int month1, int day1, int month2, int day2) |
| | | 759 | | { |
| | 0 | 760 | | if (month1 == month2) |
| | | 761 | | { |
| | 0 | 762 | | return day1 - day2; |
| | | 763 | | } |
| | | 764 | | |
| | | 765 | | // Make sure that (month1, day1) < (month2, day2) |
| | 0 | 766 | | bool swap = (month1 > month2); |
| | 0 | 767 | | if (swap) |
| | | 768 | | { |
| | | 769 | | // (month1, day1) < (month2, day2). Swap the values. |
| | | 770 | | // The result will be a negative number. |
| | | 771 | | int tempMonth, tempDay; |
| | 0 | 772 | | tempMonth = month1; tempDay = day1; |
| | 0 | 773 | | month1 = month2; day1 = day2; |
| | 0 | 774 | | month2 = tempMonth; day2 = tempDay; |
| | | 775 | | } |
| | | 776 | | |
| | | 777 | | // Get the number of days from (month1,day1) to (month1, end of month1) |
| | 0 | 778 | | int days = LunarMonthLen[lunarYearType * MaxMonthPlusOne + month1] - day1; |
| | | 779 | | |
| | | 780 | | // Move to next month. |
| | 0 | 781 | | month1++; |
| | | 782 | | |
| | | 783 | | // Add up the days. |
| | 0 | 784 | | while (month1 < month2) |
| | | 785 | | { |
| | 0 | 786 | | days += LunarMonthLen[lunarYearType * MaxMonthPlusOne + month1++]; |
| | | 787 | | } |
| | 0 | 788 | | days += day2; |
| | | 789 | | |
| | 0 | 790 | | return swap ? days : -days; |
| | | 791 | | } |
| | | 792 | | |
| | | 793 | | /// <summary> |
| | | 794 | | /// Convert Hebrew date to Gregorian date. |
| | | 795 | | /// The algorithm is like this: |
| | | 796 | | /// The hebrew year has an offset to the Gregorian year, so we can guess the Gregorian year for |
| | | 797 | | /// the specified Hebrew year. That is, GreogrianYear = HebrewYear - FirstHebrewYearOf1AD. |
| | | 798 | | /// |
| | | 799 | | /// From the Gregorian year and HebrewTable, we can get the Hebrew month/day value |
| | | 800 | | /// of the Gregorian date January 1st. Let's call this month/day value [hebrewDateForJan1] |
| | | 801 | | /// |
| | | 802 | | /// If the requested Hebrew month/day is less than [hebrewDateForJan1], we know the result |
| | | 803 | | /// Gregorian date falls in previous year. So we decrease the Gregorian year value, and |
| | | 804 | | /// retrieve the Hebrew month/day value of the Gregorian date january 1st again. |
| | | 805 | | /// |
| | | 806 | | /// Now, we get the answer of the Gregorian year. |
| | | 807 | | /// |
| | | 808 | | /// The next step is to get the number of days between the requested Hebrew month/day |
| | | 809 | | /// and [hebrewDateForJan1]. When we get that, we can create the DateTime by adding/subtracting |
| | | 810 | | /// the ticks value of the number of days. |
| | | 811 | | /// </summary> |
| | | 812 | | private static DateTime HebrewToGregorian(int hebrewYear, int hebrewMonth, int hebrewDay, int hour, int minute, |
| | | 813 | | { |
| | | 814 | | // Get the rough Gregorian year for the specified hebrewYear. |
| | 0 | 815 | | int gregorianYear = hebrewYear - HebrewYearOf1AD; |
| | | 816 | | |
| | 0 | 817 | | DateBuffer hebrewDateOfJan1 = new DateBuffer(); // year value is unused. |
| | 0 | 818 | | int lunarYearType = GetLunarMonthDay(gregorianYear, hebrewDateOfJan1); |
| | | 819 | | |
| | 0 | 820 | | if ((hebrewMonth == hebrewDateOfJan1.month) && (hebrewDay == hebrewDateOfJan1.day)) |
| | | 821 | | { |
| | 0 | 822 | | return new DateTime(gregorianYear, 1, 1, hour, minute, second, millisecond); |
| | | 823 | | } |
| | | 824 | | |
| | 0 | 825 | | int days = GetDayDifference(lunarYearType, hebrewMonth, hebrewDay, hebrewDateOfJan1.month, hebrewDateOfJan1. |
| | | 826 | | |
| | 0 | 827 | | DateTime gregorianNewYear = new DateTime(gregorianYear, 1, 1); |
| | 0 | 828 | | return new DateTime(gregorianNewYear.Ticks + days * TimeSpan.TicksPerDay + TimeToTicks(hour, minute, second, |
| | | 829 | | } |
| | | 830 | | |
| | | 831 | | public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisec |
| | | 832 | | { |
| | 0 | 833 | | CheckHebrewYearValue(year, era, nameof(year)); |
| | 0 | 834 | | CheckHebrewMonthValue(year, month, era); |
| | 0 | 835 | | CheckHebrewDayValue(year, month, day, era); |
| | 0 | 836 | | DateTime dt = HebrewToGregorian(year, month, day, hour, minute, second, millisecond); |
| | 0 | 837 | | CheckTicksRange(dt.Ticks); |
| | 0 | 838 | | return dt; |
| | | 839 | | } |
| | | 840 | | |
| | | 841 | | private const int DefaultTwoDigitYearMax = 5790; |
| | | 842 | | |
| | | 843 | | public override int TwoDigitYearMax |
| | | 844 | | { |
| | | 845 | | get |
| | | 846 | | { |
| | 0 | 847 | | if (_twoDigitYearMax == -1) |
| | | 848 | | { |
| | 0 | 849 | | _twoDigitYearMax = GetSystemTwoDigitYearSetting(ID, DefaultTwoDigitYearMax); |
| | | 850 | | } |
| | | 851 | | |
| | 0 | 852 | | return _twoDigitYearMax; |
| | | 853 | | } |
| | | 854 | | set |
| | | 855 | | { |
| | 0 | 856 | | VerifyWritable(); |
| | 0 | 857 | | if (value == 99) |
| | | 858 | | { |
| | | 859 | | // Do nothing here. Year 99 is allowed so that TwoDitYearMax is disabled. |
| | | 860 | | } |
| | | 861 | | else |
| | | 862 | | { |
| | 0 | 863 | | CheckHebrewYearValue(value, HebrewEra, nameof(value)); |
| | | 864 | | } |
| | | 865 | | |
| | 0 | 866 | | _twoDigitYearMax = value; |
| | 0 | 867 | | } |
| | | 868 | | } |
| | | 869 | | |
| | | 870 | | public override int ToFourDigitYear(int year) |
| | | 871 | | { |
| | 0 | 872 | | ArgumentOutOfRangeException.ThrowIfNegative(year); |
| | | 873 | | |
| | 0 | 874 | | if (year < 100) |
| | | 875 | | { |
| | 0 | 876 | | return base.ToFourDigitYear(year); |
| | | 877 | | } |
| | | 878 | | |
| | 0 | 879 | | if (year > MaxHebrewYear || year < MinHebrewYear) |
| | | 880 | | { |
| | 0 | 881 | | throw new ArgumentOutOfRangeException( |
| | 0 | 882 | | nameof(year), |
| | 0 | 883 | | year, |
| | 0 | 884 | | SR.Format(SR.ArgumentOutOfRange_Range, MinHebrewYear, MaxHebrewYear)); |
| | | 885 | | } |
| | 0 | 886 | | return year; |
| | | 887 | | } |
| | | 888 | | |
| | | 889 | | internal sealed class DateBuffer |
| | | 890 | | { |
| | | 891 | | internal int year; |
| | | 892 | | internal int month; |
| | | 893 | | internal int day; |
| | | 894 | | } |
| | | 895 | | } |
| | | 896 | | } |
| | | 897 | | |