Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0651a57

Browse files
committed
Backed out:
---------------------------------------------------------------------------Attached is a set of patches for a couple of bugs dealing withtimestamps in JDBC.Bug#1) Incorrect timestamp stored in DB if client timezone differentthan DB.
1 parent526427f commit0651a57

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-
privatestaticfinalSimpleDateFormatDF1 =newSimpleDateFormat("yyyy-MM-dd");
314313
publicvoidsetDate(intparameterIndex,java.sql.Datex)throwsSQLException
315314
{
316-
set(parameterIndex,DF1.format(x));
317-
315+
SimpleDateFormatdf =newSimpleDateFormat("''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-
privatestaticSimpleDateFormatDF2 =getDF2();
352-
privatestaticSimpleDateFormatgetDF2() {
353-
SimpleDateFormatsdf =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
354-
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
355-
returnsdf;
356-
}
357352
publicvoidsetTimestamp(intparameterIndex,Timestampx)throwsSQLException
358-
{
359-
StringBufferstrBuf =newStringBuffer("'");
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
Strings =getString(columnIndex);
413413
if(s==null)
414414
returnnull;
415+
SimpleDateFormatdf =newSimpleDateFormat("yyyy-MM-dd");
415416
try {
416-
returnnewjava.sql.Date(DF5.parse(s).getTime());
417+
returnnewjava.sql.Date(df.parse(s).getTime());
417418
}catch (ParseExceptione) {
418419
thrownewPSQLException("postgresql.res.baddate",newInteger(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-
privatestaticfinalSimpleDateFormatDF1 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSzzzzzzzzz");
460-
privatestaticfinalSimpleDateFormatDF2 =newSimpleDateFormat("yyyy-MM-dd HH:mm:sszzzzzzzzz");
461-
privatestaticfinalSimpleDateFormatDF3 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
462-
privatestaticfinalSimpleDateFormatDF4 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
463-
privatestaticfinalSimpleDateFormatDF5 =newSimpleDateFormat("yyyy-MM-dd");
464460
publicTimestampgetTimestamp(intcolumnIndex)throwsSQLException
465461
{
466462
Strings =getString(columnIndex);
467463
if(s==null)
468464
returnnull;
469-
470-
booleansubsecond;
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-
StringBufferstrBuf =newStringBuffer(s);
484-
charsub =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-
}elseif (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
SimpleDateFormatdf =null;
499-
500-
if (s.length()>23 &&subsecond) {
501-
df =DF1;
502-
}elseif (s.length()>23 && !subsecond) {
503-
df =DF2;
504-
}elseif (s.length()>10 &&subsecond) {
505-
df =DF3;
506-
}elseif (s.length()>10 && !subsecond) {
507-
df =DF4;
472+
if (s.length()>21 &&s.indexOf('.') != -1) {
473+
df =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSzzz");
474+
}elseif (s.length()>19 &&s.indexOf('.') == -1) {
475+
df =newSimpleDateFormat("yyyy-MM-dd HH:MM:sszzz");
476+
}elseif (s.length()>19 &&s.indexOf('.') != -1) {
477+
df =newSimpleDateFormat("yyyy-MM-dd HH:MM:ss.SS");
478+
}elseif (s.length()>10 &&s.length()<=18) {
479+
df =newSimpleDateFormat("yyyy-MM-dd HH:MM:ss");
508480
}else {
509-
df =DF5;
481+
df =newSimpleDateFormat("yyyy-MM-dd");
510482
}
511-
483+
512484
try {
513485
returnnewTimestamp(df.parse(s).getTime());
514486
}catch(ParseExceptione) {

‎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-
privatestaticfinalSimpleDateFormatDF1 =newSimpleDateFormat("yyyy-MM-dd");
314313
publicvoidsetDate(intparameterIndex,java.sql.Datex)throwsSQLException
315314
{
316-
set(parameterIndex,DF1.format(x));
317-
315+
SimpleDateFormatdf =newSimpleDateFormat("''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-
privatestaticSimpleDateFormatDF2 =getDF2();
352-
privatestaticSimpleDateFormatgetDF2() {
353-
SimpleDateFormatsdf =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
354-
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
355-
returnsdf;
356-
}
357352
publicvoidsetTimestamp(intparameterIndex,Timestampx)throwsSQLException
358-
{
359-
StringBufferstrBuf =newStringBuffer("'");
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
Strings =getString(columnIndex);
416416
if(s==null)
417417
returnnull;
418+
SimpleDateFormatdf =newSimpleDateFormat("yyyy-MM-dd");
418419
try {
419-
returnnewjava.sql.Date(DF5.parse(s).getTime());
420+
returnnewjava.sql.Date(df.parse(s).getTime());
420421
}catch (ParseExceptione) {
421422
thrownewPSQLException("postgresql.res.baddate",newInteger(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-
privatestaticfinalSimpleDateFormatDF1 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSzzzzzzzzz");
463-
privatestaticfinalSimpleDateFormatDF2 =newSimpleDateFormat("yyyy-MM-dd HH:mm:sszzzzzzzzz");
464-
privatestaticfinalSimpleDateFormatDF3 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
465-
privatestaticfinalSimpleDateFormatDF4 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
466-
privatestaticfinalSimpleDateFormatDF5 =newSimpleDateFormat("yyyy-MM-dd");
467463
publicTimestampgetTimestamp(intcolumnIndex)throwsSQLException
468464
{
469465
Strings =getString(columnIndex);
470466
if(s==null)
471467
returnnull;
472-
473-
booleansubsecond;
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-
StringBufferstrBuf =newStringBuffer(s);
487-
charsub =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-
}elseif (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+
Stringsub =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
SimpleDateFormatdf =null;
502480

503-
if (s.length()>23 &&subsecond) {
504-
df =DF1;
505-
}elseif (s.length()>23 && !subsecond) {
506-
df =DF2;
507-
}elseif (s.length()>10 &&subsecond) {
508-
df =DF3;
509-
}elseif (s.length()>10 && !subsecond) {
510-
df =DF4;
481+
// Modification by Jan Thomae
482+
if (s.length()>27) {
483+
df =newSimpleDateFormat("yyyy-MM-dd HH:mm:sszzzzzzzzz");
484+
}else
485+
// -------
486+
if (s.length()>21 &&s.indexOf('.') != -1) {
487+
df =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSzzz");
488+
}elseif (s.length()>19 &&s.indexOf('.') == -1) {
489+
df =newSimpleDateFormat("yyyy-MM-dd HH:MM:sszzz");
490+
}elseif (s.length()>19 &&s.indexOf('.') != -1) {
491+
df =newSimpleDateFormat("yyyy-MM-dd HH:MM:ss.SS");
492+
}elseif (s.length()>10 &&s.length()<=18) {
493+
df =newSimpleDateFormat("yyyy-MM-dd HH:MM:ss");
511494
}else {
512-
df =DF5;
495+
df =newSimpleDateFormat("yyyy-MM-dd");
513496
}
514-
497+
515498
try {
516499
returnnewTimestamp(df.parse(s).getTime());
517500
}catch(ParseExceptione) {
518501
thrownewPSQLException("postgresql.res.badtimestamp",newInteger(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)

[8]ページ先頭

©2009-2025 Movatter.jp