use of org.h2.value.ValueDate in project gridgain by gridgain.
the class DateTimeUtils method dateAndTimeFromValue.
/**
* Extracts date value and nanos of day from the specified value.
*
* @param value
* value to extract fields from
* @return array with date value and nanos of day
*/
public static long[] dateAndTimeFromValue(Value value) {
long dateValue = EPOCH_DATE_VALUE;
long timeNanos = 0;
if (value instanceof ValueTimestamp) {
ValueTimestamp v = (ValueTimestamp) value;
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
} 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) value.convertTo(Value.TIMESTAMP);
dateValue = v.getDateValue();
timeNanos = v.getTimeNanos();
}
return new long[] { dateValue, timeNanos };
}
use of org.h2.value.ValueDate in project gridgain by gridgain.
the class TestTimeStampWithTimeZone method testConversionsImpl.
private void testConversionsImpl(String timeStr, boolean testReverse) {
ValueTimestamp ts = ValueTimestamp.parse(timeStr);
ValueDate d = (ValueDate) ts.convertTo(Value.DATE);
ValueTime t = (ValueTime) ts.convertTo(Value.TIME);
ValueTimestampTimeZone tstz = ValueTimestampTimeZone.parse(timeStr);
assertEquals(ts, tstz.convertTo(Value.TIMESTAMP));
assertEquals(d, tstz.convertTo(Value.DATE));
assertEquals(t, tstz.convertTo(Value.TIME));
assertEquals(ts.getTimestamp(), tstz.getTimestamp());
if (testReverse) {
assertEquals(0, tstz.compareTo(ts.convertTo(Value.TIMESTAMP_TZ), null, null));
assertEquals(d.convertTo(Value.TIMESTAMP).convertTo(Value.TIMESTAMP_TZ), d.convertTo(Value.TIMESTAMP_TZ));
assertEquals(t.convertTo(Value.TIMESTAMP).convertTo(Value.TIMESTAMP_TZ), t.convertTo(Value.TIMESTAMP_TZ));
}
}
use of org.h2.value.ValueDate in project gridgain by gridgain.
the class IntervalOperation method getDateTimeWithInterval.
private Value getDateTimeWithInterval(Value l, Value r, int lType, int rType) {
switch(lType) {
case Value.TIME:
{
if (DataType.isYearMonthIntervalType(rType)) {
throw DbException.throwInternalError("type=" + rType);
}
BigInteger a1 = BigInteger.valueOf(((ValueTime) l).getNanos());
BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2);
if (n.signum() < 0 || n.compareTo(NANOS_PER_DAY_BI) >= 0) {
throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, n.toString());
}
return ValueTime.fromNanos(n.longValue());
}
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
if (DataType.isYearMonthIntervalType(rType)) {
long m = IntervalUtils.intervalToAbsolute((ValueInterval) r).longValue();
if (opType == IntervalOpType.DATETIME_MINUS_INTERVAL) {
m = -m;
}
return DateTimeFunctions.dateadd("MONTH", m, l);
} else {
BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
if (lType == Value.DATE) {
BigInteger a1 = BigInteger.valueOf(absoluteDayFromDateValue(((ValueDate) l).getDateValue()));
a2 = a2.divide(NANOS_PER_DAY_BI);
BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2);
return ValueDate.fromDateValue(dateValueFromAbsoluteDay(n.longValue()));
} else {
long[] a = dateAndTimeFromValue(l);
long absoluteDay = absoluteDayFromDateValue(a[0]);
long timeNanos = a[1];
BigInteger[] dr = a2.divideAndRemainder(NANOS_PER_DAY_BI);
if (opType == IntervalOpType.DATETIME_PLUS_INTERVAL) {
absoluteDay += dr[0].longValue();
timeNanos += dr[1].longValue();
} else {
absoluteDay -= dr[0].longValue();
timeNanos -= dr[1].longValue();
}
if (timeNanos >= NANOS_PER_DAY) {
timeNanos -= NANOS_PER_DAY;
absoluteDay++;
} else if (timeNanos < 0) {
timeNanos += NANOS_PER_DAY;
absoluteDay--;
}
return dateTimeToValue(l, dateValueFromAbsoluteDay(absoluteDay), timeNanos, false);
}
}
}
throw DbException.throwInternalError("type=" + opType);
}
use of org.h2.value.ValueDate in project gridgain by gridgain.
the class Function method truncate.
private static Value truncate(Session session, Value v0, Value v1) {
Value result;
int t = v0.getValueType();
switch(t) {
case Value.TIMESTAMP:
result = ValueTimestamp.fromDateValueAndNanos(((ValueTimestamp) v0).getDateValue(), 0);
break;
case Value.DATE:
result = ValueTimestamp.fromDateValueAndNanos(((ValueDate) v0).getDateValue(), 0);
break;
case Value.TIMESTAMP_TZ:
{
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v0;
result = ValueTimestampTimeZone.fromDateValueAndNanos(ts.getDateValue(), 0, ts.getTimeZoneOffsetMins());
break;
}
case Value.STRING:
result = ValueTimestamp.fromDateValueAndNanos(ValueTimestamp.parse(v0.getString(), session.getDatabase().getMode()).getDateValue(), 0);
break;
default:
int scale = v1 == null ? 0 : v1.getInt();
if (t == Value.DOUBLE || t == Value.FLOAT) {
double d = v0.getDouble();
if (scale == 0) {
d = d < 0 ? Math.ceil(d) : Math.floor(d);
} else {
double f = Math.pow(10, scale);
d *= f;
d = (d < 0 ? Math.ceil(d) : Math.floor(d)) / f;
}
result = t == Value.DOUBLE ? ValueDouble.get(d) : ValueFloat.get((float) d);
} else {
result = ValueDecimal.get(v0.getBigDecimal().setScale(scale, RoundingMode.DOWN));
}
break;
}
return result;
}
Aggregations