ostern.c revision 15737
113840Swosch/*
213840Swosch * Copyright (c) 1995 Wolfram Schneider. Public domain.
313840Swosch *
415737Sache * $Id: ostern.c,v 1.3 1996/05/10 17:32:05 ache Exp $
513840Swosch*/
613840Swosch
715737Sache#include <stdio.h>
815737Sache#include <stdlib.h>
913840Swosch#include <string.h>
1015737Sache#include <time.h>
1113840Swosch
1215720Sache#include "calendar.h"
1315720Sache
1413840Swosch/* return year day for Easter */
1513840Swosch
1613840Swoschint easter (year)
1713840Swosch    int year;            /* 0 ... abcd, NOT since 1900 */
1813840Swosch{
1913840Swosch
2013840Swosch    int e_a, e_b, e_c, e_d, e_e,e_f, e_g, e_h, e_i, e_k,
2113840Swosch        e_l, e_m, e_n, e_p, e_q;
2213840Swosch
2313840Swosch    /* silly, but it works */
2413840Swosch    e_a = year % 19;
2513840Swosch    e_b = year / 100;
2613840Swosch    e_c = year % 100;
2713840Swosch
2813840Swosch    e_d = e_b / 4;
2913840Swosch    e_e = e_b % 4;
3013840Swosch    e_f = (e_b + 8) / 25;
3113840Swosch    e_g = (e_b + 1 - e_f) / 3;
3213840Swosch    e_h = ((19 * e_a) + 15 + e_b - (e_d + e_g)) % 30;
3313840Swosch    e_i = e_c / 4;
3413840Swosch    e_k = e_c % 4;
3513840Swosch    e_l = (32 + 2 * e_e + 2 * e_i - (e_h + e_k)) % 7;
3613840Swosch    e_m = (e_a + 11 * e_h + 22 * e_l) / 451;
3713840Swosch    e_n = (e_h + e_l + 114 - (7 * e_m)) / 31;
3813840Swosch    e_p = (e_h + e_l + 114 - (7 * e_m)) % 31;
3913840Swosch    e_p = e_p + 1;
4013840Swosch
4113840Swosch    e_q = 31 + 28;
4213840Swosch
4313840Swosch    if (e_k == 0 && e_c != 0)
4413840Swosch	e_q += 1;
4513840Swosch
4613840Swosch    if (e_n == 4)
4713840Swosch	e_q += 31;
4813840Swosch
4913840Swosch    e_q += e_p;
5013840Swosch
5113840Swosch#if DEBUG
5213840Swosch    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);
5313840Swosch#endif
5413840Swosch
5513840Swosch    return (e_q);
5613840Swosch}
5713840Swosch
5813840Swosch/* return year day for  Easter or easter depending days
5913840Swosch * Match: Easter([+-][0-9]+)?
6013840Swosch * e.g: Easter-2 is  Good Friday (2 days before Easter)
6113840Swosch */
6213840Swosch
6313840Swoschint
6413840Swoschgeteaster(s, year)
6513840Swosch	char *s;
6613840Swosch        int year;
6713840Swosch{
6813840Swosch	register int offset = 0;
6915720Sache	extern struct fixs neaster;
7013840Swosch
7113840Swosch#define EASTER "easter"
7213840Swosch#define EASTERNAMELEN (sizeof(EASTER) - 1)
7313840Swosch
7415720Sache	if (strncasecmp(s, EASTER, EASTERNAMELEN) == 0)
7515720Sache	    s += EASTERNAMELEN;
7615720Sache	else if (   neaster.name != NULL
7715720Sache		 && strncasecmp(s, neaster.name, neaster.len) == 0
7815720Sache		)
7915720Sache	    s += neaster.len;
8015720Sache	else
8113840Swosch	    return(0);
8213840Swosch
8313840Swosch#if DEBUG
8413840Swosch	printf("%s %d %d\n", s, year, EASTERNAMELEN);
8513840Swosch#endif
8613840Swosch
8713840Swosch	/* Easter+1  or Easter-2
8813840Swosch	 *       ^            ^   */
8913840Swosch
9015720Sache	switch(*s) {
9113840Swosch
9213840Swosch	case '-':
9313840Swosch	case '+':
9415720Sache	    offset = atoi(s);
9513840Swosch	    break;
9613840Swosch
9713840Swosch	default:
9813840Swosch	    offset = 0;
9913840Swosch	}
10013840Swosch
10113840Swosch	return (easter(year) + offset);
10213840Swosch}
103