Skip to content

Commit

Permalink
Fix time zone check failure
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Dec 30, 2021
1 parent 27d034f commit 70358b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
Expand Down Expand Up @@ -126,40 +125,41 @@ public ClickHouseDateTimeValue copy(boolean deep) {

@Override
public byte asByte() {
return isNullOrEmpty() ? (byte) 0 : (byte) getValue().toEpochSecond(ZoneOffset.UTC);
return isNullOrEmpty() ? (byte) 0 : (byte) getValue().atZone(tz.toZoneId()).toEpochSecond();
}

@Override
public short asShort() {
return isNullOrEmpty() ? (short) 0 : (short) getValue().toEpochSecond(ZoneOffset.UTC);
return isNullOrEmpty() ? (short) 0 : (short) getValue().atZone(tz.toZoneId()).toEpochSecond();
}

@Override
public int asInteger() {
return isNullOrEmpty() ? 0 : (int) getValue().toEpochSecond(ZoneOffset.UTC);
return isNullOrEmpty() ? 0 : (int) getValue().atZone(tz.toZoneId()).toEpochSecond();
}

@Override
public long asLong() {
return isNullOrEmpty() ? 0L : getValue().toEpochSecond(ZoneOffset.UTC);
return isNullOrEmpty() ? 0L : getValue().atZone(tz.toZoneId()).toEpochSecond();
}

@Override
public float asFloat() {
return isNullOrEmpty() ? 0F
: getValue().toEpochSecond(ZoneOffset.UTC) + getValue().getNano() / ClickHouseValues.NANOS.floatValue();
: getValue().atZone(tz.toZoneId()).toEpochSecond()
+ getValue().getNano() / ClickHouseValues.NANOS.floatValue();
}

@Override
public double asDouble() {
return isNullOrEmpty() ? 0D
: getValue().toEpochSecond(ZoneOffset.UTC)
: getValue().atZone(tz.toZoneId()).toEpochSecond()
+ getValue().getNano() / ClickHouseValues.NANOS.doubleValue();
}

@Override
public BigInteger asBigInteger() {
return isNullOrEmpty() ? null : BigInteger.valueOf(getValue().toEpochSecond(ZoneOffset.UTC));
return isNullOrEmpty() ? null : BigInteger.valueOf(getValue().atZone(tz.toZoneId()).toEpochSecond());
}

@Override
Expand All @@ -168,7 +168,7 @@ public BigDecimal asBigDecimal(int scale) {
BigDecimal v = null;
if (value != null) {
int nanoSeconds = value.getNano();
v = new BigDecimal(BigInteger.valueOf(value.toEpochSecond(ZoneOffset.UTC)), scale);
v = new BigDecimal(BigInteger.valueOf(value.atZone(tz.toZoneId()).toEpochSecond()), scale);
if (scale != 0 && nanoSeconds != 0) {
v = v.add(BigDecimal.valueOf(nanoSeconds).divide(ClickHouseValues.NANOS).setScale(scale,
RoundingMode.HALF_UP));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ public void testInsertQueryDateTime64() throws SQLException {
+ "CREATE TABLE IF NOT EXISTS test_issue_612 (id UUID, date DateTime64(6)) ENGINE = MergeTree() ORDER BY (id, date)");
UUID id = UUID.randomUUID();
long value = 1617359745321000L;
Instant i = Instant.ofEpochMilli(value / 1000L);
LocalDateTime dt = LocalDateTime.ofInstant(i, conn.getServerTimeZone().toZoneId());
try (PreparedStatement ps = conn.prepareStatement("insert into test_issue_612 values(?,?)")) {
ps.setLong(2, value);
ps.setObject(1, id);
Expand All @@ -224,8 +226,8 @@ public void testInsertQueryDateTime64() throws SQLException {
ResultSet rs = ps.executeQuery();
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getObject(1), id);
Assert.assertEquals(rs.getObject(2), LocalDateTime.of(2021, 4, 2, 10, 35, 45, 321000000));
Assert.assertEquals(rs.getLong(2), 1617359745L);
Assert.assertEquals(rs.getObject(2), dt);
Assert.assertEquals(rs.getLong(2), dt.atZone(conn.getServerTimeZone().toZoneId()).toEpochSecond());
Assert.assertFalse(rs.next());
}

Expand Down

0 comments on commit 70358b1

Please sign in to comment.