Skip to content

Commit 2843a13

Browse files
author
Barry Lind
committed
Applied patch submitted by Ryouichi Matsuda (r-matuda@sra.co.jp) that fixed a problem with leading zeros being lost on fractional seconds when setting a timestamp value on a PreparedStatement.
1 parent d013dbe commit 2843a13

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,19 @@ public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
388388
{
389389
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
390390
df.setTimeZone(TimeZone.getTimeZone("GMT"));
391+
392+
// Make decimal from nanos.
393+
StringBuffer decimal = new StringBuffer("000000000"); // max nanos length
394+
String nanos = String.valueOf(x.getNanos());
395+
decimal.setLength(decimal.length() - nanos.length());
396+
decimal.append(nanos);
397+
if (! connection.haveMinimumServerVersion("7.2")) {
398+
// Because 7.1 include bug that "hh:mm:59.999" becomes "hh:mm:60.00".
399+
decimal.setLength(2);
400+
}
401+
391402
StringBuffer strBuf = new StringBuffer("'");
392-
strBuf.append(df.format(x)).append('.').append(x.getNanos() / 10000000).append("+00'");
403+
strBuf.append(df.format(x)).append('.').append(decimal).append("+00'");
393404
set(parameterIndex, strBuf.toString());
394405
}
395406
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,21 @@ public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
412412
tl_tsdf.set(df);
413413
}
414414

415+
// Make decimal from nanos.
416+
StringBuffer decimal = new StringBuffer("000000000"); // max nanos length
417+
String nanos = String.valueOf(x.getNanos());
418+
decimal.setLength(decimal.length() - nanos.length());
419+
decimal.append(nanos);
420+
if (! connection.haveMinimumServerVersion("7.2")) {
421+
// Because 7.1 include bug that "hh:mm:59.999" becomes "hh:mm:60.00".
422+
decimal.setLength(2);
423+
}
424+
415425
// Use the shared StringBuffer
416426
synchronized (sbuf)
417427
{
418428
sbuf.setLength(0);
419-
sbuf.append("'").append(df.format(x)).append('.').append(x.getNanos() / 10000000).append("+00'");
429+
sbuf.append("'").append(df.format(x)).append('.').append(decimal).append("+00'");
420430
set(parameterIndex, sbuf.toString());
421431
}
422432

0 commit comments

Comments
 (0)