use of com.mysql.cj.protocol.InternalTimestamp in project aws-mysql-jdbc by awslabs.
the class XProtocolDecoder method decodeDatetime.
@Override
public <T> T decodeDatetime(byte[] bytes, int offset, int length, int scale, ValueFactory<T> vf) {
try {
CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
int year = (int) inputStream.readUInt64();
int month = (int) inputStream.readUInt64();
int day = (int) inputStream.readUInt64();
// do we have a time too?
if (inputStream.getBytesUntilLimit() > 0) {
int hours = 0;
int minutes = 0;
int seconds = 0;
int nanos = 0;
if (!inputStream.isAtEnd()) {
hours = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
minutes = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
seconds = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
nanos = 1000 * (int) inputStream.readInt64();
}
}
}
}
return vf.createFromDatetime(new InternalTimestamp(year, month, day, hours, minutes, seconds, nanos, scale));
}
return vf.createFromDate(new InternalDate(year, month, day));
} catch (IOException e) {
throw new DataReadException(e);
}
}
use of com.mysql.cj.protocol.InternalTimestamp in project aws-mysql-jdbc by awslabs.
the class MysqlBinaryValueDecoder method decodeDatetime.
public <T> T decodeDatetime(byte[] bytes, int offset, int length, int scale, ValueFactory<T> vf) {
if (length == 0) {
return vf.createFromTimestamp(new InternalTimestamp());
} else if (length != NativeConstants.BIN_LEN_DATE && length != NativeConstants.BIN_LEN_TIMESTAMP_WITH_MICROS && length != NativeConstants.BIN_LEN_TIMESTAMP_NO_FRAC) {
// the value can be any of these lengths (check protocol docs)
throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIMESTAMP" }));
}
int year = 0;
int month = 0;
int day = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
int nanos = 0;
year = (bytes[offset + 0] & 0xff) | ((bytes[offset + 1] & 0xff) << 8);
month = bytes[offset + 2];
day = bytes[offset + 3];
if (length > NativeConstants.BIN_LEN_DATE) {
hours = bytes[offset + 4];
minutes = bytes[offset + 5];
seconds = bytes[offset + 6];
}
if (length > NativeConstants.BIN_LEN_TIMESTAMP_NO_FRAC) {
// MySQL PS protocol uses microseconds
nanos = 1000 * ((bytes[offset + 7] & 0xff) | ((bytes[offset + 8] & 0xff) << 8) | ((bytes[offset + 9] & 0xff) << 16) | ((bytes[offset + 10] & 0xff) << 24));
}
return vf.createFromDatetime(new InternalTimestamp(year, month, day, hours, minutes, seconds, nanos, scale));
}
use of com.mysql.cj.protocol.InternalTimestamp in project aws-mysql-jdbc by awslabs.
the class MysqlTextValueDecoder method getTimestamp.
public static InternalTimestamp getTimestamp(byte[] bytes, int offset, int length, int scale) {
if (length < TIMESTAMP_STR_LEN_NO_FRAC || (length > TIMESTAMP_STR_LEN_WITH_MICROS && length != TIMESTAMP_STR_LEN_WITH_NANOS)) {
throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIMESTAMP" }));
} else if (length != TIMESTAMP_STR_LEN_NO_FRAC) {
// need at least two extra bytes for fractional, '.' and a digit
if (bytes[offset + TIMESTAMP_STR_LEN_NO_FRAC] != (byte) '.' || length < TIMESTAMP_STR_LEN_NO_FRAC + 2) {
throw new DataReadException(Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
}
}
// delimiter verification
if (bytes[offset + 4] != (byte) '-' || bytes[offset + 7] != (byte) '-' || bytes[offset + 10] != (byte) ' ' || bytes[offset + 13] != (byte) ':' || bytes[offset + 16] != (byte) ':') {
throw new DataReadException(Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
}
int year = getInt(bytes, offset, offset + 4);
int month = getInt(bytes, offset + 5, offset + 7);
int day = getInt(bytes, offset + 8, offset + 10);
int hours = getInt(bytes, offset + 11, offset + 13);
int minutes = getInt(bytes, offset + 14, offset + 16);
int seconds = getInt(bytes, offset + 17, offset + 19);
// nanos from MySQL fractional
int nanos;
if (length == TIMESTAMP_STR_LEN_WITH_NANOS) {
nanos = getInt(bytes, offset + 20, offset + length);
} else {
nanos = (length == TIMESTAMP_STR_LEN_NO_FRAC) ? 0 : getInt(bytes, offset + 20, offset + length);
// scale out nanos appropriately. mysql supports up to 6 digits of fractional seconds, each additional digit increasing the range by a factor of
// 10. one digit is tenths, two is hundreths, etc
nanos = nanos * (int) Math.pow(10, 9 - (length - TIMESTAMP_STR_LEN_NO_FRAC - 1));
}
return new InternalTimestamp(year, month, day, hours, minutes, seconds, nanos, scale);
}
use of com.mysql.cj.protocol.InternalTimestamp in project aws-mysql-jdbc by awslabs.
the class ValueEncoder method asInternalTimestampNoTz.
private InternalTimestamp asInternalTimestampNoTz(Object value) {
if (LocalDateTime.class.isInstance(value)) {
LocalDateTime localDateTime = (LocalDateTime) value;
InternalTimestamp internalTimestamp = new InternalTimestamp();
internalTimestamp.setYear(localDateTime.getYear());
internalTimestamp.setMonth(localDateTime.getMonthValue());
internalTimestamp.setDay(localDateTime.getDayOfMonth());
internalTimestamp.setHours(localDateTime.getHour());
internalTimestamp.setMinutes(localDateTime.getMinute());
internalTimestamp.setSeconds(localDateTime.getSecond());
internalTimestamp.setNanos(localDateTime.getNano());
return internalTimestamp;
}
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ValueEncoder.WrongDatetimeValueType", new Object[] { value.getClass() }));
}
use of com.mysql.cj.protocol.InternalTimestamp in project aws-mysql-jdbc by awslabs.
the class ValueEncoder method asInternalTimestampTz.
private InternalTimestamp asInternalTimestampTz(Object value) {
if (Instant.class.isInstance(value)) {
Instant instant = (Instant) value;
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.UTC);
InternalTimestamp internalTimestamp = new InternalTimestamp();
internalTimestamp.setYear(offsetDateTime.getYear());
internalTimestamp.setMonth(offsetDateTime.getMonthValue());
internalTimestamp.setDay(offsetDateTime.getDayOfMonth());
internalTimestamp.setHours(offsetDateTime.getHour());
internalTimestamp.setMinutes(offsetDateTime.getMinute());
internalTimestamp.setSeconds(offsetDateTime.getSecond());
internalTimestamp.setNanos(offsetDateTime.getNano());
// UTC
internalTimestamp.setOffset(0);
return internalTimestamp;
}
if (OffsetDateTime.class.isInstance(value)) {
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
InternalTimestamp internalTimestamp = new InternalTimestamp();
internalTimestamp.setYear(offsetDateTime.getYear());
internalTimestamp.setMonth(offsetDateTime.getMonthValue());
internalTimestamp.setDay(offsetDateTime.getDayOfMonth());
internalTimestamp.setHours(offsetDateTime.getHour());
internalTimestamp.setMinutes(offsetDateTime.getMinute());
internalTimestamp.setSeconds(offsetDateTime.getSecond());
internalTimestamp.setNanos(offsetDateTime.getNano());
internalTimestamp.setOffset((int) TimeUnit.SECONDS.toMinutes(offsetDateTime.getOffset().getTotalSeconds()));
return internalTimestamp;
}
if (ZonedDateTime.class.isInstance(value)) {
ZonedDateTime zonedDateTime = (ZonedDateTime) value;
InternalTimestamp internalTimestamp = new InternalTimestamp();
internalTimestamp.setYear(zonedDateTime.getYear());
internalTimestamp.setMonth(zonedDateTime.getMonthValue());
internalTimestamp.setDay(zonedDateTime.getDayOfMonth());
internalTimestamp.setHours(zonedDateTime.getHour());
internalTimestamp.setMinutes(zonedDateTime.getMinute());
internalTimestamp.setSeconds(zonedDateTime.getSecond());
internalTimestamp.setNanos(zonedDateTime.getNano());
internalTimestamp.setOffset((int) TimeUnit.SECONDS.toMinutes(zonedDateTime.getOffset().getTotalSeconds()));
return internalTimestamp;
}
if (Calendar.class.isInstance(value)) {
Calendar calendar = (Calendar) value;
InternalTimestamp internalTimestamp = new InternalTimestamp();
internalTimestamp.setYear(calendar.get(Calendar.YEAR));
internalTimestamp.setMonth(calendar.get(Calendar.MONTH) + 1);
internalTimestamp.setDay(calendar.get(Calendar.DAY_OF_MONTH));
internalTimestamp.setHours(calendar.get(Calendar.HOUR_OF_DAY));
internalTimestamp.setMinutes(calendar.get(Calendar.MINUTE));
internalTimestamp.setSeconds(calendar.get(Calendar.SECOND));
internalTimestamp.setNanos((int) TimeUnit.MILLISECONDS.toNanos(calendar.get(Calendar.MILLISECOND)));
internalTimestamp.setOffset((int) TimeUnit.MILLISECONDS.toMinutes(calendar.getTimeZone().getOffset(calendar.getTimeInMillis())));
return internalTimestamp;
}
if (Timestamp.class.isInstance(value)) {
// must be checked before java.util.Date.
Timestamp timestamp = (Timestamp) value;
Calendar calendar = Calendar.getInstance(this.timezone);
calendar.setTime(timestamp);
InternalTimestamp internalTimestamp = new InternalTimestamp();
internalTimestamp.setYear(calendar.get(Calendar.YEAR));
internalTimestamp.setMonth(calendar.get(Calendar.MONTH) + 1);
internalTimestamp.setDay(calendar.get(Calendar.DAY_OF_MONTH));
internalTimestamp.setHours(calendar.get(Calendar.HOUR_OF_DAY));
internalTimestamp.setMinutes(calendar.get(Calendar.MINUTE));
internalTimestamp.setSeconds(calendar.get(Calendar.SECOND));
internalTimestamp.setNanos(timestamp.getNanos());
internalTimestamp.setOffset((int) TimeUnit.MILLISECONDS.toMinutes(calendar.getTimeZone().getOffset(calendar.getTimeInMillis())));
return internalTimestamp;
}
if (java.util.Date.class.isInstance(value)) {
java.util.Date date = (java.util.Date) value;
Calendar calendar = Calendar.getInstance(this.timezone);
calendar.setTime(date);
InternalTimestamp internalTimestamp = new InternalTimestamp();
internalTimestamp.setYear(calendar.get(Calendar.YEAR));
internalTimestamp.setMonth(calendar.get(Calendar.MONTH) + 1);
internalTimestamp.setDay(calendar.get(Calendar.DAY_OF_MONTH));
internalTimestamp.setHours(calendar.get(Calendar.HOUR_OF_DAY));
internalTimestamp.setMinutes(calendar.get(Calendar.MINUTE));
internalTimestamp.setSeconds(calendar.get(Calendar.SECOND));
internalTimestamp.setNanos((int) TimeUnit.MILLISECONDS.toNanos(calendar.get(Calendar.MILLISECOND)));
internalTimestamp.setOffset((int) TimeUnit.MILLISECONDS.toMinutes(calendar.getTimeZone().getOffset(date.getTime())));
return internalTimestamp;
}
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ValueEncoder.WrongTimestampValueType", new Object[] { value.getClass() }));
}
Aggregations