You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/06/16 05:23:14 UTC
[shardingsphere] branch master updated: Fix java.sql.time convert failed when there are negative values (#26380)
This is an automated email from the ASF dual-hosted git repository.
zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new baf33e0fa4d Fix java.sql.time convert failed when there are negative values (#26380)
baf33e0fa4d is described below
commit baf33e0fa4d85ea794e98ec8863f905d424a8c37
Author: Xinze Guo <10...@users.noreply.github.com>
AuthorDate: Fri Jun 16 13:23:07 2023 +0800
Fix java.sql.time convert failed when there are negative values (#26380)
* Fix java.sql.time convert failed when there are negative values
* Improve test case
* Improve codestyle
---
.../data/pipeline/cdc/util/ColumnValueConvertUtils.java | 9 +--------
.../data/pipeline/cdc/util/ColumnValueConvertUtilsTest.java | 10 ++++++++++
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtils.java b/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtils.java
index 87cbb812d09..1398aaa36a5 100644
--- a/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtils.java
+++ b/kernel/data-pipeline/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtils.java
@@ -47,7 +47,6 @@ import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.Date;
-import java.util.concurrent.TimeUnit;
/**
* Column value convert utility class.
@@ -56,10 +55,6 @@ import java.util.concurrent.TimeUnit;
@Slf4j
public final class ColumnValueConvertUtils {
- private static final long MILLISECONDS_PER_SECOND = TimeUnit.SECONDS.toMillis(1);
-
- private static final long NANOSECONDS_PER_MILLISECOND = TimeUnit.MILLISECONDS.toNanos(1);
-
/**
* Convert java object to protobuf message.
*
@@ -108,9 +103,7 @@ public final class ColumnValueConvertUtils {
}
if (object instanceof Time) {
Time time = (Time) object;
- long millis = (int) (time.getTime() % MILLISECONDS_PER_SECOND);
- int nanosOfSecond = (int) (millis * NANOSECONDS_PER_MILLISECOND);
- LocalTime localTime = LocalTime.of(time.getHours(), time.getMinutes(), time.getSeconds(), nanosOfSecond);
+ LocalTime localTime = LocalTime.of(time.getHours(), time.getMinutes(), time.getSeconds(), new Timestamp(time.getTime()).getNanos());
return Int64Value.of(localTime.toNanoOfDay());
}
if (object instanceof java.sql.Date) {
diff --git a/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtilsTest.java b/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtilsTest.java
index df1572c3d8b..e9d33ef5d18 100644
--- a/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtilsTest.java
+++ b/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/ColumnValueConvertUtilsTest.java
@@ -30,7 +30,9 @@ import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.sql.Time;
import java.sql.Timestamp;
+import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.Date;
@@ -105,4 +107,12 @@ class ColumnValueConvertUtilsTest {
assertThat(((com.google.protobuf.Timestamp) actualMessage).getSeconds(), is(offsetDateTime.toEpochSecond()));
assertThat(((com.google.protobuf.Timestamp) actualMessage).getNanos(), is(offsetDateTime.getNano()));
}
+
+ @Test
+ void assertTimeConvert() {
+ Time time = new Time(-3600 * 1000 - 1234);
+ int nanos = new Timestamp(time.getTime()).getNanos();
+ Int64Value actualMessage = (Int64Value) ColumnValueConvertUtils.convertToProtobufMessage(time);
+ assertThat(LocalTime.ofNanoOfDay(actualMessage.getValue()), is(time.toLocalTime().withNano(nanos)));
+ }
}