[Jython-checkins] jython: time.strptime now uses _strptime.py, addressing issue #2112.

jeff.allen jython-checkins at python.org
Thu Jun 12 00:10:32 CEST 2014


http://hg.python.org/jython/rev/1f517f1e5a08
changeset:   7289:1f517f1e5a08
user:        Santoso Wijaya <santoso.wijaya at gmail.com>
date:        Wed Jun 11 20:39:11 2014 +0100
summary:
  time.strptime now uses _strptime.py, addressing issue #2112.

files:
  Lib/test/test_strptime_jy.py          |    4 +
  src/org/python/modules/time/Time.java |  138 +-------------
  2 files changed, 5 insertions(+), 137 deletions(-)


diff --git a/Lib/test/test_strptime_jy.py b/Lib/test/test_strptime_jy.py
--- a/Lib/test/test_strptime_jy.py
+++ b/Lib/test/test_strptime_jy.py
@@ -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 --git a/src/org/python/modules/time/Time.java b/src/org/python/modules/time/Time.java
--- a/src/org/python/modules/time/Time.java
+++ b/src/org/python/modules/time/Time.java
@@ -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<Character, String> py2java = new HashMap<Character, String>() {{
-        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<Character> notSupported = new ArrayList<Character>() {{
-        add('w');
-        add('f');
-    }};
-
-
-    /**
-     * Returns a {@link SimpleDateFormat} format string equivalent to the <code>strptime</code>
-     * format string supplied as parameter. If there is no reliable equivalent, it returns
-     * <code>null</code>, and the caller will use the Python implementation.
-     *
-     * @return format equivalent or <code>null</code>
-     */
-    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();
-    }
 }

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list