Skip to content

Commit c0695ec

Browse files
nokiaMSgithubgxll
authored andcommitted
[fix][runtime] Adapt zone when date and timestamp union.
1 parent d608305 commit c0695ec

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/TimestampCastOp.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
import java.sql.Timestamp;
3131
import java.time.LocalDate;
3232
import java.time.LocalDateTime;
33+
import java.time.ZoneId;
34+
import java.time.ZoneOffset;
35+
import java.time.ZonedDateTime;
36+
import java.util.TimeZone;
3337

3438
//@Operators
3539
abstract class TimestampCastOp extends CastOp {
@@ -43,8 +47,14 @@ abstract class TimestampCastOp extends CastOp {
4347
return new Timestamp(DateTimeUtils.fromSecond(value));
4448
}
4549

46-
static @NonNull Timestamp timestampCast(Date value) {
47-
return new Timestamp(value.getTime());
50+
static @NonNull Timestamp timestampCast(Date value, ExprConfig config) {
51+
LocalDateTime ts = new Timestamp(value.getTime()).toLocalDateTime();
52+
TimeZone timeZone = (config != null ? config.getTimeZone() : TimeZone.getDefault());
53+
ZonedDateTime zonedDateTime = ts.atZone(timeZone.toZoneId());
54+
55+
java.time.ZoneOffset zoneOffset = zonedDateTime.getOffset();
56+
ZonedDateTime targetZonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
57+
return new Timestamp(targetZonedDateTime.toLocalDateTime().toInstant(zoneOffset).toEpochMilli());
4858
}
4959

5060
static @NonNull Timestamp timestampCast(@NonNull BigDecimal value) {

runtime/src/main/java/io/dingodb/expr/runtime/op/cast/TimestampCastOpFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static final class TimestampCastAny extends TimestampCastOp {
176176
@Override
177177
protected Timestamp evalNonNullValue(@NonNull Object value, ExprConfig config) {
178178
if (value instanceof Date) {
179-
return timestampCast((Date) value);
179+
return timestampCast((Date) value, config);
180180
} else if (value instanceof Integer) {
181181
return timestampCast((Integer) value);
182182
} else if (value instanceof Timestamp) {

0 commit comments

Comments
 (0)