Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/org/duckdb/DuckDBResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.*;

public class DuckDBResultSet implements ResultSet {
private final DuckDBPreparedStatement stmt;
Expand Down Expand Up @@ -372,7 +368,7 @@ public Date getDate(int columnIndex) throws SQLException {
}

public Time getTime(int columnIndex) throws SQLException {
return check_and_null(columnIndex) ? null : current_chunk[columnIndex - 1].getTime(chunk_idx - 1);
return getTime(columnIndex, null);
}

public Timestamp getTimestamp(int columnIndex) throws SQLException {
Expand Down Expand Up @@ -911,7 +907,10 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
}

public Time getTime(int columnIndex, Calendar cal) throws SQLException {
return getTime(columnIndex);
if (check_and_null(columnIndex)) {
return null;
}
return current_chunk[columnIndex - 1].getTime(chunk_idx - 1, cal);
}

public Time getTime(String columnLabel, Calendar cal) throws SQLException {
Expand Down
76 changes: 40 additions & 36 deletions src/main/java/org/duckdb/DuckDBTimestamp.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.duckdb;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.*;
import java.time.temporal.ChronoUnit;

public class DuckDBTimestamp {
Expand Down Expand Up @@ -38,18 +33,47 @@ public DuckDBTimestamp(Timestamp sqlTimestamp) {
final static LocalDateTime RefLocalDateTime;
protected long timeMicros;

public static Timestamp toSqlTimestamp(long timeMicros) {
return Timestamp.valueOf(
LocalDateTime.ofEpochSecond(micros2seconds(timeMicros), nanosPartMicros(timeMicros), ZoneOffset.UTC));
private static Instant createInstant(long value, ChronoUnit unit) throws SQLException {
switch (unit) {
case SECONDS:
return Instant.ofEpochSecond(value);
case MILLIS:
return Instant.ofEpochMilli(value);
case MICROS: {
long epochSecond = value / 1_000_000;
int nanoAdjustment = nanosPartMicros(value);
return Instant.ofEpochSecond(epochSecond, nanoAdjustment);
}
case NANOS: {
long epochSecond = value / 1_000_000_000;
long nanoAdjustment = nanosPartNanos(value);
return Instant.ofEpochSecond(epochSecond, nanoAdjustment);
}
default:
throw new SQLException("Unsupported unit type: [" + unit + "]");
}
}

public static Timestamp toSqlTimestampNanos(long timeNanos) {
return Timestamp.valueOf(
LocalDateTime.ofEpochSecond(nanos2seconds(timeNanos), nanosPartNanos(timeNanos), ZoneOffset.UTC));
public static LocalDateTime localDateTimeFromTimestampWithTimezone(long value, ChronoUnit unit,
ZoneId zoneIdNullable) throws SQLException {
Instant instant = createInstant(value, unit);
ZoneId zoneId = zoneIdNullable != null ? zoneIdNullable : ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zoneId);
}

public static LocalDateTime toLocalDateTime(long timeMicros) {
return LocalDateTime.ofEpochSecond(micros2seconds(timeMicros), nanosPartMicros(timeMicros), ZoneOffset.UTC);
public static LocalDateTime localDateTimeFromTimestamp(long value, ChronoUnit unit, ZoneId zoneIdNullable)
throws SQLException {
Instant instant = createInstant(value, unit);
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
if (null == zoneIdNullable) {
return ldt;
}
ZoneId zoneIdDefault = ZoneId.systemDefault();
LocalDateTime ldtDefault = LocalDateTime.ofInstant(instant, zoneIdDefault);
LocalDateTime ldtZoned = LocalDateTime.ofInstant(instant, zoneIdNullable);
Duration duration = Duration.between(ldtZoned, ldtDefault);
LocalDateTime ldtAdjusted = ldt.plus(duration);
return ldtAdjusted;
}

public static OffsetTime toOffsetTime(long timeBits) {
Expand Down Expand Up @@ -78,26 +102,6 @@ private static LocalTime toLocalTime(long timeMicros) {
return LocalTime.ofNanoOfDay(timeMicros * 1000);
}

public static OffsetDateTime toOffsetDateTime(long timeMicros) {
return OffsetDateTime.of(toLocalDateTime(timeMicros), ZoneOffset.UTC);
}

public static Timestamp fromSecondInstant(long seconds) {
return fromMilliInstant(seconds * 1_000);
}

public static Timestamp fromMilliInstant(long millis) {
return new Timestamp(millis);
}

public static Timestamp fromMicroInstant(long micros) {
return Timestamp.from(Instant.ofEpochSecond(micros / 1_000_000, nanosPartMicros(micros)));
}

public static Timestamp fromNanoInstant(long nanos) {
return Timestamp.from(Instant.ofEpochSecond(nanos / 1_000_000_000, nanosPartNanos(nanos)));
}

public static long localDateTime2Micros(LocalDateTime localDateTime) {
return DuckDBTimestamp.RefLocalDateTime.until(localDateTime, ChronoUnit.MICROS);
}
Expand Down Expand Up @@ -130,7 +134,7 @@ public LocalDateTime toLocalDateTime() {
}

public OffsetDateTime toOffsetDateTime() {
return OffsetDateTime.of(toLocalDateTime(this.timeMicros), ZoneOffset.UTC);
return OffsetDateTime.of(toLocalDateTime(), ZoneOffset.UTC);
}

public static long getMicroseconds(Timestamp sqlTimestamp) {
Expand Down
Loading
Loading