提交 69317d74 authored 作者: Noel Grandin's avatar Noel Grandin

Issue #229: DATEDIFF does not work for 'WEEK'

上级 2e5761a2
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>Issue #229: DATEDIFF does not work for 'WEEK'
</li>
<li>Issue #156: Add support for getGeneratedKeys() when executing commands via PreparedStatement#executeBatch <li>Issue #156: Add support for getGeneratedKeys() when executing commands via PreparedStatement#executeBatch
</li> </li>
<li>Issue #195: The new Maven uses a .cmd file instead of a .bat file <li>Issue #195: The new Maven uses a .cmd file instead of a .bat file
......
...@@ -21,7 +21,6 @@ import java.util.HashMap; ...@@ -21,7 +21,6 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.Command; import org.h2.command.Command;
import org.h2.command.Parser; import org.h2.command.Parser;
...@@ -1819,7 +1818,9 @@ public class Function extends Expression implements FunctionCall { ...@@ -1819,7 +1818,9 @@ public class Function extends Expression implements FunctionCall {
return t2 - t1; return t2 - t1;
case Calendar.SECOND: case Calendar.SECOND:
case Calendar.MINUTE: case Calendar.MINUTE:
case Calendar.HOUR_OF_DAY: { case Calendar.HOUR_OF_DAY:
case Calendar.DAY_OF_YEAR:
case Calendar.WEEK_OF_YEAR: {
// first 'normalize' the numbers so both are not negative // first 'normalize' the numbers so both are not negative
long hour = 60 * 60 * 1000; long hour = 60 * 60 * 1000;
long add = Math.min(t1 / hour * hour, t2 / hour * hour); long add = Math.min(t1 / hour * hour, t2 / hour * hour);
...@@ -1832,6 +1833,10 @@ public class Function extends Expression implements FunctionCall { ...@@ -1832,6 +1833,10 @@ public class Function extends Expression implements FunctionCall {
return t2 / (60 * 1000) - t1 / (60 * 1000); return t2 / (60 * 1000) - t1 / (60 * 1000);
case Calendar.HOUR_OF_DAY: case Calendar.HOUR_OF_DAY:
return t2 / hour - t1 / hour; return t2 / hour - t1 / hour;
case Calendar.DAY_OF_YEAR:
return t2 / (hour * 24) - t1 / (hour * 24);
case Calendar.WEEK_OF_YEAR:
return t2 / (hour * 24 * 7) - t1 / (hour * 24 * 7);
default: default:
throw DbException.throwInternalError("field:" + field); throw DbException.throwInternalError("field:" + field);
} }
...@@ -1849,9 +1854,12 @@ public class Function extends Expression implements FunctionCall { ...@@ -1849,9 +1854,12 @@ public class Function extends Expression implements FunctionCall {
int month2 = calendar.get(Calendar.MONTH); int month2 = calendar.get(Calendar.MONTH);
int result = year2 - year1; int result = year2 - year1;
if (field == Calendar.MONTH) { if (field == Calendar.MONTH) {
result = 12 * result + (month2 - month1); return 12 * result + (month2 - month1);
} else if (field == Calendar.YEAR) {
return result;
} else {
throw DbException.getUnsupportedException("DATEDIFF " + part);
} }
return result;
} }
private static String substring(String s, int start, int length) { private static String substring(String s, int start, int length) {
......
...@@ -3964,6 +3964,12 @@ SELECT DATEDIFF('HOUR', '1900-01-01 01:00:00.001', '1900-01-01 01:00:01.000'), D ...@@ -3964,6 +3964,12 @@ SELECT DATEDIFF('HOUR', '1900-01-01 01:00:00.001', '1900-01-01 01:00:01.000'), D
> 0 0 > 0 0
> rows: 1 > rows: 1
select datediff(day, '2015-12-09 23:59:00.0', '2016-01-16 23:59:00.0'), datediff(wk, '2015-12-09 23:59:00.0', '2016-01-16 23:59:00.0');
> 38 5
> -- -
> 38 5
> rows: 1
create table test(id int); create table test(id int);
> ok > ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论