提交 54efc692 authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

Allow DATEADD to take a long value for count when manipulating milliseconds.

上级 a021f586
......@@ -3427,10 +3427,12 @@ CURRENT_TIMESTAMP()
"
"Functions (Time and Date)","DATEADD","
{ DATEADD| TIMESTAMPADD } (unitString, addInt, timestamp)
{ DATEADD| TIMESTAMPADD } (unitString, addIntLong, timestamp)
","
Adds units to a timestamp. The string indicates the unit.
Use negative values to subtract units.
addIntLong may be a long value when manipulating milliseconds,
otherwise it's range is restricted to int.
The same units as in the EXTRACT function are supported.
This method returns a timestamp.
","
......
......@@ -32,6 +32,7 @@ Change Log
Please note it is strongly recommended to avoid calling Thread.interrupt;
this is a problem for various libraries, including Apache Lucene.
</li><li>MVStore: use RandomAccessFile file system if the file name starts with "file:".
</li><li>Allow DATEADD to take a long value for count when manipulating milliseconds.
</li></ul>
<h2>Version 1.4.186 Beta (2015-03-02)</h2>
......
......@@ -1413,7 +1413,7 @@ public class Function extends Expression implements FunctionCall {
break;
case DATE_ADD:
result = ValueTimestamp.get(dateadd(
v0.getString(), v1.getInt(), v2.getTimestamp()));
v0.getString(), v1.getLong(), v2.getTimestamp()));
break;
case DATE_DIFF:
result = ValueLong.get(datediff(
......@@ -1696,12 +1696,22 @@ public class Function extends Expression implements FunctionCall {
return p.intValue();
}
private static Timestamp dateadd(String part, int count, Timestamp d) {
private static Timestamp dateadd(String part, long count, Timestamp d) {
int field = getDatePart(part);
if (field == Calendar.MILLISECOND) {
Timestamp ts = new Timestamp(d.getTime() + count);
ts.setNanos(ts.getNanos() + (d.getNanos() % 1000000));
return ts;
}
// We allow long for manipulating the millisecond component,
// for the rest we only allow int.
if (count > Integer.MAX_VALUE) {
throw DbException.getInvalidValueException("DATEADD count", count);
}
Calendar calendar = Calendar.getInstance();
int nanos = d.getNanos() % 1000000;
calendar.setTime(d);
calendar.add(field, count);
calendar.add(field, (int)count);
long t = calendar.getTime().getTime();
Timestamp ts = new Timestamp(t);
ts.setNanos(ts.getNanos() + nanos);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论