Skip to content

Commit 70358b1

Browse files
committed
Fix time zone check failure
1 parent 27d034f commit 70358b1

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseDateTimeValue.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.time.LocalDateTime;
1111
import java.time.LocalTime;
1212
import java.time.OffsetDateTime;
13-
import java.time.ZoneOffset;
1413
import java.time.ZonedDateTime;
1514
import java.time.format.DateTimeFormatter;
1615
import java.util.TimeZone;
@@ -126,40 +125,41 @@ public ClickHouseDateTimeValue copy(boolean deep) {
126125

127126
@Override
128127
public byte asByte() {
129-
return isNullOrEmpty() ? (byte) 0 : (byte) getValue().toEpochSecond(ZoneOffset.UTC);
128+
return isNullOrEmpty() ? (byte) 0 : (byte) getValue().atZone(tz.toZoneId()).toEpochSecond();
130129
}
131130

132131
@Override
133132
public short asShort() {
134-
return isNullOrEmpty() ? (short) 0 : (short) getValue().toEpochSecond(ZoneOffset.UTC);
133+
return isNullOrEmpty() ? (short) 0 : (short) getValue().atZone(tz.toZoneId()).toEpochSecond();
135134
}
136135

137136
@Override
138137
public int asInteger() {
139-
return isNullOrEmpty() ? 0 : (int) getValue().toEpochSecond(ZoneOffset.UTC);
138+
return isNullOrEmpty() ? 0 : (int) getValue().atZone(tz.toZoneId()).toEpochSecond();
140139
}
141140

142141
@Override
143142
public long asLong() {
144-
return isNullOrEmpty() ? 0L : getValue().toEpochSecond(ZoneOffset.UTC);
143+
return isNullOrEmpty() ? 0L : getValue().atZone(tz.toZoneId()).toEpochSecond();
145144
}
146145

147146
@Override
148147
public float asFloat() {
149148
return isNullOrEmpty() ? 0F
150-
: getValue().toEpochSecond(ZoneOffset.UTC) + getValue().getNano() / ClickHouseValues.NANOS.floatValue();
149+
: getValue().atZone(tz.toZoneId()).toEpochSecond()
150+
+ getValue().getNano() / ClickHouseValues.NANOS.floatValue();
151151
}
152152

153153
@Override
154154
public double asDouble() {
155155
return isNullOrEmpty() ? 0D
156-
: getValue().toEpochSecond(ZoneOffset.UTC)
156+
: getValue().atZone(tz.toZoneId()).toEpochSecond()
157157
+ getValue().getNano() / ClickHouseValues.NANOS.doubleValue();
158158
}
159159

160160
@Override
161161
public BigInteger asBigInteger() {
162-
return isNullOrEmpty() ? null : BigInteger.valueOf(getValue().toEpochSecond(ZoneOffset.UTC));
162+
return isNullOrEmpty() ? null : BigInteger.valueOf(getValue().atZone(tz.toZoneId()).toEpochSecond());
163163
}
164164

165165
@Override
@@ -168,7 +168,7 @@ public BigDecimal asBigDecimal(int scale) {
168168
BigDecimal v = null;
169169
if (value != null) {
170170
int nanoSeconds = value.getNano();
171-
v = new BigDecimal(BigInteger.valueOf(value.toEpochSecond(ZoneOffset.UTC)), scale);
171+
v = new BigDecimal(BigInteger.valueOf(value.atZone(tz.toZoneId()).toEpochSecond()), scale);
172172
if (scale != 0 && nanoSeconds != 0) {
173173
v = v.add(BigDecimal.valueOf(nanoSeconds).divide(ClickHouseValues.NANOS).setScale(scale,
174174
RoundingMode.HALF_UP));

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ public void testInsertQueryDateTime64() throws SQLException {
210210
+ "CREATE TABLE IF NOT EXISTS test_issue_612 (id UUID, date DateTime64(6)) ENGINE = MergeTree() ORDER BY (id, date)");
211211
UUID id = UUID.randomUUID();
212212
long value = 1617359745321000L;
213+
Instant i = Instant.ofEpochMilli(value / 1000L);
214+
LocalDateTime dt = LocalDateTime.ofInstant(i, conn.getServerTimeZone().toZoneId());
213215
try (PreparedStatement ps = conn.prepareStatement("insert into test_issue_612 values(?,?)")) {
214216
ps.setLong(2, value);
215217
ps.setObject(1, id);
@@ -224,8 +226,8 @@ public void testInsertQueryDateTime64() throws SQLException {
224226
ResultSet rs = ps.executeQuery();
225227
Assert.assertTrue(rs.next());
226228
Assert.assertEquals(rs.getObject(1), id);
227-
Assert.assertEquals(rs.getObject(2), LocalDateTime.of(2021, 4, 2, 10, 35, 45, 321000000));
228-
Assert.assertEquals(rs.getLong(2), 1617359745L);
229+
Assert.assertEquals(rs.getObject(2), dt);
230+
Assert.assertEquals(rs.getLong(2), dt.atZone(conn.getServerTimeZone().toZoneId()).toEpochSecond());
229231
Assert.assertFalse(rs.next());
230232
}
231233

0 commit comments

Comments
 (0)