提交 7617a13d authored 作者: noelgrandin's avatar noelgrandin

reduce unnecessary object allocation of date/time types

上级 d2117a53
...@@ -872,14 +872,14 @@ public class Function extends Expression implements FunctionCall { ...@@ -872,14 +872,14 @@ public class Function extends Expression implements FunctionCall {
case CURRENT_DATE: { case CURRENT_DATE: {
long now = session.getTransactionStart(); long now = session.getTransactionStart();
// need to normalize // need to normalize
result = ValueDate.get(new Date(now)); result = ValueDate.fromMillis(now);
break; break;
} }
case CURTIME: case CURTIME:
case CURRENT_TIME: { case CURRENT_TIME: {
long now = session.getTransactionStart(); long now = session.getTransactionStart();
// need to normalize // need to normalize
result = ValueTime.get(new Time(now)); result = ValueTime.fromMillis(now);
break; break;
} }
case NOW: case NOW:
...@@ -1769,10 +1769,10 @@ public class Function extends Expression implements FunctionCall { ...@@ -1769,10 +1769,10 @@ public class Function extends Expression implements FunctionCall {
default: default:
break; break;
} }
calendar.setTime(new Timestamp(t1)); calendar.setTimeInMillis(t1);
int year1 = calendar.get(Calendar.YEAR); int year1 = calendar.get(Calendar.YEAR);
int month1 = calendar.get(Calendar.MONTH); int month1 = calendar.get(Calendar.MONTH);
calendar.setTime(new Timestamp(t2)); calendar.setTimeInMillis(t2);
int year2 = calendar.get(Calendar.YEAR); int year2 = calendar.get(Calendar.YEAR);
int month2 = calendar.get(Calendar.MONTH); int month2 = calendar.get(Calendar.MONTH);
int result = year2 - year1; int result = year2 - year1;
......
...@@ -226,7 +226,7 @@ public class TraceSystem implements TraceWriter { ...@@ -226,7 +226,7 @@ public class TraceSystem implements TraceWriter {
if (dateFormat == null) { if (dateFormat == null) {
dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss "); dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss ");
} }
return dateFormat.format(new Date()) + module + ": " + s; return dateFormat.format(System.currentTimeMillis()) + module + ": " + s;
} }
@Override @Override
......
...@@ -273,7 +273,7 @@ public class WebServer implements Service { ...@@ -273,7 +273,7 @@ public class WebServer implements Service {
SimpleDateFormat format = new SimpleDateFormat( SimpleDateFormat format = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z", new Locale("en", "")); "EEE, d MMM yyyy HH:mm:ss z", new Locale("en", ""));
format.setTimeZone(TimeZone.getTimeZone("GMT")); format.setTimeZone(TimeZone.getTimeZone("GMT"));
startDateTime = format.format(new Date()); startDateTime = format.format(System.currentTimeMillis());
} }
return startDateTime; return startDateTime;
} }
......
...@@ -746,8 +746,7 @@ public class Data { ...@@ -746,8 +746,7 @@ public class Data {
} }
case Value.DATE: { case Value.DATE: {
long x = readVarLong() * MILLIS_PER_MINUTE; long x = readVarLong() * MILLIS_PER_MINUTE;
return ValueDate.get(new Date( return ValueDate.fromMillis(DateTimeUtils.getTimeUTCWithoutDst(x));
DateTimeUtils.getTimeUTCWithoutDst(x)));
} }
case LOCAL_TIME: { case LOCAL_TIME: {
long nanos = readVarLong() * 1000000 + readVarLong(); long nanos = readVarLong() * 1000000 + readVarLong();
...@@ -755,8 +754,8 @@ public class Data { ...@@ -755,8 +754,8 @@ public class Data {
} }
case Value.TIME: case Value.TIME:
// need to normalize the year, month and day // need to normalize the year, month and day
return ValueTime.get(new Time( return ValueTime.fromMillis(
DateTimeUtils.getTimeUTCWithoutDst(readVarLong()))); DateTimeUtils.getTimeUTCWithoutDst(readVarLong()));
case LOCAL_TIMESTAMP: { case LOCAL_TIMESTAMP: {
long dateValue = readVarLong(); long dateValue = readVarLong();
long nanos = readVarLong() * 1000000 + readVarLong(); long nanos = readVarLong() * 1000000 + readVarLong();
......
...@@ -298,8 +298,7 @@ public class Column { ...@@ -298,8 +298,7 @@ public class Column {
} else if (dt.type == Value.TIME) { } else if (dt.type == Value.TIME) {
value = ValueTime.fromNanos(0); value = ValueTime.fromNanos(0);
} else if (dt.type == Value.DATE) { } else if (dt.type == Value.DATE) {
value = ValueDate.get(new Date( value = ValueDate.fromMillis(session.getTransactionStart());
session.getTransactionStart()));
} else { } else {
value = ValueString.get("").convertTo(type); value = ValueString.get("").convertTo(type);
} }
......
...@@ -551,16 +551,16 @@ public class Transfer { ...@@ -551,16 +551,16 @@ public class Transfer {
if (version >= Constants.TCP_PROTOCOL_VERSION_9) { if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
return ValueDate.fromDateValue(readLong()); return ValueDate.fromDateValue(readLong());
} else if (version >= Constants.TCP_PROTOCOL_VERSION_7) { } else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
return ValueDate.get(new Date(DateTimeUtils.getTimeUTCWithoutDst(readLong()))); return ValueDate.fromMillis(DateTimeUtils.getTimeUTCWithoutDst(readLong()));
} }
return ValueDate.get(new Date(readLong())); return ValueDate.fromMillis(readLong());
case Value.TIME: case Value.TIME:
if (version >= Constants.TCP_PROTOCOL_VERSION_9) { if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
return ValueTime.fromNanos(readLong()); return ValueTime.fromNanos(readLong());
} else if (version >= Constants.TCP_PROTOCOL_VERSION_7) { } else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
return ValueTime.get(new Time(DateTimeUtils.getTimeUTCWithoutDst(readLong()))); return ValueTime.fromMillis(DateTimeUtils.getTimeUTCWithoutDst(readLong()));
} }
return ValueTime.get(new Time(readLong())); return ValueTime.fromMillis(readLong());
case Value.TIMESTAMP: { case Value.TIMESTAMP: {
if (version >= Constants.TCP_PROTOCOL_VERSION_9) { if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
return ValueTimestamp.fromDateValueAndNanos(readLong(), readLong()); return ValueTimestamp.fromDateValueAndNanos(readLong(), readLong());
......
...@@ -58,6 +58,17 @@ public class ValueDate extends Value { ...@@ -58,6 +58,17 @@ public class ValueDate extends Value {
return fromDateValue(DateTimeUtils.dateValueFromDate(date.getTime())); return fromDateValue(DateTimeUtils.dateValueFromDate(date.getTime()));
} }
/**
* Calculate the date value (in the default timezone) from a given time in
* milliseconds in UTC.
*
* @param ms the milliseconds
* @return the value
*/
public static ValueDate fromMillis(long ms) {
return fromDateValue(DateTimeUtils.dateValueFromDate(ms));
}
/** /**
* Parse a string to a ValueDate. * Parse a string to a ValueDate.
* *
......
...@@ -58,6 +58,17 @@ public class ValueTime extends Value { ...@@ -58,6 +58,17 @@ public class ValueTime extends Value {
return fromNanos(DateTimeUtils.nanosFromDate(time.getTime())); return fromNanos(DateTimeUtils.nanosFromDate(time.getTime()));
} }
/**
* Calculate the time value from a given time in
* milliseconds in UTC.
*
* @param ms the milliseconds
* @return the value
*/
public static ValueTime fromMillis(long ms) {
return fromNanos(DateTimeUtils.nanosFromDate(ms));
}
/** /**
* Parse a string to a ValueTime. * Parse a string to a ValueTime.
* *
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论