Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 75868 invoked from network); 21 Jul 2006 05:47:33 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 21 Jul 2006 05:47:33 -0000 Received: (qmail 89156 invoked by uid 500); 21 Jul 2006 05:47:30 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 89059 invoked by uid 500); 21 Jul 2006 05:47:29 -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 89048 invoked by uid 500); 21 Jul 2006 05:47:29 -0000 Received: (qmail 89045 invoked by uid 99); 21 Jul 2006 05:47:29 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Jul 2006 22:47:29 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Jul 2006 22:47:28 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id D27801A981A; Thu, 20 Jul 2006 22:47:08 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Fri, 21 Jul 2006 05:47:08 -0000 To: commons-cvs@jakarta.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060721054708.D27801A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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