diff -r 8c1f52a7094c -r dbe4a1ab422e Lib/test/test_strptime_jy.py --- a/Lib/test/test_strptime_jy.py Mon Jan 13 17:40:36 2014 -0800 +++ b/Lib/test/test_strptime_jy.py Tue Jan 14 12:08:08 2014 -0800 @@ -22,6 +22,10 @@ d = strptime('0', '%f') self.assertEqual(1900, d.tm_year) + def test_issue2112(self): + d = strptime('1', '%d') + self.assertEqual(1900, d.tm_year) + def test_main(): test_support.run_unittest( ParsingTests diff -r 8c1f52a7094c -r dbe4a1ab422e src/org/python/modules/time/Time.java --- a/src/org/python/modules/time/Time.java Mon Jan 13 17:40:36 2014 -0800 +++ b/src/org/python/modules/time/Time.java Tue Jan 14 12:08:08 2014 -0800 @@ -694,145 +694,9 @@ } public static PyTuple strptime(String data_string, String format) { - if (format == null || data_string == null) { - // this is not a very interesting error message, but it's the same - // as what CPython outputs - throw Py.TypeError("expected string of buffer"); - } - String jformat = py2java_format(format); - if (jformat == null) { - // Format not translatable to java, fallback to _strptime - return pystrptime(data_string, format); - } - SimpleDateFormat d = null; - try { - d = new SimpleDateFormat(jformat); - } catch (IllegalArgumentException e) { - throwValueError(e.getLocalizedMessage()); - } - Calendar cal = Calendar.getInstance(); - try { - cal.setTime(d.parse(data_string)); - } catch (ParseException e) { - throwValueError("time data did not match format: data=" + data_string + " fmt=" + format); - } - int isdst = -1; - if (jformat.contains("zzz")) { - isdst = cal.getTimeZone().inDaylightTime(cal.getTime()) ? 1 : 0; - } - return toTimeTuple(cal, isdst); + return pystrptime(data_string, format); } private static final String DEFAULT_FORMAT_PY = "%a %b %d %H:%M:%S %Y"; - private static final HashMap py2java = new HashMap() {{ - put('a', "EEE"); - put('A', "EEEE"); - put('b', "MMM"); - put('B', "MMMM"); - put('c', "EEE MMM dd HH:mm:ss yyyy"); - put('d', "dd"); - put('H', "HH"); - put('I', "hh"); - put('j', "DDD"); - put('m', "MM"); - put('M', "mm"); - put('p', "a"); - put('S', "ss"); - put('U', "ww"); - put('W', "ww"); // same as U ?? - put('x', "MM/dd/yy"); - put('X', "HH:mm:ss"); - put('y', "yy"); - put('Y', "yyyy"); - put('Z', "zzz"); - put('%', "%"); - }}; - // strptime formats not supported by SimpleDateFormat: - private static final List notSupported = new ArrayList() {{ - add('w'); - add('f'); - }}; - - - /** - * Returns a {@link SimpleDateFormat} format string equivalent to the strptime - * format string supplied as parameter. If there is no reliable equivalent, it returns - * null, and the caller will use the Python implementation. - * - * @return format equivalent or null - */ - private static String py2java_format(String format) { - StringBuilder builder = new StringBuilder(); - boolean directive = false; - boolean inQuote = false; - boolean containsYear = false; - boolean containsMonth = false; - - if (format.length() == 0) { - return null; - } - - for (int i = 0; i < format.length(); i++) { - char charAt = format.charAt(i); - - if (charAt == '%' && !directive) { - directive = true; - continue; - } - - if (!directive) { - // ascii letters are considered SimpleDateFormat directive patterns unless - // escaped - boolean needsQuote = (charAt >= 'A' && charAt <= 'Z') || - (charAt >= 'a' && charAt <= 'z'); - if (needsQuote && !inQuote || !needsQuote && inQuote) { - builder.append("'"); - inQuote = needsQuote; - } - if (charAt == '\'') { - // a single quote always needs to be escaped, regardless - // whether already in a quote or not - builder.append("'"); - } - builder.append(charAt); - continue; - } else if (inQuote) { - builder.append("'"); - inQuote = false; - } - - String translated = py2java.get(charAt); - if (translated == null && notSupported.contains(charAt)) { - return null; - } - - switch (charAt) { - case 'c': - case 'x': - containsMonth = containsYear = true; - break; - case 'y': - case 'Y': - containsYear = true; - break; - case 'b': - case 'B': - case 'm': - containsMonth = true; - break; - } - - builder.append(translated != null ? translated : charAt); - directive = false; - } - if (containsMonth && !containsYear) { - // Java differs from Python concerning 29 Feb with no year: safe choice is fall-back. - return null; - } - if (inQuote) { - builder.append("'"); - } - return builder.toString(); - } }