From b08728f54cf96c87aea68f04e5e7700ad2153856 Mon Sep 17 00:00:00 2001 From: Daniel Weschke Date: Tue, 9 Feb 2021 14:06:18 +0100 Subject: [PATCH] add a better method to calculate easter sunday --- src/pylib/date.py | 76 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/src/pylib/date.py b/src/pylib/date.py index 4cf20be..314d956 100644 --- a/src/pylib/date.py +++ b/src/pylib/date.py @@ -10,16 +10,58 @@ .. moduleauthor:: Daniel Weschke """ +import datetime +def computus_anonymous_gregorian(year): + """Computus anonymous Gregorian algorithm or Meeus/Jones/Butcher + algorithm. + + :param year: the year to calculate the Easter Sunday + :type year: int + + :returns: the day of Easter Sunday + :rtype: datetime.date + + For Gregorian calendar (1583 and onward) + Works without exeptions. + + Published + 1876 by Anonymous in Nature + 1877 by Samuel Butcher in The Ecclesiastical Calendar + 1916 by Arthur Downing in The Observatory + 1922 by Harold Spencer Jones in General Astronomy + 1988 by Peter Duffett-Smith in Practical Astronomy with your Calculator + 1991 by Jean Meeus in Astronomical Algorithms + + source: https://de.wikipedia.org/wiki/Spencers_Osterformel + source: https://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm + """ + a = year % 19 + b = year // 100 + c = year % 100 + d = b // 4 + e = b % 4 + f = (b + 8) // 25 + g = (b - f + 1) // 3 + h = (19 * a + b - d - g + 15) % 30 + i = c // 4 + k = c % 4 + l = (32 + 2 * e + 2 * i - h - k) % 7 + m = (a + 11 * h + 22 * l) // 451 + n = (h + l - 7 * m + 114) // 31 + o = (h + l - 7 * m + 114) % 31 + return datetime.date(year, n, o + 1) + +# was not correct for 2020 def gaußsche_osterformel(year): """Gaußsche Osterformel. :param year: the year to calculate the Easter Sunday :type year: int - - :returns: the day of Easter Sunday as a day in march. - :rtype: int - + + :returns: the day of Easter Sunday (computed as a day in march). + :rtype: datetime.date + :ivar X: Das Jahr / year :vartype X: int :ivar K(X): Die Säkularzahl @@ -42,7 +84,7 @@ def gaußsche_osterformel(year): :vartype OE(OG,SZ): int :ivar OS(OG,OE): Das Datum des Ostersonntags als Märzdatum (32. März = 1. April usw.) :vartype OS(OG,OE): int - + Algorithmus gilt für den Gregorianischen Kalender. source: https://de.wikipedia.org/wiki/Gau%C3%9Fsche_Osterformel @@ -58,30 +100,29 @@ def gaußsche_osterformel(year): sz = 7 - (x + x // 4 + s) % 7 oe = 7 - (og - sz) % 7 os = og + oe - return os + + march = datetime.date(year, 3, 1) + day = march + datetime.timedelta(days=os) + return day def easter_sunday(year): """Easter Sunday. :param year: the year to calculate the Easter Sunday :type year: int - + :returns: the day of Easter Sunday :rtype: datetime.date""" - import datetime - march = datetime.date(year, 3, 1) - day = march + datetime.timedelta(days=gaußsche_osterformel(year)) - return day + return computus_anonymous_gregorian(year) def easter_friday(year): """Easter Friday. :param year: the year to calculate the Easter Friday :type year: int - + :returns: the day of Easter Friday :rtype: datetime.date""" - import datetime day = easter_sunday(year) + datetime.timedelta(days=-2) return day @@ -90,10 +131,9 @@ def easter_monday(year): :param year: the year to calculate the Easter Monday :type year: int - + :returns: the day of Easter Monday :rtype: datetime.date""" - import datetime day = easter_sunday(year) + datetime.timedelta(days=+1) return day @@ -102,10 +142,9 @@ def ascension_of_jesus(year): :param year: the year to calculate the ascension of Jesus :type year: int - + :returns: the day of ascension of Jesus :rtype: datetime.date""" - import datetime day = easter_sunday(year) + datetime.timedelta(days=+39) return day @@ -114,9 +153,8 @@ def pentecost(year): :param year: the year to calculate the Pentecost :type year: int - + :returns: the day of Pentecost :rtype: datetime.date""" - import datetime day = easter_sunday(year) + datetime.timedelta(days=+49) return day