Skip to content

Commit

Permalink
Add utility to convert date time and time.
Browse files Browse the repository at this point in the history
  • Loading branch information
neoremind committed May 30, 2020
1 parent 33a4811 commit a9ec2a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ public interface Constants {

String PRIMARY_KEY_NAME = "PRIMARY_KEY";

String ZERO_DATETIME = "0000-00-00 00:00:00";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit a9ec2a5

Please sign in to comment.