xref: /dflybsd-src/usr.bin/calendar/ecclesiastical.c (revision 7db513258e8d5ff7cdf4f590463a8002a2e6230a)
1d19ef5a2SAaron LI /*-
2d19ef5a2SAaron LI  * SPDX-License-Identifier: BSD-3-Clause
3d19ef5a2SAaron LI  *
4d19ef5a2SAaron LI  * Copyright (c) 2020 The DragonFly Project.  All rights reserved.
5d19ef5a2SAaron LI  *
6d19ef5a2SAaron LI  * This code is derived from software contributed to The DragonFly Project
7d19ef5a2SAaron LI  * by Aaron LI <aly@aaronly.me>
8d19ef5a2SAaron LI  *
9d19ef5a2SAaron LI  * Redistribution and use in source and binary forms, with or without
10d19ef5a2SAaron LI  * modification, are permitted provided that the following conditions
11d19ef5a2SAaron LI  * are met:
12d19ef5a2SAaron LI  *
13d19ef5a2SAaron LI  * 1. Redistributions of source code must retain the above copyright
14d19ef5a2SAaron LI  *    notice, this list of conditions and the following disclaimer.
15d19ef5a2SAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
16d19ef5a2SAaron LI  *    notice, this list of conditions and the following disclaimer in
17d19ef5a2SAaron LI  *    the documentation and/or other materials provided with the
18d19ef5a2SAaron LI  *    distribution.
19d19ef5a2SAaron LI  * 3. Neither the name of The DragonFly Project nor the names of its
20d19ef5a2SAaron LI  *    contributors may be used to endorse or promote products derived
21d19ef5a2SAaron LI  *    from this software without specific, prior written permission.
22d19ef5a2SAaron LI  *
23d19ef5a2SAaron LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24d19ef5a2SAaron LI  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25d19ef5a2SAaron LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26d19ef5a2SAaron LI  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27d19ef5a2SAaron LI  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28d19ef5a2SAaron LI  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29d19ef5a2SAaron LI  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30d19ef5a2SAaron LI  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31d19ef5a2SAaron LI  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32d19ef5a2SAaron LI  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33d19ef5a2SAaron LI  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34d19ef5a2SAaron LI  * SUCH DAMAGE.
35d19ef5a2SAaron LI  *
36d19ef5a2SAaron LI  * Reference:
37d19ef5a2SAaron LI  * Calendrical Calculations, The Ultimate Edition (4th Edition)
38d19ef5a2SAaron LI  * by Edward M. Reingold and Nachum Dershowitz
39d19ef5a2SAaron LI  * 2018, Cambridge University Press
40d19ef5a2SAaron LI  */
41d19ef5a2SAaron LI 
42d19ef5a2SAaron LI #include <math.h>
43d19ef5a2SAaron LI #include <stdbool.h>
44d19ef5a2SAaron LI 
45d19ef5a2SAaron LI #include "calendar.h"
46d19ef5a2SAaron LI #include "basics.h"
47d19ef5a2SAaron LI #include "ecclesiastical.h"
48d19ef5a2SAaron LI #include "gregorian.h"
49d19ef5a2SAaron LI #include "julian.h"
50d19ef5a2SAaron LI #include "utils.h"
51d19ef5a2SAaron LI 
52d19ef5a2SAaron LI /*
53d19ef5a2SAaron LI  * Calculate the fixed date (RD) of Orthodox Easter in Gregorian year $g_year.
54d19ef5a2SAaron LI  * Ref: Sec.(9.1), Eq.(9.1)
55d19ef5a2SAaron LI  */
56d19ef5a2SAaron LI int
orthodox_easter(int g_year)57d19ef5a2SAaron LI orthodox_easter(int g_year)
58d19ef5a2SAaron LI {
59d19ef5a2SAaron LI 	/* Age of moon for April 5 */
60d19ef5a2SAaron LI 	int shifted_epact = mod(14 + 11 * mod(g_year, 19), 30);
61d19ef5a2SAaron LI 
62d19ef5a2SAaron LI 	/* Day after full moon on/after March 21 */
63d19ef5a2SAaron LI 	int j_year = (g_year > 0) ? g_year : (g_year - 1);
64d19ef5a2SAaron LI 	struct date april19 = { j_year, 4, 19 };
65d19ef5a2SAaron LI 	int paschal_moon = fixed_from_julian(&april19) - shifted_epact;
66d19ef5a2SAaron LI 
67d19ef5a2SAaron LI 	return kday_after(SUNDAY, paschal_moon);
68d19ef5a2SAaron LI }
69d19ef5a2SAaron LI 
70d19ef5a2SAaron LI /*
71*7db51325SAaron LI  * Calculate the fixed date (RD) of Gregorian Easter (used by Catholic and
72*7db51325SAaron LI  * Protestant churches) in Gregorian year $g_year.
73d19ef5a2SAaron LI  * Ref: Sec.(9.2), Eq.(9.3)
74d19ef5a2SAaron LI  */
75d19ef5a2SAaron LI int
easter(int g_year)76d19ef5a2SAaron LI easter(int g_year)
77d19ef5a2SAaron LI {
78d19ef5a2SAaron LI 	int century = div_floor(g_year, 100) + 1;
79d19ef5a2SAaron LI 	int y_mod19 = mod(g_year, 19);
80d19ef5a2SAaron LI 	int n = (14 + 11 * y_mod19 - (int)floor(century * 0.75) +
81d19ef5a2SAaron LI 		 div_floor(5 + 8 * century, 25));
82d19ef5a2SAaron LI 	int shifted_epact = mod(n, 30);
83d19ef5a2SAaron LI 	if (shifted_epact == 0 || (shifted_epact == 1 && y_mod19 > 10))
84d19ef5a2SAaron LI 		shifted_epact++;
85d19ef5a2SAaron LI 
86d19ef5a2SAaron LI 	struct date april19 = { g_year, 4, 19 };
87d19ef5a2SAaron LI 	int paschal_moon = fixed_from_gregorian(&april19) - shifted_epact;
88d19ef5a2SAaron LI 
89d19ef5a2SAaron LI 	return kday_after(SUNDAY, paschal_moon);
90d19ef5a2SAaron LI }
91d19ef5a2SAaron LI 
92d19ef5a2SAaron LI /*
93d19ef5a2SAaron LI  * Calculate the fixed date (RD) of Advent Sunday (the 4th Sunday
94d19ef5a2SAaron LI  * before Christmas, equivalent to the Sunday closest to November 30)
95d19ef5a2SAaron LI  * in Gregorian year $g_year.
96d19ef5a2SAaron LI  * Ref: Sec.(2.5), Eq.(2.42)
97d19ef5a2SAaron LI  */
98d19ef5a2SAaron LI int
advent(int g_year)99d19ef5a2SAaron LI advent(int g_year)
100d19ef5a2SAaron LI {
101d19ef5a2SAaron LI 	struct date date = { g_year, 11, 30 };
102d19ef5a2SAaron LI 	int rd;
103d19ef5a2SAaron LI 
104d19ef5a2SAaron LI 	if (Calendar->id == CAL_JULIAN)
105d19ef5a2SAaron LI 		rd = fixed_from_julian(&date);
106d19ef5a2SAaron LI 	else
107d19ef5a2SAaron LI 		rd = fixed_from_gregorian(&date);
108d19ef5a2SAaron LI 
109d19ef5a2SAaron LI 	return kday_nearest(SUNDAY, rd);
110d19ef5a2SAaron LI }
111