提交 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:
......
......@@ -48,6 +48,21 @@ public class DateTimeUtils {
*/
public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
/**
* The number of nanoseconds per second.
*/
public static final long NANOS_PER_SECOND = 1_000_000_000;
/**
* The number of nanoseconds per minute.
*/
public static final long NANOS_PER_MINUTE = 60 * NANOS_PER_SECOND;
/**
* The number of nanoseconds per hour.
*/
public static final long NANOS_PER_HOUR = 60 * NANOS_PER_MINUTE;
/**
* The number of nanoseconds per day.
*/
......@@ -406,7 +421,7 @@ public class DateTimeUtils {
if (timeOfDay && hour >= 24) {
throw new IllegalArgumentException(s);
}
nanos += ((((hour * 60L) + minute) * 60) + second) * 1_000_000_000;
nanos += ((((hour * 60L) + minute) * 60) + second) * NANOS_PER_SECOND;
return negative ? -nanos : nanos;
}
......@@ -946,7 +961,7 @@ public class DateTimeUtils {
long timeNanos) {
Timestamp ts = new Timestamp(convertDateTimeValueToMillis(null, dateValue, timeNanos / 1_000_000));
// This method expects the complete nanoseconds value including milliseconds
ts.setNanos((int) (timeNanos % 1_000_000_000));
ts.setNanos((int) (timeNanos % NANOS_PER_SECOND));
return ts;
}
......@@ -961,7 +976,7 @@ public class DateTimeUtils {
*/
public static Timestamp convertTimestampTimeZoneToTimestamp(long dateValue, long timeNanos, short offsetMins) {
Timestamp ts = new Timestamp(getMillis(dateValue, timeNanos, offsetMins));
ts.setNanos((int) (timeNanos % 1_000_000_000));
ts.setNanos((int) (timeNanos % NANOS_PER_SECOND));
return ts;
}
......@@ -1701,15 +1716,15 @@ public class DateTimeUtils {
leading = parseIntervalLeading(s, 0, space, negative);
int colon = s.indexOf(':', space + 1);
if (colon < 0) {
remaining = parseIntervalRemaining(s, space + 1, s.length(), 23) * 3_600_000_000_000L;
remaining = parseIntervalRemaining(s, space + 1, s.length(), 23) * NANOS_PER_HOUR;
} else {
int colon2 = s.indexOf(':', colon + 1);
if (colon2 < 0) {
remaining = parseIntervalRemaining(s, space + 1, colon, 23) * 3_600_000_000_000L
+ parseIntervalRemaining(s, colon + 1, s.length(), 59) * 60_000_000_000L;
remaining = parseIntervalRemaining(s, space + 1, colon, 23) * NANOS_PER_HOUR
+ parseIntervalRemaining(s, colon + 1, s.length(), 59) * NANOS_PER_MINUTE;
} else {
remaining = parseIntervalRemaining(s, space + 1, colon, 23) * 3_600_000_000_000L
+ parseIntervalRemaining(s, colon + 1, colon2, 59) * 60_000_000_000L
remaining = parseIntervalRemaining(s, space + 1, colon, 23) * NANOS_PER_HOUR
+ parseIntervalRemaining(s, colon + 1, colon2, 59) * NANOS_PER_MINUTE
+ parseIntervalRemainingSeconds(s, colon2 + 1);
}
}
......@@ -1727,9 +1742,9 @@ public class DateTimeUtils {
leading = parseIntervalLeading(s, 0, colon, negative);
int colon2 = s.indexOf(':', colon + 1);
if (colon2 < 0) {
remaining = parseIntervalRemaining(s, colon + 1, s.length(), 59) * 60_000_000_000L;
remaining = parseIntervalRemaining(s, colon + 1, s.length(), 59) * NANOS_PER_MINUTE;
} else {
remaining = parseIntervalRemaining(s, colon + 1, colon2, 59) * 60_000_000_000L
remaining = parseIntervalRemaining(s, colon + 1, colon2, 59) * NANOS_PER_MINUTE
+ parseIntervalRemainingSeconds(s, colon2 + 1);
}
}
......@@ -1811,7 +1826,7 @@ public class DateTimeUtils {
if (seconds < 0 || seconds > 59) {
throw new IllegalArgumentException(s);
}
return seconds * 1_000_000_000L + nanos;
return seconds * NANOS_PER_SECOND + nanos;
}
/**
......@@ -1856,8 +1871,8 @@ public class DateTimeUtils {
StringUtils.appendZeroPadded(buff, 2, remaining % 60);
break;
case DAY_TO_SECOND: {
long nanos = remaining % 60_000_000_000L;
remaining /= 60_000_000_000L;
long nanos = remaining % NANOS_PER_MINUTE;
remaining /= NANOS_PER_MINUTE;
buff.append(leading).append(' ');
StringUtils.appendZeroPadded(buff, 2, remaining / 60);
buff.append(':');
......@@ -1872,9 +1887,9 @@ public class DateTimeUtils {
break;
case HOUR_TO_SECOND:
buff.append(leading).append(':');
StringUtils.appendZeroPadded(buff, 2, remaining / 60_000_000_000L);
StringUtils.appendZeroPadded(buff, 2, remaining / NANOS_PER_MINUTE);
buff.append(':');
appendSecondsWithNanos(buff, remaining % 60_000_000_000L);
appendSecondsWithNanos(buff, remaining % NANOS_PER_MINUTE);
break;
case MINUTE_TO_SECOND:
buff.append(leading).append(':');
......@@ -1886,8 +1901,8 @@ public class DateTimeUtils {
}
private static void appendSecondsWithNanos(StringBuilder buff, long nanos) {
StringUtils.appendZeroPadded(buff, 2, nanos / 1_000_000_000);
appendNanos(buff, nanos % 1_000_000_000);
StringUtils.appendZeroPadded(buff, 2, nanos / NANOS_PER_SECOND);
appendNanos(buff, nanos % NANOS_PER_SECOND);
}
private static void appendNanos(StringBuilder buff, long nanos) {
......@@ -1937,34 +1952,34 @@ public class DateTimeUtils {
r = BigInteger.valueOf(interval.getLeading()).multiply(BigInteger.valueOf(NANOS_PER_DAY));
break;
case HOUR:
r = BigInteger.valueOf(interval.getLeading()).multiply(BigInteger.valueOf(3_600_000_000_000L));
r = BigInteger.valueOf(interval.getLeading()).multiply(BigInteger.valueOf(NANOS_PER_HOUR));
break;
case MINUTE:
r = BigInteger.valueOf(interval.getLeading()).multiply(BigInteger.valueOf(60_000_000_000L));
r = BigInteger.valueOf(interval.getLeading()).multiply(BigInteger.valueOf(NANOS_PER_MINUTE));
break;
case SECOND:
r = intervalToAbsolute(interval, 1_000_000_000);
r = intervalToAbsolute(interval, NANOS_PER_SECOND);
break;
case YEAR_TO_MONTH:
r = intervalToAbsolute(interval, 12);
break;
case DAY_TO_HOUR:
r = intervalToAbsolute(interval, 24, 3_600_000_000_000L);
r = intervalToAbsolute(interval, 24, NANOS_PER_HOUR);
break;
case DAY_TO_MINUTE:
r = intervalToAbsolute(interval, 24 * 60, 60_000_000_000L);
r = intervalToAbsolute(interval, 24 * 60, NANOS_PER_MINUTE);
break;
case DAY_TO_SECOND:
r = intervalToAbsolute(interval, NANOS_PER_DAY);
break;
case HOUR_TO_MINUTE:
r = intervalToAbsolute(interval, 60, 60_000_000_000L);
r = intervalToAbsolute(interval, 60, NANOS_PER_MINUTE);
break;
case HOUR_TO_SECOND:
r = intervalToAbsolute(interval, 3_600_000_000_000L);
r = intervalToAbsolute(interval, NANOS_PER_HOUR);
break;
case MINUTE_TO_SECOND:
r = intervalToAbsolute(interval, 60_000_000_000L);
r = intervalToAbsolute(interval, NANOS_PER_MINUTE);
break;
default:
throw new IllegalArgumentException();
......@@ -2001,26 +2016,26 @@ public class DateTimeUtils {
leadingExact(absolute.divide(BigInteger.valueOf(NANOS_PER_DAY))), 0);
case HOUR:
return ValueInterval.from(qualifier, absolute.signum() < 0,
leadingExact(absolute.divide(BigInteger.valueOf(3_600_000_000_000L))), 0);
leadingExact(absolute.divide(BigInteger.valueOf(NANOS_PER_HOUR))), 0);
case MINUTE:
return ValueInterval.from(qualifier, absolute.signum() < 0,
leadingExact(absolute.divide(BigInteger.valueOf(60_000_000_000L))), 0);
leadingExact(absolute.divide(BigInteger.valueOf(NANOS_PER_MINUTE))), 0);
case SECOND:
return intervalFromAbsolute(qualifier, absolute, 1_000_000_000);
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_SECOND);
case YEAR_TO_MONTH:
return intervalFromAbsolute(qualifier, absolute, 12);
case DAY_TO_HOUR:
return intervalFromAbsolute(qualifier, absolute.divide(BigInteger.valueOf(3_600_000_000_000L)), 24);
return intervalFromAbsolute(qualifier, absolute.divide(BigInteger.valueOf(NANOS_PER_HOUR)), 24);
case DAY_TO_MINUTE:
return intervalFromAbsolute(qualifier, absolute.divide(BigInteger.valueOf(60_000_000_000L)), 24 * 60);
return intervalFromAbsolute(qualifier, absolute.divide(BigInteger.valueOf(NANOS_PER_MINUTE)), 24 * 60);
case DAY_TO_SECOND:
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_DAY);
case HOUR_TO_MINUTE:
return intervalFromAbsolute(qualifier, absolute.divide(BigInteger.valueOf(60_000_000_000L)), 60);
return intervalFromAbsolute(qualifier, absolute.divide(BigInteger.valueOf(NANOS_PER_MINUTE)), 60);
case HOUR_TO_SECOND:
return intervalFromAbsolute(qualifier, absolute, 3_600_000_000_000L);
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_HOUR);
case MINUTE_TO_SECOND:
return intervalFromAbsolute(qualifier, absolute, 60_000_000_000L);
return intervalFromAbsolute(qualifier, absolute, NANOS_PER_MINUTE);
default:
throw new IllegalArgumentException();
}
......@@ -2142,7 +2157,7 @@ public class DateTimeUtils {
v = remaining / 60;
break;
case DAY_TO_SECOND:
v = remaining / 3_600_000_000_000L;
v = remaining / NANOS_PER_HOUR;
break;
default:
return 0;
......@@ -2176,13 +2191,13 @@ public class DateTimeUtils {
v = remaining % 60;
break;
case DAY_TO_SECOND:
v = remaining / 60_000_000_000L % 60;
v = remaining / NANOS_PER_MINUTE % 60;
break;
case HOUR_TO_MINUTE:
v = remaining;
break;
case HOUR_TO_SECOND:
v = remaining / 60_000_000_000L;
v = remaining / NANOS_PER_MINUTE;
break;
default:
return 0;
......@@ -2208,11 +2223,11 @@ public class DateTimeUtils {
long v;
switch (qualifier) {
case SECOND:
v = leading * 1_000_000_000 + remaining;
v = leading * NANOS_PER_SECOND + remaining;
break;
case DAY_TO_SECOND:
case HOUR_TO_SECOND:
v = remaining % 60_000_000_000L;
v = remaining % NANOS_PER_MINUTE;
break;
case MINUTE_TO_SECOND:
v = remaining;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论