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

Implement 'MILLENNIUM' in 'DATE_TRUNC'

上级 fdcee04f
...@@ -107,7 +107,8 @@ public class Function extends Expression implements FunctionCall { ...@@ -107,7 +107,8 @@ public class Function extends Expression implements FunctionCall {
* Pseudo functions for DATEADD, DATEDIFF, and EXTRACT. * Pseudo functions for DATEADD, DATEDIFF, and EXTRACT.
*/ */
public static final int MILLISECOND = 126, EPOCH = 127, MICROSECOND = 128, NANOSECOND = 129, public static final int MILLISECOND = 126, EPOCH = 127, MICROSECOND = 128, NANOSECOND = 129,
TIMEZONE_HOUR = 130, TIMEZONE_MINUTE = 131, DECADE = 132, CENTURY = 133; TIMEZONE_HOUR = 130, TIMEZONE_MINUTE = 131, DECADE = 132, CENTURY = 133,
MILLENNIUM = 134;
public static final int DATABASE = 150, USER = 151, CURRENT_USER = 152, public static final int DATABASE = 150, USER = 151, CURRENT_USER = 152,
IDENTITY = 153, SCOPE_IDENTITY = 154, AUTOCOMMIT = 155, IDENTITY = 153, SCOPE_IDENTITY = 154, AUTOCOMMIT = 155,
......
...@@ -26,6 +26,7 @@ import static org.h2.expression.Function.TIMEZONE_HOUR; ...@@ -26,6 +26,7 @@ import static org.h2.expression.Function.TIMEZONE_HOUR;
import static org.h2.expression.Function.TIMEZONE_MINUTE; import static org.h2.expression.Function.TIMEZONE_MINUTE;
import static org.h2.expression.Function.WEEK; import static org.h2.expression.Function.WEEK;
import static org.h2.expression.Function.YEAR; import static org.h2.expression.Function.YEAR;
import static org.h2.expression.Function.MILLENNIUM;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DateFormatSymbols; import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -107,6 +108,7 @@ public final class DateTimeFunctions { ...@@ -107,6 +108,7 @@ public final class DateTimeFunctions {
DATE_PART.put("TIMEZONE_MINUTE", TIMEZONE_MINUTE); DATE_PART.put("TIMEZONE_MINUTE", TIMEZONE_MINUTE);
DATE_PART.put("DECADE", DECADE); DATE_PART.put("DECADE", DECADE);
DATE_PART.put("CENTURY", CENTURY); DATE_PART.put("CENTURY", CENTURY);
DATE_PART.put("MILLENNIUM", MILLENNIUM);
} }
/** /**
...@@ -503,7 +505,16 @@ public final class DateTimeFunctions { ...@@ -503,7 +505,16 @@ public final class DateTimeFunctions {
timeNanos = 0l; timeNanos = 0l;
break; break;
case MILLENNIUM:
long yearForMillennium = DateTimeUtils.yearFromDateValue(dateValue);
yearForMillennium = (long) (Math.floor((yearForMillennium - 1) / 1000) * 1000) + 1;
dateValue = DateTimeUtils.dateValue(yearForMillennium, 1, 1);
timeNanos = 0l;
break;
} }
if (timeNanos == null) { if (timeNanos == null) {
// Return an exception in the timeUnit is not recognized // Return an exception in the timeUnit is not recognized
......
...@@ -990,14 +990,84 @@ SELECT DATE_TRUNC('century', '2000-05-29 15:14:13'); ...@@ -990,14 +990,84 @@ SELECT DATE_TRUNC('century', '2000-05-29 15:14:13');
SELECT DATE_TRUNC('CENTURY', '2000-05-29 15:14:13'); SELECT DATE_TRUNC('CENTURY', '2000-05-29 15:14:13');
>> 1901-01-01 00:00:00 >> 1901-01-01 00:00:00
SELECT DATE_TRUNC('century', '2001-05-29 15:14:13');
>> 2001-01-01 00:00:00
SELECT DATE_TRUNC('CENTURY', '2001-05-29 15:14:13');
>> 2001-01-01 00:00:00
-- --
-- Test unhandled time unit -- Test time unit 'MILLENNIUM'
--
select DATE_TRUNC('millennium', time '00:00:00');
>> 1001-01-01 00:00:00
select DATE_TRUNC('MILLENNIUM', time '00:00:00');
>> 1001-01-01 00:00:00
select DATE_TRUNC('millennium', time '15:14:13');
>> 1001-01-01 00:00:00
select DATE_TRUNC('MILLENNIUM', time '15:14:13');
>> 1001-01-01 00:00:00
select DATE_TRUNC('millennium', date '2015-05-28');
>> 2001-01-01 00:00:00
select DATE_TRUNC('MILLENNIUM', date '2015-05-28');
>> 2001-01-01 00:00:00
select DATE_TRUNC('millennium', timestamp '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00
select DATE_TRUNC('MILLENNIUM', timestamp '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00
select DATE_TRUNC('millennium', timestamp with time zone '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00+00
select DATE_TRUNC('MILLENNIUM', timestamp with time zone '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00+00
select DATE_TRUNC('millennium', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2001-01-01 00:00:00-06
select DATE_TRUNC('MILLENNIUM', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2001-01-01 00:00:00-06
select DATE_TRUNC('millennium', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2001-01-01 00:00:00+10
select DATE_TRUNC('MILLENNIUM', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2001-01-01 00:00:00+10
SELECT DATE_TRUNC('millennium', '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00
SELECT DATE_TRUNC('MILLENNIUM', '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00
SELECT DATE_TRUNC('millennium', '2001-05-29 15:14:13');
>> 2001-01-01 00:00:00
SELECT DATE_TRUNC('MILLENNIUM', '2001-05-29 15:14:13');
>> 2001-01-01 00:00:00
SELECT DATE_TRUNC('millennium', '2000-05-29 15:14:13');
>> 1001-01-01 00:00:00
SELECT DATE_TRUNC('MILLENNIUM', '2000-05-29 15:14:13');
>> 1001-01-01 00:00:00
--
-- Test unhandled time unit and bad date
-- --
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception > exception
SELECT DATE_TRUNC('millennium', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('', '2015-05-29 15:14:13');
> exception > exception
SELECT DATE_TRUNC('MILLENNIUM', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('YEAR', '');
> exception > exception
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论