mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 10:38:57 +00:00
#653 - Change format of all Timestamp/Date time types to use epoch millis
This commit is contained in:
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user