Calculating the date of Easter in Python
I had to calculate the date of Easter for a given year and got me Spencer Jones’ algorithm as republished in Jean Meeus’ excellent book “Astronomical Algorithms (2nd ed.)”.
I also cooked up a Forth version, but I need to refactor it a little.
from datetime import date
def westEaster(x):
"""westEaster(int year): Calculates the date of Easter of West Church Year
Must not be older than 1538, when the Gregorian calendar went into effect.
Returns a date object."""
if not isinstance(x, int):
raise AssertionError("Year must be given as int")
if x < 1583:
raise ValueError("Year can't be older than 1583 (Gregorian)")
# t is a throwaway variable to make divmod happy
t,a = divmod(x, 19)
b,c = divmod(x, 100)
d,e = divmod(b, 4)
f,t = divmod(b+8, 25)
g,t = divmod(b-f+1, 3)
t,h = divmod(19*a+b-d-g+15, 30)
i,k = divmod(c, 4)
t,l = divmod(32+2*e+2*i-h-k, 7)
m,t = divmod(a+11*h+22*l, 451)
# p-1 is the day of easter sunday, n the month
n,p = divmod(h+l-7*m+114, 31)
p = p+1
return date(x, n, p)