提交 29a989e2 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #389 from katzyn/LocalTime

Handle LocalTime with nanosecond resolution
......@@ -63,13 +63,15 @@ public class LocalDateTimeUtils {
// java.sql.Date#toLocalDate()
private static Method TO_LOCAL_DATE;
// java.sql.Time#toLocalTime()
private static Method TO_LOCAL_TIME;
// java.time.LocalTime#ofNanoOfDay()
private static Method LOCAL_TIME_OF_NANO;
// java.sql.Date#valueOf(LocalDate)
private static Method DATE_VALUE_OF;
// java.sql.Time#valueOf(LocalTime)
private static Method TIME_VALUE_OF;
// java.time.LocalTime#toNanoOfDay()
private static Method LOCAL_TIME_TO_NANO;
// java.time.LocalDate#of(int, int, int)
private static Method LOCAL_DATE_OF_YEAR_MONTH_DAY;
......@@ -145,10 +147,12 @@ public class LocalDateTimeUtils {
Class<?> temporal = getClass("java.time.temporal.Temporal");
TO_LOCAL_DATE = getMethod(java.sql.Date.class, "toLocalDate");
TO_LOCAL_TIME = getMethod(java.sql.Time.class, "toLocalTime");
LOCAL_TIME_OF_NANO = getMethod(LOCAL_TIME, "ofNanoOfDay", long.class);
DATE_VALUE_OF = getMethod(java.sql.Date.class, "valueOf", LOCAL_DATE);
TIME_VALUE_OF = getMethod(java.sql.Time.class, "valueOf", LOCAL_TIME);
LOCAL_TIME_TO_NANO = getMethod(LOCAL_TIME, "toNanoOfDay");
LOCAL_DATE_OF_YEAR_MONTH_DAY = getMethod(LOCAL_DATE, "of",
int.class, int.class, int.class);
......@@ -390,26 +394,22 @@ public class LocalDateTimeUtils {
* @return the LocalTime
*/
public static Object valueToLocalTime(Value value) {
return timeToLocalTime(value.getTime());
}
private static Object dateToLocalDate(Date date) {
try {
return TO_LOCAL_DATE.invoke(date);
return LOCAL_TIME_OF_NANO.invoke(null, ((ValueTime) value.convertTo(Value.TIME)).getNanos());
} catch (IllegalAccessException e) {
throw DbException.convert(e);
} catch (InvocationTargetException e) {
throw DbException.convertInvocation(e, "date conversion failed");
throw DbException.convertInvocation(e, "time conversion failed");
}
}
private static Object timeToLocalTime(Time time) {
private static Object dateToLocalDate(Date date) {
try {
return TO_LOCAL_TIME.invoke(time);
return TO_LOCAL_DATE.invoke(date);
} catch (IllegalAccessException e) {
throw DbException.convert(e);
} catch (InvocationTargetException e) {
throw DbException.convertInvocation(e, "time conversion failed");
throw DbException.convertInvocation(e, "date conversion failed");
}
}
......@@ -495,8 +495,7 @@ public class LocalDateTimeUtils {
*/
public static Value localTimeToTimeValue(Object localTime) {
try {
Time time = (Time) TIME_VALUE_OF.invoke(null, localTime);
return ValueTime.get(time);
return ValueTime.fromNanos((Long) LOCAL_TIME_TO_NANO.invoke(localTime));
} catch (IllegalAccessException e) {
throw DbException.convert(e);
} catch (InvocationTargetException e) {
......
......@@ -620,6 +620,13 @@ public class TestPreparedStatement extends TestBase {
Object localTime2 = rs.getObject(1, LocalDateTimeUtils.getLocalTimeClass());
assertEquals(localTime, localTime2);
rs.close();
localTime = LocalDateTimeUtils.parseLocalTime("04:05:06.123456789");
prep.setObject(1, localTime);
rs = prep.executeQuery();
rs.next();
localTime2 = rs.getObject(1, LocalDateTimeUtils.getLocalTimeClass());
assertEquals(localTime, localTime2);
rs.close();
}
private void testDateTime8(Connection conn) throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论