ostern.c revision 181322
117849Swosch/*
217849Swosch * Copyright (c) 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
3181322Sedwin * 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 181322 2008-08-05 08:11:54Z edwin $");
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{
49181322Sedwin	int G,	/* Golden number - 1 */
50181322Sedwin	    C,	/* Century */
51181322Sedwin	    H,	/* 23 - epact % 30 */
52181322Sedwin	    I,	/* days from 21 March to Paschal full moon */
53181322Sedwin	    J,	/* weekday of full moon */
54181322Sedwin	    L;	/* days from 21 March to Sunday on of before full moon */
5513840Swosch
56181322Sedwin	G = year % 19;
57181322Sedwin	C = year / 100;
58181322Sedwin	H = (C - C / 4 - (8 * C + 13) / 25 + 19 * G + 15) % 30;
59181322Sedwin	I = H - (H / 28) * (1 - (H / 28) * (29 / (H + 1)) * ((21 - G) / 11));
60181322Sedwin	J = (year + year / 4 + I + 2 - C + C / 4) % 7;
6113840Swosch
62181322Sedwin	L = I - J;
6313840Swosch
64181322Sedwin	if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
65181322Sedwin		return 31 + 29 + 21 + L + 7;
66181322Sedwin	else
67181322Sedwin		return 31 + 28 + 21 + L + 7;
6813840Swosch}
6913840Swosch
70181322Sedwin/* 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
80181322Sedwin#define	EASTER "easter"
81181322Sedwin#define	EASTERNAMELEN (sizeof(EASTER) - 1)
8213840Swosch
8315720Sache	if (strncasecmp(s, EASTER, EASTERNAMELEN) == 0)
84181322Sedwin		s += EASTERNAMELEN;
85181322Sedwin	else if (neaster.name != NULL
86181322Sedwin	    && strncasecmp(s, neaster.name, neaster.len) == 0)
87181322Sedwin		s += neaster.len;
8815720Sache	else
89181322Sedwin		return (0);
9013840Swosch
9113840Swosch#if DEBUG
9213840Swosch	printf("%s %d %d\n", s, year, EASTERNAMELEN);
9313840Swosch#endif
9413840Swosch
9513840Swosch	/* Easter+1  or Easter-2
9613840Swosch	 *       ^            ^   */
9713840Swosch
98181322Sedwin	switch (*s) {
9913840Swosch
10013840Swosch	case '-':
10113840Swosch	case '+':
102181322Sedwin		offset = atoi(s);
103181322Sedwin		break;
10413840Swosch
10513840Swosch	default:
106181322Sedwin		offset = 0;
10713840Swosch	}
108181322Sedwin
10913840Swosch	return (easter(year) + offset);
11013840Swosch}
111