Skip to content

Commit

Permalink
fix: pgjdbc#1677 NumberFormatException when fetching PGInterval with …
Browse files Browse the repository at this point in the history
…small value
  • Loading branch information
baardsen committed Jan 24, 2020
1 parent 39c73a6 commit fedda9d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
17 changes: 4 additions & 13 deletions pgjdbc/src/main/java/org/postgresql/util/PGInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;

/**
Expand Down Expand Up @@ -367,20 +368,10 @@ public int getMicroSeconds() {
* @param seconds seconds to set
*/
public void setSeconds(double seconds) {
String str = Double.toString(seconds);
String str = String.format(Locale.ROOT,"%.6f",seconds);
int decimal = str.indexOf('.');
if (decimal > 0) {

/* how many 10's do we need to multiply by to get microseconds */
String micSeconds = str.substring(decimal + 1);
int power = 6 - micSeconds.length();

microSeconds = Integer.parseInt(micSeconds) * (int)Math.pow(10,power);
wholeSeconds = Integer.parseInt(str.substring(0,decimal));
} else {
microSeconds = 0;
wholeSeconds = Integer.parseInt(str);
}
microSeconds = Integer.parseInt(str.substring(decimal + 1));
wholeSeconds = Integer.parseInt(str.substring(0,decimal));
if ( seconds < 0 ) {
microSeconds = -microSeconds;
}
Expand Down
23 changes: 23 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/IntervalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,29 @@ public void testISO8601() throws Exception {
assertEquals(-4, pgi.getHours());
}

@Test
public void testSmallValue() throws SQLException {
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO testinterval VALUES (?)");
pstmt.setObject(1, new PGInterval("0.0001 seconds"));
pstmt.executeUpdate();
pstmt.close();

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT v FROM testinterval");
assertTrue(rs.next());
PGInterval pgi = (PGInterval) rs.getObject(1);
assertEquals(0, pgi.getYears());
assertEquals(0, pgi.getMonths());
assertEquals(0, pgi.getDays());
assertEquals(0, pgi.getHours());
assertEquals(0, pgi.getMinutes());
assertEquals(0, pgi.getWholeSeconds());
assertEquals(100, pgi.getMicroSeconds());
assertFalse(rs.next());
rs.close();
stmt.close();
}

private java.sql.Date makeDate(int y, int m, int d) {
return new java.sql.Date(y - 1900, m - 1, d);
}
Expand Down

0 comments on commit fedda9d

Please sign in to comment.