db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r538349 - in /db/derby/code/trunk/java/engine/org/apache/derby/iapi/types: SQLDate.java SQLTime.java SQLTimestamp.java
Date Tue, 15 May 2007 22:46:27 GMT
Author: djd
Date: Tue May 15 15:46:27 2007
New Revision: 538349

URL: http://svn.apache.org/viewvc?view=rev&rev=538349
Log:
DERBY-1985 (partial) Cleanup code in getTime,getDate & getTimestamap for the date-type
types by adding utility methods, sharing code and using the Calendar.getTimeInMillis() to
avoid
creating a java.util.Date object during conversion.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDate.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTime.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTimestamp.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDate.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDate.java?view=diff&rev=538349&r1=538348&r2=538349
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDate.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDate.java Tue May 15 15:46:27
2007
@@ -142,25 +142,39 @@
 		{
 			return null;
 		}
-		else 
-			// date is converted to a timestamp filling the time in with 00:00:00
-            return newTimestamp(cal);
+        
+        return new Timestamp(getTimeInMillis(cal));
     }
 
-    private long getTimeInMillis( Calendar cal)
+    /**
+     * Convert the date into a milli-seconds since the epoch
+     * with the time set to 00:00 based upon the passed in Calendar.
+     */
+    private long getTimeInMillis(Calendar cal)
     {
         if( cal == null)
             cal = new GregorianCalendar();
         cal.clear();
-        cal.set( getYear( encodedDate), getMonth( encodedDate)-1, getDay( encodedDate));
-        return cal.getTime().getTime();
+        
+        SQLDate.setDateInCalendar(cal, encodedDate);
+        
+        return cal.getTimeInMillis();
     }
     
-    private Timestamp newTimestamp(java.util.Calendar cal)
+    /**
+     * Set the date portion of a date-time value into
+     * the passed in Calendar object from its encodedDate
+     * value. Only the YEAR, MONTH and DAY_OF_MONTH
+     * fields are modified. The remaining
+     * state of the Calendar is not modified.
+     */
+    static void setDateInCalendar(Calendar cal, int encodedDate)
     {
-        return new Timestamp(getTimeInMillis( cal));
-	}
-
+        // Note Calendar uses 0 for January, Derby uses 1.
+        cal.set(getYear(encodedDate),
+                getMonth(encodedDate)-1, getDay(encodedDate));     
+    }
+    
 	/**
 		getObject returns the date value
 
@@ -729,10 +743,10 @@
 	 */
 	public Date getDate( Calendar cal)
 	{
-		if (encodedDate != 0)
-            return new Date( getTimeInMillis( cal));
-
-		return null;
+        if (isNull())
+            return null;
+        
+        return new Date(getTimeInMillis(cal));
 	}
 
 	/**
@@ -747,7 +761,8 @@
 	}
 
 	/**
-	 * Get the month from the encodedDate.
+	 * Get the month from the encodedDate,
+     * January is one.
 	 *
 	 * @param encodedDate	the encoded date
 	 * @return	 			month value.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTime.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTime.java?view=diff&rev=538349&r1=538348&r2=538349
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTime.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTime.java Tue May 15 15:46:27
2007
@@ -164,18 +164,18 @@
             else
             {
                 cal.clear();
-                // Set Calendar to current date and time.
-                cal.setTime(new Date(System.currentTimeMillis()));
+                // Set Calendar to current date and time
+                // to pick up the current date. Time portion
+                // will be overridden by this value's time.
+                cal.setTimeInMillis(System.currentTimeMillis());
             }
-
-			cal.set(Calendar.HOUR_OF_DAY, getHour(encodedTime));
-			cal.set(Calendar.MINUTE, getMinute(encodedTime));
-			cal.set(Calendar.SECOND, getSecond(encodedTime));
             
+            SQLTime.setTimeInCalendar(cal, encodedTime);
+          
             // Derby's resolution for the TIME type is only seconds.
 			cal.set(Calendar.MILLISECOND, 0);
             
-			return new Timestamp(cal.getTime().getTime());
+			return new Timestamp(cal.getTimeInMillis());
 		}
 	}
 
@@ -800,20 +800,54 @@
 	{
 		if (isNull())
 			return null;
-
+        
+        // Derby's SQL TIME type only has second resolution
+        // so pass in 0 for nano-seconds
+        return getTime(cal, encodedTime, 0);
+	}
+    
+    /**
+     * Set the time portion of a date-time value into
+     * the passed in Calendar object from its encodedTime
+     * value. Note that this is only the time down
+     * to a resolution of one second. Only the HOUR_OF_DAY,
+     * MINUTE and SECOND fields are modified. The remaining
+     * state of the Calendar is not modified.
+     */
+    static void setTimeInCalendar(Calendar cal, int encodedTime)
+    {
+        cal.set(Calendar.HOUR_OF_DAY, getHour(encodedTime));
+        cal.set(Calendar.MINUTE, getMinute(encodedTime));
+        cal.set(Calendar.SECOND, getSecond(encodedTime));        
+    }
+    
+    /**
+     * Get a java.sql.Time object from an encoded time
+     * and nano-second value. As required by JDBC the
+     * date component of the Time object will be set to
+     * Jan. 1, 1970
+     * @param cal Calendar to use for conversion
+     * @param encodedTime Derby encoded time value
+     * @param nanos number of nano-seconds.
+     * @return Valid Time object.
+     */
+    static Time getTime(Calendar cal, int encodedTime, int nanos)
+    {
         if( cal == null)
             cal = new GregorianCalendar();
         
         cal.clear();
-		cal.set(Calendar.YEAR, 1970);
-		cal.set(Calendar.MONTH, Calendar.JANUARY);
-		cal.set(Calendar.DATE, 1);
-		cal.set(Calendar.HOUR_OF_DAY, getHour(encodedTime));
-		cal.set(Calendar.MINUTE, getMinute(encodedTime));
-		cal.set(Calendar.SECOND, getSecond(encodedTime));
-		cal.set(Calendar.MILLISECOND, 0);	//only 0 fractional seconds currently
-		return new Time(cal.getTime().getTime());
-	}
+        
+        cal.set(1970, Calendar.JANUARY, 1);
+
+        SQLTime.setTimeInCalendar(cal, encodedTime);
+
+        cal.set(Calendar.MILLISECOND, nanos/1000000);
+        
+        return new Time(cal.getTimeInMillis());
+    }
+    
+    
 	/**
 	 * Get the encoded hour value (may be different than hour value for
 	 *  	current timezone if value encoded in a different timezone)

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTimestamp.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTimestamp.java?view=diff&rev=538349&r1=538348&r2=538349
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTimestamp.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTimestamp.java Tue May
15 15:46:27 2007
@@ -162,22 +162,18 @@
 	{
 		if (isNull())
 			return null;
-        return newDate(cal);
-    }
 
-    private Date newDate(java.util.Calendar cal) throws StandardException
-    {
         if( cal == null)
             cal = new GregorianCalendar();
+        
+        // Will clear all the other fields to zero
+        // that we require at zero, specifically
+        // HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND
         cal.clear();
-		cal.set(Calendar.YEAR, SQLDate.getYear(encodedDate) );
-		cal.set(Calendar.MONTH, SQLDate.getMonth(encodedDate)-1);
-		cal.set(Calendar.DATE, SQLDate.getDay(encodedDate) );
-		cal.set(Calendar.HOUR_OF_DAY, 0);
-		cal.set(Calendar.MINUTE, 0);
-		cal.set(Calendar.SECOND, 0);
-		cal.set(Calendar.MILLISECOND, 0);
-		return new Date(cal.getTime().getTime());
+        
+        SQLDate.setDateInCalendar(cal, encodedDate);
+
+		return new Date(cal.getTimeInMillis());
 	}
 
 	/**
@@ -191,22 +187,12 @@
 	{
 		if (isNull())
 			return null;
-        return newTime(cal);
-    }
-
-    private Time newTime(java.util.Calendar cal) throws StandardException
-    {
-        if( cal == null)
-            cal = new GregorianCalendar();
-        cal.clear();
-		cal.set(Calendar.YEAR, 1970);
-		cal.set(Calendar.MONTH, Calendar.JANUARY);
-		cal.set(Calendar.DATE, 1);
-		cal.set(Calendar.HOUR_OF_DAY, SQLTime.getHour(encodedTime));
-		cal.set(Calendar.MINUTE, SQLTime.getMinute(encodedTime));
-		cal.set(Calendar.SECOND, SQLTime.getSecond(encodedTime));
-		cal.set(Calendar.MILLISECOND, (int)(nanos/1000000));
-		return new Time(cal.getTime().getTime());
+        
+        // Derby's SQL TIMESTAMP type supports resolution
+        // to nano-seconds so ensure the Time object
+        // maintains that since it has milli-second
+        // resolutiuon.
+        return SQLTime.getTime(cal, encodedTime, nanos);
 	}
 
 	public Object getObject()
@@ -876,29 +862,23 @@
 	{
 		if (isNull())
 			return null;
-        return newTimestamp(cal);
-    }
 
-    private Timestamp newTimestamp(Calendar currentCal)
-    {
-        if( currentCal == null)
-            currentCal = new GregorianCalendar();
-        setCalendar( currentCal);
-		Timestamp t = new Timestamp(currentCal.getTime().getTime());
+        if (cal == null)
+            cal = new GregorianCalendar();
+        setCalendar(cal);
+		Timestamp t = new Timestamp(cal.getTimeInMillis());
 		t.setNanos(nanos);
 		return t;
 	}
 
-    private void setCalendar( Calendar cal)
+    private void setCalendar(Calendar cal)
     {
         cal.clear();
-		cal.set(Calendar.YEAR, SQLDate.getYear(encodedDate));
-		/* Note calendar month is zero based so we subtract 1*/
-		cal.set(Calendar.MONTH, (SQLDate.getMonth(encodedDate)-1));
-		cal.set(Calendar.DATE, SQLDate.getDay(encodedDate));
-		cal.set(Calendar.HOUR_OF_DAY, SQLTime.getHour(encodedTime));
-		cal.set(Calendar.MINUTE, SQLTime.getMinute(encodedTime));
-		cal.set(Calendar.SECOND, SQLTime.getSecond(encodedTime));
+        
+        SQLDate.setDateInCalendar(cal, encodedDate);
+        
+        SQLTime.setTimeInCalendar(cal, encodedTime);
+
 		cal.set(Calendar.MILLISECOND, 0);
     } // end of setCalendar
         



Mime
View raw message