xref: /PHP-8.4/ext/calendar/jewish.c (revision e3015de7)
1 /* $selId: jewish.c,v 2.0 1995/10/24 01:13:06 lees Exp $
2  * Copyright 1993-1995, Scott E. Lee, all rights reserved.
3  * Permission granted to use, copy, modify, distribute and sell so long as
4  * the above copyright and this permission statement are retained in all
5  * copies.  THERE IS NO WARRANTY - USE AT YOUR OWN RISK.
6  */
7 
8 /**************************************************************************
9  *
10  * These are the externally visible components of this file:
11  *
12  *     void
13  *     SdnToJewish(
14  *         long int sdn,
15  *         int *pYear,
16  *         int *pMonth,
17  *         int *pDay);
18  *
19  * Convert a SDN to a Jewish calendar date.  If the input SDN is before the
20  * first day of year 1, the three output values will all be set to zero,
21  * otherwise *pYear will be > 0; *pMonth will be in the range 1 to 13
22  * inclusive; *pDay will be in the range 1 to 30 inclusive.  Note that Adar
23  * II is assigned the month number 7 and Elul is always 13.
24  *
25  *     long int
26  *     JewishToSdn(
27  *         int year,
28  *         int month,
29  *         int day);
30  *
31  * Convert a Jewish calendar date to a SDN.  Zero is returned when the
32  * input date is detected as invalid or out of the supported range.  The
33  * return value will be > 0 for all valid, supported dates, but there are
34  * some invalid dates that will return a positive value.  To verify that a
35  * date is valid, convert it to SDN and then back and compare with the
36  * original.
37  *
38  *     char *JewishMonthName[14];
39  *
40  * Convert a Jewish month number (1 to 13) to the name of the Jewish month
41  * (null terminated).  An index of zero will return a zero length string.
42  *
43  * VALID RANGE
44  *
45  *     Although this software can handle dates all the way back to the year
46  *     1 (3761 B.C.), such use may not be meaningful.
47  *
48  *     The Jewish calendar has been in use for several thousand years, but
49  *     in the early days there was no formula to determine the start of a
50  *     month.  A new month was started when the new moon was first
51  *     observed.
52  *
53  *     It is not clear when the current rule based calendar replaced the
54  *     observation based calendar.  According to the book "Jewish Calendar
55  *     Mystery Dispelled" by George Zinberg, the patriarch Hillel II
56  *     published these rules in 358 A.D.  But, according to The
57  *     Encyclopedia Judaica, Hillel II may have only published the 19 year
58  *     rule for determining the occurrence of leap years.
59  *
60  *     I have yet to find a specific date when the current set of rules
61  *     were known to be in use.
62  *
63  * CALENDAR OVERVIEW
64  *
65  *     The Jewish calendar is based on lunar as well as solar cycles.  A
66  *     month always starts on or near a new moon and has either 29 or 30
67  *     days (a lunar cycle is about 29 1/2 days).  Twelve of these
68  *     alternating 29-30 day months gives a year of 354 days, which is
69  *     about 11 1/4 days short of a solar year.
70  *
71  *     Since a month is defined to be a lunar cycle (new moon to new moon),
72  *     this 11 1/4 day difference cannot be overcome by adding days to a
73  *     month as with the Gregorian calendar, so an entire month is
74  *     periodically added to the year, making some years 13 months long.
75  *
76  *     For astronomical as well as ceremonial reasons, the start of a new
77  *     year may be delayed until a day or two after the new moon causing
78  *     years to vary in length.  Leap years can be from 383 to 385 days and
79  *     common years can be from 353 to 355 days.  These are the months of
80  *     the year and their possible lengths:
81  *
82  *                       COMMON YEAR          LEAP YEAR
83  *          1 Tishri    30   30   30         30   30   30
84  *          2 Heshvan   29   29   30         29   29   30 (variable)
85  *          3 Kislev    29   30   30         29   30   30 (variable)
86  *          4 Tevet     29   29   29         29   29   29
87  *          5 Shevat    30   30   30         30   30   30
88  *          6 Adar I    --   --   --         30   30   30 (optional)
89  *          7 Adar (II) 29   29   29         29   29   29
90  *          8 Nisan     30   30   30         30   30   30
91  *          9 Iyyar     29   29   29         29   29   29
92  *         10 Sivan     30   30   30         30   30   30
93  *         11 Tammuz    29   29   29         29   29   29
94  *         12 Av        30   30   30         30   30   30
95  *         13 Elul      29   29   29         29   29   29
96  *                     ---  ---  ---        ---  ---  ---
97  *                     353  354  355        383  384  385
98  *
99  *     Note that the month names and other words that appear in this file
100  *     have multiple possible spellings in the Roman character set.  I have
101  *     chosen to use the spellings found in the Encyclopedia Judaica.
102  *
103  *     Adar I, the month added for leap years, is sometimes referred to as
104  *     the 13th month, but I have chosen to assign it the number 6 to keep
105  *     the months in chronological order.  This may not be consistent with
106  *     other numbering schemes.
107  *
108  *     Leap years occur in a fixed pattern of 19 years called the metonic
109  *     cycle.  The 3rd, 6th, 8th, 11th, 14th, 17th and 19th years of this
110  *     cycle are leap years.  The first metonic cycle starts with Jewish
111  *     year 1, or 3761/60 B.C.  This is believed to be the year of
112  *     creation.
113  *
114  *     To construct the calendar for a year, you must first find the length
115  *     of the year by determining the first day of the year (Tishri 1, or
116  *     Rosh Ha-Shanah) and the first day of the following year.  This
117  *     selects one of the six possible month length configurations listed
118  *     above.
119  *
120  *     Finding the first day of the year is the most difficult part.
121  *     Finding the date and time of the new moon (or molad) is the first
122  *     step.  For this purpose, the lunar cycle is assumed to be 29 days 12
123  *     hours and 793 halakim.  A halakim is 1/1080th of an hour or 3 1/3
124  *     seconds.  (This assumed value is only about 1/2 second less than the
125  *     value used by modern astronomers -- not bad for a number that was
126  *     determined so long ago.)  The first molad of year 1 occurred on
127  *     Sunday at 11:20:11 P.M.  This would actually be Monday, because the
128  *     Jewish day is considered to begin at sunset.
129  *
130  *     Since sunset varies, the day is assumed to begin at 6:00 P.M.  for
131  *     calendar calculation purposes.  So, the first molad was 5 hours 793
132  *     halakim after the start of Tishri 1, 0001 (which was Monday
133  *     September 7, 4761 B.C. by the Gregorian calendar).  All subsequent
134  *     molads can be calculated from this starting point by adding the
135  *     length of a lunar cycle.
136  *
137  *     Once the molad that starts a year is determined the actual start of
138  *     the year (Tishri 1) can be determined.  Tishri 1 will be the day of
139  *     the molad unless it is delayed by one of the following four rules
140  *     (called dehiyyot).  Each rule can delay the start of the year by one
141  *     day, and since rule #1 can combine with one of the other rules, it
142  *     can be delayed as much as two days.
143  *
144  *         1.  Tishri 1 must never be Sunday, Wednesday or Friday.  (This
145  *             is largely to prevent certain holidays from occurring on the
146  *             day before or after the Sabbath.)
147  *
148  *         2.  If the molad occurs on or after noon, Tishri 1 must be
149  *             delayed.
150  *
151  *         3.  If it is a common (not leap) year and the molad occurs on
152  *             Tuesday at or after 3:11:20 A.M., Tishri 1 must be delayed.
153  *
154  *         4.  If it is the year following a leap year and the molad occurs
155  *             on Monday at or after 9:32:43 and 1/3 sec, Tishri 1 must be
156  *             delayed.
157  *
158  * GLOSSARY
159  *
160  *     dehiyyot         The set of 4 rules that determine when the new year
161  *                      starts relative to the molad.
162  *
163  *     halakim          1/1080th of an hour or 3 1/3 seconds.
164  *
165  *     lunar cycle      The period of time between mean conjunctions of the
166  *                      sun and moon (new moon to new moon).  This is
167  *                      assumed to be 29 days 12 hours and 793 halakim for
168  *                      calendar purposes.
169  *
170  *     metonic cycle    A 19 year cycle which determines which years are
171  *                      leap years and which are common years.  The 3rd,
172  *                      6th, 8th, 11th, 14th, 17th and 19th years of this
173  *                      cycle are leap years.
174  *
175  *     molad            The date and time of the mean conjunction of the
176  *                      sun and moon (new moon).  This is the approximate
177  *                      beginning of a month.
178  *
179  *     Rosh Ha-Shanah   The first day of the Jewish year (Tishri 1).
180  *
181  *     Tishri           The first month of the Jewish year.
182  *
183  * ALGORITHMS
184  *
185  *     SERIAL DAY NUMBER TO JEWISH DATE
186  *
187  *     The simplest approach would be to use the rules stated above to find
188  *     the molad of Tishri before and after the given day number.  Then use
189  *     the molads to find Tishri 1 of the current and following years.
190  *     From this the length of the year can be determined and thus the
191  *     length of each month.  But this method is used as a last resort.
192  *
193  *     The first 59 days of the year are the same regardless of the length
194  *     of the year.  As a result, only the day number of the start of the
195  *     year is required.
196  *
197  *     Similarly, the last 6 months do not change from year to year.  And
198  *     since it can be determined whether the year is a leap year by simple
199  *     division, the lengths of Adar I and II can be easily calculated.  In
200  *     fact, all dates after the 3rd month are consistent from year to year
201  *     (once it is known whether it is a leap year).
202  *
203  *     This means that if the given day number falls in the 3rd month or on
204  *     the 30th day of the 2nd month the length of the year must be found,
205  *     but in no other case.
206  *
207  *     So, the approach used is to take the given day number and round it
208  *     to the closest molad of Tishri (first new moon of the year).  The
209  *     rounding is not really to the *closest* molad, but is such that if
210  *     the day number is before the middle of the 3rd month the molad at
211  *     the start of the year is found, otherwise the molad at the end of
212  *     the year is found.
213  *
214  *     Only if the day number is actually found to be in the ambiguous
215  *     period of 29 to 31 days is the other molad calculated.
216  *
217  *     JEWISH DATE TO SERIAL DAY NUMBER
218  *
219  *     The year number is used to find which 19 year metonic cycle contains
220  *     the date and which year within the cycle (this is a division and
221  *     modulus).  This also determines whether it is a leap year.
222  *
223  *     If the month is 1 or 2, the calculation is simple addition to the
224  *     first of the year.
225  *
226  *     If the month is 8 (Nisan) or greater, the calculation is simple
227  *     subtraction from beginning of the following year.
228  *
229  *     If the month is 4 to 7, it is considered whether it is a leap year
230  *     and then simple subtraction from the beginning of the following year
231  *     is used.
232  *
233  *     Only if it is the 3rd month is both the start and end of the year
234  *     required.
235  *
236  * TESTING
237  *
238  *     This algorithm has been tested in two ways.  First, 510 dates from a
239  *     table in "Jewish Calendar Mystery Dispelled" were calculated and
240  *     compared to the table.  Second, the calculation algorithm described
241  *     in "Jewish Calendar Mystery Dispelled" was coded and used to verify
242  *     all dates from the year 1 (3761 B.C.) to the year 13760 (10000
243  *     A.D.).
244  *
245  *     The source code of the verification program is included in this
246  *     package.
247  *
248  * REFERENCES
249  *
250  *     The Encyclopedia Judaica, the entry for "Calendar"
251  *
252  *     The Jewish Encyclopedia
253  *
254  *     Jewish Calendar Mystery Dispelled by George Zinberg, Vantage Press,
255  *     1963
256  *
257  *     The Comprehensive Hebrew Calendar by Arthur Spier, Behrman House
258  *
259  *     The Book of Calendars [note that this work contains many typos]
260  *
261  **************************************************************************/
262 
263 #include "sdncal.h"
264 
265 #define HALAKIM_PER_HOUR 1080
266 #define HALAKIM_PER_DAY 25920
267 #define HALAKIM_PER_LUNAR_CYCLE ((29 * HALAKIM_PER_DAY) + 13753)
268 #define HALAKIM_PER_METONIC_CYCLE (HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7))
269 
270 #define JEWISH_SDN_OFFSET 347997
271 #define JEWISH_SDN_MAX 324542846L /* 12/13/887605, greater value raises integer overflow */
272 #define NEW_MOON_OF_CREATION 31524
273 
274 #define SUNDAY    0
275 #define MONDAY    1
276 #define TUESDAY   2
277 #define WEDNESDAY 3
278 #define THURSDAY  4
279 #define FRIDAY    5
280 #define SATURDAY  6
281 
282 #define NOON (18 * HALAKIM_PER_HOUR)
283 #define AM3_11_20 ((9 * HALAKIM_PER_HOUR) + 204)
284 #define AM9_32_43 ((15 * HALAKIM_PER_HOUR) + 589)
285 
286 const int monthsPerYear[19] =
287 {
288 12, 12, 13, 12, 12, 13, 12, 13, 12, 12, 13, 12, 12, 13, 12, 12, 13, 12, 13
289 };
290 
291 static const int yearOffset[19] =
292 {
293 	0, 12, 24, 37, 49, 61, 74, 86, 99, 111, 123,
294 	136, 148, 160, 173, 185, 197, 210, 222
295 };
296 
297 /* names for leap (13-month) year */
298 const char * const JewishMonthNameLeap[14] =
299 {
300 	"",
301 	"Tishri",
302 	"Heshvan",
303 	"Kislev",
304 	"Tevet",
305 	"Shevat",
306 	"Adar I",
307 	"Adar II",
308 	"Nisan",
309 	"Iyyar",
310 	"Sivan",
311 	"Tammuz",
312 	"Av",
313 	"Elul"
314 };
315 
316 /* names for regular year */
317 const char * const JewishMonthName[14] =
318 {
319 	"",
320 	"Tishri",
321 	"Heshvan",
322 	"Kislev",
323 	"Tevet",
324 	"Shevat",
325 	"",
326 	"Adar",
327 	"Nisan",
328 	"Iyyar",
329 	"Sivan",
330 	"Tammuz",
331 	"Av",
332 	"Elul"
333 };
334 
335 /* names for leap (13-month) year */
336 const char * const JewishMonthHebNameLeap[14] =
337 {
338 	"",
339 	"\xFA\xF9\xF8\xE9",
340 	"\xE7\xF9\xE5\xEF",
341 	"\xEB\xF1\xEC\xE5",
342 	"\xE8\xE1\xFA",
343 	"\xF9\xE1\xE8",
344 	"\xE0\xE3\xF8 \xE0'",
345 	"\xE0\xE3\xF8 \xE1'",
346 	"\xF0\xE9\xF1\xEF",
347 	"\xE0\xE9\xE9\xF8",
348 	"\xF1\xE9\xe5\xEF",
349 	"\xFA\xEE\xE5\xE6",
350 	"\xE0\xE1",
351 	"\xE0\xEC\xE5\xEC"
352 };
353 
354 /* names for regular year */
355 const char * const JewishMonthHebName[14] =
356 {
357 	"",
358 	"\xFA\xF9\xF8\xE9",
359 	"\xE7\xF9\xE5\xEF",
360 	"\xEB\xF1\xEC\xE5",
361 	"\xE8\xE1\xFA",
362 	"\xF9\xE1\xE8",
363 	"",
364 	"\xE0\xE3\xF8",
365 	"\xF0\xE9\xF1\xEF",
366 	"\xE0\xE9\xE9\xF8",
367 	"\xF1\xE9\xE5\xEF",
368 	"\xFA\xEE\xE5\xE6",
369 	"\xE0\xE1",
370 	"\xE0\xEC\xE5\xEC"
371 };
372 
373 /************************************************************************
374  * Given the year within the 19 year metonic cycle and the time of a molad
375  * (new moon) which starts that year, this routine will calculate what day
376  * will be the actual start of the year (Tishri 1 or Rosh Ha-Shanah).  This
377  * first day of the year will be the day of the molad unless one of 4 rules
378  * (called dehiyyot) delays it.  These 4 rules can delay the start of the
379  * year by as much as 2 days.
380  */
Tishri1(int metonicYear,zend_long moladDay,zend_long moladHalakim)381 static zend_long Tishri1(
382 						   int metonicYear,
383 						   zend_long moladDay,
384 						   zend_long moladHalakim)
385 {
386 	zend_long tishri1;
387 	int dow;
388 	int leapYear;
389 	int lastWasLeapYear;
390 
391 	tishri1 = moladDay;
392 	dow = tishri1 % 7;
393 	leapYear = metonicYear == 2 || metonicYear == 5 || metonicYear == 7
394 		|| metonicYear == 10 || metonicYear == 13 || metonicYear == 16
395 		|| metonicYear == 18;
396 	lastWasLeapYear = metonicYear == 3 || metonicYear == 6
397 		|| metonicYear == 8 || metonicYear == 11 || metonicYear == 14
398 		|| metonicYear == 17 || metonicYear == 0;
399 
400 	/* Apply rules 2, 3 and 4. */
401 	if ((moladHalakim >= NOON) ||
402 		((!leapYear) && dow == TUESDAY && moladHalakim >= AM3_11_20) ||
403 		(lastWasLeapYear && dow == MONDAY && moladHalakim >= AM9_32_43)) {
404 		tishri1++;
405 		dow++;
406 		if (dow == 7) {
407 			dow = 0;
408 		}
409 	}
410 	/* Apply rule 1 after the others because it can cause an additional
411 	 * delay of one day. */
412 	if (dow == WEDNESDAY || dow == FRIDAY || dow == SUNDAY) {
413 		tishri1++;
414 	}
415 	return (tishri1);
416 }
417 
418 /************************************************************************
419  * Given a metonic cycle number, calculate the date and time of the molad
420  * (new moon) that starts that cycle.  Since the length of a metonic cycle
421  * is a constant, this is a simple calculation, except that it requires an
422  * intermediate value which is bigger that 32 bits.  Because this
423  * intermediate value only needs 36 to 37 bits and the other numbers are
424  * constants, the process has been reduced to just a few steps.
425  */
MoladOfMetonicCycle(int metonicCycle,zend_long * pMoladDay,zend_long * pMoladHalakim)426 static void MoladOfMetonicCycle(
427 								   int metonicCycle,
428 								   zend_long *pMoladDay,
429 								   zend_long *pMoladHalakim)
430 {
431 	register zend_ulong r1, r2, d1, d2;
432 	zend_long chk;
433 
434 	/* Start with the time of the first molad after creation. */
435 	r1 = NEW_MOON_OF_CREATION;
436 	chk = (zend_long)metonicCycle;
437 
438 	if (chk > (ZEND_LONG_MAX - NEW_MOON_OF_CREATION) / (HALAKIM_PER_METONIC_CYCLE & 0xFFFF)) {
439 		*pMoladDay = 0;
440 		*pMoladHalakim = 0;
441 		return;
442 	}
443 
444 	/* Calculate metonicCycle * HALAKIM_PER_METONIC_CYCLE.  The upper 32
445 	 * bits of the result will be in r2 and the lower 16 bits will be
446 	 * in r1. */
447 	r1 += chk * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF);
448 
449 	if (chk > (ZEND_LONG_MAX - (r1 >> 16)) / ((HALAKIM_PER_METONIC_CYCLE >> 16) & 0xFFFF)) {
450 		*pMoladDay = 0;
451 		*pMoladHalakim = 0;
452 		return;
453 	}
454 
455 	r2 = r1 >> 16;
456 	r2 += chk * ((HALAKIM_PER_METONIC_CYCLE >> 16) & 0xFFFF);
457 
458 	/* Calculate r2r1 / HALAKIM_PER_DAY.  The remainder will be in r1, the
459 	 * upper 16 bits of the quotient will be in d2 and the lower 16 bits
460 	 * will be in d1. */
461 	d2 = r2 / HALAKIM_PER_DAY;
462 	r2 -= d2 * HALAKIM_PER_DAY;
463 	r1 = (r2 << 16) | (r1 & 0xFFFF);
464 	d1 = r1 / HALAKIM_PER_DAY;
465 	r1 -= d1 * HALAKIM_PER_DAY;
466 
467 	*pMoladDay = (d2 << 16) | d1;
468 	*pMoladHalakim = r1;
469 }
470 
471 /************************************************************************
472  * Given a day number, find the molad of Tishri (the new moon at the start
473  * of a year) which is closest to that day number.  It's not really the
474  * *closest* molad that we want here.  If the input day is in the first two
475  * months, we want the molad at the start of the year.  If the input day is
476  * in the fourth to last months, we want the molad at the end of the year.
477  * If the input day is in the third month, it doesn't matter which molad is
478  * returned, because both will be required.  This type of "rounding" allows
479  * us to avoid calculating the length of the year in most cases.
480  */
FindTishriMolad(zend_long inputDay,int * pMetonicCycle,int * pMetonicYear,zend_long * pMoladDay,zend_long * pMoladHalakim)481 static void FindTishriMolad(
482 							   zend_long inputDay,
483 							   int *pMetonicCycle,
484 							   int *pMetonicYear,
485 							   zend_long *pMoladDay,
486 							   zend_long *pMoladHalakim)
487 {
488 	zend_long moladDay;
489 	zend_long moladHalakim;
490 	int metonicCycle;
491 	int metonicYear;
492 
493 	/* Estimate the metonic cycle number.  Note that this may be an under
494 	 * estimate because there are 6939.6896 days in a metonic cycle not
495 	 * 6940, but it will never be an over estimate.  The loop below will
496 	 * correct for any error in this estimate. */
497 	metonicCycle = (inputDay + 310) / 6940;
498 
499 	/* Calculate the time of the starting molad for this metonic cycle. */
500 	MoladOfMetonicCycle(metonicCycle, &moladDay, &moladHalakim);
501 
502 	/* If the above was an under estimate, increment the cycle number until
503 	 * the correct one is found.  For modern dates this loop is about 98.6%
504 	 * likely to not execute, even once, because the above estimate is
505 	 * really quite close. */
506 	while (moladDay < inputDay - 6940 + 310) {
507 		metonicCycle++;
508 		moladHalakim += HALAKIM_PER_METONIC_CYCLE;
509 		moladDay += moladHalakim / HALAKIM_PER_DAY;
510 		moladHalakim = moladHalakim % HALAKIM_PER_DAY;
511 	}
512 
513 	/* Find the molad of Tishri closest to this date. */
514 	for (metonicYear = 0; metonicYear < 18; metonicYear++) {
515 		if (moladDay > inputDay - 74) {
516 			break;
517 		}
518 		moladHalakim += HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear];
519 		moladDay += moladHalakim / HALAKIM_PER_DAY;
520 		moladHalakim = moladHalakim % HALAKIM_PER_DAY;
521 	}
522 
523 	*pMetonicCycle = metonicCycle;
524 	*pMetonicYear = metonicYear;
525 	*pMoladDay = moladDay;
526 	*pMoladHalakim = moladHalakim;
527 }
528 
529 /************************************************************************
530  * Given a year, find the number of the first day of that year and the date
531  * and time of the starting molad.
532  */
FindStartOfYear(int year,int * pMetonicCycle,int * pMetonicYear,zend_long * pMoladDay,zend_long * pMoladHalakim,int * pTishri1)533 static void FindStartOfYear(
534 							   int year,
535 							   int *pMetonicCycle,
536 							   int *pMetonicYear,
537 							   zend_long *pMoladDay,
538 							   zend_long *pMoladHalakim,
539 							   int *pTishri1)
540 {
541 	*pMetonicCycle = (year - 1) / 19;
542 	*pMetonicYear = (year - 1) % 19;
543 	MoladOfMetonicCycle(*pMetonicCycle, pMoladDay, pMoladHalakim);
544 
545 	*pMoladHalakim += HALAKIM_PER_LUNAR_CYCLE * yearOffset[*pMetonicYear];
546 	*pMoladDay += *pMoladHalakim / HALAKIM_PER_DAY;
547 	*pMoladHalakim = *pMoladHalakim % HALAKIM_PER_DAY;
548 
549 	*pTishri1 = Tishri1(*pMetonicYear, *pMoladDay, *pMoladHalakim);
550 }
551 
552 /************************************************************************
553  * Given a serial day number (SDN), find the corresponding year, month and
554  * day in the Jewish calendar.  The three output values will always be
555  * modified.  If the input SDN is before the first day of year 1, they will
556  * all be set to zero, otherwise *pYear will be > 0; *pMonth will be in the
557  * range 1 to 13 inclusive; *pDay will be in the range 1 to 30 inclusive.
558  */
SdnToJewish(zend_long sdn,int * pYear,int * pMonth,int * pDay)559 void SdnToJewish(
560 					zend_long sdn,
561 					int *pYear,
562 					int *pMonth,
563 					int *pDay)
564 {
565 	zend_long inputDay;
566 	zend_long day;
567 	zend_long halakim;
568 	int metonicCycle;
569 	int metonicYear;
570 	int tishri1;
571 	int tishri1After;
572 	int yearLength;
573 
574 	if (sdn <= JEWISH_SDN_OFFSET || sdn > JEWISH_SDN_MAX) {
575 		*pYear = 0;
576 		*pMonth = 0;
577 		*pDay = 0;
578 		return;
579 	}
580 	inputDay = sdn - JEWISH_SDN_OFFSET;
581 
582 	FindTishriMolad(inputDay, &metonicCycle, &metonicYear, &day, &halakim);
583 	tishri1 = Tishri1(metonicYear, day, halakim);
584 
585 	if (inputDay >= tishri1) {
586 		/* It found Tishri 1 at the start of the year. */
587 		*pYear = metonicCycle * 19 + metonicYear + 1;
588 		if (inputDay < tishri1 + 59) {
589 			if (inputDay < tishri1 + 30) {
590 				*pMonth = 1;
591 				*pDay = inputDay - tishri1 + 1;
592 			} else {
593 				*pMonth = 2;
594 				*pDay = inputDay - tishri1 - 29;
595 			}
596 			return;
597 		}
598 		/* We need the length of the year to figure this out, so find
599 		 * Tishri 1 of the next year. */
600 		halakim += HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear];
601 		day += halakim / HALAKIM_PER_DAY;
602 		halakim = halakim % HALAKIM_PER_DAY;
603 		tishri1After = Tishri1((metonicYear + 1) % 19, day, halakim);
604 	} else {
605 		/* It found Tishri 1 at the end of the year. */
606 		*pYear = metonicCycle * 19 + metonicYear;
607 		if (inputDay >= tishri1 - 177) {
608 			/* It is one of the last 6 months of the year. */
609 			if (inputDay > tishri1 - 30) {
610 				*pMonth = 13;
611 				*pDay = inputDay - tishri1 + 30;
612 			} else if (inputDay > tishri1 - 60) {
613 				*pMonth = 12;
614 				*pDay = inputDay - tishri1 + 60;
615 			} else if (inputDay > tishri1 - 89) {
616 				*pMonth = 11;
617 				*pDay = inputDay - tishri1 + 89;
618 			} else if (inputDay > tishri1 - 119) {
619 				*pMonth = 10;
620 				*pDay = inputDay - tishri1 + 119;
621 			} else if (inputDay > tishri1 - 148) {
622 				*pMonth = 9;
623 				*pDay = inputDay - tishri1 + 148;
624 			} else {
625 				*pMonth = 8;
626 				*pDay = inputDay - tishri1 + 178;
627 			}
628 			return;
629 		} else {
630 			if (monthsPerYear[(*pYear - 1) % 19] == 13) {
631 				*pMonth = 7;
632 				*pDay = inputDay - tishri1 + 207;
633 				if (*pDay > 0)
634 					return;
635 				(*pMonth)--;
636 				(*pDay) += 30;
637 				if (*pDay > 0)
638 					return;
639 				(*pMonth)--;
640 				(*pDay) += 30;
641 			} else {
642 				*pMonth = 7;
643 				*pDay = inputDay - tishri1 + 207;
644 				if (*pDay > 0)
645 					return;
646 				(*pMonth) -= 2;
647 				(*pDay) += 30;
648 			}
649 			if (*pDay > 0)
650 				return;
651 			(*pMonth)--;
652 			(*pDay) += 29;
653 			if (*pDay > 0)
654 				return;
655 
656 			/* We need the length of the year to figure this out, so find
657 			 * Tishri 1 of this year. */
658 			tishri1After = tishri1;
659 			FindTishriMolad(day - 365,
660 							&metonicCycle, &metonicYear, &day, &halakim);
661 			tishri1 = Tishri1(metonicYear, day, halakim);
662 		}
663 	}
664 
665 	yearLength = tishri1After - tishri1;
666 	day = inputDay - tishri1 - 29;
667 	if (yearLength == 355 || yearLength == 385) {
668 		/* Heshvan has 30 days */
669 		if (day <= 30) {
670 			*pMonth = 2;
671 			*pDay = day;
672 			return;
673 		}
674 		day -= 30;
675 	} else {
676 		/* Heshvan has 29 days */
677 		if (day <= 29) {
678 			*pMonth = 2;
679 			*pDay = day;
680 			return;
681 		}
682 		day -= 29;
683 	}
684 
685 	/* It has to be Kislev. */
686 	*pMonth = 3;
687 	*pDay = day;
688 }
689 
690 /************************************************************************
691  * Given a year, month and day in the Jewish calendar, find the
692  * corresponding serial day number (SDN).  Zero is returned when the input
693  * date is detected as invalid.  The return value will be > 0 for all valid
694  * dates, but there are some invalid dates that will return a positive
695  * value.  To verify that a date is valid, convert it to SDN and then back
696  * and compare with the original.
697  */
JewishToSdn(int year,int month,int day)698 zend_long JewishToSdn(
699 						int year,
700 						int month,
701 						int day)
702 {
703 	zend_long sdn;
704 	int metonicCycle;
705 	int metonicYear;
706 	int tishri1;
707 	int tishri1After;
708 	zend_long moladDay;
709 	zend_long moladHalakim;
710 	int yearLength;
711 	int lengthOfAdarIAndII;
712 
713 	if (year <= 0 || day <= 0 || day > 30) {
714 		return (0);
715 	}
716 	switch (month) {
717 		case 1:
718 		case 2:
719 			/* It is Tishri or Heshvan - don't need the year length. */
720 			FindStartOfYear(year, &metonicCycle, &metonicYear,
721 							&moladDay, &moladHalakim, &tishri1);
722 			if (month == 1) {
723 				sdn = tishri1 + day - 1;
724 			} else {
725 				sdn = tishri1 + day + 29;
726 			}
727 			break;
728 
729 		case 3:
730 			/* It is Kislev - must find the year length. */
731 
732 			/* Find the start of the year. */
733 			FindStartOfYear(year, &metonicCycle, &metonicYear,
734 							&moladDay, &moladHalakim, &tishri1);
735 
736 			/* Find the end of the year. */
737 			moladHalakim += HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear];
738 			moladDay += moladHalakim / HALAKIM_PER_DAY;
739 			moladHalakim = moladHalakim % HALAKIM_PER_DAY;
740 			tishri1After = Tishri1((metonicYear + 1) % 19, moladDay, moladHalakim);
741 
742 			yearLength = tishri1After - tishri1;
743 
744 			if (yearLength == 355 || yearLength == 385) {
745 				sdn = tishri1 + day + 59;
746 			} else {
747 				sdn = tishri1 + day + 58;
748 			}
749 			break;
750 
751 		case 4:
752 		case 5:
753 		case 6:
754 			/* It is Tevet, Shevat or Adar I - don't need the year length. */
755 
756 			FindStartOfYear(year + 1, &metonicCycle, &metonicYear,
757 							&moladDay, &moladHalakim, &tishri1After);
758 
759 			if (monthsPerYear[(year - 1) % 19] == 12) {
760 				lengthOfAdarIAndII = 29;
761 			} else {
762 				lengthOfAdarIAndII = 59;
763 			}
764 
765 			if (month == 4) {
766 				sdn = tishri1After + day - lengthOfAdarIAndII - 237;
767 			} else if (month == 5) {
768 				sdn = tishri1After + day - lengthOfAdarIAndII - 208;
769 			} else {
770 				sdn = tishri1After + day - lengthOfAdarIAndII - 178;
771 			}
772 			break;
773 
774 		default:
775 			/* It is Adar II or later - don't need the year length. */
776 			FindStartOfYear(year + 1, &metonicCycle, &metonicYear,
777 							&moladDay, &moladHalakim, &tishri1After);
778 
779 			switch (month) {
780 				case 7:
781 					sdn = tishri1After + day - 207;
782 					break;
783 				case 8:
784 					sdn = tishri1After + day - 178;
785 					break;
786 				case 9:
787 					sdn = tishri1After + day - 148;
788 					break;
789 				case 10:
790 					sdn = tishri1After + day - 119;
791 					break;
792 				case 11:
793 					sdn = tishri1After + day - 89;
794 					break;
795 				case 12:
796 					sdn = tishri1After + day - 60;
797 					break;
798 				case 13:
799 					sdn = tishri1After + day - 30;
800 					break;
801 				default:
802 					return (0);
803 			}
804 	}
805 	return (sdn + JEWISH_SDN_OFFSET);
806 }
807