提交 0832553e authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Specify data types for INFORMATION_SCHEMA.SESSIONS and use timestamps with time zones

上级 b3fbef16
...@@ -11,7 +11,6 @@ import java.io.InputStreamReader; ...@@ -11,7 +11,6 @@ import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -58,6 +57,7 @@ import org.h2.schema.TriggerObject; ...@@ -58,6 +57,7 @@ import org.h2.schema.TriggerObject;
import org.h2.store.InDoubtTransaction; import org.h2.store.InDoubtTransaction;
import org.h2.store.PageStore; import org.h2.store.PageStore;
import org.h2.tools.Csv; import org.h2.tools.Csv;
import org.h2.util.DateTimeUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -509,10 +509,10 @@ public class MetaTable extends Table { ...@@ -509,10 +509,10 @@ public class MetaTable extends Table {
cols = createColumns( cols = createColumns(
"ID INT", "ID INT",
"USER_NAME", "USER_NAME",
"SESSION_START", "SESSION_START TIMESTAMP WITH TIME ZONE",
"STATEMENT", "STATEMENT",
"STATEMENT_START", "STATEMENT_START TIMESTAMP WITH TIME ZONE",
"CONTAINS_UNCOMMITTED", "CONTAINS_UNCOMMITTED BIT",
"STATE", "STATE",
"BLOCKER_ID INT" "BLOCKER_ID INT"
); );
...@@ -1827,13 +1827,13 @@ public class MetaTable extends Table { ...@@ -1827,13 +1827,13 @@ public class MetaTable extends Table {
// USER_NAME // USER_NAME
s.getUser().getName(), s.getUser().getName(),
// SESSION_START // SESSION_START
new Timestamp(s.getSessionStart()).toString(), DateTimeUtils.timestampTimeZoneFromMillis(s.getSessionStart()),
// STATEMENT // STATEMENT
command == null ? null : command.toString(), command == null ? null : command.toString(),
// STATEMENT_START // STATEMENT_START
new Timestamp(start).toString(), DateTimeUtils.timestampTimeZoneFromMillis(start),
// CONTAINS_UNCOMMITTED // CONTAINS_UNCOMMITTED
Boolean.toString(s.containsUncommitted()), ValueBoolean.get(s.containsUncommitted()),
// STATE // STATE
String.valueOf(s.getState()), String.valueOf(s.getState()),
// BLOCKER_ID // BLOCKER_ID
......
...@@ -21,17 +21,7 @@ public final class CurrentTimestamp { ...@@ -21,17 +21,7 @@ public final class CurrentTimestamp {
* @return current timestamp * @return current timestamp
*/ */
public static ValueTimestampTimeZone get() { public static ValueTimestampTimeZone get() {
long ms = System.currentTimeMillis(); return DateTimeUtils.timestampTimeZoneFromMillis(System.currentTimeMillis());
/*
* This code intentionally does not support properly dates before UNIX
* epoch and time zone offsets with seconds because such support is not
* required for current dates.
*/
int offset = DateTimeUtils.getTimeZone().getOffset(ms);
ms += offset;
return ValueTimestampTimeZone.fromDateValueAndNanos(
DateTimeUtils.dateValueFromAbsoluteDay(ms / DateTimeUtils.MILLIS_PER_DAY),
ms % DateTimeUtils.MILLIS_PER_DAY * 1_000_000, (short) (offset / 60_000));
} }
private CurrentTimestamp() { private CurrentTimestamp() {
......
...@@ -111,7 +111,7 @@ public class DateTimeUtils { ...@@ -111,7 +111,7 @@ public class DateTimeUtils {
* *
* @return local time zone * @return local time zone
*/ */
static TimeZone getTimeZone() { private static TimeZone getTimeZone() {
TimeZone tz = timeZone; TimeZone tz = timeZone;
if (tz == null) { if (tz == null) {
timeZone = tz = TimeZone.getDefault(); timeZone = tz = TimeZone.getDefault();
...@@ -1163,6 +1163,24 @@ public class DateTimeUtils { ...@@ -1163,6 +1163,24 @@ public class DateTimeUtils {
return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, timeNanos, (short) offsetMins); return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, timeNanos, (short) offsetMins);
} }
/**
* @param ms milliseconds since 1970-01-01 (UTC)
* @return timestamp with time zone with specified value and current time zone
*/
public static ValueTimestampTimeZone timestampTimeZoneFromMillis(long ms) {
int offset = getTimeZone().getOffset(ms);
ms += offset;
long absoluteDay = ms / MILLIS_PER_DAY;
// Round toward negative infinity
if (ms < 0 && (absoluteDay * MILLIS_PER_DAY != ms)) {
absoluteDay--;
}
return ValueTimestampTimeZone.fromDateValueAndNanos(
dateValueFromAbsoluteDay(absoluteDay),
(ms - absoluteDay * MILLIS_PER_DAY) * 1_000_000,
(short) (offset / 60_000));
}
/** /**
* Calculate the absolute day for a January, 1 of the specified year. * Calculate the absolute day for a January, 1 of the specified year.
* *
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论