提交 e856bfb1 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Extract method DateTimeUtils.dateAndTimeFromValue()

上级 c7348c0d
......@@ -562,33 +562,47 @@ public class DateTimeUtils {
}
/**
* Get the specified field of a date, however with years normalized to
* positive or negative, and month starting with 1.
* Extracts date value and nanos of day from the specified value.
*
* @param date the date value
* @param field the field type, see {@link Function} for constants
* @return the value
* @param value
* value to extract fields from
* @return array with date value and nanos of day
*/
public static int getDatePart(Value date, int field) {
public static long[] dateAndTimeFromValue(Value value) {
long dateValue = EPOCH_DATE_VALUE;
long timeNanos = 0;
if (date instanceof ValueTimestamp) {
ValueTimestamp v = (ValueTimestamp) date;
if (value instanceof ValueTimestamp) {
ValueTimestamp v = (ValueTimestamp) value;
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
} else if (date instanceof ValueDate) {
dateValue = ((ValueDate) date).getDateValue();
} else if (date instanceof ValueTime) {
timeNanos = ((ValueTime) date).getNanos();
} else if (date instanceof ValueTimestampTimeZone) {
ValueTimestampTimeZone v = (ValueTimestampTimeZone) date;
} else if (value instanceof ValueDate) {
dateValue = ((ValueDate) value).getDateValue();
} else if (value instanceof ValueTime) {
timeNanos = ((ValueTime) value).getNanos();
} else if (value instanceof ValueTimestampTimeZone) {
ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
} else {
ValueTimestamp v = (ValueTimestamp) date.convertTo(Value.TIMESTAMP);
ValueTimestamp v = (ValueTimestamp) value.convertTo(Value.TIMESTAMP);
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
}
return new long[] {dateValue, timeNanos};
}
/**
* Get the specified field of a date, however with years normalized to
* positive or negative, and month starting with 1.
*
* @param date the date value
* @param field the field type, see {@link Function} for constants
* @return the value
*/
public static int getDatePart(Value date, int field) {
long[] a = dateAndTimeFromValue(date);
long dateValue = a[0];
long timeNanos = a[1];
switch (field) {
case Function.YEAR:
return yearFromDateValue(dateValue);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论