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

Extract method DateTimeUtils.dateAndTimeFromValue()

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