Skip to content

Commit

Permalink
fix: trim trailing zeros in timestamp strings returned in binary mode (
Browse files Browse the repository at this point in the history
…#896)

This commit trims trailing zeros from the fractional seconds in timestamp strings returned under binary mode, where the fractional seconds used to be zero padded to 6 digits. This provides consistency in the returned timestamp strings under both forced and regular binary modes.

fixes #885
  • Loading branch information
michaelzg authored and vlsi committed Oct 25, 2017
1 parent 8f5e245 commit d28deff
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
Expand Up @@ -643,6 +643,12 @@ private static void appendTime(StringBuilder sb, int hours, int minutes, int sec
if (needZeros > 0) {
sb.insert(len, ZEROS, 0, needZeros);
}

int end = sb.length() - 1;
while (sb.charAt(end) == '0') {
sb.deleteCharAt(end);
end--;
}
}

private void appendTimeZone(StringBuilder sb, java.util.Calendar cal) {
Expand Down
83 changes: 81 additions & 2 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/TimestampTest.java
Expand Up @@ -311,6 +311,8 @@ public void testSetTimestampWTZ() throws SQLException {
@Test
public void testGetTimestampWOTZ() throws SQLException {
assumeTrue(TestUtil.haveIntegerDateTimes(con));
//Refer to #896
assumeMinimumServerVersion(ServerVersion.v8_4);

Statement stmt = con.createStatement();
TimestampUtils tsu = ((BaseConnection) con).getTimestampUtils();
Expand All @@ -328,6 +330,9 @@ public void testGetTimestampWOTZ() throws SQLException {
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS5WOTZ_PGFORMAT + "'")));
assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS6WOTZ_PGFORMAT + "'")));
assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS7WOTZ_PGFORMAT + "'")));

assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS1WOTZ_PGFORMAT + "'")));
assertEquals(1,
Expand All @@ -340,6 +345,9 @@ public void testGetTimestampWOTZ() throws SQLException {
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS5WOTZ_PGFORMAT + "'")));
assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS6WOTZ_PGFORMAT + "'")));
assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS7WOTZ_PGFORMAT + "'")));

assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS1WOTZ_PGFORMAT + "'")));
assertEquals(1,
Expand All @@ -352,6 +360,9 @@ public void testGetTimestampWOTZ() throws SQLException {
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS5WOTZ_PGFORMAT + "'")));
assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS6WOTZ_PGFORMAT + "'")));
assertEquals(1,
stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS7WOTZ_PGFORMAT + "'")));

assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
"'" + tsu.toString(null, new java.sql.Timestamp(tmpDate1WOTZ.getTime())) + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
Expand All @@ -364,6 +375,9 @@ public void testGetTimestampWOTZ() throws SQLException {
"'" + tsu.toString(null, new java.sql.Timestamp(tmpDate5WOTZ.getTime())) + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
"'" + tsu.toString(null, new java.sql.Timestamp(tmpDate6WOTZ.getTime())) + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
"'" + tsu.toString(null, new java.sql.Timestamp(tmpDate7WOTZ.getTime())) + "'")));

assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
"'" + tsu.toString(null, new java.sql.Timestamp(tmpTime1WOTZ.getTime())) + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
Expand All @@ -376,11 +390,13 @@ public void testGetTimestampWOTZ() throws SQLException {
"'" + tsu.toString(null, new java.sql.Timestamp(tmpTime5WOTZ.getTime())) + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
"'" + tsu.toString(null, new java.sql.Timestamp(tmpTime6WOTZ.getTime())) + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,
"'" + tsu.toString(null, new java.sql.Timestamp(tmpTime7WOTZ.getTime())) + "'")));

// Fall through helper
timestampTestWOTZ();

assertEquals(30, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));
assertEquals(35, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));

stmt.close();
}
Expand All @@ -393,6 +409,8 @@ public void testGetTimestampWOTZ() throws SQLException {
@Test
public void testSetTimestampWOTZ() throws SQLException {
assumeTrue(TestUtil.haveIntegerDateTimes(con));
//Refer to #896
assumeMinimumServerVersion(ServerVersion.v8_4);

Statement stmt = con.createStatement();
PreparedStatement pstmt = con.prepareStatement(TestUtil.insertSQL(TSWOTZ_TABLE, "?"));
Expand All @@ -415,6 +433,9 @@ public void testSetTimestampWOTZ() throws SQLException {
pstmt.setTimestamp(1, TS6WOTZ);
assertEquals(1, pstmt.executeUpdate());

pstmt.setTimestamp(1, TS7WOTZ);
assertEquals(1, pstmt.executeUpdate());

// With java.sql.Timestamp
pstmt.setObject(1, TS1WOTZ, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());
Expand All @@ -428,6 +449,8 @@ public void testSetTimestampWOTZ() throws SQLException {
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, TS6WOTZ, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, TS7WOTZ, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());

// With Strings
pstmt.setObject(1, TS1WOTZ_PGFORMAT, Types.TIMESTAMP);
Expand All @@ -442,6 +465,8 @@ public void testSetTimestampWOTZ() throws SQLException {
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, TS6WOTZ_PGFORMAT, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, TS7WOTZ_PGFORMAT, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());

// With java.sql.Date
pstmt.setObject(1, tmpDate1WOTZ, Types.TIMESTAMP);
Expand All @@ -456,6 +481,8 @@ public void testSetTimestampWOTZ() throws SQLException {
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, tmpDate6WOTZ, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, tmpDate7WOTZ, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());

// With java.sql.Time
pstmt.setObject(1, tmpTime1WOTZ, Types.TIMESTAMP);
Expand All @@ -470,10 +497,12 @@ public void testSetTimestampWOTZ() throws SQLException {
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, tmpTime6WOTZ, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());
pstmt.setObject(1, tmpTime7WOTZ, Types.TIMESTAMP);
assertEquals(1, pstmt.executeUpdate());
// Fall through helper
timestampTestWOTZ();

assertEquals(30, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));
assertEquals(35, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));

pstmt.close();
stmt.close();
Expand Down Expand Up @@ -566,6 +595,7 @@ private void timestampTestWTZ() throws SQLException {
private void timestampTestWOTZ() throws SQLException {
Statement stmt = con.createStatement();
java.sql.Timestamp t;
String tString;

ResultSet rs = stmt.executeQuery("select ts from " + TSWOTZ_TABLE); // removed the order by ts
assertNotNull(rs);
Expand All @@ -576,30 +606,63 @@ private void timestampTestWOTZ() throws SQLException {
assertNotNull(t);
assertEquals(TS1WOTZ, t);

tString = rs.getString(1);
assertNotNull(tString);
assertEquals(TS1WOTZ_PGFORMAT, tString);

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(TS2WOTZ, t);

tString = rs.getString(1);
assertNotNull(tString);
assertEquals(TS2WOTZ_PGFORMAT, tString);

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(TS3WOTZ, t);

tString = rs.getString(1);
assertNotNull(tString);
assertEquals(TS3WOTZ_PGFORMAT, tString);

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(TS4WOTZ, t);

tString = rs.getString(1);
assertNotNull(tString);
assertEquals(TS4WOTZ_PGFORMAT, tString);

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(TS5WOTZ, t);

tString = rs.getString(1);
assertNotNull(tString);
assertEquals(TS5WOTZ_PGFORMAT, tString);

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(TS6WOTZ, t);

tString = rs.getString(1);
assertNotNull(tString);
assertEquals(TS6WOTZ_PGFORMAT, tString);

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(TS7WOTZ, t);

tString = rs.getString(1);
assertNotNull(tString);
assertEquals(TS7WOTZ_PGFORMAT, tString);
}

// Testing for Date
Expand Down Expand Up @@ -633,6 +696,11 @@ private void timestampTestWOTZ() throws SQLException {
assertNotNull(t);
assertEquals(tmpDate6WOTZ.getTime(), t.getTime());

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(tmpDate7WOTZ.getTime(), t.getTime());

// Testing for Time
assertTrue(rs.next());
t = rs.getTimestamp(1);
Expand Down Expand Up @@ -664,6 +732,11 @@ private void timestampTestWOTZ() throws SQLException {
assertNotNull(t);
assertEquals(tmpTime6WOTZ.getTime(), t.getTime());

assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertEquals(tmpTime7WOTZ.getTime(), t.getTime());

assertTrue(!rs.next()); // end of table. Fail if more entries exist.

rs.close();
Expand Down Expand Up @@ -739,6 +812,10 @@ private static java.sql.Timestamp getTimestamp(int y, int m, int d, int h, int m
new Timestamp(PGStatement.DATE_POSITIVE_INFINITY);
private static final String TS6WOTZ_PGFORMAT = "infinity";

private static final java.sql.Timestamp TS7WOTZ =
getTimestamp(2000, 7, 7, 15, 0, 0, 0, null);
private static final String TS7WOTZ_PGFORMAT = "2000-07-07 15:00:00";

private static final String TSWTZ_TABLE = "testtimestampwtz";
private static final String TSWOTZ_TABLE = "testtimestampwotz";
private static final String DATE_TABLE = "testtimestampdate";
Expand All @@ -764,6 +841,8 @@ private static java.sql.Timestamp getTimestamp(int y, int m, int d, int h, int m
private static final java.sql.Date tmpTime5WOTZ = new java.sql.Date(TS5WOTZ.getTime());
private static final java.sql.Date tmpDate6WOTZ = new java.sql.Date(TS6WOTZ.getTime());
private static final java.sql.Date tmpTime6WOTZ = new java.sql.Date(TS6WOTZ.getTime());
private static final java.sql.Date tmpDate7WOTZ = new java.sql.Date(TS7WOTZ.getTime());
private static final java.sql.Time tmpTime7WOTZ = new java.sql.Time(TS7WOTZ.getTime());


}

0 comments on commit d28deff

Please sign in to comment.