Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/finance/easter.py
4049 views
1
"""
2
Copyright (c) 2003-2005 Gustavo Niemeyer <[email protected]>
3
4
This module offers extensions to the standard python 2.3+
5
datetime module.
6
"""
7
__author__ = "Gustavo Niemeyer <[email protected]>"
8
__license__ = "PSF License"
9
10
import datetime
11
from sage.misc.decorators import rename_keyword
12
13
__all__ = ["easter", "EASTER_JULIAN", "EASTER_ORTHODOX", "EASTER_WESTERN"]
14
15
EASTER_JULIAN = 1
16
EASTER_ORTHODOX = 2
17
EASTER_WESTERN = 3
18
19
@rename_keyword(deprecated='Sage version 4.6', method="algorithm")
20
def easter(year, algorithm=EASTER_WESTERN):
21
"""
22
This function was ported from the work done by GM Arts,
23
on top of the algorithm by Claus Tondering, which was
24
based in part on the algorithm of Ouding (1940), as
25
quoted in "Explanatory Supplement to the Astronomical
26
Almanac", P. Kenneth Seidelmann, editor.
27
28
This function implements three different easter
29
calculation algorithms:
30
31
1 - Original calculation in Julian calendar, valid in
32
dates after 326 AD
33
2 - Original algorithm, with date converted to Gregorian
34
calendar, valid in years 1583 to 4099
35
3 - Revised algorithm, in Gregorian calendar, valid in
36
years 1583 to 4099 as well
37
38
These algorithms are represented by the constants:
39
40
EASTER_JULIAN = 1
41
EASTER_ORTHODOX = 2
42
EASTER_WESTERN = 3
43
44
The default algorithm is algorithm 3.
45
46
More about the algorithm may be found at:
47
48
http://users.chariot.net.au/~gmarts/eastalg.htm
49
50
and
51
52
http://www.tondering.dk/claus/calendar.html
53
54
EXAMPLES:
55
sage: import sage.finance.easter
56
sage: sage.finance.easter.easter(2009, 1)
57
datetime.date(2009, 4, 6)
58
sage: sage.finance.easter.easter(2009, 2)
59
datetime.date(2009, 4, 19)
60
sage: sage.finance.easter.easter(2009, 3)
61
datetime.date(2009, 4, 12)
62
sage: sage.finance.easter.easter(2010, 1)
63
datetime.date(2010, 3, 22)
64
sage: sage.finance.easter.easter(2010, 2)
65
datetime.date(2010, 4, 4)
66
sage: sage.finance.easter.easter(2010, 3)
67
datetime.date(2010, 4, 4)
68
69
70
AUTHORS:
71
- Gustavo Niemeyer (author of function)
72
- Phaedon Sinis (adapted code for sage)
73
"""
74
year = int(year)
75
algorithm = int(algorithm)
76
77
if not (1 <= algorithm <= 3):
78
raise ValueError, "invalid algorithm"
79
80
# g - Golden year - 1
81
# c - Century
82
# h - (23 - Epact) mod 30
83
# i - Number of days from March 21 to Paschal Full Moon
84
# j - Weekday for PFM (0=Sunday, etc)
85
# p - Number of days from March 21 to Sunday on or before PFM
86
# (-6 to 28 algorithms 1 & 3, to 56 for algorithm 2)
87
# e - Extra days to add for algorithm 2 (converting Julian
88
# date to Gregorian date)
89
90
y = year
91
g = y % 19
92
e = 0
93
if algorithm < 3:
94
# Old algorithm
95
i = (19*g+15)%30
96
j = (y+y//4+i)%7
97
if algorithm == 2:
98
# Extra dates to convert Julian to Gregorian date
99
e = 10
100
if y > 1600:
101
e = e+y//100-16-(y//100-16)//4
102
else:
103
# New algorithm
104
c = y//100
105
h = (c-c//4-(8*c+13)//25+19*g+15)%30
106
i = h-(h//28)*(1-(h//28)*(29//(h+1))*((21-g)//11))
107
j = (y+y//4+i+2-c+c//4)%7
108
109
# p can be from -6 to 56 corresponding to dates 22 March to 23 May
110
# (later dates apply to algorithm 2, although 23 May never actually occurs)
111
p = i-j+e
112
d = 1+(p+27+(p+6)//40)%31
113
m = 3+(p+26)//30
114
return datetime.date(y,m,d)
115
116
117