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

Implement 'MONTH' in 'DATE_TRUNC'

上级 67f341e8
...@@ -26,10 +26,8 @@ import static org.h2.expression.Function.WEEK; ...@@ -26,10 +26,8 @@ import static org.h2.expression.Function.WEEK;
import static org.h2.expression.Function.YEAR; import static org.h2.expression.Function.YEAR;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.Date;
import java.text.DateFormatSymbols; import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
...@@ -452,6 +450,14 @@ public final class DateTimeFunctions { ...@@ -452,6 +450,14 @@ public final class DateTimeFunctions {
} }
timeNanos = 0l; timeNanos = 0l;
break; break;
case MONTH:
long year = DateTimeUtils.yearFromDateValue(dateValue);
int month = DateTimeUtils.monthFromDateValue(dateValue);
dateValue = DateTimeUtils.dateValue(year, month, 1);
timeNanos = 0l;
break;
} }
if (timeNanos == null) { if (timeNanos == null) {
......
...@@ -657,15 +657,73 @@ SELECT DATE_TRUNC('week', '2018-03-14 00:00:00.000'); ...@@ -657,15 +657,73 @@ SELECT DATE_TRUNC('week', '2018-03-14 00:00:00.000');
>> 2018-03-12 00:00:00 >> 2018-03-12 00:00:00
-- --
-- Test unhandled time unit -- Test time unit 'MONTH'
-- --
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13'); select DATE_TRUNC('month', time '00:00:00');
> exception >> 1970-01-01 00:00:00
select DATE_TRUNC('MONTH', time '00:00:00');
>> 1970-01-01 00:00:00
select DATE_TRUNC('month', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('MONTH', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('month', date '2015-05-28');
>> 2015-05-01 00:00:00
select DATE_TRUNC('MONTH', date '2015-05-28');
>> 2015-05-01 00:00:00
select DATE_TRUNC('month', timestamp '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00
select DATE_TRUNC('MONTH', timestamp '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00
select DATE_TRUNC('month', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00+00
select DATE_TRUNC('MONTH', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00+00
select DATE_TRUNC('month', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2015-05-01 00:00:00-06
select DATE_TRUNC('MONTH', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2015-05-01 00:00:00-06
select DATE_TRUNC('month', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-01 00:00:00+10
select DATE_TRUNC('MONTH', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-01 00:00:00+10
select DATE_TRUNC('month', '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00
select DATE_TRUNC('MONTH', '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00
SELECT DATE_TRUNC('MONTH', '2018-03-14 00:00:00.000');
>> 2018-03-01 00:00:00
SELECT DATE_TRUNC('month', '2018-03-14 00:00:00.000');
>> 2018-03-01 00:00:00
SELECT DATE_TRUNC('month', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('month', '2015-05-29 15:14:13');
> exception >> 2015-05-01 00:00:00
SELECT DATE_TRUNC('MONTH', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('MONTH', '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00
--
-- Test unhandled time unit
--
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception > exception
SELECT DATE_TRUNC('quarter', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('quarter', '2015-05-29 15:14:13');
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论