#653 - Change format of all Timestamp/Date time types to use epoch millis

This commit is contained in:
Robin Bygrave
2016-04-18 12:49:16 +12:00
parent 2de78cd20f
commit 658ce3c5d3
2 changed files with 30 additions and 5 deletions
@@ -131,14 +131,20 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
}
public String formatValue(T value) {
return convertToTimestamp(value).toString();
// format all timestamps into epoch millis
long epochMillis = convertToMillis(value);
return Long.toString(epochMillis);
}
public T parse(String value) {
return convertFromTimestamp(Timestamp.valueOf(value));
try {
long epochMillis = Long.parseLong(value);
return convertFromMillis(epochMillis);
} catch (NumberFormatException e) {
return convertFromTimestamp(Timestamp.valueOf(value));
}
}
public boolean isDateTimeCapable() {
return true;
}
@@ -69,11 +69,20 @@ public class ScalarTypeInstantTest {
Instant now = Instant.now();
Timestamp timestamp = Timestamp.from(now);
String formatted = type.formatValue(now);
assertEquals(timestamp.toString(), formatted);
assertEquals(""+timestamp.getTime(), formatted);
}
@Test
public void testParse() throws Exception {
public void testParse_when_epochMillis() throws Exception {
Instant now = Instant.now();
Timestamp timestamp = Timestamp.from(now);
Instant val1 = type.parse(""+timestamp.getTime());
assertEquals(now, val1);
}
@Test
public void testParse_when_timestampForm() throws Exception {
Instant now = Instant.now();
Timestamp timestamp = Timestamp.from(now);
@@ -81,6 +90,16 @@ public class ScalarTypeInstantTest {
assertEquals(now, val1);
}
@Test
public void testFormatAndParse() throws Exception {
Instant now = Instant.now();
String format = type.format(now);
Instant val1 = type.parse(format);
assertEquals(now, val1);
}
@Test
public void testIsDateTimeCapable() throws Exception {