paskha.c revision 181322
115714Sache/*
215714Sache * Copyright (C) 1993-1996 by Andrey A. Chernov, Moscow, Russia.
315714Sache * All rights reserved.
415714Sache *
515714Sache * Redistribution and use in source and binary forms, with or without
615714Sache * modification, are permitted provided that the following conditions
715714Sache * are met:
815714Sache * 1. Redistributions of source code must retain the above copyright
915714Sache *    notice, this list of conditions and the following disclaimer.
1015714Sache * 2. Redistributions in binary form must reproduce the above copyright
1115714Sache *    notice, this list of conditions and the following disclaimer in the
1215714Sache *    documentation and/or other materials provided with the distribution.
1315714Sache *
1415714Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
1515714Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1615714Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1715714Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1815714Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1915714Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2015714Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2115714Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2215714Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2315714Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2415714Sache * SUCH DAMAGE.
2515714Sache */
2615714Sache
2787235Smarkm#include <sys/cdefs.h>
2887235Smarkm__FBSDID("$FreeBSD: head/usr.bin/calendar/paskha.c 181322 2008-08-05 08:11:54Z edwin $");
2987235Smarkm
3015737Sache#include <stdio.h>
3115714Sache#include <stdlib.h>
3215714Sache#include <string.h>
3315737Sache#include <time.h>
3415714Sache
3515720Sache#include "calendar.h"
3615720Sache
37181322Sedwin#define	PASKHA		"paskha"
38181322Sedwin#define	PASKHALEN	(sizeof(PASKHA) - 1)
3915714Sache
40181322Sedwinstatic int paskha(int);
4187235Smarkm
4215714Sache/* return year day for Orthodox Easter using Gauss formula */
4315714Sache/* (old style result) */
4415714Sache
4515714Sachestatic int
46169343Sdwmalonepaskha(int R) /*year*/
4715714Sache{
4815714Sache	int a, b, c, d, e;
4915714Sache	static int x = 15;
5015714Sache	static int y = 6;
5115714Sache
5215714Sache	a = R % 19;
5315714Sache	b = R % 4;
5415714Sache	c = R % 7;
55181322Sedwin	d = (19 * a + x) % 30;
56181322Sedwin	e = (2 * b + 4 * c + 6 * d + y) % 7;
5715723Sache	return (((cumdays[3] + 1) + 22) + (d + e));
5815714Sache}
5915714Sache
6015714Sache/* return year day for Orthodox Easter depending days */
6115714Sache
6215714Sacheint
63169343Sdwmalonegetpaskha(char *s, int year)
6415714Sache{
6515714Sache	int offset;
6615714Sache
6715714Sache	if (strncasecmp(s, PASKHA, PASKHALEN) == 0)
68181322Sedwin		s += PASKHALEN;
69181322Sedwin	else if (npaskha.name != NULL
70181322Sedwin	    && strncasecmp(s, npaskha.name, npaskha.len) == 0)
71181322Sedwin		s += npaskha.len;
7215714Sache	else
73181322Sedwin		return 0;
7415714Sache
7515714Sache	/* Paskha+1  or Paskha-2
7615714Sache	 *       ^            ^   */
7715714Sache
78181322Sedwin	switch (*s) {
7915714Sache
8015714Sache	case '-':
8115714Sache	case '+':
82181322Sedwin		offset = atoi(s);
83181322Sedwin		break;
8415714Sache
8515714Sache	default:
86181322Sedwin		offset = 0;
87181322Sedwin		break;
8815714Sache	}
89181322Sedwin
90181322Sedwin	return (paskha(year) + offset + 13 /* new style */);
9115714Sache}
92