commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r453818 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/time/DurationFormatUtils.java test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
Date Sat, 07 Oct 2006 00:22:43 GMT
Author: bayard
Date: Fri Oct  6 17:22:42 2006
New Revision: 453818

URL: http://svn.apache.org/viewvc?view=rev&rev=453818
Log:
Adding a unit test for #LANG-281 and a fix. The fix involves removing the reduceAndCorrect
method. It appears that this method was doing sod all - which is worrying as it used to be
important. I'm guessing that it was a bad fix for a bug that was then subsequently fixed with
other code. I'll create a JIRA issue to create more tests to test out the +31 block of code.


Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java?view=diff&rev=453818&r1=453817&r2=453818
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
Fri Oct  6 17:22:42 2006
@@ -303,23 +303,21 @@
             days -= 1;
         }
         while (days < 0) {
-            days += 31; // such overshooting is taken care of later on
+            end.add(Calendar.MONTH, -1);
+            days += end.getActualMaximum(Calendar.DAY_OF_MONTH);
+//days += 31; // TODO: Need tests to show this is bad and the new code is good.
+// HEN: It's a tricky subject. Jan 15th to March 10th. If I count days-first it is 
+// 1 month and 26 days, but if I count month-first then it is 1 month and 23 days.
+// Also it's contextual - if asked for no M in the format then I should probably 
+// be doing no calculating here.
             months -= 1;
+            end.add(Calendar.MONTH, 1);
         }
         while (months < 0) {
             months += 12;
             years -= 1;
         }
 
-        // take estimates off of end to see if we can equal start, when it overshoots recalculate
-        milliseconds -= reduceAndCorrect(start, end, Calendar.MILLISECOND, milliseconds);
-        seconds -= reduceAndCorrect(start, end, Calendar.SECOND, seconds);
-        minutes -= reduceAndCorrect(start, end, Calendar.MINUTE, minutes);
-        hours -= reduceAndCorrect(start, end, Calendar.HOUR_OF_DAY, hours);
-        days -= reduceAndCorrect(start, end, Calendar.DAY_OF_MONTH, days);
-        months -= reduceAndCorrect(start, end, Calendar.MONTH, months);
-        years -= reduceAndCorrect(start, end, Calendar.YEAR, years);
-
         // This next block of code adds in values that 
         // aren't requested. This allows the user to ask for the 
         // number of months and get the real count and not just 0->11.
@@ -425,29 +423,6 @@
             }
         }
         return buffer.toString();
-    }
-
-    /**
-     * Reduces by difference, then if it overshot, calculates the overshot amount and 
-     * fixes and returns the amount to change by.
-     *
-     * @param start Start of period being formatted
-     * @param end End of period being formatted
-     * @param field Field to reduce, as per constants in {@link java.util.Calendar}
-     * @param difference amount to reduce by
-     * @return int reduced value
-     */
-    static int reduceAndCorrect(Calendar start, Calendar end, int field, int difference)
{
-        end.add( field, -1 * difference );
-        int endValue = end.get(field);
-        int startValue = start.get(field);
-        if (endValue < startValue) {
-            int newdiff = startValue - endValue;
-            end.add( field, newdiff );
-            return newdiff;
-        } else {
-            return 0;
-        }
     }
 
     static final Object y = "y";

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java?view=diff&rev=453818&r1=453817&r2=453818
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
Fri Oct  6 17:22:42 2006
@@ -411,6 +411,29 @@
         assertEquals( "0000/00/30 16:00:00 000", DurationFormatUtils.formatPeriod(cal1.getTime().getTime(),
cal2.getTime().getTime(), "yyyy/MM/dd HH:mm:ss SSS") );
     }
 
+    // https://issues.apache.org/jira/browse/LANG-281
+    public void testJiraLang281() {
+        Calendar cal = Calendar.getInstance();
+        cal.set(Calendar.MONTH, Calendar.DECEMBER);
+        cal.set(Calendar.DAY_OF_MONTH, 31);
+        cal.set(Calendar.YEAR, 2005);
+        cal.set(Calendar.HOUR_OF_DAY, 0);
+        cal.set(Calendar.MINUTE, 0);
+        cal.set(Calendar.SECOND, 0);
+        cal.set(Calendar.MILLISECOND, 0);
+
+        Calendar cal2 = Calendar.getInstance();
+        cal2.set(Calendar.MONTH, Calendar.OCTOBER);
+        cal2.set(Calendar.DAY_OF_MONTH, 6);
+        cal2.set(Calendar.YEAR, 2006);
+        cal2.set(Calendar.HOUR_OF_DAY, 0);
+        cal2.set(Calendar.MINUTE, 0);
+        cal2.set(Calendar.SECOND, 0);
+        cal2.set(Calendar.MILLISECOND, 0);
+        String result = DurationFormatUtils.formatPeriod(cal.getTime().getTime(), cal2.getTime().getTime(),
"MM");
+        assertEquals("09", result);
+    }
+
     private void assertArrayEquals(DurationFormatUtils.Token[] obj1, DurationFormatUtils.Token[]
obj2) {
         assertEquals("Arrays are unequal length. ", obj1.length, obj2.length);
         for (int i = 0; i < obj1.length; i++) {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message