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