Search in sources :

Example 1 with ValueTimeTimeZone

use of org.h2.value.ValueTimeTimeZone in project h2database by h2database.

the class IntervalOperation method getDateTimeWithInterval.

private Value getDateTimeWithInterval(SessionLocal session, Value l, Value r, int lType, int rType) {
    switch(lType) {
        case Value.TIME:
            if (DataType.isYearMonthIntervalType(rType)) {
                throw DbException.getInternalError("type=" + rType);
            }
            return ValueTime.fromNanos(getTimeWithInterval(r, ((ValueTime) l).getNanos()));
        case Value.TIME_TZ:
            {
                if (DataType.isYearMonthIntervalType(rType)) {
                    throw DbException.getInternalError("type=" + rType);
                }
                ValueTimeTimeZone t = (ValueTimeTimeZone) l;
                return ValueTimeTimeZone.fromNanos(getTimeWithInterval(r, t.getNanos()), t.getTimeZoneOffsetSeconds());
            }
        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 DateTimeFunction.dateadd(session, DateTimeFunction.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, session);
                    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);
                }
            }
    }
    throw DbException.getInternalError("type=" + opType);
}
Also used : ValueTime(org.h2.value.ValueTime) BigInteger(java.math.BigInteger) ValueDate(org.h2.value.ValueDate) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueInterval(org.h2.value.ValueInterval)

Example 2 with ValueTimeTimeZone

use of org.h2.value.ValueTimeTimeZone in project h2database by h2database.

the class TimeZoneOperation method getValue.

@Override
public Value getValue(SessionLocal session) {
    Value a = left.getValue(session).convertTo(type, session);
    int valueType = a.getValueType();
    if ((valueType == Value.TIMESTAMP_TZ || valueType == Value.TIME_TZ) && right != null) {
        Value b = right.getValue(session);
        if (b != ValueNull.INSTANCE) {
            if (valueType == Value.TIMESTAMP_TZ) {
                ValueTimestampTimeZone v = (ValueTimestampTimeZone) a;
                long dateValue = v.getDateValue();
                long timeNanos = v.getTimeNanos();
                int offsetSeconds = v.getTimeZoneOffsetSeconds();
                int newOffset = parseTimeZone(b, dateValue, timeNanos, offsetSeconds, true);
                if (offsetSeconds != newOffset) {
                    a = DateTimeUtils.timestampTimeZoneAtOffset(dateValue, timeNanos, offsetSeconds, newOffset);
                }
            } else {
                ValueTimeTimeZone v = (ValueTimeTimeZone) a;
                long timeNanos = v.getNanos();
                int offsetSeconds = v.getTimeZoneOffsetSeconds();
                int newOffset = parseTimeZone(b, DateTimeUtils.EPOCH_DATE_VALUE, timeNanos, offsetSeconds, false);
                if (offsetSeconds != newOffset) {
                    timeNanos += (newOffset - offsetSeconds) * DateTimeUtils.NANOS_PER_SECOND;
                    a = ValueTimeTimeZone.fromNanos(DateTimeUtils.normalizeNanosOfDay(timeNanos), newOffset);
                }
            }
        } else {
            a = ValueNull.INSTANCE;
        }
    }
    return a;
}
Also used : Value(org.h2.value.Value) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone)

Example 3 with ValueTimeTimeZone

use of org.h2.value.ValueTimeTimeZone in project h2database by h2database.

the class CompatibilityDatePlusTimeOperation method getValue.

@Override
public Value getValue(SessionLocal session) {
    Value l = left.getValue(session);
    Value r = right.getValue(session);
    if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
        return ValueNull.INSTANCE;
    }
    switch(l.getValueType()) {
        case Value.TIME:
            if (r.getValueType() == Value.DATE) {
                return // 
                ValueTimestamp.fromDateValueAndNanos(// 
                ((ValueDate) r).getDateValue(), ((ValueTime) l).getNanos());
            }
            break;
        case Value.TIME_TZ:
            if (r.getValueType() == Value.DATE) {
                ValueTimeTimeZone t = (ValueTimeTimeZone) l;
                return ValueTimestampTimeZone.fromDateValueAndNanos(((ValueDate) r).getDateValue(), t.getNanos(), t.getTimeZoneOffsetSeconds());
            }
            break;
        case Value.TIMESTAMP:
            {
                if (r.getValueType() == Value.TIME_TZ) {
                    ValueTimestamp ts = (ValueTimestamp) l;
                    l = ValueTimestampTimeZone.fromDateValueAndNanos(ts.getDateValue(), ts.getTimeNanos(), ((ValueTimeTimeZone) r).getTimeZoneOffsetSeconds());
                }
                break;
            }
    }
    long[] a = DateTimeUtils.dateAndTimeFromValue(l, session);
    long dateValue = a[0], timeNanos = a[1] + (r instanceof ValueTime ? ((ValueTime) r).getNanos() : ((ValueTimeTimeZone) r).getNanos());
    if (timeNanos >= NANOS_PER_DAY) {
        timeNanos -= NANOS_PER_DAY;
        dateValue = DateTimeUtils.incrementDateValue(dateValue);
    }
    return DateTimeUtils.dateTimeToValue(l, dateValue, timeNanos);
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone)

Example 4 with ValueTimeTimeZone

use of org.h2.value.ValueTimeTimeZone in project h2database by h2database.

the class DateTimeUtils method dateAndTimeFromValue.

/**
 * Extracts date value and nanos of day from the specified value.
 *
 * @param value
 *            value to extract fields from
 * @param provider
 *            the cast information provider
 * @return array with date value and nanos of day
 */
public static long[] dateAndTimeFromValue(Value value, CastDataProvider provider) {
    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 if (value instanceof ValueTimeTimeZone) {
        timeNanos = ((ValueTimeTimeZone) value).getNanos();
    } else {
        ValueTimestamp v = (ValueTimestamp) value.convertTo(TypeInfo.TYPE_TIMESTAMP, provider);
        dateValue = v.getDateValue();
        timeNanos = v.getTimeNanos();
    }
    return new long[] { dateValue, timeNanos };
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone)

Example 5 with ValueTimeTimeZone

use of org.h2.value.ValueTimeTimeZone in project apollo by salesforce.

the class DateTimeUtils method dateAndTimeFromValue.

/**
 * Extracts date value and nanos of day from the specified value.
 *
 * @param value
 *            value to extract fields from
 * @param provider
 *            the cast information provider
 * @return array with date value and nanos of day
 */
public static long[] dateAndTimeFromValue(Value value, CastDataProvider provider) {
    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 if (value instanceof ValueTimeTimeZone) {
        timeNanos = ((ValueTimeTimeZone) value).getNanos();
    } else {
        ValueTimestamp v = (ValueTimestamp) value.convertTo(TypeInfo.TYPE_TIMESTAMP, provider);
        dateValue = v.getDateValue();
        timeNanos = v.getTimeNanos();
    }
    return new long[] { dateValue, timeNanos };
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone)

Aggregations

ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)29 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)20 ValueTimestamp (org.h2.value.ValueTimestamp)17 Value (org.h2.value.Value)16 ValueTime (org.h2.value.ValueTime)16 ValueDate (org.h2.value.ValueDate)14 ValueInterval (org.h2.value.ValueInterval)13 BigDecimal (java.math.BigDecimal)12 BigInteger (java.math.BigInteger)12 ValueBigint (org.h2.value.ValueBigint)7 ValueSmallint (org.h2.value.ValueSmallint)7 ValueArray (org.h2.value.ValueArray)4 ValueBlob (org.h2.value.ValueBlob)4 ValueClob (org.h2.value.ValueClob)4 ValueTinyint (org.h2.value.ValueTinyint)4 ValueUuid (org.h2.value.ValueUuid)4 LobData (org.h2.value.lob.LobData)4 LobDataDatabase (org.h2.value.lob.LobDataDatabase)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Charset (java.nio.charset.Charset)3