You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2006/07/21 07:47:08 UTC
svn commit: r424192 - in /jakarta/commons/proper/lang/trunk/src:
java/org/apache/commons/lang/time/DateUtils.java
test/org/apache/commons/lang/time/DateUtilsTest.java
Author: bayard
Date: Thu Jul 20 22:47:07 2006
New Revision: 424192
URL: http://svn.apache.org/viewvc?rev=424192&view=rev
Log:
Adding Niall's fix for LANG-59 - an edge case in date truncation - and his enhancement
for the unit test that was there.
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java
Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java?rev=424192&r1=424191&r2=424192&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java Thu Jul 20 22:47:07 2006
@@ -621,6 +621,51 @@
throw new ArithmeticException("Calendar value too large for accurate calculations");
}
+ if (field == Calendar.MILLISECOND) {
+ return;
+ }
+
+ // ----------------- Fix for LANG-59 ---------------------- START ---------------
+ // see http://issues.apache.org/jira/browse/LANG-59
+ //
+ // Manually truncate milliseconds, seconds and minutes, rather than using
+ // Calendar methods.
+
+ Date date = val.getTime();
+ long time = date.getTime();
+ boolean done = false;
+
+ // truncate milliseconds
+ int millisecs = val.get(Calendar.MILLISECOND);
+ if (!round || millisecs < 500) {
+ time = time - millisecs;
+ if (field == Calendar.SECOND) {
+ done = true;
+ }
+ }
+
+ // truncate seconds
+ int seconds = val.get(Calendar.SECOND);
+ if (!done && (!round || seconds < 30)) {
+ time = time - (seconds * 1000L);
+ if (field == Calendar.MINUTE) {
+ done = true;
+ }
+ }
+
+ // truncate minutes
+ int minutes = val.get(Calendar.MINUTE);
+ if (!done && (!round || minutes < 30)) {
+ time = time - (minutes * 60000L);
+ }
+
+ // reset time
+ if (date.getTime() != time) {
+ date.setTime(time);
+ val.setTime(date);
+ }
+ // ----------------- Fix for LANG-59 ----------------------- END ----------------
+
boolean roundUp = false;
for (int i = 0; i < fields.length; i++) {
for (int j = 0; j < fields[i].length; j++) {
@@ -689,7 +734,9 @@
roundUp = offset > ((max - min) / 2);
}
//We need to remove this field
- val.set(fields[i][0], val.get(fields[i][0]) - offset);
+ if (offset != 0) {
+ val.set(fields[i][0], val.get(fields[i][0]) - offset);
+ }
}
throw new IllegalArgumentException("The field " + field + " is not supported");
Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java?rev=424192&r1=424191&r2=424192&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java (original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java Thu Jul 20 22:47:07 2006
@@ -883,6 +883,81 @@
}
/**
+ * Tests for LANG-59
+ *
+ * see http://issues.apache.org/jira/browse/LANG-59
+ */
+ public void testTruncateLang59() throws Exception {
+
+ // Set TimeZone to Mountain Time
+ TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
+ TimeZone.setDefault(MST_MDT);
+ DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+ format.setTimeZone(MST_MDT);
+
+ Date oct31_01MDT = new Date(1099206000000L);
+
+ Date oct31MDT = new Date(oct31_01MDT.getTime() - 3600000L); // - 1 hour
+ Date oct31_01_02MDT = new Date(oct31_01MDT.getTime() + 120000L); // + 2 minutes
+ Date oct31_01_02_03MDT = new Date(oct31_01_02MDT.getTime() + 3000L); // + 3 seconds
+ Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L); // + 4 milliseconds
+
+ assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
+ assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 MDT", format.format(oct31_01MDT));
+ assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 MDT", format.format(oct31_01_02MDT));
+ assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 MDT", format.format(oct31_01_02_03MDT));
+ assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 MDT", format.format(oct31_01_02_03_04MDT));
+
+ // ------- Demonstrate Problem -------
+ Calendar gval = Calendar.getInstance();
+ gval.setTime(new Date(oct31_01MDT.getTime()));
+ gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
+ assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L);
+
+ // ---------- Test Truncate ----------
+ assertEquals("Truncate Calendar.MILLISECOND",
+ oct31_01_02_03_04MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MILLISECOND));
+
+ assertEquals("Truncate Calendar.SECOND",
+ oct31_01_02_03MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.SECOND));
+
+ assertEquals("Truncate Calendar.MINUTE",
+ oct31_01_02MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MINUTE));
+
+ assertEquals("Truncate Calendar.HOUR_OF_DAY",
+ oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));
+
+ assertEquals("Truncate Calendar.HOUR",
+ oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR));
+
+ assertEquals("Truncate Calendar.DATE",
+ oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE));
+
+
+ // ---------- Test Round (down) ----------
+ assertEquals("Round Calendar.MILLISECOND",
+ oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND));
+
+ assertEquals("Round Calendar.SECOND",
+ oct31_01_02_03MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND));
+
+ assertEquals("Round Calendar.MINUTE",
+ oct31_01_02MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MINUTE));
+
+ assertEquals("Round Calendar.HOUR_OF_DAY",
+ oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));
+
+ assertEquals("Round Calendar.HOUR",
+ oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR));
+
+ assertEquals("Round Calendar.DATE",
+ oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE));
+
+ // restore default time zone
+ TimeZone.setDefault(defaultZone);
+ }
+
+ /**
* Tests the iterator exceptions
*/
public void testIteratorEx() throws Exception {
@@ -976,14 +1051,6 @@
assertWeekIterator(it,
dateParser.parse("October 29, 2001"),
dateParser.parse("December 2, 2001"));
- }
-
- // Tests LANG-59
- public void testLang59() throws Exception {
- // truncate 2004-10-31 01:00:00 MDT
- Date oct31_01MDT = new Date(1099206000000L);
- Date result = DateUtils.truncate(oct31_01MDT, Calendar.HOUR_OF_DAY);
- assertEquals(oct31_01MDT, result);
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org