|
| 1 | +""" |
| 2 | +https://en.wikipedia.org/wiki/Computus#Gauss'_Easter_algorithm |
| 3 | +""" |
| 4 | +import math |
| 5 | +from datetime import datetime, timedelta |
| 6 | + |
| 7 | + |
| 8 | +def gauss_easter(year: int) -> datetime: |
| 9 | + """ |
| 10 | + Calculation Gregorian easter date for given year |
| 11 | +
|
| 12 | + >>> gauss_easter(2007) |
| 13 | + datetime.datetime(2007, 4, 8, 0, 0) |
| 14 | +
|
| 15 | + >>> gauss_easter(2008) |
| 16 | + datetime.datetime(2008, 3, 23, 0, 0) |
| 17 | +
|
| 18 | + >>> gauss_easter(2020) |
| 19 | + datetime.datetime(2020, 4, 12, 0, 0) |
| 20 | +
|
| 21 | + >>> gauss_easter(2021) |
| 22 | + datetime.datetime(2021, 4, 4, 0, 0) |
| 23 | + """ |
| 24 | + metonic_cycle = year % 19 |
| 25 | + julian_leap_year = year % 4 |
| 26 | + non_leap_year = year % 7 |
| 27 | + leap_day_inhibits = math.floor(year / 100) |
| 28 | + lunar_orbit_correction = math.floor((13 + 8 * leap_day_inhibits) / 25) |
| 29 | + leap_day_reinstall_number = leap_day_inhibits / 4 |
| 30 | + secular_moon_shift = ( |
| 31 | + 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number |
| 32 | + ) % 30 |
| 33 | + century_starting_point = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 |
| 34 | + |
| 35 | + # days to be added to March 21 |
| 36 | + days_to_add = (19 * metonic_cycle + secular_moon_shift) % 30 |
| 37 | + |
| 38 | + # PHM -> Paschal Full Moon |
| 39 | + days_from_phm_to_sunday = ( |
| 40 | + 2 * julian_leap_year |
| 41 | + + 4 * non_leap_year |
| 42 | + + 6 * days_to_add |
| 43 | + + century_starting_point |
| 44 | + ) % 7 |
| 45 | + |
| 46 | + if days_to_add == 29 and days_from_phm_to_sunday == 6: |
| 47 | + return datetime(year, 4, 19) |
| 48 | + elif days_to_add == 28 and days_from_phm_to_sunday == 6: |
| 49 | + return datetime(year, 4, 18) |
| 50 | + else: |
| 51 | + return datetime(year, 3, 22) + timedelta( |
| 52 | + days=int(days_to_add + days_from_phm_to_sunday) |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + for year in (1994, 2000, 2010, 2021, 2023): |
| 58 | + tense = "will be" if year > datetime.now().year else "was" |
| 59 | + print(f"Easter in {year} {tense} {gauss_easter(year)}") |
0 commit comments