use of java.sql.Timestamp in project hive by apache.
the class TestTimestampWritable method testDecimalToTimestampCornerCases.
@Test
@Concurrent(count = 4)
@Repeating(repetition = 100)
public void testDecimalToTimestampCornerCases() {
Timestamp ts = new Timestamp(parseToMillis("1969-03-04 05:44:33"));
assertEquals(0, ts.getTime() % 1000);
for (int nanos : new int[] { 100000, 900000, 999100000, 999900000 }) {
ts.setNanos(nanos);
HiveDecimal d = timestampToDecimal(ts);
assertEquals(ts, TimestampUtils.decimalToTimestamp(d));
assertEquals(ts, TimestampUtils.doubleToTimestamp(d.bigDecimalValue().doubleValue()));
}
}
use of java.sql.Timestamp in project hive by apache.
the class TestTimestampWritable method testToFromDouble.
@Test
@Concurrent(count = 4)
public void testToFromDouble() {
Random rand = new Random(294729777L);
for (int nanosPrecision = 0; nanosPrecision <= 4; ++nanosPrecision) {
for (int i = 0; i < 10000; ++i) {
long millis = randomMillis(MIN_FOUR_DIGIT_YEAR_MILLIS, MAX_FOUR_DIGIT_YEAR_MILLIS, rand);
Timestamp ts = new Timestamp(millis);
int nanos = randomNanos(rand, nanosPrecision);
ts.setNanos(nanos);
TimestampWritable tsw = new TimestampWritable(ts);
double asDouble = tsw.getDouble();
int recoveredNanos = (int) (Math.round((asDouble - Math.floor(asDouble)) * Math.pow(10, nanosPrecision)) * Math.pow(10, 9 - nanosPrecision));
assertEquals(String.format("Invalid nanosecond part recovered from %f", asDouble), nanos, recoveredNanos);
assertEquals(ts, TimestampUtils.doubleToTimestamp(asDouble));
// decimalToTimestamp should be consistent with doubleToTimestamp for this level of
// precision.
assertEquals(ts, TimestampUtils.decimalToTimestamp(HiveDecimal.create(BigDecimal.valueOf(asDouble))));
}
}
}
use of java.sql.Timestamp in project hibernate-orm by hibernate.
the class JdbcTimestampCustomSessionLevelTimeZoneTest method testTimeZone.
@Test
public void testTimeZone() {
connectionProvider.clear();
doInHibernateSessionBuilder(() -> {
return sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE);
}, s -> {
Person person = new Person();
person.id = 1L;
s.persist(person);
});
assertEquals(1, connectionProvider.getPreparedStatements().size());
PreparedStatement ps = connectionProvider.getPreparedStatements().get(0);
try {
ArgumentCaptor<Calendar> calendarArgumentCaptor = ArgumentCaptor.forClass(Calendar.class);
verify(ps, times(1)).setTimestamp(anyInt(), any(Timestamp.class), calendarArgumentCaptor.capture());
assertEquals(TIME_ZONE, calendarArgumentCaptor.getValue().getTimeZone());
} catch (SQLException e) {
fail(e.getMessage());
}
connectionProvider.clear();
doInHibernateSessionBuilder(() -> {
return sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE);
}, s -> {
s.doWork(connection -> {
try (Statement st = connection.createStatement()) {
try (ResultSet rs = st.executeQuery("select createdOn from Person")) {
while (rs.next()) {
Timestamp timestamp = rs.getTimestamp(1);
int offsetDiff = TimeZone.getDefault().getOffset(0) - TIME_ZONE.getOffset(0);
assertEquals(Math.abs(Long.valueOf(offsetDiff).longValue()), Math.abs(timestamp.getTime()));
}
}
}
});
Person person = s.find(Person.class, 1L);
assertEquals(0, person.createdOn.getTime());
});
}
use of java.sql.Timestamp in project hibernate-orm by hibernate.
the class JdbcTimestampWithoutUTCTimeZoneTest method testTimeZone.
@Test
public void testTimeZone() {
doInHibernate(this::sessionFactory, session -> {
Person person = new Person();
person.id = 1L;
long y2kMillis = LocalDateTime.of(2000, 1, 1, 0, 0, 0).atZone(ZoneId.of("UTC")).toInstant().toEpochMilli();
assertEquals(946684800000L, y2kMillis);
person.createdOn = new Timestamp(y2kMillis);
session.persist(person);
});
doInHibernate(this::sessionFactory, s -> {
s.doWork(connection -> {
try (Statement st = connection.createStatement()) {
try (ResultSet rs = st.executeQuery("SELECT to_char(createdon, 'YYYY-MM-DD HH24:MI:SS.US') " + "FROM person")) {
while (rs.next()) {
String timestamp = rs.getString(1);
assertEquals(expectedTimestampValue(), timestamp);
}
}
}
});
});
}
use of java.sql.Timestamp in project hibernate-orm by hibernate.
the class MySQL57TimestampFspFunctionTest method testTimeStampFunctions.
@Test
public void testTimeStampFunctions() {
// add an entity just so it can be queried.
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(new Entity());
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
// current_timestamp(), localtime(), and localtimestamp() are synonyms for now(),
// which returns the time at which the statement began to execute.
// the returned values for now(), current_timestamp(), localtime(), and
// localtimestamp() should be the same.
// sysdate() is the time at which the function itself is executed, so the
// value returned for sysdate() should be different.
Query q = s.createQuery("select now(), current_timestamp(), localtime(), localtimestamp(), sysdate() from MySQL57TimestampFspFunctionTest$Entity");
Object[] oArray = (Object[]) q.uniqueResult();
final Timestamp now = (Timestamp) oArray[0];
assertEquals(now, oArray[1]);
assertEquals(now, oArray[2]);
assertEquals(now, oArray[3]);
final Timestamp sysdate = (Timestamp) oArray[4];
assertTrue(now.compareTo(sysdate) < 0);
// all should have nanos > 0
assertTrue(now.getNanos() > 0);
assertTrue(sysdate.getNanos() > 0);
tx.commit();
s.close();
}
Aggregations