diff --git a/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/Constants.java b/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/Constants.java index b40d652..f15c638 100644 --- a/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/Constants.java +++ b/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/Constants.java @@ -73,4 +73,6 @@ public interface Constants { String PRIMARY_KEY_NAME = "PRIMARY_KEY"; + String ZERO_DATETIME = "0000-00-00 00:00:00"; + } diff --git a/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/util/Utils.java b/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/util/Utils.java index 3b0fafb..ca52c05 100644 --- a/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/util/Utils.java +++ b/innodb-java-reader/src/main/java/com/alibaba/innodb/java/reader/util/Utils.java @@ -17,6 +17,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.sql.Time; +import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; @@ -47,6 +49,7 @@ import static com.alibaba.innodb.java.reader.Constants.MIN_RECORD_4; import static com.alibaba.innodb.java.reader.Constants.MIN_RECORD_5; import static com.alibaba.innodb.java.reader.Constants.MIN_VAL; +import static com.alibaba.innodb.java.reader.Constants.ZERO_DATETIME; import static com.google.common.base.Preconditions.checkArgument; /** @@ -448,6 +451,14 @@ public static String formatIntLessThan100(int val) { throw new IllegalArgumentException("not possible"); } + public static Timestamp convertDateTime(String s, int precision) { + // FIXME + if (s.startsWith(ZERO_DATETIME)) { + return new Timestamp(0L); + } + return Timestamp.valueOf(Utils.parseDateTimeText(s, precision)); + } + public static LocalDateTime parseDateTimeText(String s) { return parseDateTimeText(s, 0); } @@ -459,6 +470,10 @@ public static LocalDateTime parseDateTimeText(String s, int precision) { return LocalDateTime.parse(s, TIME_FORMAT_TIMESTAMP[precision]); } + public static Time convertTime(String s, int precision) { + return Time.valueOf(Utils.parseTimeText(s, precision)); + } + public static LocalTime parseTimeText(String s) { return parseTimeText(s, 0); } diff --git a/innodb-java-reader/src/test/java/com/alibaba/innodb/java/reader/util/UtilsTest.java b/innodb-java-reader/src/test/java/com/alibaba/innodb/java/reader/util/UtilsTest.java index 3cc618f..e066873 100644 --- a/innodb-java-reader/src/test/java/com/alibaba/innodb/java/reader/util/UtilsTest.java +++ b/innodb-java-reader/src/test/java/com/alibaba/innodb/java/reader/util/UtilsTest.java @@ -6,6 +6,8 @@ import org.junit.Test; +import java.sql.Time; +import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.ArrayList; @@ -399,6 +401,13 @@ public void testProcessFileWithDelimiter() { } } + @Test + public void testConvertDateTimeText() { + String s = "2020-05-01 12:15:59"; + Timestamp dt = Utils.convertDateTime(s, 0); + assertThat(dt.toString(), is("2020-05-01 12:15:59.0")); + } + @Test public void testParseDateTimeText() { String s = "2020-05-01 12:15:59"; @@ -479,6 +488,13 @@ public void testParseTimeText() { assertThat(time.toString(), is("12:15:59.500")); } + @Test + public void testConvertTimeText() { + String s = "12:15:59"; + Time time = Utils.convertTime(s, 0); + assertThat(time.toString(), is("12:15:59")); + } + @Test(expected = IllegalArgumentException.class) public void testParseTimeTextNegate2() { String s = "12:15:59.456789";