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