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

implement hour in date_trunc

上级 458ff1cd
......@@ -1484,9 +1484,28 @@ public class DateTimeUtils {
// Retrieve the dateValue.
long[] fieldDateAndTime = DateTimeUtils.dateAndTimeFromValue(value);
long dateValue = fieldDateAndTime[0];
long timeNanos = fieldDateAndTime[1];
// Case where the date has to be truncated to the day.
if (timeUnit.equals("DAY")) {
long nanoInHours = 3_600_000_000_000l;
if (timeUnit.equals("HOUR")) {
long hour = timeNanos / nanoInHours;
long hourInNano = hour * nanoInHours;
if (value instanceof ValueTimestampTimeZone) {
ValueTimestampTimeZone vTmp = (ValueTimestampTimeZone) value;
result = ValueTimestampTimeZone.fromDateValueAndNanos(vTmp.getDateValue(), hourInNano,
vTmp.getTimeZoneOffsetMins());
} else {
result = ValueTimestamp.fromDateValueAndNanos(dateValue, hourInNano);
}
} else if (timeUnit.equals("DAY")) {
if (value instanceof ValueTimestampTimeZone) {
......@@ -1505,7 +1524,9 @@ public class DateTimeUtils {
result = ValueTimestamp.fromDateValueAndNanos(dateValue, 0);
}
} else {
}
else {
// Return an exception for the other possible value (not yet
// supported).
......
--
-- Test time unit 'HOUR'
--
SELECT DATE_TRUNC('HOUR', time '00:00:00');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('hour', time '00:00:00');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('HOUR', time '15:00:00');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('hour', time '15:00:00');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('HOUR', time '15:14:13');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('hour', time '15:14:13');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('HOUR', date '2015-05-29');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('hour', date '2015-05-29');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('HOUR', date '1970-01-01');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('hour', date '1970-01-01');
>> 1970-01-01 00:00:00
select DATE_TRUNC('HOUR', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-29 15:00:00+00
select DATE_TRUNC('hour', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-29 15:00:00+00
select DATE_TRUNC('HOUR', timestamp with time zone '2015-05-29 15:14:13-06');
>> 2015-05-29 15:00:00-06
select DATE_TRUNC('hour', timestamp with time zone '2015-05-29 15:14:13-06');
>> 2015-05-29 15:00:00-06
select DATE_TRUNC('HOUR', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-29 15:00:00+10
select DATE_TRUNC('hour', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-29 15:00:00+10
SELECT DATE_TRUNC('hour', timestamp '2015-05-29 15:14:13');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('HOUR', timestamp '2015-05-29 15:14:13');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('hour', timestamp '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('HOUR', timestamp '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('hour', timestamp '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('HOUR', timestamp '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('hour', '2015-05-29 15:14:13');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('HOUR', '2015-05-29 15:14:13');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('hour', '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('HOUR', '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('hour', '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('HOUR', '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
--
-- Test time unit 'DAY'
--
select DATE_TRUNC('day', time '00:00:00');
>> 1970-01-01 00:00:00
......@@ -46,6 +139,10 @@ select DATE_TRUNC('day', '2015-05-29 15:14:13');
select DATE_TRUNC('DAY', '2015-05-29 15:14:13');
>> 2015-05-29 00:00:00
--
-- Test unhandled time unit
--
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception
......@@ -73,12 +170,6 @@ SELECT DATE_TRUNC('minute', '2015-05-29 15:14:13');
SELECT DATE_TRUNC('MINUTE', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('hour', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('HOUR', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('week', '2015-05-29 15:14:13');
> exception
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论