Permalink
Browse files

fix: trim trailing zeros in timestamp strings returned in binary mode (

…#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 d28deff57684349707d2b2a357048f59b0861bb1
@@ -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) {
@@ -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();
@@ -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,
@@ -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,
@@ -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,
@@ -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,
@@ -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();
}
@@ -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, "?"));
@@ -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());
@@ -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);
@@ -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);
@@ -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);
@@ -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();
@@ -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);
@@ -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
@@ -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);
@@ -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();
@@ -739,6 +812,10 @@ private void timestampTestWOTZ() throws SQLException {
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";
@@ -764,6 +841,8 @@ private void timestampTestWOTZ() throws SQLException {
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.