提交 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;
import java.io.Reader;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.Collator;
import java.util.ArrayList;
......@@ -58,6 +57,7 @@ import org.h2.schema.TriggerObject;
import org.h2.store.InDoubtTransaction;
import org.h2.store.PageStore;
import org.h2.tools.Csv;
import org.h2.util.DateTimeUtils;
import org.h2.util.MathUtils;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
......@@ -509,10 +509,10 @@ public class MetaTable extends Table {
cols = createColumns(
"ID INT",
"USER_NAME",
"SESSION_START",
"SESSION_START TIMESTAMP WITH TIME ZONE",
"STATEMENT",
"STATEMENT_START",
"CONTAINS_UNCOMMITTED",
"STATEMENT_START TIMESTAMP WITH TIME ZONE",
"CONTAINS_UNCOMMITTED BIT",
"STATE",
"BLOCKER_ID INT"
);
......@@ -1827,13 +1827,13 @@ public class MetaTable extends Table {
// USER_NAME
s.getUser().getName(),
// SESSION_START
new Timestamp(s.getSessionStart()).toString(),
DateTimeUtils.timestampTimeZoneFromMillis(s.getSessionStart()),
// STATEMENT
command == null ? null : command.toString(),
// STATEMENT_START
new Timestamp(start).toString(),
DateTimeUtils.timestampTimeZoneFromMillis(start),
// CONTAINS_UNCOMMITTED
Boolean.toString(s.containsUncommitted()),
ValueBoolean.get(s.containsUncommitted()),
// STATE
String.valueOf(s.getState()),
// BLOCKER_ID
......
......@@ -21,17 +21,7 @@ public final class CurrentTimestamp {
* @return current timestamp
*/
public static ValueTimestampTimeZone get() {
long ms = 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));
return DateTimeUtils.timestampTimeZoneFromMillis(System.currentTimeMillis());
}
private CurrentTimestamp() {
......
......@@ -111,7 +111,7 @@ public class DateTimeUtils {
*
* @return local time zone
*/
static TimeZone getTimeZone() {
private static TimeZone getTimeZone() {
TimeZone tz = timeZone;
if (tz == null) {
timeZone = tz = TimeZone.getDefault();
......@@ -1163,6 +1163,24 @@ public class DateTimeUtils {
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.
*
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论