-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Description
Bug report
Bug description:
I have this function to deal with a gui button "get dates for last week".
I programmed the function in 2024, using datetime objects and the isocalendar() method.
The button worked like a charm, returning the dates for Monday (first date of the week) and Sunday (last date of the week)
In 2025 my program returns the wrong dates.
Running this code (stripdown of the actual code)
from datetime import datetime
str_today_iso = "2025-07-25"
dt_today_1st = datetime.strptime(str_today_iso, "%Y-%m-%d")
str_today_in_week_format = str(dt_today_1st.isocalendar().year) + " " + str(dt_today_1st.isocalendar().week) + " " + str(dt_today_1st.isocalendar().weekday)
dt_today_2nd = datetime.strptime(str_today_in_week_format, "%Y %W %w")
str_today_final = dt_today_2nd.strftime("%Y-%m-%d")
str_monday_this_week = str(dt_today_1st.isocalendar().year) + " " + str(dt_today_1st.isocalendar().week) + " 1"
print(f"today in iso-format: {str_today_iso}")
print(f"today in week-format: {str_today_in_week_format}")
print(f"today after conversion: {str_today_final}")
Returns this output
today in iso-format: 2025-07-25
today in week-format: 2025 30 5
today after conversion: 2025-08-01
Double-checking with kalenderwoche.de the 25. July 2025 is Week 30. With a Friday its day 5. Converting "2025 30 5" yet returns the Aug 1 2025
Exchanging the year (still using the 25th July):
- 2023 -> the result looks fine.
- 2024 -> the result looks fine.
- 2025 -> false result
- 2026 -> false result
- 2027 -> error - as the weekday returns a 7, which is invalid for weekday as the highest number is 6 (0 to 6)
- 2028 -> the result looks fine.
According to kalenderwoche.de and isoweeks.com Jan 01 2025 is in Week 1.
Hence, the week 30 in 2025 starts on SUN July 20th 2025
According to isocalendar() Jan 01 2025 is in Week 0.
Hence, the week 30 in 2025 starts on SUN July 27th 2025
Shouldn't that be standardized?
I mean it is called ISO-calendar.
Does anybody have the same problem?
Do I have a logical error?
Using Python 3.13.1 - This is such an obvious bug, that I doubt myself if I made an error.
CPython versions tested on:
3.13
Operating systems tested on:
Windows