From 8f83590aa18ed5416b094225fe46b1e0611569e3 Mon Sep 17 00:00:00 2001
From: Dave Cramer <davec@fastcrypt.com>
Date: Tue, 5 Mar 2002 03:29:30 +0000
Subject: [PATCH] Patch from Ryouichi Matsuda  An attached patch corrects
 problem of this bug and fractional second.

 The handling of time zone was as follows:

   (a) with time zone
       using  SimpleDateFormat("yyyy-MM-dd HH:mm:ss z")
   (b) without time zone
       using  SimpleDateFormat("yyyy-MM-dd HH:mm:ss")


 About problem of fractional second,
 Fractional second was changed from milli-second to nano-second
---
 .../jdbc/org/postgresql/jdbc1/ResultSet.java  | 41 +++++++++++-------
 .../jdbc/org/postgresql/jdbc2/ResultSet.java  | 43 +++++++++++--------
 2 files changed, 51 insertions(+), 33 deletions(-)

diff --git a/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
index c5cf5619bb1..949b919541e 100644
--- a/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
+++ b/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
@@ -514,7 +514,9 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 		StringBuffer sbuf = new StringBuffer(s);
 		SimpleDateFormat df = null;
 
-		if (s.length() > 19)
+		int slen = s.length();
+
+		if (slen > 19)
 		{
 			// The len of the ISO string to the second value is 19 chars. If
 			// greater then 19, there should be tz info and perhaps fractional
@@ -534,7 +536,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 					if (i < 24)
 						sbuf.append(c);
 					c = s.charAt(i++);
-				} while (Character.isDigit(c));
+				} while (i < slen && Character.isDigit(c));
 
 				// If there wasn't at least 3 digits we should add some zeros
 				// to make up the 3 digits we tell java to expect.
@@ -547,21 +549,28 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 				sbuf.append(".000");
 			}
 
-			// prepend the GMT part and then add the remaining bit of
-			// the string.
-			sbuf.append(" GMT");
-			sbuf.append(c);
-			sbuf.append(s.substring(i, s.length()));
-
-			// Lastly, if the tz part doesn't specify the :MM part then
-			// we add ":00" for java.
-			if (s.length() - i < 5)
-				sbuf.append(":00");
-
-			// we'll use this dateformat string to parse the result.
-			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+			if (i < slen)
+			{
+				// prepend the GMT part and then add the remaining bit of
+				// the string.
+				sbuf.append(" GMT");
+				sbuf.append(c);
+				sbuf.append(s.substring(i, slen));
+
+				// Lastly, if the tz part doesn't specify the :MM part then
+				// we add ":00" for java.
+				if (slen - i < 5)
+					sbuf.append(":00");
+
+				// we'll use this dateformat string to parse the result.
+				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+			}
+			else
+			{
+				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+			}
 		}
-		else if (s.length() == 19)
+		else if (slen == 19)
 		{
 			// No tz or fractional second info. 
 			// I'm not sure if it is
diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
index 58773b819ba..a983284e5cc 100644
--- a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
+++ b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
@@ -1630,11 +1630,12 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 
 			// Copy s into sbuf for parsing.
 			resultSet.sbuf.append(s);
+			int slen = s.length();
 
-			if (s.length() > 19)
+			if (slen > 19)
 			{
 				// The len of the ISO string to the second value is 19 chars. If
-				// greater then 19, there should be tz info and perhaps fractional
+				// greater then 19, there may be tz info and perhaps fractional
 				// second info which we need to change to java to read it.
 
 				// cut the copy to second value "2001-12-07 16:29:22"
@@ -1651,7 +1652,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 						if (i < 24)
 							resultSet.sbuf.append(c);
 						c = s.charAt(i++);
-					} while (Character.isDigit(c));
+					} while (i < slen && Character.isDigit(c));
 
 					// If there wasn't at least 3 digits we should add some zeros
 					// to make up the 3 digits we tell java to expect.
@@ -1664,21 +1665,29 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 					resultSet.sbuf.append(".000");
 				}
 
-				// prepend the GMT part and then add the remaining bit of
-				// the string.
-				resultSet.sbuf.append(" GMT");
-				resultSet.sbuf.append(c);
-				resultSet.sbuf.append(s.substring(i, s.length()));
-
-				// Lastly, if the tz part doesn't specify the :MM part then
-				// we add ":00" for java.
-				if (s.length() - i < 5)
-					resultSet.sbuf.append(":00");
-
-				// we'll use this dateformat string to parse the result.
-				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+				if (i < slen)
+				{
+					// prepend the GMT part and then add the remaining bit of
+					// the string.
+					resultSet.sbuf.append(" GMT");
+					resultSet.sbuf.append(c);
+					resultSet.sbuf.append(s.substring(i, slen));
+
+					// Lastly, if the tz part doesn't specify the :MM part then
+					// we add ":00" for java.
+					if (slen - i < 5)
+						resultSet.sbuf.append(":00");
+
+					// we'll use this dateformat string to parse the result.
+					df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+				}
+				else
+				{
+					// Just found fractional seconds but no timezone.
+					df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+				}
 			}
-			else if (s.length() == 19)
+			else if (slen == 19)
 			{
 				// No tz or fractional second info. 
 				// I'm not sure if it is
-- 
GitLab