Problem with datetime [SOLVED] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem with datetime [SOLVED] (/thread-37002.html) |
Problem with datetime [SOLVED] - AlphaInc - Apr-22-2022 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' objectDoes anyone know how to help? RE: Problem with datetime - VadimCr - Apr-22-2022 have you tried to ask on stackoverlfow? I do not have any ideas how to help you RE: Problem with datetime - GOTO10 - Apr-22-2022 I think this stems from the module "datetime" having the same name as the class "datetime", which is confusing. Try changing your line 34 to this: date1 = calc_easter(datetime.datetime.now().year) RE: Problem with datetime - snippsat - Apr-22-2022 Your import can be simplified to one line. from datetime import datetime, date #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 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') A couple test of other stuff.>>> date1 datetime.date(2022, 4, 17) >>> date2 datetime.date(2022, 4, 22) >>> delta 5 Look into Pendulum - Python datetimes made easy Is build on datetime(it's also drop-in replacement) and it make stuff easier. Also this is reason why to use it. pendulum doc Wrote:Special care has been taken to ensure |