Reviewers: jat, Description: To behave more like the JRE, TimeStamp.valueOf() should accept strings without a nanoseconds part. Also, if the nanoseconds part has fewer than 9 digits it should be implicitly right-padded with zeros.
Please review this at http://gwt-code-reviews.appspot.com/126819 Affected files: user/super/com/google/gwt/emul/java/sql/Timestamp.java user/test/com/google/gwt/emultest/java/sql/SqlTimestampTest.java Index: user/test/com/google/gwt/emultest/java/sql/SqlTimestampTest.java =================================================================== --- user/test/com/google/gwt/emultest/java/sql/SqlTimestampTest.java (revision 7346) +++ user/test/com/google/gwt/emultest/java/sql/SqlTimestampTest.java (working copy) @@ -174,5 +174,17 @@ Timestamp expected = new Timestamp(2000 - 1900, 1 - 1, 1, 12, 34, 56, 123456789); Timestamp actual = Timestamp.valueOf("2000-01-01 12:34:56.123456789"); assertEquals(expected, actual); + + expected = new Timestamp(2000 - 1900, 1 - 1, 1, 12, 34, 56, 0); + actual = Timestamp.valueOf("2000-01-01 12:34:56"); + assertEquals(expected, actual); + + expected = new Timestamp(2000 - 1900, 1 - 1, 1, 12, 34, 56, 100000000); + actual = Timestamp.valueOf("2000-01-01 12:34:56.1"); + assertEquals(expected, actual); + + expected = new Timestamp(2000 - 1900, 1 - 1, 1, 12, 34, 56, 123450000); + actual = Timestamp.valueOf("2000-01-01 12:34:56.12345"); + assertEquals(expected, actual); } } Index: user/super/com/google/gwt/emul/java/sql/Timestamp.java =================================================================== --- user/super/com/google/gwt/emul/java/sql/Timestamp.java (revision 7346) +++ user/super/com/google/gwt/emul/java/sql/Timestamp.java (working copy) @@ -29,7 +29,11 @@ } String[] timeComponents = components[1].split("\\."); - if (timeComponents.length != 2) { + boolean hasNanos = true; + if (timeComponents.length == 1) { + // Allow timestamps without .fffffffff nanoseconds field + hasNanos = false; + } else if (timeComponents.length != 2) { throw new IllegalArgumentException("Invalid escape format: " + s); } else if (timeComponents[1].length() != 9) { throw new IllegalArgumentException("Invalid escape format: " + s); @@ -37,9 +41,18 @@ Date d = Date.valueOf(components[0]); Time t = Time.valueOf(timeComponents[0]); - int nanos; + int nanos = 0; try { - nanos = Integer.valueOf(timeComponents[1]); + if (hasNanos) { + String nanosString = timeComponents[1]; + int len = nanosString.length(); + nanos = Integer.valueOf(nanosString); + if (len > 9) { + throw new IllegalArgumentException("Invalid escape format: " + s); + } else if (len > 0 && len < 9) { + nanos *= (int) Math.pow(10, 9 - len); + } + } } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid escape format: " + s); } -- http://groups.google.com/group/Google-Web-Toolkit-Contributors
