xref: /openbsd/usr.bin/calendar/pesach.c (revision 771fbea0)
1 /*	$OpenBSD: pesach.c,v 1.4 2015/03/15 00:41:28 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Michael Shalayeff
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
16  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <stdio.h>
21 
22 #include "calendar.h"
23 
24 /* Calculate the Julian date of Pesach using the Gauss formula */
25 
26 #define	T	(33. + 14. / 24.)
27 #define	L	((1.  + 485. / 1080.) / 24. / 19.)
28 #define	K	((29. + (12. + 793. / 1080.) / 24. ) / 19.)
29 
30 int
31 pesach(int R)
32 {
33 	int a, b, y, cumdays;
34 	double d;
35 
36 	y = R + 3760;
37 
38 	a = (12 * y + 17) % 19;
39 	b = y % 4;
40 	d = (T - 10 * K + L + 14) + K * a +  b / 4. - L * y;
41 	cumdays = d;
42 
43 	/* the postponement */
44 	switch ((int)(cumdays + 3 * y + 5 * b + 5) % 7) {
45 	case 1:
46 		if (a > 6 && d - cumdays >= (15. + 204. / 1080.) / 24.)
47 			cumdays += 2;
48 		break;
49 
50 	case 0:
51 		if (a <= 11 || d - cumdays < (21. + 589. / 1080.) / 24.)
52 			break;
53 		/* FALLTHROUGH */
54 	case 2:
55 	case 4:
56 	case 6:
57 		cumdays++;
58 		break;
59 	}
60 
61 	if (R > 1582)
62 		cumdays += R / 100 - R /400 - 2;
63 
64 	return (31 + 28 + cumdays + (isleap(R)? 1 : 0));
65 }
66