Skip to content

Commit

Permalink
fix: use 00:00:00 and 24:00:00 for LocalTime.MIN/MAX (#992)
Browse files Browse the repository at this point in the history
'infinity'::time does not work, so 00:00:00 and 24:00:00 are used.

# select '24:00:01'::time;
ERROR:  date/time field value out of range: "24:00:01"
LINE 1: select '24:00:01'::time;

fixes #991
  • Loading branch information
davecramer authored and vlsi committed Oct 24, 2017
1 parent c759a58 commit f2d8ec5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
8 changes: 7 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Expand Up @@ -529,7 +529,13 @@ public Time getTime(int i, java.util.Calendar cal) throws SQLException {
}

String string = getString(i);
return connection.getTimestampUtils().toTime(cal, string);
if (string.equals("24:00:00")) {
return Time.valueOf(string);
} else if (string.equals("00:00:00")) {
return new Time(0);
} else {
return connection.getTimestampUtils().toTime(cal, string);
}
}

//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
Expand Down
14 changes: 9 additions & 5 deletions pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
Expand Up @@ -405,6 +405,11 @@ public LocalTime toLocalTime(String s) throws SQLException {
if (s == null) {
return null;
}

if (s.equals("24:00:00")) {
return LocalTime.MAX;
}

try {
return LocalTime.parse(s);
} catch (DateTimeParseException nfe) {
Expand Down Expand Up @@ -692,14 +697,13 @@ public synchronized String toString(LocalDate localDate) {
}

public synchronized String toString(LocalTime localTime) {
if (LocalTime.MAX.equals(localTime)) {
return "infinity";
} else if (LocalTime.MIN.equals(localTime)) {
return "-infinity";
}

sbuf.setLength(0);

if (localTime.equals( LocalTime.MAX )) {
return "24:00:00";
}

appendTime(sbuf, localTime);

return sbuf.toString();
Expand Down
Expand Up @@ -8,11 +8,14 @@
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;

import org.junit.Assert;
import org.junit.Test;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalTime;


public class PreparedStatementTest extends BaseTest4 {
Expand All @@ -22,12 +25,14 @@ public void setUp() throws Exception {
super.setUp();
TestUtil.createTable(con, "timestamptztable", "tstz timestamptz");
TestUtil.createTable(con, "timetztable", "ttz timetz");
TestUtil.createTable(con, "timetable", "id serial, tt time");
}

@Override
public void tearDown() throws SQLException {
TestUtil.dropTable(con, "timestamptztable");
TestUtil.dropTable(con, "timetztable");
TestUtil.dropTable(con, "timetable");
super.tearDown();
}

Expand Down Expand Up @@ -60,4 +65,30 @@ public void testTimeTzSetNull() throws SQLException {

pstmt.close();
}

@Test
public void testLocalTimeMax() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("INSERT INTO timetable (tt) VALUES (?)");

pstmt.setObject(1, LocalTime.MAX);
pstmt.executeUpdate();

pstmt.setObject(1, LocalTime.MIN);
pstmt.executeUpdate();

ResultSet rs = con.createStatement().executeQuery("select tt from timetable order by id asc");
Assert.assertTrue(rs.next());

LocalTime localTime = (LocalTime)rs.getObject(1,LocalTime.class);


Assert.assertEquals( LocalTime.MAX, localTime);

Assert.assertTrue(rs.next());

localTime = (LocalTime)rs.getObject(1, LocalTime.class);

Assert.assertEquals( LocalTime.MIN, localTime);

}
}

0 comments on commit f2d8ec5

Please sign in to comment.