Serhiy Storchaka added the comment:

Thank you Jim for your patch. That all right and the patch fixes the issue. But 
the code looks a little tricky, and it would be more robust to use other 
signal value for julian (e.g. None).

However I'm not sure that strptime should accept weekdays before the start of 
year. In C the strptime function doesn't return valid date for such input.

$ ./strptimetest "0 2015 1" "%W %Y %w"
2015-00--2 00:00:00
$ ./strptimetest "0 2015 2" "%W %Y %w"
2015-00--1 00:00:00
$ ./strptimetest "0 2015 3" "%W %Y %w"
2015-00-00 00:00:00
$ ./strptimetest "0 2015 4" "%W %Y %w"
2015-01-01 00:00:00
$ ./strptimetest "0 2015 5" "%W %Y %w"
2015-01-02 00:00:00
$ ./strptimetest "0 2015 6" "%W %Y %w"
2015-01-03 00:00:00
$ ./strptimetest "0 2015 7" "%W %Y %w"
2015-01-00 00:00:00

May be Python strptime should raise an exception for weekdays before the start 
of year as well as it raise an exception for weekdays out of range 0-6.

I'm not sure that any patch should be applied to maintained releases.

----------
keywords: +patch
Added file: http://bugs.python.org/file37573/strptimetest.c
Added file: http://bugs.python.org/file37574/strptime_julian_none.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23136>
_______________________________________
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int
main(int argc, char *argv[])
{
    struct tm tm;
    char buf[255];

    memset(&tm, 0, sizeof(struct tm));
    if (argc != 3) {
        fprintf(stderr, "Usage:\n  %s date format", argv[0]);
        exit(1);
    }
    strptime(argv[1], argv[2], &tm);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
    puts(buf);
    return 0;
}
diff -r ac176a69d188 Lib/_strptime.py
--- a/Lib/_strptime.py  Sun Dec 28 22:14:23 2014 -0600
+++ b/Lib/_strptime.py  Wed Dec 31 18:30:51 2014 +0200
@@ -348,9 +348,9 @@ def _strptime(data_string, format="%a %b
     # though
     week_of_year = -1
     week_of_year_start = -1
-    # weekday and julian defaulted to -1 so as to signal need to calculate
+    # weekday and julian defaulted to None so as to signal need to calculate
     # values
-    weekday = julian = -1
+    weekday = julian = None
     found_dict = found.groupdict()
     for group_key in found_dict.keys():
         # Directives not explicitly handled below:
@@ -452,14 +452,14 @@ def _strptime(data_string, format="%a %b
         year = 1900
     # If we know the week of the year and what day of that week, we can figure
     # out the Julian day of the year.
-    if julian == -1 and week_of_year != -1 and weekday != -1:
+    if julian is None and week_of_year != -1 and weekday is not None:
         week_starts_Mon = True if week_of_year_start == 0 else False
         julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
                                             week_starts_Mon)
     # Cannot pre-calculate datetime_date() since can change in Julian
     # calculation and thus could have different value for the day of the week
     # calculation.
-    if julian == -1:
+    if julian is None:
         # Need to add 1 to result since first day of the year is 1, not 0.
         julian = datetime_date(year, month, day).toordinal() - \
                   datetime_date(year, 1, 1).toordinal() + 1
@@ -469,7 +469,7 @@ def _strptime(data_string, format="%a %b
         year = datetime_result.year
         month = datetime_result.month
         day = datetime_result.day
-    if weekday == -1:
+    if weekday is None:
         weekday = datetime_date(year, month, day).weekday()
     # Add timezone info
     tzname = found_dict.get("Z")
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to