Advent Sundays calculation in Python

Script to calculate Advent dates:

from datetime import datetime, timedelta
# Create service
@service
def update_advent_dates():
  # Find this year's Christmas day and its day of week
  ThisChristmasDay = datetime.strptime(str(datetime.today().year) + "-12-24", "%Y-%m-%d")
  ChristmasWeekDay = int(ThisChristmasDay.isoweekday())
  # Reset if also Sunday
  if (ChristmasWeekDay == 7): ChristmasWeekDay = 0
  # Calculate dates of Sundays and pass data to HAss
  AdventSunLast = ThisChristmasDay - timedelta(days = ChristmasWeekDay)
  variable.advent_sunday_4 = AdventSunLast
  variable.advent_sunday_3 = AdventSunLast - timedelta(days = 7)
  variable.advent_sunday_2 = AdventSunLast - timedelta(days = 14)
  variable.advent_sunday_1 = AdventSunLast - timedelta(days = 21)

Is there a particular reason why the variable ThisChristmasDay is set to the 24th of December and not 25th?

It’s the European version.

:grin: Love it!

Because in central Europe we have that day :slight_smile: I must say that is is a bit challenging to make ready the tree and gifts for the evening when kids are around. Definitely better is to have it next day in the morning … But … :smiley:

1 Like

this is a more pythonic way to do this specific line instead of parsing a generated string:

ThisChristmasDay = datetime(datetime.now().year, 12, 24)

1 Like