[issue29081] time.strptime() return wrong result

Jeff Knupp report at bugs.python.org
Sun Jan 8 00:15:20 EST 2017


Jeff Knupp added the comment:

I believe this is working as intended. Remember, the '%w' directive instructs strptime to consider 0 to be Sunday, while tm_wday considers 0 Monday. In 2016, the %W directive means that the first week (week #1) starts on Monday, January 4th. If you go 52 weeks forward from the 4th, you get to Monday, December 26th. By asking for day 0 (%w=0), you want the *Sunday* of the 52nd week *from the first Monday of the year*. Since Monday is day 0 of that week, you want the Sunday that is 6 days from the 26th, or Jan 1, 2017.

One can certainly argue that tm_yday is documented to return an integer between [0,366] and thus we should never see 367, but it's the correct value given your input. The only other choice would be to raise an exception, which definitely shouldn't happen since the values you entered clearly match the format string spec.

Perhaps the docs should be updated, but when you consider that %W goes from [0,53], tm_yday can go well past 366 and still represent a semantically valid value.

----------
nosy: +jeffknupp

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29081>
_______________________________________


More information about the Python-bugs-list mailing list