Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 85336 invoked from network); 13 Dec 2006 07:10:15 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Dec 2006 07:10:15 -0000 Received: (qmail 72204 invoked by uid 500); 13 Dec 2006 07:10:18 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 72157 invoked by uid 500); 13 Dec 2006 07:10:18 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 72146 invoked by uid 500); 13 Dec 2006 07:10:18 -0000 Received: (qmail 72143 invoked by uid 99); 13 Dec 2006 07:10:18 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Dec 2006 23:10:18 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Dec 2006 23:10:10 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 553E61A981A; Tue, 12 Dec 2006 23:09:25 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r486523 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/time/DurationFormatUtils.java test/org/apache/commons/lang/time/DurationFormatUtilsTest.java Date: Wed, 13 Dec 2006 07:09:25 -0000 To: commons-cvs@jakarta.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061213070925.553E61A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bayard Date: Tue Dec 12 23:09:24 2006 New Revision: 486523 URL: http://svn.apache.org/viewvc?view=rev&rev=486523 Log: More unit tests and bugfixes. The currently failing test is commented out, so more bugfixes to come. 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=486523&r1=486522&r2=486523 ============================================================================== --- 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 Tue Dec 12 23:09:24 2006 @@ -20,6 +20,7 @@ import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.TimeZone; /** @@ -312,6 +313,9 @@ hours += 24; days -= 1; } + // TODO: Create a test to see if this should be while. ie) one that makes hours above + // overflow and pushes this above the maximum # of days in a month? + int leapDays = 0; if (days < 0) { days += start.getActualMaximum(Calendar.DAY_OF_MONTH); // It's a tricky subject. Jan 15th to March 10th. If I count days-first it is @@ -320,10 +324,25 @@ // Also it's contextual - if asked for no M in the format then I should probably // be doing no calculating here. months -= 1; + start.add(Calendar.MONTH, 1); } while (months < 0) { months += 12; years -= 1; + if (start instanceof GregorianCalendar) { + if ( ((GregorianCalendar) start).isLeapYear(start.get(Calendar.YEAR) + 1) && + ( end.get(Calendar.MONTH) > 1) ) + { + leapDays += 1; + } + } + if (end instanceof GregorianCalendar) { + if ( ((GregorianCalendar) end).isLeapYear(end.get(Calendar.YEAR)) && + ( end.get(Calendar.MONTH) < 1) ) + { + leapDays -= 1; + } + } start.add(Calendar.YEAR, 1); } @@ -331,31 +350,31 @@ // aren't requested. This allows the user to ask for the // number of months and get the real count and not just 0->11. - if (!Token.containsTokenWithValue(tokens, y)) { + if (!Token.containsTokenWithValue(tokens, y) && years != 0) { if (Token.containsTokenWithValue(tokens, M)) { months += 12 * years; years = 0; } else { - while(start.get(Calendar.YEAR) != end.get(Calendar.YEAR)) { - days += start.getActualMaximum(Calendar.DAY_OF_YEAR); - start.add(Calendar.YEAR, 1); + while ( (start.get(Calendar.YEAR) != end.get(Calendar.YEAR))) { + days += start.getActualMaximum(Calendar.DAY_OF_YEAR); + start.add(Calendar.YEAR, 1); } years = 0; } } + start.set(Calendar.YEAR, end.get(Calendar.YEAR)); - if (!Token.containsTokenWithValue(tokens, M) && months != 0) { - start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), 0, 0, 0, 0); + if (!Token.containsTokenWithValue(tokens, M) && months != 0) { + while(start.get(Calendar.MONTH) != end.get(Calendar.MONTH)) { + String date = start.getTime().toString(); + days += start.getActualMaximum(Calendar.DAY_OF_MONTH); start.add(Calendar.MONTH, 1); - end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH), 0, 0, 0, 0); - days += end.get(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR); - months = 0; - - // WARNING: For performance sake the Calendar instances are not being - // cloned but modified inline. They should not be trusted after this point - start = null; - end = null; + } + days += leapDays; + months = 0; } + start.set(Calendar.MONTH, end.get(Calendar.MONTH)); + if (!Token.containsTokenWithValue(tokens, d)) { hours += 24 * days; days = 0; 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=486523&r1=486522&r2=486523 ============================================================================== --- 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 Tue Dec 12 23:09:24 2006 @@ -441,9 +441,61 @@ assertEqualDuration( "365", new int[] { 2006, 0, 1, 0, 0, 0 }, new int[] { 2007, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration( "31", new int[] { 2006, 0, 1, 0, 0, 0 }, + new int[] { 2006, 1, 1, 0, 0, 0 }, "dd"); + + assertEqualDuration( "92", new int[] { 2005, 9, 1, 0, 0, 0 }, + new int[] { 2006, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration( "77", new int[] { 2005, 9, 16, 0, 0, 0 }, + new int[] { 2006, 0, 1, 0, 0, 0 }, "dd"); + + // test month larger in start than end + assertEqualDuration( "136", new int[] { 2005, 9, 16, 0, 0, 0 }, + new int[] { 2006, 2, 1, 0, 0, 0 }, "dd"); + // test when start in leap year + assertEqualDuration( "136", new int[] { 2004, 9, 16, 0, 0, 0 }, + new int[] { 2005, 2, 1, 0, 0, 0 }, "dd"); + // test when end in leap year + assertEqualDuration( "137", new int[] { 2003, 9, 16, 0, 0, 0 }, + new int[] { 2004, 2, 1, 0, 0, 0 }, "dd"); + // test when end in leap year but less than end of feb + assertEqualDuration( "135", new int[] { 2003, 9, 16, 0, 0, 0 }, + new int[] { 2004, 1, 28, 0, 0, 0 }, "dd"); + + assertEqualDuration( "364", new int[] { 2007, 0, 2, 0, 0, 0 }, + new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration( "729", new int[] { 2006, 0, 2, 0, 0, 0 }, + new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); + + assertEqualDuration( "365", new int[] { 2007, 2, 2, 0, 0, 0 }, + new int[] { 2008, 2, 1, 0, 0, 0 }, "dd"); + } + + public void testDurationsByBruteForce() { + bruteForce(2006, 0, 1); + bruteForce(2006, 0, 2); +// bruteForce(2006, 1, 2); + } + + private void bruteForce(int year, int month, int day) { + String msg = year + "-" + month + "-" + day + " at "; + Calendar c = Calendar.getInstance(); + c.set(year, month, day, 0, 0, 0); + int[] array1 = new int[] { year, month, day, 0, 0, 0 }; + int[] array2 = new int[] { year, month, day, 0, 0, 0 }; + for (int i=0; i < 1500; i++) { + array2[0] = c.get(Calendar.YEAR); + array2[1] = c.get(Calendar.MONTH); + array2[2] = c.get(Calendar.DAY_OF_MONTH); + assertEqualDuration( msg + i, Integer.toString(i), array1, array2, "d" ); + c.add(Calendar.DAY_OF_MONTH, 1); + } } private void assertEqualDuration(String expected, int[] start, int[] end, String format) { + assertEqualDuration(null, expected, start, end, format); + } + private void assertEqualDuration(String message, String expected, int[] start, int[] end, String format) { Calendar cal1 = Calendar.getInstance(); cal1.set(start[0], start[1], start[2], start[3], start[4], start[5]); cal1.set(Calendar.MILLISECOND, 0); @@ -453,7 +505,11 @@ long milli1 = cal1.getTime().getTime(); long milli2 = cal2.getTime().getTime(); String result = DurationFormatUtils.formatPeriod(milli1, milli2, format); - assertEquals(expected, result); + if (message == null) { + assertEquals(expected, result); + } else { + assertEquals(message, expected, result); + } } private void assertArrayEquals(DurationFormatUtils.Token[] obj1, DurationFormatUtils.Token[] obj2) { --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org