Search in sources :

Example 21 with Timestamp

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()));
    }
}
Also used : HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) Timestamp(java.sql.Timestamp)

Example 22 with Timestamp

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))));
        }
    }
}
Also used : Random(java.util.Random) Timestamp(java.sql.Timestamp)

Example 23 with Timestamp

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());
    });
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Calendar(java.util.Calendar) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 24 with Timestamp

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);
                    }
                }
            }
        });
    });
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 25 with 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();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Timestamp(java.sql.Timestamp) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Timestamp (java.sql.Timestamp)3153 Test (org.junit.Test)526 Date (java.util.Date)458 PreparedStatement (java.sql.PreparedStatement)450 SQLException (java.sql.SQLException)367 ResultSet (java.sql.ResultSet)353 BigDecimal (java.math.BigDecimal)351 ArrayList (java.util.ArrayList)236 Date (java.sql.Date)218 Connection (java.sql.Connection)216 HashMap (java.util.HashMap)201 GenericValue (org.apache.ofbiz.entity.GenericValue)194 Calendar (java.util.Calendar)184 Time (java.sql.Time)173 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)167 Delegator (org.apache.ofbiz.entity.Delegator)157 SimpleDateFormat (java.text.SimpleDateFormat)150 IOException (java.io.IOException)129 Locale (java.util.Locale)129 Map (java.util.Map)111