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