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

add comment

上级 44350ad1
...@@ -1481,20 +1481,27 @@ public class DateTimeUtils { ...@@ -1481,20 +1481,27 @@ public class DateTimeUtils {
public static Value truncateDate(String timeUnit, Value value) { public static Value truncateDate(String timeUnit, Value value) {
Value result; Value result;
// Retrieve the dateValue. // Retrieve the dateValue and the time in nanoseconds if the date.
long[] fieldDateAndTime = DateTimeUtils.dateAndTimeFromValue(value); long[] fieldDateAndTime = DateTimeUtils.dateAndTimeFromValue(value);
long dateValue = fieldDateAndTime[0]; long dateValue = fieldDateAndTime[0];
long timeNanosRetrieved = fieldDateAndTime[1]; long timeNanosRetrieved = fieldDateAndTime[1];
// Variable that contains the number of nanoseconds in 'hour', 'minute',
// etc.
long nanoInHour = 3_600_000_000_000l; long nanoInHour = 3_600_000_000_000l;
long nanoInMinute = 60_000_000_000l; long nanoInMinute = 60_000_000_000l;
long nanoInSecond = 1_000_000_000l; long nanoInSecond = 1_000_000_000l;
long nanoInMilliSecond = 1_000_000l; long nanoInMilliSecond = 1_000_000l;
long nanoInMicroSecond = 1_000l; long nanoInMicroSecond = 1_000l;
// Variable used to the time in nanoseconds of the date truncated.
long timeNanos; long timeNanos;
// Compute the number of time unit in the date, for example, the
// number of time unit 'HOUR' in '15:14:13' is '15'. Then convert the
// result to nanoseconds.
if (timeUnit.equals("MICROSECONDS")) { if (timeUnit.equals("MICROSECONDS")) {
long microseconds = timeNanosRetrieved / nanoInMicroSecond; long microseconds = timeNanosRetrieved / nanoInMicroSecond;
timeNanos = microseconds * nanoInMicroSecond; timeNanos = microseconds * nanoInMicroSecond;
...@@ -1521,29 +1528,26 @@ public class DateTimeUtils { ...@@ -1521,29 +1528,26 @@ public class DateTimeUtils {
} else if (timeUnit.equals("DAY")) { } else if (timeUnit.equals("DAY")) {
timeNanos = 0l; timeNanos = 0l;
} else { } else {
// Return an exception for the other possible value (not yet // Return an exception for the other possible value (not yet
// supported). // supported).
throw DbException.getUnsupportedException(timeUnit); throw DbException.getUnsupportedException(timeUnit);
} }
if (value instanceof ValueTimestampTimeZone) { if (value instanceof ValueTimestampTimeZone) {
// Create a new ValueTimestampTimeZone by only setting the // Case we create a timestamp with timezone with the dateValue and
// date. The time in nanoseconds since midnight will be set // timeNanos computed.
// to 0.
ValueTimestampTimeZone vTmp = (ValueTimestampTimeZone) value; ValueTimestampTimeZone vTmp = (ValueTimestampTimeZone) value;
result = ValueTimestampTimeZone.fromDateValueAndNanos(vTmp.getDateValue(), timeNanos, result = ValueTimestampTimeZone.fromDateValueAndNanos(vTmp.getDateValue(), timeNanos,
vTmp.getTimeZoneOffsetMins()); vTmp.getTimeZoneOffsetMins());
} else { } else {
// By default, we create a timestamp by setting the // By default, we create a timestamp with the dateValue and
// datevalue to the datevalue retrieved and the time in // timeNanos computed.
// nanoseconds since midnight to 0.
result = ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos); result = ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论