Skip to content

Commit 0086304

Browse files
authored
Merge pull request #166 from staticlibs/result_set_timestamp
Result set Timestamp handling improvements
2 parents fc0e885 + 5fd6d92 commit 0086304

File tree

4 files changed

+342
-182
lines changed

4 files changed

+342
-182
lines changed

src/main/java/org/duckdb/DuckDBResultSet.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@
3131
import java.time.LocalDateTime;
3232
import java.time.OffsetDateTime;
3333
import java.time.OffsetTime;
34-
import java.util.Arrays;
35-
import java.util.Calendar;
36-
import java.util.Map;
37-
import java.util.Objects;
38-
import java.util.UUID;
34+
import java.util.*;
3935

4036
public class DuckDBResultSet implements ResultSet {
4137
private final DuckDBPreparedStatement stmt;
@@ -372,7 +368,7 @@ public Date getDate(int columnIndex) throws SQLException {
372368
}
373369

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

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

913909
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
914-
return getTime(columnIndex);
910+
if (check_and_null(columnIndex)) {
911+
return null;
912+
}
913+
return current_chunk[columnIndex - 1].getTime(chunk_idx - 1, cal);
915914
}
916915

917916
public Time getTime(String columnLabel, Calendar cal) throws SQLException {

src/main/java/org/duckdb/DuckDBTimestamp.java

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package org.duckdb;
22

33
import java.sql.Date;
4+
import java.sql.SQLException;
45
import java.sql.Time;
56
import java.sql.Timestamp;
6-
import java.time.Instant;
7-
import java.time.LocalDate;
8-
import java.time.LocalDateTime;
9-
import java.time.LocalTime;
10-
import java.time.OffsetDateTime;
11-
import java.time.OffsetTime;
12-
import java.time.ZoneOffset;
7+
import java.time.*;
138
import java.time.temporal.ChronoUnit;
149

1510
public class DuckDBTimestamp {
@@ -38,18 +33,47 @@ public DuckDBTimestamp(Timestamp sqlTimestamp) {
3833
final static LocalDateTime RefLocalDateTime;
3934
protected long timeMicros;
4035

41-
public static Timestamp toSqlTimestamp(long timeMicros) {
42-
return Timestamp.valueOf(
43-
LocalDateTime.ofEpochSecond(micros2seconds(timeMicros), nanosPartMicros(timeMicros), ZoneOffset.UTC));
36+
private static Instant createInstant(long value, ChronoUnit unit) throws SQLException {
37+
switch (unit) {
38+
case SECONDS:
39+
return Instant.ofEpochSecond(value);
40+
case MILLIS:
41+
return Instant.ofEpochMilli(value);
42+
case MICROS: {
43+
long epochSecond = value / 1_000_000;
44+
int nanoAdjustment = nanosPartMicros(value);
45+
return Instant.ofEpochSecond(epochSecond, nanoAdjustment);
46+
}
47+
case NANOS: {
48+
long epochSecond = value / 1_000_000_000;
49+
long nanoAdjustment = nanosPartNanos(value);
50+
return Instant.ofEpochSecond(epochSecond, nanoAdjustment);
51+
}
52+
default:
53+
throw new SQLException("Unsupported unit type: [" + unit + "]");
54+
}
4455
}
4556

46-
public static Timestamp toSqlTimestampNanos(long timeNanos) {
47-
return Timestamp.valueOf(
48-
LocalDateTime.ofEpochSecond(nanos2seconds(timeNanos), nanosPartNanos(timeNanos), ZoneOffset.UTC));
57+
public static LocalDateTime localDateTimeFromTimestampWithTimezone(long value, ChronoUnit unit,
58+
ZoneId zoneIdNullable) throws SQLException {
59+
Instant instant = createInstant(value, unit);
60+
ZoneId zoneId = zoneIdNullable != null ? zoneIdNullable : ZoneId.systemDefault();
61+
return LocalDateTime.ofInstant(instant, zoneId);
4962
}
5063

51-
public static LocalDateTime toLocalDateTime(long timeMicros) {
52-
return LocalDateTime.ofEpochSecond(micros2seconds(timeMicros), nanosPartMicros(timeMicros), ZoneOffset.UTC);
64+
public static LocalDateTime localDateTimeFromTimestamp(long value, ChronoUnit unit, ZoneId zoneIdNullable)
65+
throws SQLException {
66+
Instant instant = createInstant(value, unit);
67+
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
68+
if (null == zoneIdNullable) {
69+
return ldt;
70+
}
71+
ZoneId zoneIdDefault = ZoneId.systemDefault();
72+
LocalDateTime ldtDefault = LocalDateTime.ofInstant(instant, zoneIdDefault);
73+
LocalDateTime ldtZoned = LocalDateTime.ofInstant(instant, zoneIdNullable);
74+
Duration duration = Duration.between(ldtZoned, ldtDefault);
75+
LocalDateTime ldtAdjusted = ldt.plus(duration);
76+
return ldtAdjusted;
5377
}
5478

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

81-
public static OffsetDateTime toOffsetDateTime(long timeMicros) {
82-
return OffsetDateTime.of(toLocalDateTime(timeMicros), ZoneOffset.UTC);
83-
}
84-
85-
public static Timestamp fromSecondInstant(long seconds) {
86-
return fromMilliInstant(seconds * 1_000);
87-
}
88-
89-
public static Timestamp fromMilliInstant(long millis) {
90-
return new Timestamp(millis);
91-
}
92-
93-
public static Timestamp fromMicroInstant(long micros) {
94-
return Timestamp.from(Instant.ofEpochSecond(micros / 1_000_000, nanosPartMicros(micros)));
95-
}
96-
97-
public static Timestamp fromNanoInstant(long nanos) {
98-
return Timestamp.from(Instant.ofEpochSecond(nanos / 1_000_000_000, nanosPartNanos(nanos)));
99-
}
100-
101105
public static long localDateTime2Micros(LocalDateTime localDateTime) {
102106
return DuckDBTimestamp.RefLocalDateTime.until(localDateTime, ChronoUnit.MICROS);
103107
}
@@ -130,7 +134,7 @@ public LocalDateTime toLocalDateTime() {
130134
}
131135

132136
public OffsetDateTime toOffsetDateTime() {
133-
return OffsetDateTime.of(toLocalDateTime(this.timeMicros), ZoneOffset.UTC);
137+
return OffsetDateTime.of(toLocalDateTime(), ZoneOffset.UTC);
134138
}
135139

136140
public static long getMicroseconds(Timestamp sqlTimestamp) {

0 commit comments

Comments
 (0)