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

add hour/minute/second/microsecond and millisecond

上级 94d785d6
......@@ -1484,54 +1484,69 @@ public class DateTimeUtils {
// Retrieve the dateValue.
long[] fieldDateAndTime = DateTimeUtils.dateAndTimeFromValue(value);
long dateValue = fieldDateAndTime[0];
long timeNanos = fieldDateAndTime[1];
long timeNanosRetrieved = fieldDateAndTime[1];
long nanoInHours = 3_600_000_000_000l;
long nanoInHour = 3_600_000_000_000l;
long nanoInMinute = 60_000_000_000l;
long nanoInSecond = 1_000_000_000l;
long nanoInMilliSecond = 1_000_000l;
long nanoInMicroSecond = 1_000l;
long timeNanos;
if (timeUnit.equals("HOUR")) {
if (timeUnit.equals("MICROSECONDS")) {
long microseconds = timeNanosRetrieved / nanoInMicroSecond;
timeNanos = microseconds * nanoInMicroSecond;
long hour = timeNanos / nanoInHours;
long hourInNano = hour * nanoInHours;
} else if (timeUnit.equals("MILLISECONDS")) {
if (value instanceof ValueTimestampTimeZone) {
long milliseconds = timeNanosRetrieved / nanoInMilliSecond;
timeNanos = milliseconds * nanoInMilliSecond;
ValueTimestampTimeZone vTmp = (ValueTimestampTimeZone) value;
result = ValueTimestampTimeZone.fromDateValueAndNanos(vTmp.getDateValue(), hourInNano,
vTmp.getTimeZoneOffsetMins());
} else if (timeUnit.equals("SECOND")) {
} else {
long seconds = timeNanosRetrieved / nanoInSecond;
timeNanos = seconds * nanoInSecond;
result = ValueTimestamp.fromDateValueAndNanos(dateValue, hourInNano);
} else if (timeUnit.equals("MINUTE")) {
}
long minutes = timeNanosRetrieved / nanoInMinute;
timeNanos = minutes * nanoInMinute;
} else if (timeUnit.equals("DAY")) {
} else if (timeUnit.equals("HOUR")) {
if (value instanceof ValueTimestampTimeZone) {
long hours = timeNanosRetrieved / nanoInHour;
timeNanos = hours * nanoInHour;
// Create a new ValueTimestampTimeZone by only setting the
// date. The time in nanoseconds since midnight will be set
// to 0.
ValueTimestampTimeZone vTmp = (ValueTimestampTimeZone) value;
result = ValueTimestampTimeZone.fromDateValueAndNanos(vTmp.getDateValue(), 0,
vTmp.getTimeZoneOffsetMins());
} else {
// By default, we create a timestamp by setting the
// datevalue to the datevalue retrieved and the time in
// nanoseconds since midnight to 0.
result = ValueTimestamp.fromDateValueAndNanos(dateValue, 0);
}
}
} else if (timeUnit.equals("DAY")) {
else {
timeNanos = 0l;
} else {
// Return an exception for the other possible value (not yet
// supported).
throw DbException.getUnsupportedException(timeUnit);
}
if (value instanceof ValueTimestampTimeZone) {
// Create a new ValueTimestampTimeZone by only setting the
// date. The time in nanoseconds since midnight will be set
// to 0.
ValueTimestampTimeZone vTmp = (ValueTimestampTimeZone) value;
result = ValueTimestampTimeZone.fromDateValueAndNanos(vTmp.getDateValue(), timeNanos,
vTmp.getTimeZoneOffsetMins());
} else {
// By default, we create a timestamp by setting the
// datevalue to the datevalue retrieved and the time in
// nanoseconds since midnight to 0.
result = ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos);
}
return result;
}
......
--
-- Test time unit 'SECOND'
--
SELECT DATE_TRUNC('SECOND', time '00:00:00');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('second', time '00:00:00');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('SECOND', time '15:00:00');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('second', time '15:00:00');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('SECOND', time '15:14:13');
>> 1970-01-01 15:14:13
SELECT DATE_TRUNC('second', time '15:14:13');
>> 1970-01-01 15:14:13
SELECT DATE_TRUNC('SECOND', date '2015-05-29');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('second', date '2015-05-29');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('SECOND', date '1970-01-01');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('second', date '1970-01-01');
>> 1970-01-01 00:00:00
select DATE_TRUNC('SECOND', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-29 15:14:13+00
select DATE_TRUNC('second', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-29 15:14:13+00
select DATE_TRUNC('SECOND', timestamp with time zone '2015-05-29 15:14:13-06');
>> 2015-05-29 15:14:13-06
select DATE_TRUNC('second', timestamp with time zone '2015-05-29 15:14:13-06');
>> 2015-05-29 15:14:13-06
select DATE_TRUNC('SECOND', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-29 15:14:13+10
select DATE_TRUNC('second', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-29 15:14:13+10
SELECT DATE_TRUNC('second', timestamp '2015-05-29 15:14:13');
>> 2015-05-29 15:14:13
SELECT DATE_TRUNC('SECOND', timestamp '2015-05-29 15:14:13');
>> 2015-05-29 15:14:13
SELECT DATE_TRUNC('second', timestamp '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('SECOND', timestamp '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('second', timestamp '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('SECOND', timestamp '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('second', '2015-05-29 15:14:13');
>> 2015-05-29 15:14:13
SELECT DATE_TRUNC('SECOND', '2015-05-29 15:14:13');
>> 2015-05-29 15:14:13
SELECT DATE_TRUNC('second', '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('SECOND', '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('second', '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('SECOND', '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
--
-- Test time unit 'MINUTE'
--
SELECT DATE_TRUNC('MINUTE', time '00:00:00');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('minute', time '00:00:00');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('MINUTE', time '15:00:00');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('minute', time '15:00:00');
>> 1970-01-01 15:00:00
SELECT DATE_TRUNC('MINUTE', time '15:14:13');
>> 1970-01-01 15:14:00
SELECT DATE_TRUNC('minute', time '15:14:13');
>> 1970-01-01 15:14:00
SELECT DATE_TRUNC('MINUTE', date '2015-05-29');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('minute', date '2015-05-29');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('MINUTE', date '1970-01-01');
>> 1970-01-01 00:00:00
SELECT DATE_TRUNC('minute', date '1970-01-01');
>> 1970-01-01 00:00:00
select DATE_TRUNC('MINUTE', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-29 15:14:00+00
select DATE_TRUNC('minute', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-29 15:14:00+00
select DATE_TRUNC('MINUTE', timestamp with time zone '2015-05-29 15:14:13-06');
>> 2015-05-29 15:14:00-06
select DATE_TRUNC('minute', timestamp with time zone '2015-05-29 15:14:13-06');
>> 2015-05-29 15:14:00-06
select DATE_TRUNC('MINUTE', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-29 15:14:00+10
select DATE_TRUNC('minute', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-29 15:14:00+10
SELECT DATE_TRUNC('minute', timestamp '2015-05-29 15:14:13');
>> 2015-05-29 15:14:00
SELECT DATE_TRUNC('MINUTE', timestamp '2015-05-29 15:14:13');
>> 2015-05-29 15:14:00
SELECT DATE_TRUNC('minute', timestamp '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('MINUTE', timestamp '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('minute', timestamp '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('MINUTE', timestamp '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('minute', '2015-05-29 15:14:13');
>> 2015-05-29 15:14:00
SELECT DATE_TRUNC('MINUTE', '2015-05-29 15:14:13');
>> 2015-05-29 15:14:00
SELECT DATE_TRUNC('minute', '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('MINUTE', '2015-05-29 15:00:00');
>> 2015-05-29 15:00:00
SELECT DATE_TRUNC('minute', '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
SELECT DATE_TRUNC('MINUTE', '2015-05-29 00:00:00');
>> 2015-05-29 00:00:00
--
-- Test time unit 'HOUR'
--
SELECT DATE_TRUNC('HOUR', time '00:00:00');
>> 1970-01-01 00:00:00
......@@ -146,29 +319,17 @@ select DATE_TRUNC('DAY', '2015-05-29 15:14:13');
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('microseconds', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('MICROSECONDS', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('milliseconds', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('MILLISECONDS', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('second', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('SECOND', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('minute', '2015-05-29 15:14:13');
> exception
SELECT DATE_TRUNC('MINUTE', '2015-05-29 15:14:13');
> exception
--SELECT DATE_TRUNC('microseconds', '2015-05-29 15:14:13');
--> exception
--
--SELECT DATE_TRUNC('MICROSECONDS', '2015-05-29 15:14:13');
--> exception
--
--SELECT DATE_TRUNC('milliseconds', '2015-05-29 15:14:13');
--> exception
--
--SELECT DATE_TRUNC('MILLISECONDS', '2015-05-29 15:14:13');
--> exception
SELECT DATE_TRUNC('week', '2015-05-29 15:14:13');
> exception
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论