Python Forum
Printing Easter date occurrences
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing Easter date occurrences
#1
Hi all.
The following code computes the Easter dates in a given interval. My goal is to print out only the ones with a given string, i.e. 04-21, like the actual year Easter date. It should be found (1957,2019,2030,2041).
Thanks in advance.

# ---------- easter.py ---------------
from datetime import date

def calc_easter(self, year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)

start_interval= 1950
end_interval= 2050

r = range(start_interval, end_interval+1)
for n in r:
	print (calc_easter(date, n))
# ------------------------------
Output:
1950-04-09 1951-03-25 1952-04-13 1953-04-05 1954-04-18 1955-04-10 1956-04-01 1957-04-21 1958-04-06 1959-03-29 1960-04-17 1961-04-02 1962-04-22 1963-04-14 1964-03-29 1965-04-18 1966-04-10 1967-03-26 1968-04-14 1969-04-06 1970-03-29 1971-04-11 1972-04-02 1973-04-22 1974-04-14 1975-03-30 1976-04-18 1977-04-10 1978-03-26 1979-04-15 1980-04-06 1981-04-19 1982-04-11 1983-04-03 1984-04-22 1985-04-07 1986-03-30 1987-04-19 1988-04-03 1989-03-26 1990-04-15 1991-03-31 1992-04-19 1993-04-11 1994-04-03 1995-04-16 1996-04-07 1997-03-30 1998-04-12 1999-04-04 2000-04-23 2001-04-15 2002-03-31 2003-04-20 2004-04-11 2005-03-27 2006-04-16 2007-04-08 2008-03-23 2009-04-12 2010-04-04 2011-04-24 2012-04-08 2013-03-31 2014-04-20 2015-04-05 2016-03-27 2017-04-16 2018-04-01 2019-04-21 2020-04-12 2021-04-04 2022-04-17 2023-04-09 2024-03-31 2025-04-20 2026-04-05 2027-03-28 2028-04-16 2029-04-01 2030-04-21 2031-04-13 2032-03-28 2033-04-17 2034-04-09 2035-03-25 2036-04-13 2037-04-05 2038-04-25 2039-04-10 2040-04-01 2041-04-21 2042-04-06 2043-03-29 2044-04-17 2045-04-09 2046-03-25 2047-04-14 2048-04-05 2049-04-18 2050-04-10
Reply
#2
Failed tentative, as follows:
# ---------- ea.py ---------------
from datetime import date

def calc_easter(self, year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)

start_interval= 1950
end_interval= 2050

r = range(start_interval, end_interval+1)
for n in r:
	s=calc_easter(date, n)
	c=s[5:]
#	if c == '04-21'
#		print (c)
# ------------------------------
Arising error
Error:
C:\Training>python ea.py Traceback (most recent call last): File "ea.py", line 22, in <module> c=s[5:] TypeError: 'datetime.date' object is not subscriptable
Help required. Thx
Reply
#3
Easter is a lunar holiday, i.e. the first Sunday, after the first full moon, after the Spring Equinox. I do not see you calculating any of these, although it is next to impossible to tell what variable names a, b, c, etc. are supposed to contain.
Reply
#4
(Mar-05-2019, 05:38 PM)woooee Wrote: Easter is a lunar holiday, i.e. the first Sunday, after the first full moon, after the Spring Equinox. I do not see you calculating any of these, although it is next to impossible to tell what variable names a, b, c, etc. are supposed to contain.
You are fully wrong, such an algorithm was invented by Friedrick Gauss.
Reply
#5
My last trial! Please, may I have help? Thank you
# ---------- ea2.py ---------------
# https://docs.python.org/2/library/datetime.html
from datetime import date

def calc_easter(self, year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)

start_interval= 1950
end_interval= 1960

r = range(start_interval, end_interval+1)
for n in r:
	s=calc_easter(date, n)
	print(date.month)  # my intention is to extract the month number (3 or 4),
	                   # same story for the day
                       # and then if condition will solve the matter
# -------- OUTPUT ----------
#     Where is my fault?
# -------------------------- 
#C:\Training>python ea2.py
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
#<attribute 'month' of 'datetime.date' objects>
# ---------------------------------------------
Reply
#6
1. you don't need self in calc_easter(). It's not a class attribute. Thus you don't need to pass date as first argument when call it.
2. when you pass date you pass just generic datetime.date object (i.e. not an instance) and when you print date.month it's just the attribute month of that date object.

from datetime import date
 
def calc_easter(year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)
 
start_interval = 1950
end_interval = 2042
for year in range(start_interval, end_interval):
    easter = calc_easter(year=year)
    if easter.month == 4 and easter.day == 21:
        print(easter.strftime('%Y-%m-%d'))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thank you very much, buran. Problem solved, according to your precious drifts. The core of solution goes around this command easter = calc_easter(year=year) along with the erasing of (self) attribute. Good, now. Final code, better detailed in comments, is below, and best wishes
# ---------- easter_final.py ----------
from datetime import date
  
def calc_easter(year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)
    
# ----- Years interval ---------  
start_interval = 1950
end_interval = 2050
# ----- Wished month/day occurrence ---------
m_target= 4    # 3 or 4
d_target= 21   # 1 to 31
#
for year in range(start_interval, end_interval):
    easter = calc_easter(year=year)
    if easter.month == m_target and easter.day == d_target:
        print(easter.strftime('%Y-%m-%d'))
#
# --------- OUTPUT ------------
# C:\Training>python easter_final.py
# 1957-04-21
# 2019-04-21
# 2030-04-21
# 2041-04-21
# ------------------------------
Reply
#8
I used easter.month and easter.day in order to be as close as possible to your attempt (based on attempted printing date.month). But you can do

if easter == date(year, m_target, d_target):
    print(easter.strftime('%Y-%m-%d'))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Superb, buran! That date(year, m_target, d_target) is what I was looking for at the beginning of my investigations. Thanks again, and cheers

# ---------- pasqua.py ----------
from datetime import date
   
def calc_easter(year):
    "Returns Easter as a date object."
    a = year % 19
    b = year // 100
    c = year % 100
    d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
    e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
    f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
    month = f // 31
    day = f % 31 + 1    
    return date(year, month, day)
     
# ----- Years interval ---------  
start_interval = 1950
end_interval = 2050
# ----- Wished month/day occurrence ---------
m_target= 4    # 3 or 4
d_target= 21   # 1 to 31
#
for year in range(start_interval, end_interval):
	easter = calc_easter(year=year)
	if easter == date(year, m_target, d_target):	
		print(easter.strftime('%Y-%m-%d'))
#
# --------- OUTPUT ------------
# C:\Training>python pasqua.py
# 1957-04-21
# 2019-04-21
# 2030-04-21
# 2041-04-21
# ------------------------------
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 105 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 512 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format and past date check function Turtle 5 4,066 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,190 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Count & Sort occurrences of text in a file oradba4u 7 3,005 Sep-06-2020, 03:23 PM
Last Post: oradba4u
  How to add date and years(integer) to get a date NG0824 4 2,802 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,357 Aug-12-2019, 01:31 PM
Last Post: perfringo
  Occurrences using FOR and IF cycle P86 2 2,472 Jul-29-2019, 04:37 PM
Last Post: ThomasL
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,099 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt
  How to change existing date to current date in a filename? shankar455 1 2,271 Apr-17-2019, 01:53 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020