Fixes a bug where DateTimeFormat would accept 0 as a valid month or day. Also
adds a test to demonstrate the fix.
patch by: shanjianli
review by: ajr
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@5257 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/i18n/client/DateTimeFormat.java b/user/src/com/google/gwt/i18n/client/DateTimeFormat.java
index 31dd9aa..038610c 100644
--- a/user/src/com/google/gwt/i18n/client/DateTimeFormat.java
+++ b/user/src/com/google/gwt/i18n/client/DateTimeFormat.java
@@ -1705,6 +1705,9 @@
case 'y': // 'y' - YEAR
return subParseYear(text, pos, start, value, part, cal);
case 'd': // 'd' - DATE
+ if (value <= 0) {
+ return false;
+ }
cal.setDayOfMonth(value);
return true;
case 'S': // 'S' - FRACTIONAL_SECOND
@@ -1823,10 +1826,11 @@
}
cal.setMonth(value);
return true;
- } else {
+ } else if (value > 0) {
cal.setMonth(value - 1);
return true;
}
+ return false;
}
/**
diff --git a/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java b/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java
index 8bc38a9..ca45c24 100644
--- a/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java
+++ b/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java
@@ -638,4 +638,25 @@
assertTrue(date.getDate() == 02);
}
+ public void testInvalidDayAndMonth() {
+ DateTimeFormat fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
+ try {
+ fmt.parseStrict("00/22/1999");
+ fail("Should have thrown an exception on failure to parse");
+ } catch (IllegalArgumentException e) {
+ // Success
+ }
+ try {
+ fmt.parseStrict("01/00/1999");
+ fail("Should have thrown an exception on failure to parse");
+ } catch (IllegalArgumentException e) {
+ // Success
+ }
+ try {
+ fmt.parseStrict("01/22/1999");
+ // success
+ } catch (IllegalArgumentException e) {
+ fail("Should succeed to parse");
+ }
+ }
}