xref: /openbsd/usr.bin/calendar/pesach.c (revision 404b540a)
1 /*	$OpenBSD: pesach.c,v 1.2 2004/12/10 20:50:45 mickey 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 #ifndef lint
21 static const char rcsid[] = "$OpenBSD: pesach.c,v 1.2 2004/12/10 20:50:45 mickey Exp $";
22 #endif /* not lint */
23 
24 #include <stdio.h>
25 #include <tzfile.h>
26 
27 #include "calendar.h"
28 
29 /* Calculate the Julian date of Pesach using the Gauss formula */
30 
31 #define	T	(33. + 14. / 24.)
32 #define	L	((1.  + 485. / 1080.) / 24. / 19.)
33 #define	K	((29. + (12. + 793. / 1080.) / 24. ) / 19.)
34 
35 int
36 pesach(int R)
37 {
38 	int a, b, y, cumdays;
39 	double d;
40 
41 	y = R + 3760;
42 
43 	a = (12 * y + 17) % 19;
44 	b = y % 4;
45 	d = (T - 10 * K + L + 14) + K * a +  b / 4. - L * y;
46 	cumdays = d;
47 
48 	/* the postponement */
49 	switch ((int)(cumdays + 3 * y + 5 * b + 5) % 7) {
50 	case 1:
51 		if (a > 6 && d - cumdays >= (15. + 204. / 1080.) / 24.)
52 			cumdays += 2;
53 		break;
54 
55 	case 0:
56 		if (a <= 11 || d - cumdays < (21. + 589. / 1080.) / 24.)
57 			break;
58 		/* FALLTHROUGH */
59 	case 2:
60 	case 4:
61 	case 6:
62 		cumdays++;
63 		break;
64 	}
65 
66 	if (R > 1582)
67 		cumdays += R / 100 - R /400 - 2;
68 
69 	return (31 + 28 + cumdays + (isleap(R)? 1 : 0));
70 }
71