提交 8998eab6 authored 作者: Stéphane Eintrazi's avatar Stéphane Eintrazi

Remove useless Math.floor and add block scope

上级 42cda05c
...@@ -16,6 +16,7 @@ import static org.h2.expression.Function.ISO_DAY_OF_WEEK; ...@@ -16,6 +16,7 @@ import static org.h2.expression.Function.ISO_DAY_OF_WEEK;
import static org.h2.expression.Function.ISO_WEEK; import static org.h2.expression.Function.ISO_WEEK;
import static org.h2.expression.Function.ISO_YEAR; import static org.h2.expression.Function.ISO_YEAR;
import static org.h2.expression.Function.MICROSECOND; import static org.h2.expression.Function.MICROSECOND;
import static org.h2.expression.Function.MILLENNIUM;
import static org.h2.expression.Function.MILLISECOND; import static org.h2.expression.Function.MILLISECOND;
import static org.h2.expression.Function.MINUTE; import static org.h2.expression.Function.MINUTE;
import static org.h2.expression.Function.MONTH; import static org.h2.expression.Function.MONTH;
...@@ -26,7 +27,6 @@ import static org.h2.expression.Function.TIMEZONE_HOUR; ...@@ -26,7 +27,6 @@ import static org.h2.expression.Function.TIMEZONE_HOUR;
import static org.h2.expression.Function.TIMEZONE_MINUTE; import static org.h2.expression.Function.TIMEZONE_MINUTE;
import static org.h2.expression.Function.WEEK; import static org.h2.expression.Function.WEEK;
import static org.h2.expression.Function.YEAR; import static org.h2.expression.Function.YEAR;
import static org.h2.expression.Function.MILLENNIUM;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DateFormatSymbols; import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -389,7 +389,7 @@ public final class DateTimeFunctions { ...@@ -389,7 +389,7 @@ public final class DateTimeFunctions {
* @return date truncated to 'day' * @return date truncated to 'day'
*/ */
public static Value truncateDate(String timeUnitStr, Value value) { public static Value truncateDate(String timeUnitStr, Value value) {
Value result;
int timeUnit = getDatePart(timeUnitStr); int timeUnit = getDatePart(timeUnitStr);
// Retrieve the dateValue and the time in nanoseconds if the date. // Retrieve the dateValue and the time in nanoseconds if the date.
...@@ -398,7 +398,7 @@ public final class DateTimeFunctions { ...@@ -398,7 +398,7 @@ public final class DateTimeFunctions {
long timeNanosRetrieved = fieldDateAndTime[1]; long timeNanosRetrieved = fieldDateAndTime[1];
// Variable used to the time in nanoseconds of the date truncated. // Variable used to the time in nanoseconds of the date truncated.
Long timeNanos = null; long timeNanos;
// Compute the number of time unit in the date, for example, the // 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 // number of time unit 'HOUR' in '15:14:13' is '15'. Then convert the
...@@ -407,42 +407,42 @@ public final class DateTimeFunctions { ...@@ -407,42 +407,42 @@ public final class DateTimeFunctions {
case MICROSECOND: case MICROSECOND:
long nanoInMicroSecond = 1_000l; long nanoInMicroSecond = 1_000L;
long microseconds = timeNanosRetrieved / nanoInMicroSecond; long microseconds = timeNanosRetrieved / nanoInMicroSecond;
timeNanos = microseconds * nanoInMicroSecond; timeNanos = microseconds * nanoInMicroSecond;
break; break;
case MILLISECOND: case MILLISECOND:
long nanoInMilliSecond = 1_000_000l; long nanoInMilliSecond = 1_000_000L;
long milliseconds = timeNanosRetrieved / nanoInMilliSecond; long milliseconds = timeNanosRetrieved / nanoInMilliSecond;
timeNanos = milliseconds * nanoInMilliSecond; timeNanos = milliseconds * nanoInMilliSecond;
break; break;
case SECOND: case SECOND:
long nanoInSecond = 1_000_000_000l; long nanoInSecond = 1_000_000_000L;
long seconds = timeNanosRetrieved / nanoInSecond; long seconds = timeNanosRetrieved / nanoInSecond;
timeNanos = seconds * nanoInSecond; timeNanos = seconds * nanoInSecond;
break; break;
case MINUTE: case MINUTE:
long nanoInMinute = 60_000_000_000l; long nanoInMinute = 60_000_000_000L;
long minutes = timeNanosRetrieved / nanoInMinute; long minutes = timeNanosRetrieved / nanoInMinute;
timeNanos = minutes * nanoInMinute; timeNanos = minutes * nanoInMinute;
break; break;
case HOUR: case HOUR:
long nanoInHour = 3_600_000_000_000l; long nanoInHour = 3_600_000_000_000L;
long hours = timeNanosRetrieved / nanoInHour; long hours = timeNanosRetrieved / nanoInHour;
timeNanos = hours * nanoInHour; timeNanos = hours * nanoInHour;
break; break;
case DAY_OF_MONTH: case DAY_OF_MONTH:
timeNanos = 0l; timeNanos = 0L;
break; break;
case WEEK: case WEEK:
...@@ -452,77 +452,72 @@ public final class DateTimeFunctions { ...@@ -452,77 +452,72 @@ public final class DateTimeFunctions {
if (dayOfWeek != 1) { if (dayOfWeek != 1) {
dateValue = DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay - dayOfWeek + 1); dateValue = DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay - dayOfWeek + 1);
} }
timeNanos = 0l; timeNanos = 0L;
break; break;
case MONTH: case MONTH: {
long year = DateTimeUtils.yearFromDateValue(dateValue); long year = DateTimeUtils.yearFromDateValue(dateValue);
int month = DateTimeUtils.monthFromDateValue(dateValue); int month = DateTimeUtils.monthFromDateValue(dateValue);
dateValue = DateTimeUtils.dateValue(year, month, 1); dateValue = DateTimeUtils.dateValue(year, month, 1);
timeNanos = 0l; timeNanos = 0L;
break; break;
case QUARTER: }
case QUARTER: {
long yearForQuarter = DateTimeUtils.yearFromDateValue(dateValue);
int monthForQuarter = DateTimeUtils.monthFromDateValue(dateValue);
// Round the month to lower quarter
if (monthForQuarter >= 10) {
monthForQuarter = 10;
} else if (monthForQuarter >= 7) {
monthForQuarter = 7;
} else if (monthForQuarter >= 4) {
monthForQuarter = 4;
} else {
monthForQuarter = 1;
}
dateValue = DateTimeUtils.dateValue(yearForQuarter, monthForQuarter, 1); long year = DateTimeUtils.yearFromDateValue(dateValue);
timeNanos = 0l; int month = DateTimeUtils.monthFromDateValue(dateValue);
month = ((month - 1) / 3) * 3 + 1;
dateValue = DateTimeUtils.dateValue(year, month, 1);
timeNanos = 0L;
break; break;
case YEAR: }
case YEAR: {
long yearForYear = DateTimeUtils.yearFromDateValue(dateValue); long year = DateTimeUtils.yearFromDateValue(dateValue);
dateValue = DateTimeUtils.dateValue(yearForYear, 1, 1); dateValue = DateTimeUtils.dateValue(year, 1, 1);
timeNanos = 0l; timeNanos = 0L;
break; break;
case DECADE: }
case DECADE: {
long yearForDecade = DateTimeUtils.yearFromDateValue(dateValue); long year = DateTimeUtils.yearFromDateValue(dateValue);
yearForDecade = (long) (Math.floor(yearForDecade / 10) * 10); year = (year / 10) * 10;
dateValue = DateTimeUtils.dateValue(yearForDecade, 1, 1); dateValue = DateTimeUtils.dateValue(year, 1, 1);
timeNanos = 0l; timeNanos = 0L;
break; break;
case CENTURY: }
case CENTURY: {
long yearForCentury = DateTimeUtils.yearFromDateValue(dateValue); long year = DateTimeUtils.yearFromDateValue(dateValue);
yearForCentury = (long) (Math.floor((yearForCentury - 1) / 100) * 100) + 1; year = ((year - 1) / 100) * 100 + 1;
dateValue = DateTimeUtils.dateValue(yearForCentury, 1, 1); dateValue = DateTimeUtils.dateValue(year, 1, 1);
timeNanos = 0l; timeNanos = 0L;
break; break;
case MILLENNIUM: }
case MILLENNIUM: {
long yearForMillennium = DateTimeUtils.yearFromDateValue(dateValue); long year = DateTimeUtils.yearFromDateValue(dateValue);
yearForMillennium = (long) (Math.floor((yearForMillennium - 1) / 1000) * 1000) + 1; year = ((year - 1) / 1000) * 1000 + 1;
dateValue = DateTimeUtils.dateValue(yearForMillennium, 1, 1); dateValue = DateTimeUtils.dateValue(year, 1, 1);
timeNanos = 0l; timeNanos = 0L;
break; break;
} }
default:
if (timeNanos == null) {
// Return an exception in the timeUnit is not recognized // Return an exception in the timeUnit is not recognized
throw DbException.getUnsupportedException(timeUnitStr); throw DbException.getUnsupportedException(timeUnitStr);
} }
Value result;
if (value instanceof ValueTimestampTimeZone) { if (value instanceof ValueTimestampTimeZone) {
// Case we create a timestamp with timezone with the dateValue and // Case we create a timestamp with timezone with the dateValue and
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论