Skip to content

Commit c6015c9

Browse files
nokiaMSgithubgxll
authored andcommitted
[fix][runtime] Fix timezone when time and timestamp union.
1 parent 652f37f commit c6015c9

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.sql.Timestamp;
3131
import java.time.LocalDate;
3232
import java.time.LocalDateTime;
33+
import java.time.LocalTime;
3334
import java.time.ZoneId;
3435
import java.time.ZoneOffset;
3536
import java.time.ZonedDateTime;
@@ -61,12 +62,24 @@ abstract class TimestampCastOp extends CastOp {
6162
return new Timestamp(DateTimeUtils.fromSecond(value));
6263
}
6364

64-
static @NonNull Timestamp timestampCast(@NonNull Time value) {
65+
static @NonNull Timestamp timestampCast(@NonNull Time value, ExprConfig config) {
6566
int hours = value.toLocalTime().getHour();
6667
int minutes = value.toLocalTime().getMinute();
6768
int seconds = value.toLocalTime().getSecond();
69+
6870
LocalDateTime localDatetime = LocalDate.now().atTime(hours,minutes,seconds);
69-
return Timestamp.valueOf(localDatetime);
71+
TimeZone timeZone = (config != null ? config.getTimeZone() : TimeZone.getDefault());
72+
ZonedDateTime zonedDateTime = localDatetime.atZone(timeZone.toZoneId());
73+
74+
java.time.ZoneOffset zoneOffset = zonedDateTime.getOffset();
75+
ZonedDateTime targetZonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
76+
int zonedDayOfMonth = targetZonedDateTime.getDayOfMonth();
77+
int localDayofMonth = localDatetime.getDayOfMonth();
78+
if (zonedDayOfMonth != localDayofMonth) {
79+
targetZonedDateTime = targetZonedDateTime.plusDays(localDayofMonth - zonedDayOfMonth);
80+
}
81+
82+
return new Timestamp(targetZonedDateTime.toLocalDateTime().toInstant(zoneOffset).toEpochMilli());
7083
}
7184

7285
static @Nullable Timestamp timestampCast(String value, @NonNull ExprConfig config) {

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
@@ -188,7 +188,7 @@ protected Timestamp evalNonNullValue(@NonNull Object value, ExprConfig config) {
188188
} else if (value instanceof BigDecimal) {
189189
return timestampCast((BigDecimal) value);
190190
} else if (value instanceof Time) {
191-
return timestampCast((Time) value);
191+
return timestampCast((Time) value, config);
192192
} else {
193193
return null;
194194
}

0 commit comments

Comments
 (0)