Index: Makefile
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/Makefile,v
retrieving revision 1.38
diff -c -p -r1.38 Makefile
*** Makefile	12 Feb 2003 06:13:04 -0000	1.38
--- Makefile	8 Jul 2003 17:08:14 -0000
***************
*** 4,10 ****
  #
  # Copyright (c) 2001, PostgreSQL Global Development Group
  #
! # $Header: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/Makefile,v 1.38 2003/02/12 06:13:04 barry Exp $
  #
  #-------------------------------------------------------------------------
  
--- 4,10 ----
  #
  # Copyright (c) 2001, PostgreSQL Global Development Group
  #
! # $Header: /cvsroot/pgsql-server/src/interfaces/jdbc/Makefile,v 1.38 2003/02/12 06:13:04 barry Exp $
  #
  #-------------------------------------------------------------------------
  
Index: build.xml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/build.xml,v
retrieving revision 1.32
diff -c -p -r1.32 build.xml
*** build.xml	27 Feb 2003 05:45:43 -0000	1.32
--- build.xml	8 Jul 2003 17:08:14 -0000
***************
*** 6,12 ****
  
    This file now requires Ant 1.4.1.  2002-04-18
  
!   $Header: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/build.xml,v 1.32 2003/02/27 05:45:43 barry Exp $
  
  -->
  
--- 6,12 ----
  
    This file now requires Ant 1.4.1.  2002-04-18
  
!   $Header: /cvsroot/pgsql-server/src/interfaces/jdbc/build.xml,v 1.32 2003/02/27 05:45:43 barry Exp $
  
  -->
  
Index: org/postgresql/errors.properties
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/errors.properties,v
retrieving revision 1.21
diff -c -p -r1.21 errors.properties
*** org/postgresql/errors.properties	30 Jun 2003 16:38:30 -0000	1.21
--- org/postgresql/errors.properties	8 Jul 2003 17:08:15 -0000
*************** postgresql.call.wrongrtntype:A CallableS
*** 100,102 ****
--- 100,105 ----
  postgresql.input.fetch.gt0:Fetch size must be a value greater than or equal to 0.
  postgresql.input.query.gt0:Query Timeout must be a value greater than or equal to 0.
  postgresql.input.rows.gt0:Maximum number of rows must be a value greater than or equal to 0.
+ postgresql.format.baddate:The date given: {0} does not match the format required: {1}.
+ postgresql.format.badtime:The time given: {0} does not match the format required: {1}.
+ postgresql.format.badtimestamp:The timestamp given {0} does not match the format required: {1}.
Index: org/postgresql/jdbc1/AbstractJdbc1Statement.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java,v
retrieving revision 1.26
diff -c -p -r1.26 AbstractJdbc1Statement.java
*** org/postgresql/jdbc1/AbstractJdbc1Statement.java	30 Jun 2003 21:10:55 -0000	1.26
--- org/postgresql/jdbc1/AbstractJdbc1Statement.java	8 Jul 2003 17:08:16 -0000
*************** public abstract class AbstractJdbc1State
*** 1127,1133 ****
  		}
  		else
  		{
! 			bind(parameterIndex, "'" + x.toString() + "'", PG_TIME);
  		}
  	}
  
--- 1127,1139 ----
  		}
  		else
  		{
! 			int offset = x.getTimezoneOffset()/60;
! 			boolean neg = -offset < 0;
! 			String soffset = (offset < 10 && offset > -10) ? "0" + String.valueOf(offset) : String.valueOf(offset);
! 			soffset = (neg) ? "-" + soffset : "+" + soffset;
! 		
! 			//rememeber to remove cast when callable is added
! 			bind(parameterIndex, "'" + x.toString() + soffset + "'" + "::timetz", PG_TIME);
  		}
  	}
  
*************** public abstract class AbstractJdbc1State
*** 1230,1235 ****
--- 1236,1244 ----
  					sbuf.append(l_minos);
  				}
  				sbuf.append("'");
+ 				//remove when cast is done in Query Executor
+ 				char[] tmpCast = {':',':','t','i','m','e','s','t','a','m','p','t','z'};
+ 				sbuf.append(tmpCast, 0, tmpCast.length);
  				bind(parameterIndex, sbuf.toString(), PG_TIMESTAMPTZ);
  			}
  
*************** public abstract class AbstractJdbc1State
*** 1488,1500 ****
  				setString(parameterIndex, x.toString());
  				break;
  			case Types.DATE:
! 				setDate(parameterIndex, (java.sql.Date)x);
  				break;
  			case Types.TIME:
! 				setTime(parameterIndex, (Time)x);
  				break;
  			case Types.TIMESTAMP:
! 				setTimestamp(parameterIndex, (Timestamp)x);
  				break;
  			case Types.BIT:
  				if (x instanceof Boolean)
--- 1497,1527 ----
  				setString(parameterIndex, x.toString());
  				break;
  			case Types.DATE:
! 				if (x instanceof java.sql.Date) 
! 					setDate(parameterIndex, (java.sql.Date)x);
! 				else
! 				{
! 					java.sql.Date tmpd = (x instanceof java.util.Date) ? new java.sql.Date(((java.util.Date)x).getTime()) : dateFromString(x.toString());
! 					setDate(parameterIndex, tmpd);
! 				}
  				break;
  			case Types.TIME:
! 				if (x instanceof java.sql.Time)
! 					setTime(parameterIndex, (java.sql.Time)x);
! 				else
! 				{
! 					java.sql.Time tmpt = (x instanceof java.util.Date) ? new java.sql.Time(((java.util.Date)x).getTime()) : timeFromString(x.toString());
! 					setTime(parameterIndex, tmpt);
! 				}
  				break;
  			case Types.TIMESTAMP:
! 				if (x instanceof java.sql.Timestamp)
! 					setTimestamp(parameterIndex ,(java.sql.Timestamp)x);
! 				else
! 				{
! 					java.sql.Timestamp tmpts = (x instanceof java.util.Date) ? new java.sql.Timestamp(((java.util.Date)x).getTime()) : timestampFromString(x.toString());
! 					setTimestamp(parameterIndex, tmpts);
! 				}
  				break;
  			case Types.BIT:
  				if (x instanceof Boolean)
*************** public abstract class AbstractJdbc1State
*** 2032,2038 ****
  		return m_useServerPrepare;
  	}
  
! 
  	private static final String PG_TEXT = "text";
  	private static final String PG_INTEGER = "integer";
  	private static final String PG_INT2 = "int2";
--- 2059,2171 ----
  		return m_useServerPrepare;
  	}
  
! 	public static java.sql.Date dateFromString (String s) throws SQLException
! 	{
! 		int timezone = 0;
! 		long millis = 0;
! 		long localoffset = 0;
! 		int timezoneLocation = (s.indexOf('+') == -1) ? s.lastIndexOf("-") : s.indexOf('+');
! 		//if the last index of '-' or '+' is past 8. we are guaranteed that it is a timezone marker
! 		//shortest = yyyy-m-d
! 		//longest = yyyy-mm-dd
! 		try
! 		{
! 			timezone = (timezoneLocation>7) ? timezoneLocation : s.length();
! 			millis = java.sql.Date.valueOf(s.substring(0,timezone)).getTime();
! 		}
! 		catch (Exception e)
! 		{
! 			throw new PSQLException("postgresql.format.baddate",s , "yyyy-MM-dd[-tz]");
! 		}
! 		timezone = 0;
! 		if (timezoneLocation>7 && timezoneLocation+3 == s.length())
! 		{
! 			timezone = Integer.parseInt(s.substring(timezoneLocation+1,s.length()));
! 			localoffset = java.util.Calendar.getInstance().getTimeZone().getOffset(millis);
! 			if (s.charAt(timezoneLocation)=='+')
! 				timezone*=-1;
! 		}
! 		millis = millis + timezone*60*60*1000 + localoffset;
! 		return new java.sql.Date(millis);
! 	}
! 	
! 	public static java.sql.Time timeFromString (String s) throws SQLException
! 	{
! 		int timezone = 0;
! 		long millis = 0;
! 		long localoffset = 0;
! 		int timezoneLocation = (s.indexOf('+') == -1) ? s.lastIndexOf("-") : s.indexOf('+');
! 		//if the index of the last '-' or '+' is greater than 0 that means this time has a timezone.
! 		//everything earlier than that position, we treat as the time and parse it as such.
! 		try
! 		{
! 			timezone = (timezoneLocation==-1) ? s.length() : timezoneLocation;	
! 			millis = java.sql.Time.valueOf(s.substring(0,timezone)).getTime();
! 		}
! 		catch (Exception e)
! 		{
! 			throw new PSQLException("postgresql.format.badtime",s, "HH:mm:ss[-tz]");
! 		}
! 		timezone = 0;
! 		if (timezoneLocation != -1 && timezoneLocation+3 == s.length())
! 		{
! 			timezone = Integer.parseInt(s.substring(timezoneLocation+1,s.length()));
! 			localoffset = java.util.Calendar.getInstance().getTimeZone().getOffset(millis);
! 			if (s.charAt(timezoneLocation)=='+')
! 				timezone*=-1;
! 		}
! 		millis = millis + timezone*60*60*1000 + localoffset;
! 		return new java.sql.Time(millis);
! 	}
! 	
! 	public static java.sql.Timestamp timestampFromString (String s) throws SQLException
! 	{
! 		int timezone = 0;
! 		long millis = 0;
! 		long localoffset = 0;
! 		int nanosVal = 0;
! 		int timezoneLocation = (s.indexOf('+') == -1) ? s.lastIndexOf("-") : s.indexOf('+');
! 		int nanospos = s.indexOf(".");
! 		//if there is a '.', that means there are nanos info, and we take the timestamp up to that point
! 		//if not, then we check to see if the last +/- (to indicate a timezone) is greater than 8
! 		//8 is because the shortest date, will have last '-' at position 7. e.g yyyy-x-x
! 		try
! 		{
! 			if (nanospos != -1)
! 				timezone = nanospos;
! 			else if (timezoneLocation > 8)
! 				timezone = timezoneLocation;
! 			else 
! 				timezone = s.length();
! 			millis = java.sql.Timestamp.valueOf(s.substring(0,timezone)).getTime();
! 		}
! 		catch (Exception e)
! 		{
! 			throw new PSQLException("postgresql.format.badtimestamp", s, "yyyy-MM-dd HH:mm:ss[.xxxxxx][-tz]");
! 		}
! 		timezone = 0;
! 		if (nanospos != -1)
! 		{
! 			int tmploc = (timezoneLocation > 8) ? timezoneLocation : s.length();
! 			nanosVal = Integer.parseInt(s.substring(nanospos+1,tmploc));
! 			int diff = 8-((tmploc-1)-(nanospos+1));
! 			for (int i=0;i<diff;i++)
! 				nanosVal*=10;
! 		}
! 		if (timezoneLocation>8 && timezoneLocation+3 == s.length())
! 		{
! 			timezone = Integer.parseInt(s.substring(timezoneLocation+1,s.length()));
! 			localoffset = java.util.Calendar.getInstance().getTimeZone().getOffset(millis);
! 			if (s.charAt(timezoneLocation)=='+')
! 				timezone*=-1;
! 		}
! 		millis = millis + timezone*60*60*1000 + localoffset;
! 		java.sql.Timestamp tmpts = new java.sql.Timestamp(millis);
! 		tmpts.setNanos(nanosVal);
! 		return tmpts;
! 	}
! 			
! 	
  	private static final String PG_TEXT = "text";
  	private static final String PG_INTEGER = "integer";
  	private static final String PG_INT2 = "int2";
*************** public abstract class AbstractJdbc1State
*** 2042,2048 ****
  	private static final String PG_DOUBLE = "double precision";
  	private static final String PG_BOOLEAN = "boolean";
  	private static final String PG_DATE = "date";
! 	private static final String PG_TIME = "time";
  	private static final String PG_TIMESTAMPTZ = "timestamptz";
      private static final String PG_BYTEA = "bytea";
  
--- 2175,2181 ----
  	private static final String PG_DOUBLE = "double precision";
  	private static final String PG_BOOLEAN = "boolean";
  	private static final String PG_DATE = "date";
! 	private static final String PG_TIME = "timetz";
  	private static final String PG_TIMESTAMPTZ = "timestamptz";
      private static final String PG_BYTEA = "bytea";
  
Index: org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v
retrieving revision 1.20
diff -c -p -r1.20 AbstractJdbc2ResultSet.java
*** org/postgresql/jdbc2/AbstractJdbc2ResultSet.java	30 Jun 2003 16:38:30 -0000	1.20
--- org/postgresql/jdbc2/AbstractJdbc2ResultSet.java	8 Jul 2003 17:08:17 -0000
*************** public abstract class AbstractJdbc2Resul
*** 339,345 ****
  		// If I read the specs, this should use cal only if we don't
  		// store the timezone, and if we do, then act just like getDate()?
  		// for now...
! 		return getDate(i);
  	}
  
  
--- 339,351 ----
  		// If I read the specs, this should use cal only if we don't
  		// store the timezone, and if we do, then act just like getDate()?
  		// for now...
! 		String tmpString = getString(i);
! 		java.util.Date tmpDate = org.postgresql.jdbc1.AbstractJdbc1Statement.dateFromString(tmpString);
! 		if (cal == null || cal.getTimeZone().getRawOffset() == java.util.Calendar.getInstance().getTimeZone().getRawOffset())
! 			return (java.sql.Date)tmpDate;
! 		cal = toLocalTime(tmpDate, cal);
! 		return new java.sql.Date(cal.getTime().getTime());
! 
  	}
  
  
*************** public abstract class AbstractJdbc2Resul
*** 348,354 ****
  		// If I read the specs, this should use cal only if we don't
  		// store the timezone, and if we do, then act just like getTime()?
  		// for now...
! 		return getTime(i);
  	}
  
  
--- 354,365 ----
  		// If I read the specs, this should use cal only if we don't
  		// store the timezone, and if we do, then act just like getTime()?
  		// for now...
! 		String tmpString = getString(i);
! 		java.util.Date tmpTime = org.postgresql.jdbc1.AbstractJdbc1Statement.timeFromString(tmpString);
! 		if (cal==null || cal.getTimeZone().getRawOffset() == java.util.Calendar.getInstance().getTimeZone().getRawOffset())
! 			return (java.sql.Time)tmpTime;
! 		cal = toLocalTime(tmpTime, cal);
! 		return new java.sql.Time(cal.getTime().getTime());
  	}
  
  
*************** public abstract class AbstractJdbc2Resul
*** 357,363 ****
  		// If I read the specs, this should use cal only if we don't
  		// store the timezone, and if we do, then act just like getDate()?
  		// for now...
! 		return getTimestamp(i);
  	}
  
  
--- 368,379 ----
  		// If I read the specs, this should use cal only if we don't
  		// store the timezone, and if we do, then act just like getDate()?
  		// for now...
! 		String tmpString = getString(i);
! 		java.util.Date tmpTimets = org.postgresql.jdbc1.AbstractJdbc1Statement.timestampFromString(tmpString);
! 		if (cal==null || cal.getTimeZone().getRawOffset() == java.util.Calendar.getInstance().getTimeZone().getRawOffset())
! 			return (java.sql.Timestamp)tmpTimets;
! 		cal = toLocalTime(tmpTimets, cal);
! 		return new java.sql.Timestamp(cal.getTime().getTime());
  	}
  
  
*************** public abstract class AbstractJdbc2Resul
*** 1528,1533 ****
--- 1544,1561 ----
  			updateNull(columnIndex);
  		else
  			updateValues.put(fields[columnIndex - 1].getName(), value);
+ 	}
+ 
+ 	public static java.util.Calendar toLocalTime(java.util.Date d, java.util.Calendar cal)
+ 	{
+ 		long millis = d.getTime();
+ 		cal.setTime(d);
+ 		java.util.Calendar localCal = java.util.Calendar.getInstance();
+ 		int localoffset = localCal.getTimeZone().getOffset(millis);
+ 		int caloffset = cal.getTimeZone().getOffset(millis);
+ 		cal.add(java.util.Calendar.SECOND,(localoffset-caloffset)/1000);
+ 		return cal;
+ 		
  	}
  
  	private class PrimaryKey
Index: org/postgresql/jdbc2/AbstractJdbc2Statement.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v
retrieving revision 1.15
diff -c -p -r1.15 AbstractJdbc2Statement.java
*** org/postgresql/jdbc2/AbstractJdbc2Statement.java	30 Jun 2003 16:38:30 -0000	1.15
--- org/postgresql/jdbc2/AbstractJdbc2Statement.java	8 Jul 2003 17:08:18 -0000
*************** public abstract class AbstractJdbc2State
*** 342,348 ****
  			setDate(i, d);
  		else
  		{
! 			cal.setTime(d);
  			setDate(i, new java.sql.Date(cal.getTime().getTime()));
  		}
  	}
--- 342,348 ----
  			setDate(i, d);
  		else
  		{
! 			cal = changeTime(d, cal);		
  			setDate(i, new java.sql.Date(cal.getTime().getTime()));
  		}
  	}
*************** public abstract class AbstractJdbc2State
*** 353,359 ****
  			setTime(i, t);
  		else
  		{
! 			cal.setTime(t);
  			setTime(i, new java.sql.Time(cal.getTime().getTime()));
  		}
  	}
--- 353,359 ----
  			setTime(i, t);
  		else
  		{
! 			cal = changeTime(t, cal);
  			setTime(i, new java.sql.Time(cal.getTime().getTime()));
  		}
  	}
*************** public abstract class AbstractJdbc2State
*** 364,370 ****
  			setTimestamp(i, t);
  		else
  		{
! 			cal.setTime(t);
  			setTimestamp(i, new java.sql.Timestamp(cal.getTime().getTime()));
  		}
  	}
--- 364,370 ----
  			setTimestamp(i, t);
  		else
  		{
! 			cal = changeTime(t, cal);
  			setTimestamp(i, new java.sql.Timestamp(cal.getTime().getTime()));
  		}
  	}
*************** public abstract class AbstractJdbc2State
*** 428,433 ****
--- 428,444 ----
  	protected String[] getSqlFragments()
  	{
  		return m_sqlFragments;
+ 	}
+ 	
+ 	private java.util.Calendar changeTime(java.util.Date t, java.util.Calendar cal)
+ 	{
+ 		java.util.Calendar local = java.util.Calendar.getInstance();
+ 		long millis = t.getTime();
+ 		int localoffset = local.getTimeZone().getOffset(millis);
+ 		int caloffset = cal.getTimeZone().getOffset(millis);
+ 		cal.setTime(t);
+ 		cal.add(java.util.Calendar.SECOND,(caloffset-localoffset)/1000);
+ 		return cal;
  	}
  
  }
