use of org.h2.util.DateTimeUtils.dateTimeToValue 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);
}
use of org.h2.util.DateTimeUtils.dateTimeToValue in project SpringStudy by myounghaklee.
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);
}
use of org.h2.util.DateTimeUtils.dateTimeToValue in project SpringStudy by myounghaklee.
the class DateTimeFunction method dateadd.
/**
* DATEADD function.
*
* @param session
* the session
* @param field
* the date-time field
* @param count
* count to add
* @param v
* value to add to
* @return result
*/
public static Value dateadd(SessionLocal session, int field, long count, Value v) {
if (field != MILLISECOND && field != MICROSECOND && field != NANOSECOND && (count > Integer.MAX_VALUE || count < Integer.MIN_VALUE)) {
throw DbException.getInvalidValueException("DATEADD count", count);
}
long[] a = DateTimeUtils.dateAndTimeFromValue(v, session);
long dateValue = a[0];
long timeNanos = a[1];
int type = v.getValueType();
switch(field) {
case MILLENNIUM:
return addYearsMonths(field, true, count * 1_000, v, type, dateValue, timeNanos);
case CENTURY:
return addYearsMonths(field, true, count * 100, v, type, dateValue, timeNanos);
case DECADE:
return addYearsMonths(field, true, count * 10, v, type, dateValue, timeNanos);
case YEAR:
return addYearsMonths(field, true, count, v, type, dateValue, timeNanos);
case QUARTER:
return addYearsMonths(field, false, count *= 3, v, type, dateValue, timeNanos);
case MONTH:
return addYearsMonths(field, false, count, v, type, dateValue, timeNanos);
case WEEK:
case ISO_WEEK:
count *= 7;
// $FALL-THROUGH$
case DAY_OF_WEEK:
case DOW:
case ISO_DAY_OF_WEEK:
case DAY:
case DAY_OF_YEAR:
if (type == Value.TIME || type == Value.TIME_TZ) {
throw DbException.getInvalidValueException("DATEADD time part", getFieldName(field));
}
dateValue = DateTimeUtils.dateValueFromAbsoluteDay(DateTimeUtils.absoluteDayFromDateValue(dateValue) + count);
return DateTimeUtils.dateTimeToValue(v, dateValue, timeNanos);
case HOUR:
count *= NANOS_PER_HOUR;
break;
case MINUTE:
count *= NANOS_PER_MINUTE;
break;
case SECOND:
case EPOCH:
count *= NANOS_PER_SECOND;
break;
case MILLISECOND:
count *= 1_000_000;
break;
case MICROSECOND:
count *= 1_000;
break;
case NANOSECOND:
break;
case TIMEZONE_HOUR:
return addToTimeZone(field, count * 3_600, v, type, dateValue, timeNanos);
case TIMEZONE_MINUTE:
return addToTimeZone(field, count * 60, v, type, dateValue, timeNanos);
case TIMEZONE_SECOND:
return addToTimeZone(field, count, v, type, dateValue, timeNanos);
default:
throw DbException.getUnsupportedException("DATEADD " + getFieldName(field));
}
timeNanos += count;
if (timeNanos >= NANOS_PER_DAY || timeNanos < 0) {
long d;
if (timeNanos >= NANOS_PER_DAY) {
d = timeNanos / NANOS_PER_DAY;
} else {
d = (timeNanos - NANOS_PER_DAY + 1) / NANOS_PER_DAY;
}
dateValue = DateTimeUtils.dateValueFromAbsoluteDay(DateTimeUtils.absoluteDayFromDateValue(dateValue) + d);
timeNanos -= d * NANOS_PER_DAY;
}
if (type == Value.DATE) {
return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos);
}
return DateTimeUtils.dateTimeToValue(v, dateValue, timeNanos);
}
use of org.h2.util.DateTimeUtils.dateTimeToValue in project h2database by h2database.
the class DateTimeFunction method truncateDate.
/**
* Truncate the given date-time value to the specified field.
*
* @param session
* the session
* @param field
* the date-time field
* @param value
* the date-time value
* @return date the truncated value
*/
private static Value truncateDate(SessionLocal session, int field, Value value) {
long[] fieldDateAndTime = DateTimeUtils.dateAndTimeFromValue(value, session);
long dateValue = fieldDateAndTime[0];
long timeNanos = fieldDateAndTime[1];
switch(field) {
case MICROSECOND:
timeNanos = timeNanos / 1_000L * 1_000L;
break;
case MILLISECOND:
timeNanos = timeNanos / 1_000_000L * 1_000_000L;
break;
case SECOND:
timeNanos = timeNanos / NANOS_PER_SECOND * NANOS_PER_SECOND;
break;
case MINUTE:
timeNanos = timeNanos / NANOS_PER_MINUTE * NANOS_PER_MINUTE;
break;
case HOUR:
timeNanos = timeNanos / NANOS_PER_HOUR * NANOS_PER_HOUR;
break;
case DAY:
timeNanos = 0L;
break;
case ISO_WEEK:
dateValue = truncateToWeek(dateValue, 1);
timeNanos = 0L;
break;
case WEEK:
dateValue = truncateToWeek(dateValue, getWeekFields().getFirstDayOfWeek().getValue());
timeNanos = 0L;
break;
case ISO_WEEK_YEAR:
dateValue = truncateToWeekYear(dateValue, 1, 4);
timeNanos = 0L;
break;
case WEEK_YEAR:
{
WeekFields weekFields = getWeekFields();
dateValue = truncateToWeekYear(dateValue, weekFields.getFirstDayOfWeek().getValue(), weekFields.getMinimalDaysInFirstWeek());
break;
}
case MONTH:
dateValue = dateValue & (-1L << DateTimeUtils.SHIFT_MONTH) | 1L;
timeNanos = 0L;
break;
case QUARTER:
dateValue = DateTimeUtils.dateValue(DateTimeUtils.yearFromDateValue(dateValue), ((DateTimeUtils.monthFromDateValue(dateValue) - 1) / 3) * 3 + 1, 1);
timeNanos = 0L;
break;
case YEAR:
dateValue = dateValue & (-1L << DateTimeUtils.SHIFT_YEAR) | (1L << DateTimeUtils.SHIFT_MONTH | 1L);
timeNanos = 0L;
break;
case DECADE:
{
int year = DateTimeUtils.yearFromDateValue(dateValue);
if (year >= 0) {
year = year / 10 * 10;
} else {
year = (year - 9) / 10 * 10;
}
dateValue = DateTimeUtils.dateValue(year, 1, 1);
timeNanos = 0L;
break;
}
case CENTURY:
{
int year = DateTimeUtils.yearFromDateValue(dateValue);
if (year > 0) {
year = (year - 1) / 100 * 100 + 1;
} else {
year = year / 100 * 100 - 99;
}
dateValue = DateTimeUtils.dateValue(year, 1, 1);
timeNanos = 0L;
break;
}
case MILLENNIUM:
{
int year = DateTimeUtils.yearFromDateValue(dateValue);
if (year > 0) {
year = (year - 1) / 1000 * 1000 + 1;
} else {
year = year / 1000 * 1000 - 999;
}
dateValue = DateTimeUtils.dateValue(year, 1, 1);
timeNanos = 0L;
break;
}
default:
throw DbException.getUnsupportedException("DATE_TRUNC " + getFieldName(field));
}
Value result = DateTimeUtils.dateTimeToValue(value, dateValue, timeNanos);
if (session.getMode().getEnum() == ModeEnum.PostgreSQL && result.getValueType() == Value.DATE) {
result = result.convertTo(Value.TIMESTAMP_TZ, session);
}
return result;
}
use of org.h2.util.DateTimeUtils.dateTimeToValue in project h2database by h2database.
the class DateTimeFunction method dateadd.
/**
* DATEADD function.
*
* @param session
* the session
* @param field
* the date-time field
* @param count
* count to add
* @param v
* value to add to
* @return result
*/
public static Value dateadd(SessionLocal session, int field, long count, Value v) {
if (field != MILLISECOND && field != MICROSECOND && field != NANOSECOND && (count > Integer.MAX_VALUE || count < Integer.MIN_VALUE)) {
throw DbException.getInvalidValueException("DATEADD count", count);
}
long[] a = DateTimeUtils.dateAndTimeFromValue(v, session);
long dateValue = a[0];
long timeNanos = a[1];
int type = v.getValueType();
switch(field) {
case MILLENNIUM:
return addYearsMonths(field, true, count * 1_000, v, type, dateValue, timeNanos);
case CENTURY:
return addYearsMonths(field, true, count * 100, v, type, dateValue, timeNanos);
case DECADE:
return addYearsMonths(field, true, count * 10, v, type, dateValue, timeNanos);
case YEAR:
return addYearsMonths(field, true, count, v, type, dateValue, timeNanos);
case QUARTER:
return addYearsMonths(field, false, count *= 3, v, type, dateValue, timeNanos);
case MONTH:
return addYearsMonths(field, false, count, v, type, dateValue, timeNanos);
case WEEK:
case ISO_WEEK:
count *= 7;
// $FALL-THROUGH$
case DAY_OF_WEEK:
case DOW:
case ISO_DAY_OF_WEEK:
case DAY:
case DAY_OF_YEAR:
if (type == Value.TIME || type == Value.TIME_TZ) {
throw DbException.getInvalidValueException("DATEADD time part", getFieldName(field));
}
dateValue = DateTimeUtils.dateValueFromAbsoluteDay(DateTimeUtils.absoluteDayFromDateValue(dateValue) + count);
return DateTimeUtils.dateTimeToValue(v, dateValue, timeNanos);
case HOUR:
count *= NANOS_PER_HOUR;
break;
case MINUTE:
count *= NANOS_PER_MINUTE;
break;
case SECOND:
case EPOCH:
count *= NANOS_PER_SECOND;
break;
case MILLISECOND:
count *= 1_000_000;
break;
case MICROSECOND:
count *= 1_000;
break;
case NANOSECOND:
break;
case TIMEZONE_HOUR:
return addToTimeZone(field, count * 3_600, v, type, dateValue, timeNanos);
case TIMEZONE_MINUTE:
return addToTimeZone(field, count * 60, v, type, dateValue, timeNanos);
case TIMEZONE_SECOND:
return addToTimeZone(field, count, v, type, dateValue, timeNanos);
default:
throw DbException.getUnsupportedException("DATEADD " + getFieldName(field));
}
timeNanos += count;
if (timeNanos >= NANOS_PER_DAY || timeNanos < 0) {
long d;
if (timeNanos >= NANOS_PER_DAY) {
d = timeNanos / NANOS_PER_DAY;
} else {
d = (timeNanos - NANOS_PER_DAY + 1) / NANOS_PER_DAY;
}
dateValue = DateTimeUtils.dateValueFromAbsoluteDay(DateTimeUtils.absoluteDayFromDateValue(dateValue) + d);
timeNanos -= d * NANOS_PER_DAY;
}
if (type == Value.DATE) {
return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos);
}
return DateTimeUtils.dateTimeToValue(v, dateValue, timeNanos);
}
Aggregations