package org.h2.value;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.h2.api.ErrorCode;
import org.h2.api.TimestampWithTimeZone;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.util.DateTimeUtils;
import org.h2.util.LocalDateTimeUtils;
public class ValueTimestampTimeZone extends Value {
public static final int DEFAULT_PRECISION = 32;
public static final int MAXIMUM_PRECISION = 35;
static final int DEFAULT_SCALE = ValueTimestamp.DEFAULT_SCALE;
static final int MAXIMUM_SCALE = ValueTimestamp.MAXIMUM_SCALE;
private final long dateValue;
private final long timeNanos;
private final short timeZoneOffsetMins;
private ValueTimestampTimeZone(long dateValue, long timeNanos,
short timeZoneOffsetMins) {
if (timeNanos < 0 || timeNanos >= DateTimeUtils.NANOS_PER_DAY) {
throw new IllegalArgumentException(
"timeNanos out of range " + timeNanos);
}
if (timeZoneOffsetMins < (-18 * 60)
|| timeZoneOffsetMins > (18 * 60)) {
throw new IllegalArgumentException(
"timeZoneOffsetMins out of range " + timeZoneOffsetMins);
}
this.dateValue = dateValue;
this.timeNanos = timeNanos;
this.timeZoneOffsetMins = timeZoneOffsetMins;
}
public static ValueTimestampTimeZone fromDateValueAndNanos(long dateValue,
long timeNanos, short timeZoneOffsetMins) {
return (ValueTimestampTimeZone) Value.cache(new ValueTimestampTimeZone(
dateValue, timeNanos, timeZoneOffsetMins));
}
public static ValueTimestampTimeZone get(TimestampWithTimeZone timestamp) {
return fromDateValueAndNanos(timestamp.getYMD(),
timestamp.getNanosSinceMidnight(),
timestamp.getTimeZoneOffsetMins());
}
public static ValueTimestampTimeZone parse(String s) {
try {
return (ValueTimestampTimeZone) DateTimeUtils.parseTimestamp(s, null, true);
} catch (Exception e) {
throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e,
"TIMESTAMP WITH TIME ZONE", s);
}
}
public long getDateValue() {
return dateValue;
}
public long getTimeNanos() {
return timeNanos;
}
public short getTimeZoneOffsetMins() {
return timeZoneOffsetMins;
}
@Override
public Timestamp getTimestamp() {
return DateTimeUtils.convertTimestampTimeZoneToTimestamp(dateValue, timeNanos, timeZoneOffsetMins);
}
@Override
public TypeInfo getType() {
return TypeInfo.TYPE_TIMESTAMP_TZ;
}
@Override
public int getValueType() {
return TIMESTAMP_TZ;
}
@Override
public int getMemory() {
return 40;
}
@Override
public String getString() {
StringBuilder builder = new StringBuilder(ValueTimestampTimeZone.MAXIMUM_PRECISION);
DateTimeUtils.appendTimestampTimeZone(builder, dateValue, timeNanos, timeZoneOffsetMins);
return builder.toString();
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
builder.append("TIMESTAMP WITH TIME ZONE '");
DateTimeUtils.appendTimestampTimeZone(builder, dateValue, timeNanos, timeZoneOffsetMins);
return builder.append('\'');
}
@Override
public boolean checkPrecision(long precision) {
return true;
}
@Override
public Value convertScale(boolean onlyToSmallerScale, int targetScale) {
if (targetScale >= MAXIMUM_SCALE) {
return this;
}
if (targetScale < 0) {
throw DbException.getInvalidValueException("scale", targetScale);
}
long n = timeNanos;
long n2 = DateTimeUtils.convertScale(n, targetScale);
if (n2 == n) {
return this;
}
long dv = dateValue;
if (n2 >= DateTimeUtils.NANOS_PER_DAY) {
n2 -= DateTimeUtils.NANOS_PER_DAY;
dv = DateTimeUtils.incrementDateValue(dv);
}
return fromDateValueAndNanos(dv, n2, timeZoneOffsetMins);
}
@Override
public int compareTypeSafe(Value o, CompareMode mode) {
ValueTimestampTimeZone t = (ValueTimestampTimeZone) o;
long dateValueA = dateValue;
long timeA = timeNanos - timeZoneOffsetMins * 60_000_000_000L;
if (timeA < 0) {
timeA += DateTimeUtils.NANOS_PER_DAY;
dateValueA = DateTimeUtils.decrementDateValue(dateValueA);
} else if (timeA >= DateTimeUtils.NANOS_PER_DAY) {
timeA -= DateTimeUtils.NANOS_PER_DAY;
dateValueA = DateTimeUtils.incrementDateValue(dateValueA);
}
long dateValueB = t.dateValue;
long timeB = t.timeNanos - t.timeZoneOffsetMins * 60_000_000_000L;
if (timeB < 0) {
timeB += DateTimeUtils.NANOS_PER_DAY;
dateValueB = DateTimeUtils.decrementDateValue(dateValueB);
} else if (timeB >= DateTimeUtils.NANOS_PER_DAY) {
timeB -= DateTimeUtils.NANOS_PER_DAY;
dateValueB = DateTimeUtils.incrementDateValue(dateValueB);
}
int cmp = Long.compare(dateValueA, dateValueB);
if (cmp != 0) {
return cmp;
}
return Long.compare(timeA, timeB);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof ValueTimestampTimeZone)) {
return false;
}
ValueTimestampTimeZone x = (ValueTimestampTimeZone) other;
return dateValue == x.dateValue && timeNanos == x.timeNanos
&& timeZoneOffsetMins == x.timeZoneOffsetMins;
}
@Override
public int hashCode() {
return (int) (dateValue ^ (dateValue >>> 32) ^ timeNanos
^ (timeNanos >>> 32) ^ timeZoneOffsetMins);
}
@Override
public Object getObject() {
if (SysProperties.RETURN_OFFSET_DATE_TIME && LocalDateTimeUtils.isJava8DateApiPresent()) {
return LocalDateTimeUtils.valueToOffsetDateTime(this);
}
return new TimestampWithTimeZone(dateValue, timeNanos, timeZoneOffsetMins);
}
@Override
public void set(PreparedStatement prep, int parameterIndex)
throws SQLException {
prep.setString(parameterIndex, getString());
}
@Override
public Value add(Value v) {
throw DbException.getUnsupportedException(
"manipulating TIMESTAMP WITH TIME ZONE values is unsupported");
}
@Override
public Value subtract(Value v) {
throw DbException.getUnsupportedException(
"manipulating TIMESTAMP WITH TIME ZONE values is unsupported");
}
}