2 // MonthLengthFlags, Gregorian Date
4 [1769, -2168121600000],
5 [3794, -2137449600000],
6 [3748, -2106777600000],
7 [3402, -2076192000000],
8 [2710, -2045606400000],
9 [1334, -2015020800000],
10 [2741, -1984435200000],
11 [3498, -1953763200000],
12 [2980, -1923091200000],
13 [2889, -1892505600000],
14 [2707, -1861920000000],
15 [1323, -1831334400000],
16 [2647, -1800748800000],
17 [1206, -1770076800000],
18 [2741, -1739491200000],
19 [1450, -1708819200000],
20 [3413, -1678233600000],
21 [3370, -1647561600000],
22 [2646, -1616976000000],
23 [1198, -1586390400000],
24 [2397, -1555804800000],
25 [748, -1525132800000],
26 [1749, -1494547200000],
27 [1706, -1463875200000],
28 [1365, -1433289600000],
29 [1195, -1402704000000],
30 [2395, -1372118400000],
31 [698, -1341446400000],
32 [1397, -1310860800000],
33 [2994, -1280188800000],
34 [1892, -1249516800000],
35 [1865, -1218931200000],
36 [1621, -1188345600000],
37 [683, -1157760000000],
38 [1371, -1127174400000],
39 [2778, -1096502400000],
40 [1748, -1065830400000],
41 [3785, -1035244800000],
42 [3474, -1004572800000],
43 [3365, -973987200000],
44 [2637, -943401600000],
46 [1389, -882230400000],
47 [2922, -851558400000],
48 [2898, -820886400000],
49 [2725, -790300800000],
50 [2635, -759715200000],
51 [1175, -729129600000],
52 [2359, -698544000000],
54 [1397, -637286400000],
55 [3434, -606614400000],
56 [3410, -575942400000],
57 [2710, -545356800000],
58 [2349, -514771200000],
60 [1245, -453600000000],
61 [2778, -422928000000],
62 [1492, -392256000000],
63 [3497, -361670400000],
64 [3410, -330998400000],
65 [2730, -300412800000],
66 [1238, -269827200000],
67 [2486, -239241600000],
69 [1897, -177984000000],
70 [1874, -147312000000],
71 [1701, -116726400000],
100 [2890, 771206400000],
101 [2645, 801792000000],
103 [1397, 862963200000],
104 [2922, 893635200000],
105 [3026, 924307200000],
106 [3012, 954979200000],
107 [2953, 985564800000],
108 [2709, 1016150400000],
109 [1325, 1046736000000],
110 [1453, 1077321600000],
111 [2922, 1107993600000],
112 [1748, 1138665600000],
113 [3529, 1169251200000],
114 [3474, 1199923200000],
115 [2726, 1230508800000],
116 [2390, 1261094400000],
117 [686, 1291680000000],
118 [1389, 1322265600000],
119 [874, 1352937600000],
120 [2901, 1383523200000],
121 [2730, 1414195200000],
122 [2381, 1444780800000],
123 [1181, 1475366400000],
124 [2397, 1505952000000],
125 [698, 1536624000000],
126 [1461, 1567209600000],
127 [1450, 1597881600000],
128 [3413, 1628467200000],
129 [2714, 1659139200000],
130 [2350, 1689724800000],
131 [622, 1720310400000],
132 [1373, 1750896000000],
133 [2778, 1781568000000],
134 [1748, 1812240000000],
135 [1701, 1842825600000],
138 minDate: -2198707200000,
139 maxDate: 1873411199999,
140 toGregorian: function(hyear, hmonth, hday) {
142 gyear = hyear - 1318;
143 if (gyear < 0 || gyear >= this._yearInfo.length) return null;
144 var info = this._yearInfo[gyear],
145 gdate = new Date(info[1]),
146 monthLength = info[0];
147 // Date's ticks in javascript are always from the GMT time,
148 // but we are interested in the gregorian date in the same timezone,
149 // not what the gregorian date was at GMT time, so we adjust for the offset.
150 gdate.setMinutes(gdate.getMinutes() + gdate.getTimezoneOffset());
151 for (var i = 0; i < hmonth; i++) {
152 days += 29 + (monthLength & 1);
153 monthLength = monthLength >> 1;
155 gdate.setDate(gdate.getDate() + days);
158 fromGregorian: function(gdate) {
159 // Date's ticks in javascript are always from the GMT time,
160 // but we are interested in the hijri date in the same timezone,
161 // not what the hijri date was at GMT time, so we adjust for the offset.
162 var ticks = gdate - gdate.getTimezoneOffset() * 60000;
163 if (ticks < this.minDate || ticks > this.maxDate) return null;
166 // find the earliest gregorian date in the array that is greater than or equal to the given date
167 while (ticks > this._yearInfo[++hyear][1]) { }
168 if (ticks !== this._yearInfo[hyear][1]) {
171 var info = this._yearInfo[hyear],
172 // how many days has it been since the date we found in the array?
173 // 86400000 = ticks per day
174 days = Math.floor((ticks - info[1]) / 86400000),
175 monthLength = info[0];
176 hyear += 1318; // the Nth array entry corresponds to hijri year 1318+N
177 // now increment day/month based on the total days, considering
178 // how many days are in each month. We cannot run past the year
179 // mark since we would have found a different array entry in that case.
180 var daysInMonth = 29 + (monthLength & 1);
181 while (days >= daysInMonth) {
183 monthLength = monthLength >> 1;
184 daysInMonth = 29 + (monthLength & 1);
187 // remaining days is less than is in one month, thus is the day of the month we landed on
188 // hmonth-1 because in javascript months are zero based, stay consistent with that.
189 return [hyear, hmonth - 1, days + 1];