use of com.mysql.cj.protocol.InternalTime in project aws-mysql-jdbc by awslabs.
the class XProtocolDecoder method decodeTime.
@Override
public <T> T decodeTime(byte[] bytes, int offset, int length, int scale, ValueFactory<T> vf) {
try {
CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
boolean negative = inputStream.readRawByte() > 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.createFromTime(new InternalTime(negative ? -1 * hours : hours, minutes, seconds, nanos, scale));
} catch (IOException e) {
throw new DataReadException(e);
}
}
use of com.mysql.cj.protocol.InternalTime in project aws-mysql-jdbc by awslabs.
the class MysqlTextValueDecoderTest method testNanosecondParsing.
@Test
public void testNanosecondParsing() {
// test value factory to extract the parsed nano-seconds
ValueFactory<Integer> vf = new DefaultValueFactory<Integer>(new DefaultPropertySet()) {
@Override
public Integer createFromTime(InternalTime it) {
return it.getNanos();
}
@Override
public Integer createFromTimestamp(InternalTimestamp its) {
return its.getNanos();
}
public String getTargetTypeName() {
return Integer.class.getName();
}
@Override
public Integer createFromBytes(byte[] bytes, int offset, int length, Field f) {
return null;
}
};
// the fractional second part is determined by the # of digits
assertEquals(new Integer(900000000), this.valueDecoder.decodeTimestamp("2016-03-14 14:34:01.9".getBytes(), 0, 21, 9, vf));
assertEquals(new Integer(950000000), this.valueDecoder.decodeTimestamp("2016-03-14 14:34:01.95".getBytes(), 0, 22, 9, vf));
assertEquals(new Integer(956000000), this.valueDecoder.decodeTimestamp("2016-03-14 14:34:01.956".getBytes(), 0, 23, 9, vf));
assertEquals(new Integer(900000000), this.valueDecoder.decodeTime("14:34:01.9".getBytes(), 0, 10, 9, vf));
assertEquals(new Integer(950000000), this.valueDecoder.decodeTime("14:34:01.95".getBytes(), 0, 11, 9, vf));
assertEquals(new Integer(956000000), this.valueDecoder.decodeTime("14:34:01.956".getBytes(), 0, 12, 9, vf));
}
use of com.mysql.cj.protocol.InternalTime in project aws-mysql-jdbc by awslabs.
the class SqlDateValueFactoryTest method testCreateFromTime.
@Test
public void testCreateFromTime() {
assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(12, 20, 02, 4, 9)).toString());
assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(1, 1, 1, 1, 9)).toString());
assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(-1, 1, 1, 1, 9)).toString());
assertEquals("1970-01-01", this.vf.createFromTime(new InternalTime(48, 1, 1, 1, 9)).toString());
}
use of com.mysql.cj.protocol.InternalTime in project aws-mysql-jdbc by awslabs.
the class SqlTimestampValueFactoryTest method testCreateFromTime.
@Test
public void testCreateFromTime() {
Timestamp ts = this.vf.createFromTime(new InternalTime(12, 20, 02, 4, 9));
assertEquals("1970-01-01 12:20:02.000000004", ts.toString());
assertEquals(Timestamp.valueOf(LocalDateTime.of(1970, 1, 1, 1, 1, 1, 1)), this.vf.createFromTime(new InternalTime(1, 1, 1, 1, 9)));
assertThrows(DataReadException.class, "The value '-1:00:00' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.", new Callable<Void>() {
@Override
public Void call() throws Exception {
SqlTimestampValueFactoryTest.this.vf.createFromTime(new InternalTime(-1, 0, 0, 0, 9));
return null;
}
});
assertThrows(DataReadException.class, "The value '44:00:00' is an invalid TIME value. JDBC Time objects represent a wall-clock time and not a duration as MySQL treats them. If you are treating this type as a duration, consider retrieving this value as a string and dealing with it according to your requirements.", new Callable<Void>() {
@Override
public Void call() throws Exception {
SqlTimestampValueFactoryTest.this.vf.createFromTime(new InternalTime(44, 0, 0, 0, 9));
return null;
}
});
}
use of com.mysql.cj.protocol.InternalTime in project aws-mysql-jdbc by awslabs.
the class ValueEncoder method asInternalTime.
private InternalTime asInternalTime(Object value) {
if (LocalTime.class.isInstance(value)) {
LocalTime localTime = (LocalTime) value;
InternalTime internalTime = new InternalTime();
internalTime.setHours(localTime.getHour());
internalTime.setMinutes(localTime.getMinute());
internalTime.setSeconds(localTime.getSecond());
internalTime.setNanos(localTime.getNano());
return internalTime;
}
if (OffsetTime.class.isInstance(value)) {
OffsetTime offsetTime = (OffsetTime) value;
InternalTime internalTime = new InternalTime();
internalTime.setHours(offsetTime.getHour());
internalTime.setMinutes(offsetTime.getMinute());
internalTime.setSeconds(offsetTime.getSecond());
internalTime.setNanos(offsetTime.getNano());
return internalTime;
}
if (Duration.class.isInstance(value)) {
Duration duration = (Duration) value;
Duration durationAbs = duration.abs();
long fullSeconds = durationAbs.getSeconds();
int seconds = (int) (fullSeconds % 60);
long fullMinutes = fullSeconds / 60;
int minutes = (int) (fullMinutes % 60);
long fullHours = fullMinutes / 60;
InternalTime internalTime = new InternalTime();
internalTime.setNegative(duration.isNegative());
internalTime.setHours((int) fullHours);
internalTime.setMinutes(minutes);
internalTime.setSeconds(seconds);
internalTime.setNanos(durationAbs.getNano());
return internalTime;
}
if (Time.class.isInstance(value)) {
Time time = (Time) value;
Calendar calendar = Calendar.getInstance(this.timezone);
calendar.setTime(time);
InternalTime internalTime = new InternalTime();
internalTime.setHours(calendar.get(Calendar.HOUR_OF_DAY));
internalTime.setMinutes(calendar.get(Calendar.MINUTE));
internalTime.setSeconds(calendar.get(Calendar.SECOND));
internalTime.setNanos((int) TimeUnit.MILLISECONDS.toNanos(calendar.get(Calendar.MILLISECOND)));
return internalTime;
}
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ValueEncoder.WrongTimeValueType", new Object[] { value.getClass() }));
}
Aggregations