use of com.mysql.cj.protocol.InternalDate 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.InternalDate in project aws-mysql-jdbc by awslabs.
the class MysqlBinaryValueDecoder method decodeDate.
public <T> T decodeDate(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
if (length == 0) {
return vf.createFromDate(new InternalDate());
} else if (length != NativeConstants.BIN_LEN_DATE) {
throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "DATE" }));
}
int year = (bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8);
int month = bytes[offset + 2];
int day = bytes[offset + 3];
return vf.createFromDate(new InternalDate(year, month, day));
}
use of com.mysql.cj.protocol.InternalDate in project aws-mysql-jdbc by awslabs.
the class ValueEncoder method asInternalDate.
private InternalDate asInternalDate(Object value) {
if (LocalDate.class.isInstance(value)) {
LocalDate localDate = (LocalDate) value;
InternalDate internalDate = new InternalDate();
internalDate.setYear(localDate.getYear());
internalDate.setMonth(localDate.getMonthValue());
internalDate.setDay(localDate.getDayOfMonth());
return internalDate;
}
if (Date.class.isInstance(value)) {
Calendar calendar = Calendar.getInstance(this.timezone);
calendar.setTime((Date) value);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
InternalDate internalDate = new InternalDate();
internalDate.setYear(calendar.get(Calendar.YEAR));
internalDate.setMonth(calendar.get(Calendar.MONTH) + 1);
internalDate.setDay(calendar.get(Calendar.DAY_OF_MONTH));
return internalDate;
}
throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ValueEncoder.WrongDateValueType", new Object[] { value.getClass() }));
}
use of com.mysql.cj.protocol.InternalDate in project aws-mysql-jdbc by awslabs.
the class ResultSetRegressionTest method testBug23702040.
/**
* Tests fix for BUG#23702040 - JDBCDATEVALUEFACTORY FAILS TO PARSE SOME DATES.
*
* @throws Exception
*/
@Test
public void testBug23702040() throws Exception {
SimpleDateFormat sdf = TimeUtil.getSimpleDateFormat(null, "yyyy-MM-dd", TimeZone.getTimeZone("Europe/Bucharest"));
sdf.setLenient(false);
java.util.Date expected = sdf.parse("1994-03-27");
Date fromFactory = new SqlDateValueFactory(new DefaultPropertySet(), null, TimeZone.getTimeZone("Europe/Bucharest")).createFromDate(new InternalDate(1994, 3, 27));
assertEquals(expected.getTime(), fromFactory.getTime());
}
use of com.mysql.cj.protocol.InternalDate in project aws-mysql-jdbc by awslabs.
the class StringValueFactoryTest method testCreateFromDate.
@Test
public void testCreateFromDate() {
this.vf.createFromDate(new InternalDate(2006, 1, 1));
// May 1st
assertEquals("2015-05-01", this.vf.createFromDate(new InternalDate(2015, 5, 1)));
}
Aggregations