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

Add more constants to DateTimeUtils

上级 67c38904
......@@ -27,6 +27,12 @@ import static org.h2.expression.Function.TIMEZONE_HOUR;
import static org.h2.expression.Function.TIMEZONE_MINUTE;
import static org.h2.expression.Function.WEEK;
import static org.h2.expression.Function.YEAR;
import static org.h2.util.DateTimeUtils.MILLIS_PER_DAY;
import static org.h2.util.DateTimeUtils.NANOS_PER_DAY;
import static org.h2.util.DateTimeUtils.NANOS_PER_HOUR;
import static org.h2.util.DateTimeUtils.NANOS_PER_MINUTE;
import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND;
import java.math.BigDecimal;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
......@@ -34,6 +40,7 @@ import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.TimeZone;
import org.h2.api.ErrorCode;
import org.h2.api.IntervalQualifier;
import org.h2.expression.Function;
......@@ -171,14 +178,14 @@ public final class DateTimeFunctions {
.dateValueFromAbsoluteDay(DateTimeUtils.absoluteDayFromDateValue(dateValue) + count);
return DateTimeUtils.dateTimeToValue(v, dateValue, timeNanos, forceTimestamp);
case HOUR:
count *= 3_600_000_000_000L;
count *= NANOS_PER_HOUR;
break;
case MINUTE:
count *= 60_000_000_000L;
count *= NANOS_PER_MINUTE;
break;
case SECOND:
case EPOCH:
count *= 1_000_000_000;
count *= NANOS_PER_SECOND;
break;
case MILLISECOND:
count *= 1_000_000;
......@@ -206,14 +213,14 @@ public final class DateTimeFunctions {
forceTimestamp = true;
}
timeNanos += count;
if (timeNanos >= DateTimeUtils.NANOS_PER_DAY || timeNanos < 0) {
if (timeNanos >= NANOS_PER_DAY || timeNanos < 0) {
long d;
if (timeNanos >= DateTimeUtils.NANOS_PER_DAY) {
d = timeNanos / DateTimeUtils.NANOS_PER_DAY;
if (timeNanos >= NANOS_PER_DAY) {
d = timeNanos / NANOS_PER_DAY;
} else {
d = (timeNanos - DateTimeUtils.NANOS_PER_DAY + 1) / DateTimeUtils.NANOS_PER_DAY;
d = (timeNanos - NANOS_PER_DAY + 1) / NANOS_PER_DAY;
}
timeNanos -= d * DateTimeUtils.NANOS_PER_DAY;
timeNanos -= d * NANOS_PER_DAY;
return DateTimeUtils.dateTimeToValue(v,
DateTimeUtils.dateValueFromAbsoluteDay(DateTimeUtils.absoluteDayFromDateValue(dateValue) + d),
timeNanos, forceTimestamp);
......@@ -257,21 +264,22 @@ public final class DateTimeFunctions {
long timeNanos2 = a2[1];
switch (field) {
case NANOSECOND:
return (absolute2 - absolute1) * DateTimeUtils.NANOS_PER_DAY + (timeNanos2 - timeNanos1);
return (absolute2 - absolute1) * NANOS_PER_DAY + (timeNanos2 - timeNanos1);
case MICROSECOND:
return (absolute2 - absolute1) * (DateTimeUtils.MILLIS_PER_DAY * 1_000)
return (absolute2 - absolute1) * (MILLIS_PER_DAY * 1_000)
+ (timeNanos2 / 1_000 - timeNanos1 / 1_000);
case MILLISECOND:
return (absolute2 - absolute1) * DateTimeUtils.MILLIS_PER_DAY
return (absolute2 - absolute1) * MILLIS_PER_DAY
+ (timeNanos2 / 1_000_000 - timeNanos1 / 1_000_000);
case SECOND:
case EPOCH:
return (absolute2 - absolute1) * 86_400 + (timeNanos2 / 1_000_000_000 - timeNanos1 / 1_000_000_000);
return (absolute2 - absolute1) * 86_400
+ (timeNanos2 / NANOS_PER_SECOND - timeNanos1 / NANOS_PER_SECOND);
case MINUTE:
return (absolute2 - absolute1) * 1_440 + (timeNanos2 / 60_000_000_000L - timeNanos1 / 60_000_000_000L);
return (absolute2 - absolute1) * 1_440
+ (timeNanos2 / NANOS_PER_MINUTE - timeNanos1 / NANOS_PER_MINUTE);
case HOUR:
return (absolute2 - absolute1) * 24
+ (timeNanos2 / 3_600_000_000_000L - timeNanos1 / 3_600_000_000_000L);
return (absolute2 - absolute1) * 24 + (timeNanos2 / NANOS_PER_HOUR - timeNanos1 / NANOS_PER_HOUR);
}
// Fake fall-through
// $FALL-THROUGH$
......@@ -348,7 +356,7 @@ public final class DateTimeFunctions {
}
} else {
bd = new BigDecimal(DateTimeUtils.intervalToAbsolute(interval))
.divide(BigDecimal.valueOf(1_000_000_000L));
.divide(BigDecimal.valueOf(NANOS_PER_SECOND));
}
return ValueDecimal.get(bd);
}
......@@ -359,7 +367,7 @@ public final class DateTimeFunctions {
// We compute the time in nanoseconds and the total number of days.
BigDecimal timeNanosBigDecimal = new BigDecimal(timeNanos);
BigDecimal numberOfDays = new BigDecimal(DateTimeUtils.absoluteDayFromDateValue(dateValue));
BigDecimal nanosSeconds = new BigDecimal(1_000_000_000);
BigDecimal nanosSeconds = new BigDecimal(NANOS_PER_SECOND);
BigDecimal secondsPerDay = new BigDecimal(DateTimeUtils.SECONDS_PER_DAY);
// Case where the value is of type time e.g. '10:00:00'
......@@ -440,23 +448,20 @@ public final class DateTimeFunctions {
case SECOND:
long nanoInSecond = 1_000_000_000L;
long seconds = timeNanosRetrieved / nanoInSecond;
timeNanos = seconds * nanoInSecond;
long seconds = timeNanosRetrieved / NANOS_PER_SECOND;
timeNanos = seconds * NANOS_PER_SECOND;
break;
case MINUTE:
long nanoInMinute = 60_000_000_000L;
long minutes = timeNanosRetrieved / nanoInMinute;
timeNanos = minutes * nanoInMinute;
long minutes = timeNanosRetrieved / NANOS_PER_MINUTE;
timeNanos = minutes * NANOS_PER_MINUTE;
break;
case HOUR:
long nanoInHour = 3_600_000_000_000L;
long hours = timeNanosRetrieved / nanoInHour;
timeNanos = hours * nanoInHour;
long hours = timeNanosRetrieved / NANOS_PER_HOUR;
timeNanos = hours * NANOS_PER_HOUR;
break;
case DAY_OF_MONTH:
......@@ -646,7 +651,7 @@ public final class DateTimeFunctions {
v = DateTimeUtils.minutesFromInterval(qualifier, negative, leading, remaining);
break;
case SECOND:
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000_000_000;
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) / NANOS_PER_SECOND;
break;
case MILLISECOND:
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000_000 % 1_000;
......@@ -655,7 +660,7 @@ public final class DateTimeFunctions {
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000 % 1_000_000;
break;
case NANOSECOND:
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) % 1_000_000_000;
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) % NANOS_PER_SECOND;
break;
default:
throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
......@@ -673,17 +678,17 @@ public final class DateTimeFunctions {
case DAY_OF_MONTH:
return DateTimeUtils.dayFromDateValue(dateValue);
case HOUR:
return (int) (timeNanos / 3_600_000_000_000L % 24);
return (int) (timeNanos / NANOS_PER_HOUR % 24);
case MINUTE:
return (int) (timeNanos / 60_000_000_000L % 60);
return (int) (timeNanos / NANOS_PER_MINUTE % 60);
case SECOND:
return (int) (timeNanos / 1_000_000_000 % 60);
return (int) (timeNanos / NANOS_PER_SECOND % 60);
case MILLISECOND:
return (int) (timeNanos / 1_000_000 % 1_000);
case MICROSECOND:
return (int) (timeNanos / 1_000 % 1_000_000);
case NANOSECOND:
return (int) (timeNanos % 1_000_000_000);
return (int) (timeNanos % NANOS_PER_SECOND);
case DAY_OF_YEAR:
return DateTimeUtils.getDayOfYear(dateValue);
case DAY_OF_WEEK:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论