Author p-ganssle
Recipients belopolsky, eric.smith, p-ganssle, zhanying
Date 2020-04-09.14:40:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I can reproduce this on Linux with Python 3.8.2.

I think this may be a bug, but it may also just be platform-specific weirdness. Either way it's very curious behavior:

>>> datetime.strptime("2023-0-0", "%Y-%W-%w")                         
datetime.datetime(2023, 1, 1, 0, 0)
>>> datetime.strptime("2023-0-1", "%Y-%W-%w")                         
datetime.datetime(2022, 12, 26, 0, 0)

The definition for %W (and %U, which is related) goes like this:

> Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.

2024 starts on a Monday, so there should be no Week 0 in that year at all. Seems to me like it's undefined what happens when you put in a string that puts in an invalid value for "%Y-%W-%w".

Seems to me that we are just passing through the behavior of `time.strptime` in this case (which just calls out to what the platform does):

>>> time.strptime("2024-0-3", "%Y-%W-%w")                             
time.struct_time(tm_year=2024, tm_mon=1, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=3, tm_isdst=-1)

I am open to discussion about trying to rationalize this behavior - it would be a bit tricky but if we moved to our own implementation of the algorithm to calculate %W we could detect this situation and throw an exception. I'd rather see if this is intended behavior in the underlying C implementation first, though. If this is consistent across platforms and not just some random implementation detail, people may be relying on it.

I propose that we:

1. Determine what happens on different platforms (might be easy to just make a PR that asserts the current behavior and see if/how it breaks on any of the supported platforms).
2. Determine why it works the way it does.

After that, at the very least we should document the behavior with a warning or a footnote or something. If we make any changes to the behavior they would be 3.9+, but the documentation changes can be backported.

Thanks for the bug report zhanying! Very interesting!
Date User Action Args
2020-04-09 14:40:49p-gansslesetrecipients: + p-ganssle, belopolsky, eric.smith, zhanying
2020-04-09 14:40:49p-gansslesetmessageid: <>
2020-04-09 14:40:49p-gansslelinkissue40236 messages
2020-04-09 14:40:49p-gansslecreate