/* Calculates the date of Easter Sunday or Good Friday according to the Gregorian Calendar Michael Sanders 2024 https://busybox.neocities.org/notes/is-easter-or-goodfriday.txt Further reading... https://www.algorithm-archive.org/contents/computus/computus.html https://en.wikipedia.org/wiki/Date_of_Easter https://en.wikipedia.org/wiki/Gregorian_calendar */ #include #include int isLeapYear(int year) { return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); } int isEasterOrGoodFriday(const struct tm *now, int checkGoodFriday) { int Y = now->tm_year + 1900; // correcting the year int a = Y % 19; int b = Y / 100; int c = Y % 100; int d = b / 4; int e = b % 4; int f = (b + 8) / 25; int g = (b - f + 1) / 3; int h = (19 * a + b - d - g + 15) % 30; int i = c / 4; int k = c % 4; int L = (32 + 2 * e + 2 * i - h - k) % 7; int m = (a + 11 * h + 22 * L) / 451; int month = (h + L - 7 * m + 114) / 31; int day = ((h + L - 7 * m + 114) % 31) + 1; if (checkGoodFriday) { day -= 2; if (day <= 0) { month -= 1; if (month <= 0) { month = 12; Y -= 1; } day += (month == 2) ? (28 + isLeapYear(Y)) : 31; } } return (now->tm_mday == day && now->tm_mon + 1 == month); } int main() { // set a test date - change as needed struct tm testDate; // good friday 2024 testDate.tm_year = 2024 - 1900; testDate.tm_mon = 2; testDate.tm_mday = 29; /* // easter 2024 testDate.tm_year = 2024 - 1900; testDate.tm_mon = 2; testDate.tm_mday = 31; */ char Date[30] = {0}; strftime(Date, sizeof(Date), "%A, %B %d, %Y", &testDate); printf("%s\n", Date); if (isEasterOrGoodFriday(&testDate, 0)) printf("The date is Easter Sunday.\n"); else if (isEasterOrGoodFriday(&testDate, 1)) printf("The date is Good Friday.\n"); else printf("The date is neither Easter Sunday nor Good Friday.\n"); return 0; } // eof