msg284080 - (view) |
Author: (hywl51) |
Date: 2016-12-27 10:05 |
In [1]:import time In [2]: time.strptime('2016 52 0', '%Y %W %w') Out[2]: time.struct_time(tm_year=2017, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=367, tm_isdst=-1) When given the parameters above, the function return the struct_time object with tm_yday=367, that is wrong. I test the codes on Python 2.7.11 and 3.5.1, the error is same. |
|
|
msg284962 - (view) |
Author: Jeff Knupp (jeffknupp) * |
Date: 2017-01-08 05:15 |
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. |
|
|
msg332158 - (view) |
Author: Karthikeyan Singaravelan (xtreak) *  |
Date: 2018-12-19 18:31 |
This causes the round trip to be a ValueError. ./python.exe Python 3.8.0a0 (heads/master:1dd035954b, Dec 18 2018, 10:12:34) [Clang 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> value = '2016 51 0' >>> format = '%Y %W %w' >>> time.strftime(format, time.strptime('2016 51 0', format)) == value True >>> time.strptime('2016 52 0', format) time.struct_time(tm_year=2017, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=367, tm_isdst=-1) >>> time.strftime(format, time.strptime('2016 52 0', format)) == value Traceback (most recent call last): File "", line 1, in ValueError: day of year out of range On Ruby this causes runtime error irb(main):005:0> DateTime::strptime("2016 52 0", "%Y %W %w") Traceback (most recent call last): 3: from /usr/local/bin/irb:11:in `' 2: from (irb):5 1: from (irb):5:in `strptime' ArgumentError (invalid date) With C it returns 2016 00 5 on Mac OS 10.10.4 and 2017 00 0 on Ubuntu #include <stdio.h> #include <time.h> int main() { struct tm ltm = {0}; char buf[] = "2016 52 0"; strptime(buf, "%Y %W %w", <m); time_t ptm = mktime(<m); printf("tm year %d\n", ltm.tm_year); printf("tm yday %d\n", ltm.tm_yday); printf("tm wday %d\n", ltm.tm_wday); char str[50]; struct tm *tm_info = localtime(&ptm); strftime(str, 50, "%Y %W %w", tm_info); printf("%s\n", str); } Output : Mac tm year 116 tm yday 0 tm wday 5 2016 00 5 Ubuntu tm year 117 tm yday 0 tm wday 0 2017 00 0 |
|
|