So, here's a funny little programming challenge. I was writing a quick method to determine all the market holidays for a particular year, and then I started reading about Easter and discovered just how crazy* the logic is for determining its date--the first Sunday after the Paschal Full Moon following the spring equinox! Does anybody know of an existing function to calculate the date of Easter for a given year?
Granted, it's probably not all that hard to do; I just figured I'd ask in case somebody's already done this. (And that seems very likely.)
UPDATE: Actually, I'm really looking for the date of Good Friday (the Friday before Easter)... I just figured Easter would get me there. And since I'm in the U.S., I assume I'm looking for the Catholic Easter? But perhaps someone can correct me on that if I'm wrong.
*By "crazy" I meant, like, involved. Not anything offensive...
Easter falls on the first Sunday after the Full Moon date, based on mathematical calculations, that falls on or after March 21. If the Full Moon is on a Sunday, Easter is celebrated on the following Sunday.
Calculate the next Full Moon after this (NFM) – in Excel, this is BFM+LML*CEILING((NE-BFM)/LML,1) Calculate the date of Easter Sunday – in Excel, this is NFM+8-WEEKDAY(NFM,1)
The next day—April 14—is Holy Thursday, the anniversary of the Last Supper. Good Friday, on April 15, is celebrated next, and that commemorates the crucifixion of Jesus Christ. Easter Sunday, on April 17, celebrates the day that Jesus rose from the tomb and marks the end of Lent.
Good Friday: To calculate the date of Good Friday, subtract 2 from the Easter Date. Easter Monday: To calculate the date of Easter Monday, add 1 to the Easter Date.
Python: using dateutil's easter()
function.
>>> from dateutil.easter import * >>> print easter(2010) 2010-04-04 >>> print easter(2011) 2011-04-24
The functions gets, as an argument, the type of calculation you like:
EASTER_JULIAN = 1 EASTER_ORTHODOX = 2 EASTER_WESTERN = 3
You can pick the one relevant to the US.
Reducing two days from the result would give you Good Friday:
>>> from datetime import timedelta >>> d = timedelta(days=-2) >>> easter(2011) datetime.date(2011, 4, 24) >>> easter(2011)+d datetime.date(2011, 4, 22)
Oddly enough, someone was iterating this, and published the results in Wikipedia's article about the algorithm:
in SQL Server Easter Sunday would look like this, scroll down for Good Friday
CREATE FUNCTION dbo.GetEasterSunday ( @Y INT ) RETURNS SMALLDATETIME AS BEGIN DECLARE @EpactCalc INT, @PaschalDaysCalc INT, @NumOfDaysToSunday INT, @EasterMonth INT, @EasterDay INT SET @EpactCalc = (24 + 19 * (@Y % 19)) % 30 SET @PaschalDaysCalc = @EpactCalc - (@EpactCalc / 28) SET @NumOfDaysToSunday = @PaschalDaysCalc - ( (@Y + @Y / 4 + @PaschalDaysCalc - 13) % 7 ) SET @EasterMonth = 3 + (@NumOfDaysToSunday + 40) / 44 SET @EasterDay = @NumOfDaysToSunday + 28 - ( 31 * (@EasterMonth / 4) ) RETURN ( SELECT CONVERT ( SMALLDATETIME, RTRIM(@Y) + RIGHT('0'+RTRIM(@EasterMonth), 2) + RIGHT('0'+RTRIM(@EasterDay), 2) ) ) END GO
Good Friday is like this and it uses the Easter function above
CREATE FUNCTION dbo.GetGoodFriday ( @Y INT ) RETURNS SMALLDATETIME AS BEGIN RETURN (SELECT dbo.GetEasterSunday(@Y) - 2) END GO
From here: http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With