File: easter.c

package info (click to toggle)
bsdmainutils 4.7.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 796 kB
  • ctags: 463
  • sloc: ansic: 5,899; sh: 145; makefile: 129; perl: 14
file content (352 lines) | stat: -rw-r--r-- 8,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* To find Easter day and the moveable feasts, according to BCP.
   The Orthodox Church still uses the Julian calendar, so they are about
   2 weeks different for everything.  They and the Catholic Church 
   probably keep many more saints days. */

/* See README.easter.c for licence notice and bug warnings. */

/* 1999/10/26  hacked by cpbs@debian.org to generate a calendar(1)
               file directly
*/

#include <stdio.h>
#include <time.h>

void syntax(char *program);
time_t easter(int year);
int calc_offset(int year);
void easter_sunday(struct tm *date);
void display_date(char *day, struct tm *date);
void feasts(struct tm *date);

struct tab {int month; int day;};

struct tab full_moons[] = {
  /* month, day,    golden number */
	{0,  0},
	{4, 14}, 	/*  1 */
	{4,  3}, 	/*  2 */
	{3, 23}, 	/*  3 */
	{4, 11}, 	/*  4 */
	{3, 31}, 	/*  5 */
	{4, 18}, 	/*  6 */
	{4,  8},	/*  7 */
	{3, 28},	/*  8 */
	{4, 16},	/*  9 */
	{4,  5},	/* 10 */
	{3, 25},	/* 11 */
	{4, 13},	/* 12 */
	{4,  2},	/* 13 */
	{3, 22},	/* 14 */
	{4, 10},	/* 15 */
	{3, 30},	/* 16 */
	{4, 17},	/* 17 */
	{4,  7},	/* 18 */
	{3, 27}		/* 19 */
	};

char *month[] = {
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
	};

int main(int argc, char **argv) {
	int year;
	struct tm *dateptr;
	time_t t;

	/* for which year? */
	switch (argc) {
	    case 1: 
		t = time(0);
		dateptr = localtime(&t);
		year = 1900 + dateptr->tm_year;
		break;
	    case 2:
		if (!sscanf(argv[1], "%d", &year)) {
			syntax(argv[0]);
			return(1);
		}
		if (year < 29) {
			fprintf(stderr,"There can be no date for Easter before the resurrection of Jesus!\n");
                        /* and the formula wasn't settled until the 4th century */
			return(3);
		}
		if (year < 1753) {
			/* I think it was 1753 in Britain */
			fprintf(stderr,"Warning: dates before 1753 were not according to the Gregorian calendar.\n");
			return(3);
		}
		break;
	    default:
		syntax(argv[0]);
		return(1);
		break;
	}

	t = easter(year);
	dateptr = localtime(&t);
	printf("/* Christian calendar for year %d (excluding fixed events)\n  ", year);
	easter_sunday(dateptr);
	printf("*/\n\n", year);
	feasts(dateptr);
	return(0);
}

/* Display required syntax */
void syntax(char *program) {
	fprintf(stderr, "%s: syntax error\n%s [year]\n", program, program);
}

/* Calculate Easter Day */
time_t easter(int year) {
	int golden_number;
	char sunday_letter;
	int i, j;
	struct tm date;
	time_t second;

	date.tm_sec = 0;
	date.tm_min = 0;
	date.tm_hour = 0;
	date.tm_isdst = -1;

	golden_number = (year + 1) % 19;
	if (!golden_number) {
		golden_number = 19;
	}
	
	j = calc_offset(year);
	i = (year + year/4 + j) % 7;
	sunday_letter = (!i ? 'A' : ('H' - i));

	date.tm_wday = -1;
	date.tm_yday = -1;
	date.tm_mday = 19;
	date.tm_mon = 2;
	date.tm_year = (year - 1900);
	second = mktime(&date);	/* 19th March */

	if (date.tm_wday == -1) {
		fprintf(stderr,"Year %d is outside the Unix date range\n", year);
		exit(2);
	}

	i = date.tm_yday;	/* 3 days before the first possible Paschal
				   full moon */

	date.tm_wday = -1;
	date.tm_yday = -1;
	date.tm_mday = full_moons[golden_number].day;
	date.tm_mon = (full_moons[golden_number].month - 1);
	second = mktime(&date);	/* Paschal full moon */

	if (date.tm_wday == -1) {
		fprintf(stderr,"Year %d is outside the Unix date range\n", year);
		exit(2);
	}

	for (j = i; j < date.tm_yday; j += 7);

	j += (sunday_letter - 'A');

	j = (j <= i) ? j + 7 : j;

	date.tm_wday = -1;
	date.tm_yday = -1;
	date.tm_mday = j;
	date.tm_mon = 0;
	
	second = mktime(&date);	/* Easter Day */
	if (date.tm_wday != 0) {
		fprintf(stderr, "Calculation failed!\n");
	}
	return (second);
}

/* The offset for the golden number depends on the century */
int calc_offset(int year) {
	int i = 0;

	i = (year / 100);
	i -= (year / 400);
	i = (i + 1) % 7;
	if (i) {
		i = 7 - i;
	}
	return (i);
}

/* Display the date of Easter Day */
void easter_sunday(struct tm *date) {
	display_date("Easter Day", date);
}

void display_date(char *day, struct tm *date) {
	printf("%3.3s %.2d\t%s\n", month[date->tm_mon], date->tm_mday, /* 1900+date->tm_year, */ day);
}

/* Display the moveable feasts */
void feasts(struct tm *date) {
	struct tm d;
	time_t t;
	int j;

	d = *date;
	printf("/* Moveable feasts: */\n");

	d.tm_mday -= (9 * 7);		/* 9 weeks before Easter */
	t = mktime(&d);
	display_date("Septuagesima", &d);

	d.tm_mday += 7;			/* 8 weeks before Easter */
	t = mktime(&d);
	display_date("Sexagesima", &d);

	d.tm_mday += 7;			/* 7 weeks before Easter */
	t = mktime(&d);
	display_date("Quinquagesima", &d);

	d = *date;
	d.tm_mday -= 47;		/* Shrove Tuesday */
	t = mktime(&d);
	display_date("Shrove Tuesday", &d);

	d = *date;
	d.tm_mday -= 46;		/* Ash Wednesday */
	t = mktime(&d);
	display_date("Ash Wednesday", &d);

	d = *date;
	d.tm_mday -= (6 * 7);		/* 6 weeks before Easter */
	t = mktime(&d);
	display_date("Quadragesima (1st Sunday of Lent)", &d);

	/* Ember days -
	   Wednesday, Friday and Saturday after 1st Sunday of Lent */
	d.tm_mday += 3;			/* Wednesday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday += 2;			/* Friday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday++;			/* Saturday */
	t = mktime(&d);
	display_date("Ember day", &d);

	d = *date;
	d.tm_mday -= 7;			/* 1 week before Easter */
	t = mktime(&d);
	display_date("Palm Sunday", &d);
	d.tm_mday += 4;			/* Maundy Thursday */
	t = mktime(&d);
	display_date("Maundy Thursday", &d);
	d.tm_mday++;			/* Good Friday */
	t = mktime(&d);
	display_date("Good Friday", &d);

	easter_sunday(date);

	d = *date;
	d.tm_mday++;			/* Easter Monday */
	t = mktime(&d);
	display_date("Easter Monday", &d);

	d = *date;
	d.tm_mday += (5 * 7);			/* Rogation Sunday */
	t = mktime(&d);
	display_date("Rogation Sunday", &d);

	/* Monday to Wednesday before Ascension are Rogation days */
	d = *date;
	d.tm_mday += 36;		/* Monday */
	t = mktime(&d);
	display_date("Rogation Day", &d);
	d.tm_mday++;			/* Tuesday */
	t = mktime(&d);
	display_date("Rogation Day", &d);
	d.tm_mday++;			/* Wednesday */
	t = mktime(&d);
	display_date("Rogation Day", &d);
	d.tm_mday++;			/* Ascension Day */
	t = mktime(&d);
	display_date("Ascension Day", &d);

	d = *date;
	d.tm_mday += (7 * 7);		/* Pentecost */
	t = mktime(&d);
	display_date("Pentecost", &d);

	/* Ember days -
	   Wednesday, Friday and Saturday after Pentecost */
	d.tm_mday += 2;			/* Wednesday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday += 2;			/* Friday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday++;			/* Saturday */
	t = mktime(&d);
	display_date("Ember day", &d);

	d = *date;
	d.tm_mday += (8 * 7);		/* Trinity Sunday */
	t = mktime(&d);
	display_date("Trinity Sunday", &d);
	d.tm_mday += 4;			/* Corpus Christi */
	t = mktime(&d);
	display_date("Corpus Christi", &d);

	/* Ember days -
	   Wednesday, Friday and Saturday after 14th September */
	d.tm_mday = 14;
	d.tm_mon = 8;			/* 14th September */
	t = mktime(&d);
	d.tm_mday += 3 - d.tm_wday + (d.tm_wday > 2 ? 7 : 0);
					/* Wednesday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday += 2;			/* Friday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday++;			/* Saturday */
	t = mktime(&d);
	display_date("Ember day", &d);

	d.tm_mday = 30;
	d.tm_mon = 10;			/* St Andrew's day */
	t = mktime(&d);
	j = ((d.tm_wday > 3) ?
		d.tm_mday + 7 - d.tm_wday :
		d.tm_mday - d.tm_wday);	/* nearest Sunday is Advent Sunday */
	d.tm_mday = j;
	t = mktime(&d);
	display_date("Advent Sunday (First Sunday of Advent)", &d);

	/* Ember days -
	   Wednesday, Friday and Saturday after 13th December */
	d.tm_mday = 13;
	d.tm_mon = 11;			/* 13th December */
	t = mktime(&d);
	d.tm_mday += 3 - d.tm_wday + (d.tm_wday > 2 ? 7 : 0);
					/* Wednesday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday += 2;			/* Friday */
	t = mktime(&d);
	display_date("Ember day", &d);
	d.tm_mday++;			/* Saturday */
	t = mktime(&d);
	display_date("Ember day", &d);

}