ostern.c revision 70598
117849Swosch/*
217849Swosch * Copyright (c) 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
317849Swosch * All rights reserved.
413840Swosch *
517849Swosch * Redistribution and use in source and binary forms, with or without
617849Swosch * modification, are permitted provided that the following conditions
717849Swosch * are met:
817849Swosch * 1. Redistributions of source code must retain the above copyright
917849Swosch *    notice, this list of conditions and the following disclaimer.
1017849Swosch * 2. Redistributions in binary form must reproduce the above copyright
1117849Swosch *    notice, this list of conditions and the following disclaimer in the
1217849Swosch *    documentation and/or other materials provided with the distribution.
1317849Swosch *
1417849Swosch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1517849Swosch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617849Swosch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1717849Swosch * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1817849Swosch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1917849Swosch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2017849Swosch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2117849Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2217849Swosch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2317849Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2417849Swosch * SUCH DAMAGE.
2517849Swosch *
2650477Speter * $FreeBSD: head/usr.bin/calendar/ostern.c 70598 2001-01-02 11:35:57Z dwmalone $
2717849Swosch */
2813840Swosch
2915737Sache#include <stdio.h>
3015737Sache#include <stdlib.h>
3113840Swosch#include <string.h>
3215737Sache#include <time.h>
3313840Swosch
3415720Sache#include "calendar.h"
3515720Sache
3613840Swosch/* return year day for Easter */
3713840Swosch
3870598Sdwmalone/*
3970598Sdwmalone * This code is based on the Calendar FAQ's code for how to calculate
4070598Sdwmalone * easter is. This is the Gregorian calendar version. They refer to
4170598Sdwmalone * the Algorithm of Oudin in the "Explanatory Supplement to the
4270598Sdwmalone * Astronomical Almanac".
4370598Sdwmalone */
4470598Sdwmalone
4513840Swoschint easter (year)
4613840Swosch    int year;            /* 0 ... abcd, NOT since 1900 */
4713840Swosch{
4870598Sdwmalone    int G,	/* Golden number - 1 */
4970598Sdwmalone	C,	/* Century */
5070598Sdwmalone	H,	/* 23 - epact % 30 */
5170598Sdwmalone	I,	/* days from 21 March to Paschal full moon */
5270598Sdwmalone	J,	/* weekday of full moon */
5370598Sdwmalone	L;	/* days from 21 March to Sunday on of before full moon */
5413840Swosch
5570598Sdwmalone    G = year % 19;
5670598Sdwmalone    C = year / 100;
5770598Sdwmalone    H = (C - C/4 - (8*C+13)/25 + 19*G + 15) % 30;
5870598Sdwmalone    I = H - (H/28)*(1 - (H/28)*(29/(H + 1))*((21 - G)/11));
5970598Sdwmalone    J = (year + year/4 + I + 2 - C + C/4) % 7;
6013840Swosch
6170598Sdwmalone    L = I - J;
6213840Swosch
6370598Sdwmalone    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
6470598Sdwmalone	return 31 + 29 + 21 + L + 7;
6570598Sdwmalone    else
6670598Sdwmalone	return 31 + 28 + 21 + L + 7;
6713840Swosch}
6813840Swosch
6913840Swosch/* return year day for  Easter or easter depending days
7013840Swosch * Match: Easter([+-][0-9]+)?
7113840Swosch * e.g: Easter-2 is  Good Friday (2 days before Easter)
7213840Swosch */
7313840Swosch
7413840Swoschint
7513840Swoschgeteaster(s, year)
7613840Swosch	char *s;
7713840Swosch        int year;
7813840Swosch{
7913840Swosch	register int offset = 0;
8015720Sache	extern struct fixs neaster;
8113840Swosch
8213840Swosch#define EASTER "easter"
8313840Swosch#define EASTERNAMELEN (sizeof(EASTER) - 1)
8413840Swosch
8515720Sache	if (strncasecmp(s, EASTER, EASTERNAMELEN) == 0)
8615720Sache	    s += EASTERNAMELEN;
8715720Sache	else if (   neaster.name != NULL
8815720Sache		 && strncasecmp(s, neaster.name, neaster.len) == 0
8915720Sache		)
9015720Sache	    s += neaster.len;
9115720Sache	else
9213840Swosch	    return(0);
9313840Swosch
9413840Swosch#if DEBUG
9513840Swosch	printf("%s %d %d\n", s, year, EASTERNAMELEN);
9613840Swosch#endif
9713840Swosch
9813840Swosch	/* Easter+1  or Easter-2
9913840Swosch	 *       ^            ^   */
10013840Swosch
10115720Sache	switch(*s) {
10213840Swosch
10313840Swosch	case '-':
10413840Swosch	case '+':
10515720Sache	    offset = atoi(s);
10613840Swosch	    break;
10713840Swosch
10813840Swosch	default:
10913840Swosch	    offset = 0;
11013840Swosch	}
11113840Swosch
11213840Swosch	return (easter(year) + offset);
11313840Swosch}
114