db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r936215 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/types/SQLChar.java testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
Date Wed, 21 Apr 2010 09:09:12 GMT
Author: kahatlen
Date: Wed Apr 21 09:09:12 2010
New Revision: 936215

URL: http://svn.apache.org/viewvc?rev=936215&view=rev
Log:
DERBY-4621: Invalid conversion from Timestamp to String when calling setTimestamp() with Calendar

Two changes to fix the wrong time and to harmonize the format between
the methods that take a Calendar argument and those that don't:

  1) Use 24-hour clock when converting the time component to a string

  2) Add a decimal point and a single zero if the nanosecond component
     of a Timestamp is zero

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java?rev=936215&r1=936214&r2=936215&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java Wed Apr 21 09:09:12
2010
@@ -1564,13 +1564,20 @@ readingLoop:
                 formatJDBCDate( cal, sb);
                 sb.append( ' ');
                 formatJDBCTime( cal, sb);
+                sb.append('.');
+
                 int micros = 
                     (theValue.getNanos() + SQLTimestamp.FRACTION_TO_NANO/2) / 
                         SQLTimestamp.FRACTION_TO_NANO;
 
-                if( micros > 0)
+                if (micros == 0)
+                {
+                    // Add a single zero after the decimal point to match
+                    // the format from Timestamp.toString().
+                    sb.append('0');
+                }
+                else if (micros > 0)
                 {
-                    sb.append( '.');
                     String microsStr = Integer.toString( micros);
                     if(microsStr.length() > SQLTimestamp.MAX_FRACTION_DIGITS)
                     {
@@ -1606,7 +1613,7 @@ readingLoop:
     private void formatJDBCTime( Calendar cal, StringBuffer sb)
     {
         SQLTime.timeToString(
-            cal.get(Calendar.HOUR), 
+            cal.get(Calendar.HOUR_OF_DAY),
             cal.get(Calendar.MINUTE), 
             cal.get(Calendar.SECOND), 
             sb);

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java?rev=936215&r1=936214&r2=936215&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DateTimeTest.java
Wed Apr 21 09:09:12 2010
@@ -21,11 +21,15 @@
 */
 package org.apache.derbyTesting.functionTests.tests.lang;
 
+import java.sql.Date;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Calendar;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -909,6 +913,44 @@ public final class DateTimeTest extends 
         
         st.close();
     }
+
+    /**
+     * Regression test case for DERBY-4621, which caused the conversion of
+     * timestamp and time values to varchar to generate wrong results when
+     * a Calendar object was supplied.
+     */
+    public void testConversionToString() throws SQLException {
+        String timestampString = "2010-04-20 15:17:36.0";
+        String timeString = "15:17:36";
+        String dateString = "2010-04-20";
+
+        Timestamp ts = Timestamp.valueOf(timestampString);
+        Time t = Time.valueOf(timeString);
+        Date d = Date.valueOf(dateString);
+
+        PreparedStatement ps =
+                prepareStatement("VALUES CAST(? AS VARCHAR(40))");
+
+        ps.setTimestamp(1, ts);
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timestampString);
+
+        // Used to give wrong result - 2010-04-20 03:17:36
+        ps.setTimestamp(1, ts, Calendar.getInstance());
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timestampString);
+
+        ps.setTime(1, t);
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timeString);
+
+        // Used to give wrong result - 03:17:36
+        ps.setTime(1, t, Calendar.getInstance());
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), timeString);
+
+        ps.setDate(1, d);
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), dateString);
+
+        ps.setDate(1, d, Calendar.getInstance());
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), dateString);
+    }
     
     public void testConversion_Aggregates() throws SQLException{
         Statement st = createStatement();



Mime
View raw message