Python Forum
Problem with datetime [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with datetime [SOLVED]
#1
hello everbody,

I'm writing a python3 script which should check daily for the day of easter and if the difference is 5 days it should trigger another script (via an os-command). So far I tested the script with an static year (I used 2022) but now I want to be more dynamic by adding a datetime.now().year but this breaks the script:

!/usr/bin/env python3

#Imports
from datetime import datetime
from datetime import date
import datetime
import sys

#Calculation of easter date
def calc_easter(year):
    """returns the date of Easter Sunday of the given yyyy year"""
    y = int(year)
    # golden year - 1
    g = y % 19
    # offset
    e = 0
    # century
    c = y/100
    # h is (23 - Epact) mod 30
    h = (c-c/4-(8*c+13)/25+19*g+15)%30
    # number of days from March 21 to Paschal Full Moon
    i = h-(h/28)*(1-(h/28)*(29/(h+1))*((21-g)/11))
    # weekday for Paschal Full Moon (0=Sunday)
    j = (y+y/4+i+2-c+c/4)%7
    # number of days from March 21 to Sunday on or before Paschal Full Moon
    # p can be from -6 to 28
    p = i-j+e
    d = int(1+(p+27+(p+6)/40)%31)
    m = int(3+(p+26)/30)
    return datetime.date(y,m,d)

#Calculation of difference
if __name__ == '__main__':
    date1 = calc_easter(datetime.now().year)
    date2 = date.today()

    def numOfDays(date1, date2):
        return (date2-date1).days

    delta = int(numOfDays(date1, date2))

#Send mails
if delta == 5:
    print ('Success')

elif delta == 10:
    print ('No Success')

else:
    print ('Not in range')

#End
sys.exit()
Without any commenting on the Imports I get this error message:

Traceback (most recent call last):
  File "/home/pi/system/easter/beaster.py", line 34, in <module>
    date1 = calc_easter(datetime.now().year)
AttributeError: module 'datetime' has no attribute 'now'
But when I uncomment Import datetime I get this error message:

Traceback (most recent call last):
  File "/home/pi/system/easter/beaster.py", line 34, in <module>
    date1 = calc_easter(datetime.now().year)
  File "/home/pi/system/easter/beaster.py", line 30, in calc_easter
    return datetime.date(y,m,d)
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object
Does anyone know how to help?
Reply


Messages In This Thread
Problem with datetime [SOLVED] - by AlphaInc - Apr-22-2022, 09:17 AM
RE: Problem with datetime - by VadimCr - Apr-22-2022, 12:24 PM
RE: Problem with datetime - by GOTO10 - Apr-22-2022, 12:47 PM
RE: Problem with datetime - by snippsat - Apr-22-2022, 01:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  a chess endgame problem, almost solved but not quite max22 0 189 Mar-31-2024, 06:14 PM
Last Post: max22
  A new endgame problem, almost solved but not quite 2 max22 0 265 Mar-07-2024, 07:12 PM
Last Post: max22
  2-dataframe, datetime lookup problem Mark17 0 1,258 Jan-27-2022, 01:02 AM
Last Post: Mark17
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,670 Sep-18-2019, 08:32 AM
Last Post: buran
  Problem with datetime module PierreSoulier 3 9,363 Jul-24-2018, 11:03 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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