提交 7a1ca278 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Issue #644: Year changing from negative -509 to a positive 510

上级 50d287a2
...@@ -9,7 +9,6 @@ package org.h2.util; ...@@ -9,7 +9,6 @@ package org.h2.util;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.Date;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -60,15 +59,9 @@ public class LocalDateTimeUtils { ...@@ -60,15 +59,9 @@ public class LocalDateTimeUtils {
// Class<java.time.ZoneOffset> // Class<java.time.ZoneOffset>
private static final Class<?> ZONE_OFFSET; private static final Class<?> ZONE_OFFSET;
// java.sql.Date#toLocalDate()
private static final Method TO_LOCAL_DATE;
// java.time.LocalTime#ofNanoOfDay() // java.time.LocalTime#ofNanoOfDay()
private static final Method LOCAL_TIME_OF_NANO; private static final Method LOCAL_TIME_OF_NANO;
// java.sql.Date#valueOf(LocalDate)
private static final Method DATE_VALUE_OF;
// java.time.LocalTime#toNanoOfDay() // java.time.LocalTime#toNanoOfDay()
private static final Method LOCAL_TIME_TO_NANO; private static final Method LOCAL_TIME_TO_NANO;
...@@ -140,12 +133,8 @@ public class LocalDateTimeUtils { ...@@ -140,12 +133,8 @@ public class LocalDateTimeUtils {
Class<?> duration = getClass("java.time.Duration"); Class<?> duration = getClass("java.time.Duration");
Class<?> temporal = getClass("java.time.temporal.Temporal"); Class<?> temporal = getClass("java.time.temporal.Temporal");
TO_LOCAL_DATE = getMethod(java.sql.Date.class, "toLocalDate");
LOCAL_TIME_OF_NANO = getMethod(LOCAL_TIME, "ofNanoOfDay", long.class); LOCAL_TIME_OF_NANO = getMethod(LOCAL_TIME, "ofNanoOfDay", long.class);
DATE_VALUE_OF = getMethod(java.sql.Date.class, "valueOf", LOCAL_DATE);
LOCAL_TIME_TO_NANO = getMethod(LOCAL_TIME, "toNanoOfDay"); LOCAL_TIME_TO_NANO = getMethod(LOCAL_TIME, "toNanoOfDay");
LOCAL_DATE_OF_YEAR_MONTH_DAY = getMethod(LOCAL_DATE, "of", LOCAL_DATE_OF_YEAR_MONTH_DAY = getMethod(LOCAL_DATE, "of",
...@@ -179,9 +168,7 @@ public class LocalDateTimeUtils { ...@@ -179,9 +168,7 @@ public class LocalDateTimeUtils {
CHRONO_UNIT_DAYS = getFieldValue(chronoUnit, "DAYS"); CHRONO_UNIT_DAYS = getFieldValue(chronoUnit, "DAYS");
} else { } else {
TO_LOCAL_DATE = null;
LOCAL_TIME_OF_NANO = null; LOCAL_TIME_OF_NANO = null;
DATE_VALUE_OF = null;
LOCAL_TIME_TO_NANO = null; LOCAL_TIME_TO_NANO = null;
LOCAL_DATE_OF_YEAR_MONTH_DAY = null; LOCAL_DATE_OF_YEAR_MONTH_DAY = null;
LOCAL_DATE_PARSE = null; LOCAL_DATE_PARSE = null;
...@@ -409,7 +396,13 @@ public class LocalDateTimeUtils { ...@@ -409,7 +396,13 @@ public class LocalDateTimeUtils {
* @return the LocalDate * @return the LocalDate
*/ */
public static Object valueToLocalDate(Value value) { public static Object valueToLocalDate(Value value) {
return dateToLocalDate(value.getDate()); try {
return localDateFromDateValue(((ValueDate) value.convertTo(Value.DATE)).getDateValue());
} catch (IllegalAccessException e) {
throw DbException.convert(e);
} catch (InvocationTargetException e) {
throw DbException.convertInvocation(e, "date conversion failed");
}
} }
/** /**
...@@ -431,16 +424,6 @@ public class LocalDateTimeUtils { ...@@ -431,16 +424,6 @@ public class LocalDateTimeUtils {
} }
} }
private static Object dateToLocalDate(Date date) {
try {
return TO_LOCAL_DATE.invoke(date);
} catch (IllegalAccessException e) {
throw DbException.convert(e);
} catch (InvocationTargetException e) {
throw DbException.convertInvocation(e, "date conversion failed");
}
}
/** /**
* Converts a value to a LocalDateTime. * Converts a value to a LocalDateTime.
* *
...@@ -506,8 +489,7 @@ public class LocalDateTimeUtils { ...@@ -506,8 +489,7 @@ public class LocalDateTimeUtils {
*/ */
public static Value localDateToDateValue(Object localDate) { public static Value localDateToDateValue(Object localDate) {
try { try {
Date date = (Date) DATE_VALUE_OF.invoke(null, localDate); return ValueDate.fromDateValue(dateValueFromLocalDate(localDate));
return ValueDate.get(date);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw DbException.convert(e); throw DbException.convert(e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
......
...@@ -634,6 +634,13 @@ public class TestPreparedStatement extends TestBase { ...@@ -634,6 +634,13 @@ public class TestPreparedStatement extends TestBase {
Object localDate2 = rs.getObject(1, LocalDateTimeUtils.getLocalDateClass()); Object localDate2 = rs.getObject(1, LocalDateTimeUtils.getLocalDateClass());
assertEquals(localDate, localDate2); assertEquals(localDate, localDate2);
rs.close(); rs.close();
localDate = LocalDateTimeUtils.parseLocalDate("-0509-01-01");
prep.setObject(1, localDate);
rs = prep.executeQuery();
rs.next();
localDate2 = rs.getObject(1, LocalDateTimeUtils.getLocalDateClass());
assertEquals(localDate, localDate2);
rs.close();
} }
private void testTime8(Connection conn) throws SQLException { private void testTime8(Connection conn) throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论