提交 60659b6f authored 作者: Thomas Mueller's avatar Thomas Mueller

Some time and timezone functions didn't support years before 1.

上级 12ee4c9f
...@@ -81,6 +81,7 @@ public class DateTimeUtils { ...@@ -81,6 +81,7 @@ public class DateTimeUtils {
long time; long time;
synchronized (cal) { synchronized (cal) {
cal.setTime(value); cal.setTime(value);
cal.set(Calendar.ERA, GregorianCalendar.AD);
// month is 0 based // month is 0 based
cal.set(DEFAULT_YEAR, DEFAULT_MONTH - 1, DEFAULT_DAY); cal.set(DEFAULT_YEAR, DEFAULT_MONTH - 1, DEFAULT_DAY);
time = cal.getTime().getTime(); time = cal.getTime().getTime();
...@@ -185,6 +186,7 @@ public class DateTimeUtils { ...@@ -185,6 +186,7 @@ public class DateTimeUtils {
} }
private static void convertTime(Calendar from, Calendar to) { private static void convertTime(Calendar from, Calendar to) {
to.set(Calendar.ERA, from.get(Calendar.ERA));
to.set(Calendar.YEAR, from.get(Calendar.YEAR)); to.set(Calendar.YEAR, from.get(Calendar.YEAR));
to.set(Calendar.MONTH, from.get(Calendar.MONTH)); to.set(Calendar.MONTH, from.get(Calendar.MONTH));
to.set(Calendar.DAY_OF_MONTH, from.get(Calendar.DAY_OF_MONTH)); to.set(Calendar.DAY_OF_MONTH, from.get(Calendar.DAY_OF_MONTH));
...@@ -401,6 +403,20 @@ public class DateTimeUtils { ...@@ -401,6 +403,20 @@ public class DateTimeUtils {
return value; return value;
} }
/**
* Get the year (positive or negative) from a calendar.
*
* @param calendar the calendar
* @return the year
*/
public static int getYear(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
year = 1 - year;
}
return year;
}
/** /**
* Get the number of milliseconds since 1970-01-01 in the local timezone. * Get the number of milliseconds since 1970-01-01 in the local timezone.
* *
......
...@@ -10,12 +10,18 @@ import java.sql.Connection; ...@@ -10,12 +10,18 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone; import java.util.TimeZone;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate;
import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp;
/** /**
* Tests the data parsing. The problem is that some dates are not allowed * Tests the data parsing. The problem is that some dates are not allowed
...@@ -35,10 +41,35 @@ public class TestDate extends TestBase { ...@@ -35,10 +41,35 @@ public class TestDate extends TestBase {
} }
public void test() throws SQLException { public void test() throws SQLException {
testDateTimeUtils();
testAllTimeZones(); testAllTimeZones();
testCurrentTimeZone(); testCurrentTimeZone();
} }
private void testDateTimeUtils() {
java.sql.Timestamp ts1 = (Timestamp) DateTimeUtils.parseDateTime("-999-08-07 13:14:15.16", Value.TIMESTAMP, 0);
java.sql.Timestamp ts2 = (Timestamp) DateTimeUtils.parseDateTime("19999-08-07 13:14:15.16", Value.TIMESTAMP, 0);
java.sql.Time t1 = DateTimeUtils.cloneAndNormalizeTime(new java.sql.Time(ts1.getTime()));
java.sql.Time t2 = DateTimeUtils.cloneAndNormalizeTime(new java.sql.Time(ts2.getTime()));
java.sql.Date d1 = DateTimeUtils.cloneAndNormalizeDate(new java.sql.Date(ts1.getTime()));
java.sql.Date d2 = DateTimeUtils.cloneAndNormalizeDate(new java.sql.Date(ts2.getTime()));
assertEquals("-999-08-07 13:14:15.16", ValueTimestamp.get(ts1).getString());
assertEquals("-999-08-07", ValueDate.get(d1).getString());
assertEquals("13:14:15", ValueTime.get(t1).getString());
assertEquals("19999-08-07 13:14:15.16", ValueTimestamp.get(ts2).getString());
assertEquals("19999-08-07", ValueDate.get(d2).getString());
assertEquals("13:14:15", ValueTime.get(t2).getString());
Calendar cal = Calendar.getInstance();
cal.setTime(t1);
assertEquals(GregorianCalendar.AD, cal.get(Calendar.ERA));
cal.setTime(t2);
assertEquals(GregorianCalendar.AD, cal.get(Calendar.ERA));
java.sql.Timestamp ts1a = DateTimeUtils.convertTimestampToCalendar(ts1, Calendar.getInstance());
java.sql.Timestamp ts2a = DateTimeUtils.convertTimestampToCalendar(ts2, Calendar.getInstance());
assertEquals("-999-08-07 13:14:15.16", ValueTimestamp.get(ts1a).getString());
assertEquals("19999-08-07 13:14:15.16", ValueTimestamp.get(ts2a).getString());
}
private static void testCurrentTimeZone() { private static void testCurrentTimeZone() {
for (int year = 1970; year < 2050; year += 3) { for (int year = 1970; year < 2050; year += 3) {
for (int month = 1; month <= 12; month++) { for (int month = 1; month <= 12; month++) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论