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

Implement 'CENTURY' in 'DATE_TRUNC'

上级 6b216090
...@@ -107,7 +107,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -107,7 +107,7 @@ 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; TIMEZONE_HOUR = 130, TIMEZONE_MINUTE = 131, DECADE = 132, CENTURY = 133;
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,
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
package org.h2.util; package org.h2.util;
import static org.h2.expression.Function.CENTURY;
import static org.h2.expression.Function.DAY_OF_MONTH; 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_WEEK;
import static org.h2.expression.Function.DAY_OF_YEAR; import static org.h2.expression.Function.DAY_OF_YEAR;
...@@ -105,6 +106,7 @@ public final class DateTimeFunctions { ...@@ -105,6 +106,7 @@ public final class DateTimeFunctions {
DATE_PART.put("TIMEZONE_HOUR", TIMEZONE_HOUR); DATE_PART.put("TIMEZONE_HOUR", TIMEZONE_HOUR);
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);
} }
/** /**
...@@ -493,6 +495,14 @@ public final class DateTimeFunctions { ...@@ -493,6 +495,14 @@ public final class DateTimeFunctions {
timeNanos = 0l; timeNanos = 0l;
break; break;
case CENTURY:
long yearForCentury = DateTimeUtils.yearFromDateValue(dateValue);
yearForCentury = (long) (Math.floor( (yearForCentury - 1) / 100) * 100) + 1;
dateValue = DateTimeUtils.dateValue(yearForCentury, 1, 1);
timeNanos = 0l;
break;
} }
if (timeNanos == null) { if (timeNanos == null) {
......
...@@ -928,17 +928,72 @@ SELECT DATE_TRUNC('DECADE', '2010-05-29 15:14:13'); ...@@ -928,17 +928,72 @@ SELECT DATE_TRUNC('DECADE', '2010-05-29 15:14:13');
>> 2010-01-01 00:00:00 >> 2010-01-01 00:00:00
-- --
-- Test unhandled time unit -- Test time unit 'CENTURY'
-- --
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13'); select DATE_TRUNC('century', time '00:00:00');
> exception >> 1901-01-01 00:00:00
select DATE_TRUNC('CENTURY', time '00:00:00');
>> 1901-01-01 00:00:00
select DATE_TRUNC('century', time '15:14:13');
>> 1901-01-01 00:00:00
select DATE_TRUNC('CENTURY', time '15:14:13');
>> 1901-01-01 00:00:00
select DATE_TRUNC('century', date '2015-05-28');
>> 2001-01-01 00:00:00
select DATE_TRUNC('CENTURY', date '2015-05-28');
>> 2001-01-01 00:00:00
select DATE_TRUNC('century', timestamp '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00
select DATE_TRUNC('CENTURY', timestamp '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00
select DATE_TRUNC('century', timestamp with time zone '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00+00
select DATE_TRUNC('CENTURY', timestamp with time zone '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00+00
select DATE_TRUNC('century', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2001-01-01 00:00:00-06
select DATE_TRUNC('CENTURY', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2001-01-01 00:00:00-06
select DATE_TRUNC('century', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2001-01-01 00:00:00+10
select DATE_TRUNC('CENTURY', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2001-01-01 00:00:00+10
SELECT DATE_TRUNC('century', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('century', '2015-05-29 15:14:13');
> exception >> 2001-01-01 00:00:00
SELECT DATE_TRUNC('CENTURY', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('CENTURY', '2015-05-29 15:14:13');
>> 2001-01-01 00:00:00
SELECT DATE_TRUNC('century', '2199-05-29 15:14:13');
>> 2101-01-01 00:00:00
SELECT DATE_TRUNC('CENTURY', '2199-05-29 15:14:13');
>> 2101-01-01 00:00:00
SELECT DATE_TRUNC('century', '2000-05-29 15:14:13');
>> 1901-01-01 00:00:00
SELECT DATE_TRUNC('CENTURY', '2000-05-29 15:14:13');
>> 1901-01-01 00:00:00
--
-- Test unhandled time unit
--
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception > exception
SELECT DATE_TRUNC('millennium', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('millennium', '2015-05-29 15:14:13');
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论