提交 6b216090 authored 作者: Stéphane Eintrazi's avatar Stéphane Eintrazi

Implement 'DECADE' in 'DATE_TRUNC'

上级 c7dd089e
......@@ -107,7 +107,7 @@ public class Function extends Expression implements FunctionCall {
* Pseudo functions for DATEADD, DATEDIFF, and EXTRACT.
*/
public static final int MILLISECOND = 126, EPOCH = 127, MICROSECOND = 128, NANOSECOND = 129,
TIMEZONE_HOUR = 130, TIMEZONE_MINUTE = 131;
TIMEZONE_HOUR = 130, TIMEZONE_MINUTE = 131, DECADE = 132;
public static final int DATABASE = 150, USER = 151, CURRENT_USER = 152,
IDENTITY = 153, SCOPE_IDENTITY = 154, AUTOCOMMIT = 155,
......
......@@ -8,6 +8,7 @@ package org.h2.util;
import static org.h2.expression.Function.DAY_OF_MONTH;
import static org.h2.expression.Function.DAY_OF_WEEK;
import static org.h2.expression.Function.DAY_OF_YEAR;
import static org.h2.expression.Function.DECADE;
import static org.h2.expression.Function.EPOCH;
import static org.h2.expression.Function.HOUR;
import static org.h2.expression.Function.ISO_DAY_OF_WEEK;
......@@ -24,7 +25,6 @@ import static org.h2.expression.Function.TIMEZONE_HOUR;
import static org.h2.expression.Function.TIMEZONE_MINUTE;
import static org.h2.expression.Function.WEEK;
import static org.h2.expression.Function.YEAR;
import java.math.BigDecimal;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
......@@ -32,7 +32,6 @@ import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.TimeZone;
import org.h2.api.ErrorCode;
import org.h2.expression.Function;
import org.h2.message.DbException;
......@@ -105,6 +104,7 @@ public final class DateTimeFunctions {
DATE_PART.put("NS", NANOSECOND);
DATE_PART.put("TIMEZONE_HOUR", TIMEZONE_HOUR);
DATE_PART.put("TIMEZONE_MINUTE", TIMEZONE_MINUTE);
DATE_PART.put("DECADE", DECADE);
}
/**
......@@ -216,7 +216,7 @@ public final class DateTimeFunctions {
}
/**
* Calculate the number of crossed unit boundaries between two timestamps. This
* Calculate the number) of crossed unit boundaries between two timestamps. This
* method is supported for MS SQL Server compatibility.
*
* <pre>
......@@ -485,6 +485,14 @@ public final class DateTimeFunctions {
timeNanos = 0l;
break;
case DECADE:
long yearForDecade = DateTimeUtils.yearFromDateValue(dateValue);
yearForDecade = (long) (Math.floor(yearForDecade / 10) * 10);
dateValue = DateTimeUtils.dateValue(yearForDecade, 1, 1);
timeNanos = 0l;
break;
}
if (timeNanos == null) {
......
......@@ -870,6 +870,62 @@ SELECT DATE_TRUNC('year', '2015-05-29 15:14:13');
SELECT DATE_TRUNC('YEAR', '2015-05-29 15:14:13');
>> 2015-01-01 00:00:00
--
-- Test time unit 'DECADE'
--
select DATE_TRUNC('decade', time '00:00:00');
>> 1970-01-01 00:00:00
select DATE_TRUNC('DECADE', time '00:00:00');
>> 1970-01-01 00:00:00
select DATE_TRUNC('decade', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('DECADE', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('decade', date '2015-05-28');
>> 2010-01-01 00:00:00
select DATE_TRUNC('DECADE', date '2015-05-28');
>> 2010-01-01 00:00:00
select DATE_TRUNC('decade', timestamp '2015-05-29 15:14:13');
>> 2010-01-01 00:00:00
select DATE_TRUNC('DECADE', timestamp '2015-05-29 15:14:13');
>> 2010-01-01 00:00:00
select DATE_TRUNC('decade', timestamp with time zone '2015-05-29 15:14:13');
>> 2010-01-01 00:00:00+00
select DATE_TRUNC('DECADE', timestamp with time zone '2015-05-29 15:14:13');
>> 2010-01-01 00:00:00+00
select DATE_TRUNC('decade', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2010-01-01 00:00:00-06
select DATE_TRUNC('DECADE', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2010-01-01 00:00:00-06
select DATE_TRUNC('decade', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2010-01-01 00:00:00+10
select DATE_TRUNC('DECADE', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2010-01-01 00:00:00+10
SELECT DATE_TRUNC('decade', '2015-05-29 15:14:13');
>> 2010-01-01 00:00:00
SELECT DATE_TRUNC('DECADE', '2015-05-29 15:14:13');
>> 2010-01-01 00:00:00
SELECT DATE_TRUNC('decade', '2010-05-29 15:14:13');
>> 2010-01-01 00:00:00
SELECT DATE_TRUNC('DECADE', '2010-05-29 15:14:13');
>> 2010-01-01 00:00:00
--
-- Test unhandled time unit
......@@ -877,11 +933,7 @@ SELECT DATE_TRUNC('YEAR', '2015-05-29 15:14:13');
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('decade', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('DECADE', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('century', '2015-05-29 15:14:13');
> exception
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论