xref: /freebsd/usr.bin/calendar/ostern.c (revision 8e537d168674d6b65869f73c20813001af875738)
1 /*
2  * Copyright (c) 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * 	$Id: ostern.c,v 1.4 1996/05/11 20:44:20 ache Exp $
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 
34 #include "calendar.h"
35 
36 /* return year day for Easter */
37 
38 int easter (year)
39     int year;            /* 0 ... abcd, NOT since 1900 */
40 {
41 
42     int e_a, e_b, e_c, e_d, e_e,e_f, e_g, e_h, e_i, e_k,
43         e_l, e_m, e_n, e_p, e_q;
44 
45     /* silly, but it works */
46     e_a = year % 19;
47     e_b = year / 100;
48     e_c = year % 100;
49 
50     e_d = e_b / 4;
51     e_e = e_b % 4;
52     e_f = (e_b + 8) / 25;
53     e_g = (e_b + 1 - e_f) / 3;
54     e_h = ((19 * e_a) + 15 + e_b - (e_d + e_g)) % 30;
55     e_i = e_c / 4;
56     e_k = e_c % 4;
57     e_l = (32 + 2 * e_e + 2 * e_i - (e_h + e_k)) % 7;
58     e_m = (e_a + 11 * e_h + 22 * e_l) / 451;
59     e_n = (e_h + e_l + 114 - (7 * e_m)) / 31;
60     e_p = (e_h + e_l + 114 - (7 * e_m)) % 31;
61     e_p = e_p + 1;
62 
63     e_q = 31 + 28;
64 
65     if (e_k == 0 && e_c != 0)
66 	e_q += 1;
67 
68     if (e_n == 4)
69 	e_q += 31;
70 
71     e_q += e_p;
72 
73 #if DEBUG
74     printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", e_a , e_b , e_c , e_d , e_e , e_f , e_g , e_h , e_i , e_k , e_l , e_m , e_n  , e_p , e_q);
75 #endif
76 
77     return (e_q);
78 }
79 
80 /* return year day for  Easter or easter depending days
81  * Match: Easter([+-][0-9]+)?
82  * e.g: Easter-2 is  Good Friday (2 days before Easter)
83  */
84 
85 int
86 geteaster(s, year)
87 	char *s;
88         int year;
89 {
90 	register int offset = 0;
91 	extern struct fixs neaster;
92 
93 #define EASTER "easter"
94 #define EASTERNAMELEN (sizeof(EASTER) - 1)
95 
96 	if (strncasecmp(s, EASTER, EASTERNAMELEN) == 0)
97 	    s += EASTERNAMELEN;
98 	else if (   neaster.name != NULL
99 		 && strncasecmp(s, neaster.name, neaster.len) == 0
100 		)
101 	    s += neaster.len;
102 	else
103 	    return(0);
104 
105 #if DEBUG
106 	printf("%s %d %d\n", s, year, EASTERNAMELEN);
107 #endif
108 
109 	/* Easter+1  or Easter-2
110 	 *       ^            ^   */
111 
112 	switch(*s) {
113 
114 	case '-':
115 	case '+':
116 	    offset = atoi(s);
117 	    break;
118 
119 	default:
120 	    offset = 0;
121 	}
122 
123 	return (easter(year) + offset);
124 }
125