提交 f4b3e998 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Support TIMESTAMP WITH TIME ZONE in some date-time functions

上级 54e2147c
...@@ -21,6 +21,7 @@ import org.h2.value.ValueDate; ...@@ -21,6 +21,7 @@ import org.h2.value.ValueDate;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
import org.h2.value.ValueTime; import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp; import org.h2.value.ValueTimestamp;
import org.h2.value.ValueTimestampTimeZone;
/** /**
* This utility class contains time conversion functions. * This utility class contains time conversion functions.
...@@ -318,14 +319,24 @@ public class DateTimeUtils { ...@@ -318,14 +319,24 @@ public class DateTimeUtils {
} }
private static Calendar valueToCalendar(Value value) { private static Calendar valueToCalendar(Value value) {
Calendar cal = DateTimeUtils.createGregorianCalendar(); Calendar cal;
if (value instanceof ValueTimestamp) { if (value instanceof ValueTimestamp) {
cal = createGregorianCalendar();
cal.setTime(value.getTimestamp()); cal.setTime(value.getTimestamp());
} else if (value instanceof ValueDate) { } else if (value instanceof ValueDate) {
cal = createGregorianCalendar();
cal.setTime(value.getDate()); cal.setTime(value.getDate());
} else if (value instanceof ValueTime) { } else if (value instanceof ValueTime) {
cal = createGregorianCalendar();
cal.setTime(value.getTime()); cal.setTime(value.getTime());
} else if (value instanceof ValueTimestampTimeZone) {
ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
cal = createGregorianCalendar(TimeZone.getTimeZone(v.getTimeZoneName()));
cal.setTimeInMillis(DateTimeUtils.convertDateValueToMillis(DateTimeUtils.UTC, v.getDateValue())
+ v.getTimeNanos() / 1000000L
- v.getTimeZoneOffsetMins() * 60000);
} else { } else {
cal = createGregorianCalendar();
cal.setTime(value.getTimestamp()); cal.setTime(value.getTimestamp());
} }
return cal; return cal;
......
...@@ -201,6 +201,30 @@ public class ValueTimestampTimeZone extends Value { ...@@ -201,6 +201,30 @@ public class ValueTimestampTimeZone extends Value {
return timeZoneOffsetMins; return timeZoneOffsetMins;
} }
/**
* Returns name of the timezone such as UTC or GMT+1:00.
*
* @return name of the timezone
*/
public String getTimeZoneName() {
int offset = timeZoneOffsetMins;
if (offset == 0)
return "UTC";
StringBuilder builder = new StringBuilder("GMT");
if (offset > 0) {
builder.append('+');
} else {
builder.append('-');
offset = -offset;
}
builder.append(offset / 60).append(':');
offset %= 60;
if (offset < 10)
builder.append('0');
builder.append(offset);
return builder.toString();
}
@Override @Override
public Timestamp getTimestamp() { public Timestamp getTimestamp() {
throw new UnsupportedOperationException("unimplemented"); throw new UnsupportedOperationException("unimplemented");
......
...@@ -14,3 +14,22 @@ select dayofmonth(date '2005-09-12') d12 from test; ...@@ -14,3 +14,22 @@ select dayofmonth(date '2005-09-12') d12 from test;
> --- > ---
> 12 > 12
> rows: 1 > rows: 1
drop table test;
> ok
create table test(ts timestamp with time zone);
> ok
insert into test(ts) values ('2010-05-11 00:00:00+10:00'), ('2010-05-11 00:00:00-10:00');
> update count: 2
select dayofmonth(ts) d from test;
> D
> --
> 11
> 11
> rows: 2
drop table test;
> ok
...@@ -14,3 +14,22 @@ select hour(time '23:10:59') d23 from test; ...@@ -14,3 +14,22 @@ select hour(time '23:10:59') d23 from test;
> --- > ---
> 23 > 23
> rows: 1 > rows: 1
drop table test;
> ok
create table test(ts timestamp with time zone);
> ok
insert into test(ts) values ('2010-05-11 05:15:10+10:00'), ('2010-05-11 05:15:10-10:00');
> update count: 2
select hour(ts) h from test;
> H
> -
> 5
> 5
> rows: 2
drop table test;
> ok
...@@ -13,6 +13,7 @@ import org.h2.test.TestBase; ...@@ -13,6 +13,7 @@ import org.h2.test.TestBase;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
import org.h2.value.ValueTimestamp; import org.h2.value.ValueTimestamp;
import org.h2.value.ValueTimestampTimeZone;
/** /**
* Test cases for DateTimeIso8601Utils. * Test cases for DateTimeIso8601Utils.
...@@ -20,7 +21,7 @@ import org.h2.value.ValueTimestamp; ...@@ -20,7 +21,7 @@ import org.h2.value.ValueTimestamp;
public class TestDateIso8601 extends TestBase { public class TestDateIso8601 extends TestBase {
private enum Type { private enum Type {
DATE, TIMESTAMP; DATE, TIMESTAMP, TIMESTAMP_TIMEZONE_0, TIMESTAMP_TIMEZONE_PLUS_11, TIMESTAMP_TIMEZONE_MINUS_11;
} }
private static Type type; private static Type type;
...@@ -43,6 +44,12 @@ public class TestDateIso8601 extends TestBase { ...@@ -43,6 +44,12 @@ public class TestDateIso8601 extends TestBase {
return ValueDate.parse(s); return ValueDate.parse(s);
case TIMESTAMP: case TIMESTAMP:
return ValueTimestamp.parse(s); return ValueTimestamp.parse(s);
case TIMESTAMP_TIMEZONE_0:
return ValueTimestampTimeZone.parse(s + " 00:00:00.0Z");
case TIMESTAMP_TIMEZONE_PLUS_11:
return ValueTimestampTimeZone.parse(s + " 00:00:00+11:00");
case TIMESTAMP_TIMEZONE_MINUS_11:
return ValueTimestampTimeZone.parse(s + " 00:00:00-11:00");
default: default:
throw new IllegalStateException(); throw new IllegalStateException();
} }
...@@ -54,6 +61,12 @@ public class TestDateIso8601 extends TestBase { ...@@ -54,6 +61,12 @@ public class TestDateIso8601 extends TestBase {
doTest(); doTest();
type = Type.TIMESTAMP; type = Type.TIMESTAMP;
doTest(); doTest();
type = Type.TIMESTAMP_TIMEZONE_0;
doTest();
type = Type.TIMESTAMP_TIMEZONE_PLUS_11;
doTest();
type = Type.TIMESTAMP_TIMEZONE_MINUS_11;
doTest();
} }
private void doTest() throws Exception { private void doTest() throws Exception {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论