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

Encode date and time properly in PgServerThread.writeDataColumn()

上级 ac6b7643
......@@ -20,20 +20,16 @@ import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
import java.util.TimeZone;
import org.h2.command.CommandInterface;
import org.h2.engine.ConnectionInfo;
......@@ -44,6 +40,7 @@ import org.h2.jdbc.JdbcResultSet;
import org.h2.jdbc.JdbcStatement;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.util.DateTimeUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.ScriptReader;
......@@ -51,7 +48,10 @@ import org.h2.util.StringUtils;
import org.h2.util.Utils;
import org.h2.value.CaseInsensitiveMap;
import org.h2.value.Value;
import org.h2.value.ValueDate;
import org.h2.value.ValueNull;
import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp;
/**
* One server thread is opened for each client.
......@@ -530,9 +530,8 @@ public class PgServerThread implements Runnable {
sendMessage();
}
private static long toPostgreSeconds(long millis) {
// TODO handle Julian/Gregorian transitions
return millis / 1000 - 946684800L;
private static long toPostgreDays(long dateValue) {
return DateTimeUtils.prolepticGregorianAbsoluteDayFromDateValue(dateValue) - 10_957;
}
private void writeDataColumn(ResultSet rs, int column, int pgType, boolean text)
......@@ -584,45 +583,36 @@ public class PgServerThread implements Runnable {
break;
}
case PgServer.PG_TYPE_DATE: {
Date d = v.getDate();
ValueDate d = (ValueDate) v.convertTo(Value.DATE);
writeInt(4);
long millis = d.getTime();
millis += TimeZone.getDefault().getOffset(millis);
writeInt((int) (toPostgreSeconds(millis) / 86400));
writeInt((int) (toPostgreDays(d.getDateValue())));
break;
}
case PgServer.PG_TYPE_TIME: {
Time t = v.getTime();
ValueTime t = (ValueTime) v.convertTo(Value.TIME);
writeInt(8);
long m = t.getTime();
m += TimeZone.getDefault().getOffset(m);
long m = t.getNanos();
if (INTEGER_DATE_TYPES) {
// long format
m *= 1000;
m /= 1_000;
} else {
// double format
m /= 1000;
m = Double.doubleToLongBits(m);
m = Double.doubleToLongBits(m * 0.000_000_001);
}
dataOut.writeLong(m);
break;
}
case PgServer.PG_TYPE_TIMESTAMP_NO_TMZONE: {
Timestamp t = v.getTimestamp();
ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP);
writeInt(8);
long m = t.getTime();
m += TimeZone.getDefault().getOffset(m);
m = toPostgreSeconds(m);
int nanos = t.getNanos();
if (m < 0 && nanos != 0) {
m--;
}
long m = toPostgreDays(t.getDateValue()) * 86_400;
long nanos = t.getTimeNanos();
if (INTEGER_DATE_TYPES) {
// long format
m = m * 1000000 + nanos / 1000;
m = m * 1_000_000 + nanos / 1_000;
} else {
// double format
m = Double.doubleToLongBits(m + nanos * 0.000000001);
m = Double.doubleToLongBits(m + nanos * 0.000_000_001);
}
dataOut.writeLong(m);
break;
......
......@@ -1041,6 +1041,29 @@ public class DateTimeUtils {
return a;
}
/**
* Calculate the absolute day from an encoded date value in proleptic Gregorian
* calendar.
*
* @param dateValue the date value
* @return the absolute day in proleptic Gregorian calendar
*/
public static long prolepticGregorianAbsoluteDayFromDateValue(long dateValue) {
long y = yearFromDateValue(dateValue);
int m = monthFromDateValue(dateValue);
int d = dayFromDateValue(dateValue);
if (m <= 2) {
y--;
m += 12;
}
long a = ((y * 2922L) >> 3) + DAYS_OFFSET[m - 3] + d - 719484;
if (y < 1901 || y > 2099) {
// Slow mode
a += (y / 400) - (y / 100) + 15;
}
return a;
}
/**
* Calculate the encoded date value from an absolute day.
*
......
......@@ -487,13 +487,16 @@ public class TestPgServer extends TestBase {
Date[] dates = { null, Date.valueOf("2017-02-20"),
Date.valueOf("1970-01-01"), Date.valueOf("1969-12-31"),
Date.valueOf("1940-01-10"), Date.valueOf("1950-11-10") };
Date.valueOf("1940-01-10"), Date.valueOf("1950-11-10"),
Date.valueOf("1500-01-01")};
Time[] times = { null, Time.valueOf("14:15:16"),
Time.valueOf("00:00:00"), Time.valueOf("23:59:59"),
Time.valueOf("00:10:59"), Time.valueOf("08:30:42") };
Time.valueOf("00:10:59"), Time.valueOf("08:30:42"),
Time.valueOf("10:00:00")};
Timestamp[] timestamps = { null, Timestamp.valueOf("2017-02-20 14:15:16.763"),
Timestamp.valueOf("1970-01-01 00:00:00"), Timestamp.valueOf("1969-12-31 23:59:59"),
Timestamp.valueOf("1940-01-10 00:10:59"), Timestamp.valueOf("1950-11-10 08:30:42.12") };
Timestamp.valueOf("1940-01-10 00:10:59"), Timestamp.valueOf("1950-11-10 08:30:42.12"),
Timestamp.valueOf("1500-01-01 10:00:10")};
int count = dates.length;
PreparedStatement ps = conn.prepareStatement(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论