Skip to content

Commit

Permalink
Fix ResultSet#getDate for dates before 1582
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jun 12, 2020
1 parent 2f0fe3e commit 1949277
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -296,7 +298,10 @@ private Date getDate(int columnIndex, DateTimeZone localTimeZone)
}

try {
return new Date(DATE_FORMATTER.withZone(localTimeZone).parseMillis(String.valueOf(value)));
long millis = DATE_FORMATTER.withZone(localTimeZone).parseMillis(String.valueOf(value));
return Date.valueOf(Instant.ofEpochMilli(millis)
.atZone(ZoneOffset.UTC)
.toLocalDate());
}
catch (IllegalArgumentException e) {
throw new SQLException("Invalid date from server: " + value, e);
Expand Down
12 changes: 12 additions & 0 deletions presto-jdbc/src/test/java/io/prestosql/jdbc/TestJdbcResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ public void testObjectTypes()
assertThrows(IllegalArgumentException.class, () -> rs.getTimestamp(column));
});

// distant past, but apparently not an uncommon value in practice; on this date Julian and Gregorian calendars should be in sync, but they appear not to be
checkRepresentation("DATE '0001-01-01'", Types.DATE, (rs, column) -> {
assertEquals(rs.getObject(column), Date.valueOf(LocalDate.of(1, 1, 1)));
assertEquals(rs.getDate(column), Date.valueOf(LocalDate.of(1, 1, 1)));
});

// distant past, before Julian-Gregorian calendar "default cut-over", but after 0001-01-01 when Julian and Gregorian calendars are supposed to be in sync
checkRepresentation("DATE '1300-01-01'", Types.DATE, (rs, column) -> {
assertEquals(rs.getObject(column), Date.valueOf(LocalDate.of(1300, 1, 1)));
assertEquals(rs.getDate(column), Date.valueOf(LocalDate.of(1300, 1, 1)));
});

checkRepresentation("TIME '09:39:05'", Types.TIME, (rs, column) -> {
assertEquals(rs.getObject(column), Time.valueOf(LocalTime.of(9, 39, 5)));
assertThrows(() -> rs.getDate(column));
Expand Down

0 comments on commit 1949277

Please sign in to comment.