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
#2
have you tried to ask on stackoverlfow? I do not have any ideas how to help you
Reply
#3
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)
Reply
#4
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')
Output:
Success
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 timezones are handled correctly,and are based on the underlying tzinfo implementation.
For example, all comparisons are done in UTC or in the timezone of the datetime being used.
AlphaInc likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a chess endgame problem, almost solved but not quite max22 0 166 Mar-31-2024, 06:14 PM
Last Post: max22
  A new endgame problem, almost solved but not quite 2 max22 0 241 Mar-07-2024, 07:12 PM
Last Post: max22
  2-dataframe, datetime lookup problem Mark17 0 1,241 Jan-27-2022, 01:02 AM
Last Post: Mark17
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,628 Sep-18-2019, 08:32 AM
Last Post: buran
  Problem with datetime module PierreSoulier 3 9,323 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