From 18159a86f847648e5f12d989041c69bb5ade4bb2 Mon Sep 17 00:00:00 2001 From: Aminda Suomalainen Date: Tue, 18 Mar 2025 11:04:08 +0200 Subject: [PATCH] weeknumbers.py: initial improvements --- python/weeknumbers.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/python/weeknumbers.py b/python/weeknumbers.py index c1c2610..79a6743 100755 --- a/python/weeknumbers.py +++ b/python/weeknumbers.py @@ -1,22 +1,42 @@ +#!/usr/bin/env python3 +""" +A family member wanted to know what dates are on weeks 26 and 31 and instead of +the answer received this script. I thought I would try to improve it for fun, +since it doesn't pass pylint or anything. +""" import datetime # Current year -current_year = 2025 +# TODO: ask the user or if no answer, assume the actual current year? +CURRENT_YEAR = 2025 + +# TODO: ask for the week # Calculate dates for weeks 26-31 def get_dates_for_week(week_number, year): + """ + This function gets the dates for the week in question. + """ # Find the first day of the year first_day_of_year = datetime.date(year, 1, 1) # Find the first day of the desired week (ISO format week numbering) - start_of_week = first_day_of_year + datetime.timedelta(weeks=week_number - 1) + start_of_week = first_day_of_year + datetime.timedelta( + weeks=week_number - 1 + ) # Return the start and end date of the week - start_of_week = start_of_week - datetime.timedelta(days=start_of_week.weekday()) # Adjust to Monday + start_of_week = start_of_week - datetime.timedelta( + days=start_of_week.weekday() + ) # Adjust to Monday end_of_week = start_of_week + datetime.timedelta(days=6) # Sunday return (start_of_week, end_of_week) + +# TODO: ansewr in a more human format # Get the dates for weeks 26-31 -weeks = {week: get_dates_for_week(week, current_year) for week in range(26, 32)} +weeks = { + week: get_dates_for_week(week, CURRENT_YEAR) for week in range(26, 32) +} print(weeks)