ostern.c revision 169343
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 */
2613840Swosch
2787235Smarkm#include <sys/cdefs.h>
2887235Smarkm__FBSDID("$FreeBSD: head/usr.bin/calendar/ostern.c 169343 2007-05-07 11:18:30Z dwmalone $");
2987235Smarkm
3015737Sache#include <stdio.h>
3115737Sache#include <stdlib.h>
3213840Swosch#include <string.h>
3315737Sache#include <time.h>
3413840Swosch
3515720Sache#include "calendar.h"
3615720Sache
3713840Swosch/* return year day for Easter */
3813840Swosch
3970598Sdwmalone/*
4070598Sdwmalone * This code is based on the Calendar FAQ's code for how to calculate
4170598Sdwmalone * easter is. This is the Gregorian calendar version. They refer to
4270598Sdwmalone * the Algorithm of Oudin in the "Explanatory Supplement to the
4370598Sdwmalone * Astronomical Almanac".
4470598Sdwmalone */
4570598Sdwmalone
46169343Sdwmaloneint
47169343Sdwmaloneeaster(int year) /* 0 ... abcd, NOT since 1900 */
4813840Swosch{
4970598Sdwmalone    int G,	/* Golden number - 1 */
5070598Sdwmalone	C,	/* Century */
5170598Sdwmalone	H,	/* 23 - epact % 30 */
5270598Sdwmalone	I,	/* days from 21 March to Paschal full moon */
5370598Sdwmalone	J,	/* weekday of full moon */
5470598Sdwmalone	L;	/* days from 21 March to Sunday on of before full moon */
5513840Swosch
5670598Sdwmalone    G = year % 19;
5770598Sdwmalone    C = year / 100;
5870598Sdwmalone    H = (C - C/4 - (8*C+13)/25 + 19*G + 15) % 30;
5970598Sdwmalone    I = H - (H/28)*(1 - (H/28)*(29/(H + 1))*((21 - G)/11));
6070598Sdwmalone    J = (year + year/4 + I + 2 - C + C/4) % 7;
6113840Swosch
6270598Sdwmalone    L = I - J;
6313840Swosch
6470598Sdwmalone    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
6570598Sdwmalone	return 31 + 29 + 21 + L + 7;
6670598Sdwmalone    else
6770598Sdwmalone	return 31 + 28 + 21 + L + 7;
6813840Swosch}
6913840Swosch
7013840Swosch/* return year day for  Easter or easter depending days
7113840Swosch * Match: Easter([+-][0-9]+)?
7213840Swosch * e.g: Easter-2 is  Good Friday (2 days before Easter)
7313840Swosch */
7413840Swosch
7513840Swoschint
76169343Sdwmalonegeteaster(char *s, int year)
7713840Swosch{
7887235Smarkm	int offset = 0;
7913840Swosch
8013840Swosch#define EASTER "easter"
8113840Swosch#define EASTERNAMELEN (sizeof(EASTER) - 1)
8213840Swosch
8315720Sache	if (strncasecmp(s, EASTER, EASTERNAMELEN) == 0)
8415720Sache	    s += EASTERNAMELEN;
8515720Sache	else if (   neaster.name != NULL
8615720Sache		 && strncasecmp(s, neaster.name, neaster.len) == 0
8715720Sache		)
8815720Sache	    s += neaster.len;
8915720Sache	else
9013840Swosch	    return(0);
9113840Swosch
9213840Swosch#if DEBUG
9313840Swosch	printf("%s %d %d\n", s, year, EASTERNAMELEN);
9413840Swosch#endif
9513840Swosch
9613840Swosch	/* Easter+1  or Easter-2
9713840Swosch	 *       ^            ^   */
9813840Swosch
9915720Sache	switch(*s) {
10013840Swosch
10113840Swosch	case '-':
10213840Swosch	case '+':
10315720Sache	    offset = atoi(s);
10413840Swosch	    break;
10513840Swosch
10613840Swosch	default:
10713840Swosch	    offset = 0;
10813840Swosch	}
10913840Swosch
11013840Swosch	return (easter(year) + offset);
11113840Swosch}
112