提交 adfa0261 authored 作者: Dmitriy Vetutnev's avatar Dmitriy Vetutnev

fix compare TIMESTAMP WITH TIMEZONE

上级 91b2048d
...@@ -290,15 +290,21 @@ public class ValueTimestampTimeZone extends Value { ...@@ -290,15 +290,21 @@ public class ValueTimestampTimeZone extends Value {
@Override @Override
protected int compareSecure(Value o, CompareMode mode) { protected int compareSecure(Value o, CompareMode mode) {
ValueTimestampTimeZone t = (ValueTimestampTimeZone) o; ValueTimestampTimeZone t = (ValueTimestampTimeZone) o;
int c = MathUtils.compareLong(dateValue, t.dateValue); long a = DateTimeUtils.convertDateValueToMillis(TimeZone.getTimeZone("UTC"), dateValue) / ( 1000L * 60L );
long ma = timeNanos / ( 1000L * 1000L * 1000L * 60L );
a += ma;
a -= timeZoneOffsetMins;
long b = DateTimeUtils.convertDateValueToMillis(TimeZone.getTimeZone("UTC"), t.dateValue) / ( 1000L * 60L );
long mb = t.timeNanos / ( 1000L * 1000L * 1000L * 60L );
b += mb;
b -= t.timeZoneOffsetMins;
int c = MathUtils.compareLong(a, b);
if (c != 0) { if (c != 0) {
return c; return c;
} }
c = MathUtils.compareLong(timeNanos, t.timeNanos); long na = timeNanos - ( ma * 1000L * 1000L * 1000L * 60L );
if (c != 0) { long nb = t.timeNanos - ( mb * 1000L * 1000L * 1000L * 60L );
return c; return MathUtils.compareLong(na, nb);
}
return MathUtils.compareInt(timeZoneOffsetMins, t.timeZoneOffsetMins);
} }
@Override @Override
......
...@@ -11,6 +11,7 @@ import java.sql.SQLException; ...@@ -11,6 +11,7 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import org.h2.api.TimestampWithTimeZone; import org.h2.api.TimestampWithTimeZone;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.value.ValueTimestampTimeZone;
/** /**
*/ */
...@@ -29,6 +30,10 @@ public class TestTimeStampWithTimeZone extends TestBase { ...@@ -29,6 +30,10 @@ public class TestTimeStampWithTimeZone extends TestBase {
public void test() throws SQLException { public void test() throws SQLException {
deleteDb(getTestName()); deleteDb(getTestName());
test1(); test1();
test2();
test3();
test4();
testOrder();
deleteDb(getTestName()); deleteDb(getTestName());
} }
...@@ -81,4 +86,37 @@ public class TestTimeStampWithTimeZone extends TestBase { ...@@ -81,4 +86,37 @@ public class TestTimeStampWithTimeZone extends TestBase {
conn.close(); conn.close();
} }
private void test2() {
ValueTimestampTimeZone a = ValueTimestampTimeZone.parse("1970-01-01 12:00:00.00+00:15");
ValueTimestampTimeZone b = ValueTimestampTimeZone.parse("1970-01-01 12:00:01.00+01:15");
int c= a.compareTo(b, null);
assertEquals(c, 1);
}
private void test3() {
ValueTimestampTimeZone a = ValueTimestampTimeZone.parse("1970-01-02 00:00:02.00+01:15");
ValueTimestampTimeZone b = ValueTimestampTimeZone.parse("1970-01-01 23:00:01.00+00:15");
int c= a.compareTo(b, null);
assertEquals(c, 1);
}
private void test4() {
ValueTimestampTimeZone a = ValueTimestampTimeZone.parse("1970-01-02 00:00:01.00+01:15");
ValueTimestampTimeZone b = ValueTimestampTimeZone.parse("1970-01-01 23:00:01.00+00:15");
int c= a.compareTo(b, null);
assertEquals(c, 0);
}
private void testOrder() throws SQLException {
Connection conn = getConnection(getTestName());
Statement stat = conn.createStatement();
stat.execute("create table test_order(id identity, t1 timestamp with timezone)");
stat.execute("insert into test_order(t1) values('1970-01-01 12:00:00.00+00:15')");
stat.execute("insert into test_order(t1) values('1970-01-01 12:00:01.00+01:15')");
ResultSet rs = stat.executeQuery("select t1 from test_order order by t1");
rs.next();
assertEquals("1970-01-01 12:00:01.0+01:15", rs.getString(1));
conn.close();
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论