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