Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0651a57

Browse files
committed
Backed out:
--------------------------------------------------------------------------- Attached is a set of patches for a couple of bugs dealing with timestamps in JDBC. Bug#1) Incorrect timestamp stored in DB if client timezone different than DB.
1 parent 526427f commit 0651a57

File tree

4 files changed

+57
-117
lines changed

4 files changed

+57
-117
lines changed

src/interfaces/jdbc/org/postgresql/jdbc1/PreparedStatement.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,12 @@ public void setBytes(int parameterIndex, byte x[]) throws SQLException
310310
* @param x the parameter value
311311
* @exception SQLException if a database access error occurs
312312
*/
313-
private static final SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd");
314313
public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
315314
{
316-
set(parameterIndex, DF1.format(x));
317-
315+
SimpleDateFormat df = new SimpleDateFormat("''yyyy-MM-dd''");
316+
317+
set(parameterIndex, df.format(x));
318+
318319
// The above is how the date should be handled.
319320
//
320321
// However, in JDK's prior to 1.1.6 (confirmed with the
@@ -348,17 +349,9 @@ public void setTime(int parameterIndex, Time x) throws SQLException
348349
* @param x the parameter value
349350
* @exception SQLException if a database access error occurs
350351
*/
351-
private static SimpleDateFormat DF2 = getDF2();
352-
private static SimpleDateFormat getDF2() {
353-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
354-
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
355-
return sdf;
356-
}
357352
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
358-
{
359-
StringBuffer strBuf = new StringBuffer("'");
360-
strBuf.append(DF2.format(x)).append('.').append(x.getNanos()/10000000).append("+00'");
361-
set(parameterIndex, strBuf.toString());
353+
{
354+
set(parameterIndex, "'" + x.toString() + "'");
362355
}
363356

364357
/**

src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,9 @@ public java.sql.Date getDate(int columnIndex) throws SQLException
412412
String s = getString(columnIndex);
413413
if(s==null)
414414
return null;
415+
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
415416
try {
416-
return new java.sql.Date(DF5.parse(s).getTime());
417+
return new java.sql.Date(df.parse(s).getTime());
417418
} catch (ParseException e) {
418419
throw new PSQLException("postgresql.res.baddate",new Integer(e.getErrorOffset()),s);
419420
}
@@ -456,59 +457,30 @@ public Time getTime(int columnIndex) throws SQLException
456457
* @return the column value; null if SQL NULL
457458
* @exception SQLException if a database access error occurs
458459
*/
459-
private static final SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSzzzzzzzzz");
460-
private static final SimpleDateFormat DF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzzzzzzzz");
461-
private static final SimpleDateFormat DF3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
462-
private static final SimpleDateFormat DF4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
463-
private static final SimpleDateFormat DF5 = new SimpleDateFormat("yyyy-MM-dd");
464460
public Timestamp getTimestamp(int columnIndex) throws SQLException
465461
{
466462
String s = getString(columnIndex);
467463
if(s==null)
468464
return null;
469-
470-
boolean subsecond;
471-
//if string contains a '.' we have fractional seconds
472-
if (s.indexOf('.') == -1) {
473-
subsecond = false;
474-
} else {
475-
subsecond = true;
476-
}
477-
478-
//here we are modifying the string from ISO format to a format java can understand
479-
//java expects timezone info as 'GMT-08:00' instead of '-08' in postgres ISO format
480-
//and java expects three digits if fractional seconds are present instead of two for postgres
481-
//so this code strips off timezone info and adds on the GMT+/-...
482-
//as well as adds a third digit for partial seconds if necessary
483-
StringBuffer strBuf = new StringBuffer(s);
484-
char sub = strBuf.charAt(strBuf.length()-3);
485-
if (sub == '+' || sub == '-') {
486-
strBuf.setLength(strBuf.length()-3);
487-
if (subsecond) {
488-
strBuf = strBuf.append('0').append("GMT").append(s.substring(s.length()-3, s.length())).append(":00");
489-
} else {
490-
strBuf = strBuf.append("GMT").append(s.substring(s.length()-3, s.length())).append(":00");
491-
}
492-
} else if (subsecond) {
493-
strBuf = strBuf.append('0');
494-
}
495-
496-
s = strBuf.toString();
497-
465+
466+
// This works, but it's commented out because Michael Stephenson's
467+
// solution is better still:
468+
//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
469+
470+
// Michael Stephenson's solution:
498471
SimpleDateFormat df = null;
499-
500-
if (s.length()>23 && subsecond) {
501-
df = DF1;
502-
} else if (s.length()>23 && !subsecond) {
503-
df = DF2;
504-
} else if (s.length()>10 && subsecond) {
505-
df = DF3;
506-
} else if (s.length()>10 && !subsecond) {
507-
df = DF4;
472+
if (s.length()>21 && s.indexOf('.') != -1) {
473+
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSzzz");
474+
} else if (s.length()>19 && s.indexOf('.') == -1) {
475+
df = new SimpleDateFormat("yyyy-MM-dd HH:MM:sszzz");
476+
} else if (s.length()>19 && s.indexOf('.') != -1) {
477+
df = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss.SS");
478+
} else if (s.length()>10 && s.length()<=18) {
479+
df = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
508480
} else {
509-
df = DF5;
481+
df = new SimpleDateFormat("yyyy-MM-dd");
510482
}
511-
483+
512484
try {
513485
return new Timestamp(df.parse(s).getTime());
514486
} catch(ParseException e) {

src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,12 @@ public void setBytes(int parameterIndex, byte x[]) throws SQLException
310310
* @param x the parameter value
311311
* @exception SQLException if a database access error occurs
312312
*/
313-
private static final SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd");
314313
public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
315314
{
316-
set(parameterIndex, DF1.format(x));
317-
315+
SimpleDateFormat df = new SimpleDateFormat("''yyyy-MM-dd''");
316+
317+
set(parameterIndex, df.format(x));
318+
318319
// The above is how the date should be handled.
319320
//
320321
// However, in JDK's prior to 1.1.6 (confirmed with the
@@ -348,17 +349,9 @@ public void setTime(int parameterIndex, Time x) throws SQLException
348349
* @param x the parameter value
349350
* @exception SQLException if a database access error occurs
350351
*/
351-
private static SimpleDateFormat DF2 = getDF2();
352-
private static SimpleDateFormat getDF2() {
353-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
354-
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
355-
return sdf;
356-
}
357352
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
358-
{
359-
StringBuffer strBuf = new StringBuffer("'");
360-
strBuf.append(DF2.format(x)).append('.').append(x.getNanos()/10000000).append("+00'");
361-
set(parameterIndex, strBuf.toString());
353+
{
354+
set(parameterIndex, "'" + x.toString() + "'");
362355
}
363356

364357
/**

src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,9 @@ public java.sql.Date getDate(int columnIndex) throws SQLException
415415
String s = getString(columnIndex);
416416
if(s==null)
417417
return null;
418+
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
418419
try {
419-
return new java.sql.Date(DF5.parse(s).getTime());
420+
return new java.sql.Date(df.parse(s).getTime());
420421
} catch (ParseException e) {
421422
throw new PSQLException("postgresql.res.baddate",new Integer(e.getErrorOffset()),s);
422423
}
@@ -459,66 +460,47 @@ public Time getTime(int columnIndex) throws SQLException
459460
* @return the column value; null if SQL NULL
460461
* @exception SQLException if a database access error occurs
461462
*/
462-
private static final SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSzzzzzzzzz");
463-
private static final SimpleDateFormat DF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzzzzzzzz");
464-
private static final SimpleDateFormat DF3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
465-
private static final SimpleDateFormat DF4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
466-
private static final SimpleDateFormat DF5 = new SimpleDateFormat("yyyy-MM-dd");
467463
public Timestamp getTimestamp(int columnIndex) throws SQLException
468464
{
469465
String s = getString(columnIndex);
470466
if(s==null)
471467
return null;
472-
473-
boolean subsecond;
474-
//if string contains a '.' we have fractional seconds
475-
if (s.indexOf('.') == -1) {
476-
subsecond = false;
477-
} else {
478-
subsecond = true;
479-
}
480-
481-
//here we are modifying the string from ISO format to a format java can understand
482-
//java expects timezone info as 'GMT-08:00' instead of '-08' in postgres ISO format
483-
//and java expects three digits if fractional seconds are present instead of two for postgres
484-
//so this code strips off timezone info and adds on the GMT+/-...
485-
//as well as adds a third digit for partial seconds if necessary
486-
StringBuffer strBuf = new StringBuffer(s);
487-
char sub = strBuf.charAt(strBuf.length()-3);
488-
if (sub == '+' || sub == '-') {
489-
strBuf.setLength(strBuf.length()-3);
490-
if (subsecond) {
491-
strBuf = strBuf.append('0').append("GMT").append(s.substring(s.length()-3, s.length())).append(":00");
492-
} else {
493-
strBuf = strBuf.append("GMT").append(s.substring(s.length()-3, s.length())).append(":00");
494-
}
495-
} else if (subsecond) {
496-
strBuf = strBuf.append('0');
468+
469+
// This works, but it's commented out because Michael Stephenson's
470+
// solution is better still:
471+
//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
472+
// Modification by Jan Thomae
473+
String sub = s.substring(s.length() - 3, s.length()-2);
474+
if (sub.equals("+") || sub.equals("-")) {
475+
s = s.substring(0, s.length()-3) + "GMT"+ s.substring(s.length()-3, s.length())+":00";
497476
}
498-
499-
s = strBuf.toString();
500-
477+
// -------
478+
// Michael Stephenson's solution:
501479
SimpleDateFormat df = null;
502480

503-
if (s.length()>23 && subsecond) {
504-
df = DF1;
505-
} else if (s.length()>23 && !subsecond) {
506-
df = DF2;
507-
} else if (s.length()>10 && subsecond) {
508-
df = DF3;
509-
} else if (s.length()>10 && !subsecond) {
510-
df = DF4;
481+
// Modification by Jan Thomae
482+
if (s.length()>27) {
483+
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzzzzzzzz");
484+
} else
485+
// -------
486+
if (s.length()>21 && s.indexOf('.') != -1) {
487+
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSzzz");
488+
} else if (s.length()>19 && s.indexOf('.') == -1) {
489+
df = new SimpleDateFormat("yyyy-MM-dd HH:MM:sszzz");
490+
} else if (s.length()>19 && s.indexOf('.') != -1) {
491+
df = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss.SS");
492+
} else if (s.length()>10 && s.length()<=18) {
493+
df = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
511494
} else {
512-
df = DF5;
495+
df = new SimpleDateFormat("yyyy-MM-dd");
513496
}
514-
497+
515498
try {
516499
return new Timestamp(df.parse(s).getTime());
517500
} catch(ParseException e) {
518501
throw new PSQLException("postgresql.res.badtimestamp",new Integer(e.getErrorOffset()),s);
519502
}
520503
}
521-
522504

523505
/**
524506
* A column value can be retrieved as a stream of ASCII characters

0 commit comments

Comments
 (0)