This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: strptime incorrect for weekday '0' when using week number format
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Steve.J.Borba, r.david.murray
Priority: normal Keywords:

Created on 2013-11-25 19:23 by Steve.J.Borba, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg204382 - (view) Author: Steve J Borba (Steve.J.Borba) Date: 2013-11-25 19:23
OS: Windows 7 Professional (64-bit)
Hardware: Intel

datetime.strptime returns an incorrect value when calculating a date using a week number format, such as "%Y-%W-%w" (Year-Week-Weekday). The value returned for weekday '0' of a given week is consistently 7 days greater than it should be. The following code illustrates:

from datetime import datetime
for i in range(0,53):
    if i == 0:
        yr=input("Enter a valid year: ")
        print("Wk#\tBeginning of week\tEnd of week")
    BegWk = datetime.strptime((yr + "-" + str(i) + "-0"),"%Y-%W-%w")
    EndWk = datetime.strptime((yr + "-" + str(i) + "-6"),"%Y-%W-%w")
    print(str(i) + "\t" + str(BegWk) + "\t" +str(EndWk))

Here is a clip (7 lines) of the output from the code above:
Enter a valid year: 2013
Wk#	Beginning of week	End of week
0	2013-01-06 00:00:00	2013-01-05 00:00:00
1	2013-01-13 00:00:00	2013-01-12 00:00:00
2	2013-01-20 00:00:00	2013-01-19 00:00:00
3	2013-01-27 00:00:00	2013-01-26 00:00:00
4	2013-02-03 00:00:00	2013-02-02 00:00:00
5	2013-02-10 00:00:00	2013-02-09 00:00:00
6	2013-02-17 00:00:00	2013-02-16 00:00:00

The value returned for the first column of each week is exactly 7 days higher than the correct result.
msg204404 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-11-25 20:45
from the man page for strptime:

       %w     The weekday number (0-6) with Sunday = 0.

       %W     The week number with Monday the first day of  the  week  (0-53).
              The first Monday of January is the first day of week 1.

Python's documentation is just a tiny bit clearer about %W:

       %W     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.

So, the result is correct, albeit very unintuitive (the week day numbers for the purposes of %W are the sequence 1-2-3-4-5-6-0).  You will note that if you call strftime with the same format string, you will get your input strings out as the output.

Call this a design bug in posix that python has inherited.  You will get the exact same behavior if you write a C program that calls strptime/strftime.
History
Date User Action Args
2022-04-11 14:57:54adminsetgithub: 63973
2013-11-25 20:45:53r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg204404

resolution: not a bug
stage: resolved
2013-11-25 19:23:05Steve.J.Borbacreate