Another look at Easter Dates (Python)

vegaseat 2 Tallied Votes 1K Views Share

Applying the famous Gauss algorithm to determine the date of Easter for a given year.

''' EasterDate_Gauss.py
An implementation of Gauss's Algorithm for determining the date of
Easter for the Western church. Works for any date in the Gregorian
calendar (1583 and onward).  Returns a date object.
converted from ddanbe's nice C# code snippet on DaniWeb
tested with Python27 and Python33  by  vegaseat  19sep2013
'''

from datetime import date

def calc_easter(year):
    '''
    Gauss algorithm to calculate the date of easter in a given year
    note // forces integer division in Python3
    returns a date object
    '''
    month = 3
    # determine the Golden number
    golden = (year % 19) + 1
    # determine the century number
    century = year // 100 + 1
    # correct for the years who are not leap years
    xx = (3 * century) // 4 - 12
    # moon correction
    yy = (8 * century + 5) // 25 - 5
    # find Sunday
    zz = (5 * year) // 4 - xx - 10
    # determine epact
    # age of moon on January 1st of that year
    # (follows a cycle of 19 years)
    ee = (11 * golden + 20 + yy - xx) % 30
    if ee == 24:
        ee += 1
    if ee == 25 and golden > 11:
        ee += 1
    # get the full moon
    moon = 44 - ee
    if moon < 21:
        moon += 30
    # up to Sunday
    day = (moon + 7) - ((zz + moon) % 7)
    # possibly up a month in easter_date
    if day > 31:
        day -= 31
        month = 4
    return date(year, month, day)

# testing ...
year = 2013
print("Easter for the year {} is on {}".format(year, calc_easter(year)))

''' result ...
Easter for the year 2013 is on 2013-03-31
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.