From: Scott Russell
Date: Mon, 8 Oct 2012 19:13:27 +0000 (+0000)
Subject: ICU-9403 Calendar should return error for large negative millis out of range in stric...
X-Git-Tag: milestone-59-0-1~3465
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5c6dc411d8df0af0a11e07c82a81caf8f4edbaee;p=icu
ICU-9403 Calendar should return error for large negative millis out of range in strict mode
X-SVN-Rev: 32553
---
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java b/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java
index 3f8224e3e77..2bfd848ac75 100644
--- a/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java
+++ b/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java
@@ -592,7 +592,7 @@ import com.ibm.icu.util.ULocale.Category;
* boundaries. The new Calendar
protocol specifies the
* maximum range of supportable dates as those having Julian day numbers
* of -0x7F000000
to +0x7F000000
. This
- * corresponds to years from ~5,000,000 BCE to ~5,000,000 CE. Programmers
+ * corresponds to years from ~5,800,000 BCE to ~5,800,000 CE. Programmers
* should use the protected constants in Calendar
to
* specify an extremely early or extremely late date.
*
@@ -2091,14 +2091,25 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable MAX_MILLIS) {
- millis = MAX_MILLIS;
+ if(isLenient()) {
+ millis = MAX_MILLIS;
+ } else {
+ throw new IllegalArgumentException("millis value greater than upper bounds for a Calendar : " + millis);
+ }
} else if (millis < MIN_MILLIS) {
- millis = MIN_MILLIS;
+ if(isLenient()) {
+ millis = MIN_MILLIS;
+ } else {
+ throw new IllegalArgumentException("millis value less than lower bounds for a Calendar : " + millis);
+ }
}
time = millis;
areFieldsSet = areAllFieldsSet = false;
diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/calendar/CalendarRegression.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/calendar/CalendarRegression.java
index a21550d8991..22a1fee2cbd 100644
--- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/calendar/CalendarRegression.java
+++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/calendar/CalendarRegression.java
@@ -2295,6 +2295,45 @@ public class CalendarRegression extends com.ibm.icu.dev.test.TestFmwk {
logln("-1 day: " + dstr);
assertEquals("Subtract 1 day", "2011-12-29T00:00:00-10:00", dstr);
}
+
+ /**
+ * Test case for ticket 9403
+ * semantic API change when attempting to call setTimeInMillis(long) with a value outside the bounds.
+ * In strict mode an IllegalIcuArgumentException will be thrown
+ * In lenient mode the value will be pinned to the relative min/max
+ */
+ public void TestT9403() {
+ Calendar myCal = Calendar.getInstance();
+ long dateBit1, dateBit2, testMillis = 0L;
+ boolean missedException = true;
+
+ testMillis = -184303902611600000L;
+ logln("Testing invalid setMillis value in lienent mode - using value: " + testMillis);
+
+ try {
+ myCal.setTimeInMillis(testMillis);
+ } catch (IllegalArgumentException e) {
+ logln("Fail: detected as bad millis");
+ missedException = false;
+ }
+ assertTrue("Fail: out of bound millis did not trigger exception!", missedException);
+ dateBit1 = myCal.get(Calendar.MILLISECOND);
+ assertNotEquals("Fail: millis not changed to MIN_MILLIS", testMillis, dateBit1);
+
+
+ logln("Testing invalid setMillis value in strict mode - using value: " + testMillis);
+ myCal.setLenient(false);
+ try {
+ myCal.setTimeInMillis(testMillis);
+ } catch (IllegalArgumentException e) {
+ logln("Pass: correctly detected bad millis");
+ missedException = false;
+ }
+ dateBit1 = myCal.get(Calendar.DAY_OF_MONTH);
+ dateBit2 = myCal.getTimeInMillis();
+ assertFalse("Fail: error in setMillis, allowed invalid value : " + testMillis + "...returned dayOfMonth : " + dateBit1 + " millis : " + dateBit2, missedException);
+ }
+
}
//eof