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