Search in sources :

Example 1 with JDateTime

use of jodd.datetime.JDateTime in project hive by apache.

the class NanoTimeUtils method getNanoTime.

/**
   * Constructs a julian date from the floating time Timestamp.
   * If the timezone of the calendar is different from the current local
   * timezone, then the timestamp value will be adjusted.
   * Possible adjustments:
   *   - UTC Ts -> Local Ts copied to TableTZ Calendar -> UTC Ts -> JD
   * @param ts floating time timestamp to store
   * @param calendar timezone used to adjust the timestamp for parquet
   * @return adjusted julian date
   */
public static NanoTime getNanoTime(Timestamp ts, Calendar calendar) {
    Calendar localCalendar = getLocalCalendar();
    localCalendar.setTimeInMillis(ts.getTime());
    Calendar adjustedCalendar = copyToCalendarWithTZ(localCalendar, calendar);
    Calendar utcCalendar = getUTCCalendar();
    utcCalendar.setTimeInMillis(adjustedCalendar.getTimeInMillis());
    int year = utcCalendar.get(Calendar.YEAR);
    if (utcCalendar.get(Calendar.ERA) == GregorianCalendar.BC) {
        year = 1 - year;
    }
    JDateTime jDateTime = new JDateTime(year, //java calendar index starting at 1.
    utcCalendar.get(Calendar.MONTH) + 1, utcCalendar.get(Calendar.DAY_OF_MONTH));
    int days = jDateTime.getJulianDayNumber();
    long hour = utcCalendar.get(Calendar.HOUR_OF_DAY);
    long minute = utcCalendar.get(Calendar.MINUTE);
    long second = utcCalendar.get(Calendar.SECOND);
    long nanos = ts.getNanos();
    long nanosOfDay = nanos + NANOS_PER_SECOND * second + NANOS_PER_MINUTE * minute + NANOS_PER_HOUR * hour;
    return new NanoTime(days, nanosOfDay);
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) JDateTime(jodd.datetime.JDateTime)

Example 2 with JDateTime

use of jodd.datetime.JDateTime in project hive by apache.

the class NanoTimeUtils method getTimestamp.

/**
   * Constructs a floating time Timestamp from the julian date contained in NanoTime.
   * If the timezone of the calendar is different from the current local
   * timezone, then the timestamp value will be adjusted.
   * Possible adjustments:
   *   - JD -> UTC Ts -> TableTZ Calendar copied to LocalTZ Calendar -> UTC Ts
   * @param nt stored julian date
   * @param calendar timezone used to adjust the timestamp for parquet
   * @return floating time represented as a timestamp. Guaranteed to display
   * the same when formatted using the current local timezone as with the local
   * timezone at the time it was stored.
   */
public static Timestamp getTimestamp(NanoTime nt, Calendar calendar) {
    int julianDay = nt.getJulianDay();
    long nanosOfDay = nt.getTimeOfDayNanos();
    long remainder = nanosOfDay;
    julianDay += remainder / NANOS_PER_DAY;
    remainder %= NANOS_PER_DAY;
    if (remainder < 0) {
        remainder += NANOS_PER_DAY;
        julianDay--;
    }
    JDateTime jDateTime = new JDateTime((double) julianDay);
    Calendar utcCalendar = getUTCCalendar();
    utcCalendar.clear();
    utcCalendar.set(Calendar.YEAR, jDateTime.getYear());
    //java calendar index starting at 1.
    utcCalendar.set(Calendar.MONTH, jDateTime.getMonth() - 1);
    utcCalendar.set(Calendar.DAY_OF_MONTH, jDateTime.getDay());
    int hour = (int) (remainder / (NANOS_PER_HOUR));
    remainder = remainder % (NANOS_PER_HOUR);
    int minutes = (int) (remainder / (NANOS_PER_MINUTE));
    remainder = remainder % (NANOS_PER_MINUTE);
    int seconds = (int) (remainder / (NANOS_PER_SECOND));
    long nanos = remainder % NANOS_PER_SECOND;
    utcCalendar.set(Calendar.HOUR_OF_DAY, hour);
    utcCalendar.set(Calendar.MINUTE, minutes);
    utcCalendar.set(Calendar.SECOND, seconds);
    calendar.setTimeInMillis(utcCalendar.getTimeInMillis());
    Calendar adjusterCalendar = copyToCalendarWithTZ(calendar, Calendar.getInstance());
    Timestamp ts = new Timestamp(adjusterCalendar.getTimeInMillis());
    ts.setNanos((int) nanos);
    return ts;
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) JDateTime(jodd.datetime.JDateTime) Timestamp(java.sql.Timestamp)

Example 3 with JDateTime

use of jodd.datetime.JDateTime in project jodd by oblac.

the class MappingTest method testMapping.

@Test
public void testMapping() throws SQLException {
    DbSession session = new DbThreadSession(cp);
    executeUpdate(session, "drop table FOO if exists");
    String sql = "create table FOO (" + "ID			integer		not null," + "NUMBER		integer 	not null," + "STRING		integer		not null," + "STRING2	integer		not null," + "BOO		integer		not null," + "COLOR		varchar(50)		not null," + "WEIGHT		integer		not null," + "TIMESTAMP	timestamp	not null," + "TIMESTAMP2	timestamp	not null," + "CLOB		longvarchar	not null," + "BLOB		longvarbinary not null," + "DECIMAL	real		not null," + "DECIMAL2	varchar(50)		not null," + "JDT1		bigint		not null," + "JDT2		varchar(50)		not null," + "primary key (ID)" + ')';
    executeUpdate(session, sql);
    sql = "insert into FOO values (1, 555, 173, 7, 999, 'red', 1, '2009-08-07 06:05:04.3333', '2010-01-20 01:02:03.4444', 'W173', 'ABCDEF', 1.01, '-7.17', 0, '0')";
    executeUpdate(session, sql);
    DbOomManager dbOom = DbOomManager.getInstance();
    dbOom.registerEntity(Foo.class);
    SqlTypeManager.register(Boo.class, BooSqlType.class);
    SqlTypeManager.register(FooWeight.class, FooWeigthSqlType.class);
    List<Foo> foos = new DbOomQuery("select * from FOO").list(Foo.class);
    assertEquals(1, foos.size());
    Foo foo = foos.get(0);
    assertEquals(1, foo.id);
    assertEquals(555, foo.number.value);
    assertEquals("173", foo.string);
    assertEquals("7", foo.string2);
    assertEquals(999, foo.boo.value);
    assertEquals(FooColor.red, foo.color);
    assertEquals(FooWeight.light, foo.weight);
    assertNotNull(foo.timestamp);
    assertEquals(109, foo.timestamp.getYear());
    assertEquals(6, foo.timestamp.getHours());
    assertEquals(5, foo.timestamp.getMinutes());
    assertNotNull(foo.timestamp2);
    assertEquals(2010, foo.timestamp2.getYear());
    assertEquals(1, foo.timestamp2.getHour());
    assertEquals(2, foo.timestamp2.getMinute());
    assertNotNull(foo.clob);
    assertEquals(4, foo.clob.length());
    assertEquals("W173", foo.clob.getSubString(1, 4));
    assertEquals(3, foo.blob.length());
    assertEquals((byte) 0xAB, foo.blob.getBytes(1, 3)[0]);
    assertEquals((byte) 0xCD, foo.blob.getBytes(1, 3)[1]);
    assertEquals((byte) 0xEF, foo.blob.getBytes(1, 3)[2]);
    assertEquals("1.01", foo.decimal.toString().substring(0, 4));
    assertEquals("-7.17", foo.decimal2.toString().substring(0, 5));
    assertEquals("1970-01-01", foo.jdt1.toString("YYYY-MM-DD"));
    assertEquals("1970-01-01", foo.jdt2.toString("YYYY-MM-DD"));
    foo.string = "371";
    foo.string2 = "007";
    foo.boo.value = 213;
    foo.color = FooColor.yellow;
    foo.weight = FooWeight.heavy;
    foo.number.value = 222;
    foo.timestamp.setYear(108);
    foo.decimal = new Double("34.12");
    foo.decimal2 = new BigDecimal("1.099");
    DbOomQuery doq = new DbOomQuery(DbEntitySql.update(foo));
    foo.jdt1.setDay(2);
    foo.jdt1.setYear(3000);
    foo.jdt2.setDay(3);
    foo.jdt2.setYear(2900);
    doq.executeUpdate();
    doq = new DbOomQuery(DbEntitySql.updateColumn(foo, "timestamp2", new JDateTime("2010-02-02 20:20:20.222")));
    doq.executeUpdate();
    foos = new DbOomQuery("select * from FOO").list(Foo.class);
    assertEquals(1, foos.size());
    foo = foos.get(0);
    assertEquals(1, foo.id);
    assertEquals("371", foo.string);
    assertEquals("7", foo.string2);
    assertEquals(213, foo.boo.value);
    assertEquals(222, foo.number.value);
    assertEquals(FooColor.yellow, foo.color);
    assertEquals(FooWeight.heavy, foo.weight);
    assertEquals(108, foo.timestamp.getYear());
    assertEquals(2010, foo.timestamp2.getYear());
    assertEquals(20, foo.timestamp2.getHour());
    assertEquals(20, foo.timestamp2.getMinute());
    assertEquals(4, foo.clob.length());
    assertEquals("W173", foo.clob.getSubString(1, 4));
    assertEquals(3, foo.blob.length());
    assertEquals("34.12", foo.decimal.toString());
    assertEquals("1.099", foo.decimal2.toString().substring(0, 5));
    assertEquals("3000-01-02", foo.jdt1.toString("YYYY-MM-DD"));
    assertEquals("2900-01-03", foo.jdt2.toString("YYYY-MM-DD"));
    executeUpdate(session, "drop table FOO if exists");
    session.closeSession();
}
Also used : DbSession(jodd.db.DbSession) DbThreadSession(jodd.db.DbThreadSession) JDateTime(jodd.datetime.JDateTime) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with JDateTime

use of jodd.datetime.JDateTime in project jodd by oblac.

the class LiveMapperDbTest method insertEntry.

protected Tester2 insertEntry() {
    DbSession session = new DbSession();
    Tester2 tester2 = new Tester2();
    tester2.id = 1;
    tester2.name = "Hello";
    tester2.value = Integer.valueOf(123);
    tester2.time = new JDateTime(2014, 1, 30, 10, 42, 34, 0).convertToSqlTimestamp();
    tester2.time2 = new JDateTime(2014, 1, 31, 11, 41, 32, 0);
    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.insert(tester2));
    dbOomQuery.setGeneratedKey();
    int result = dbOomQuery.executeUpdate();
    assertEquals(1, result);
    session.closeSession();
    return tester2;
}
Also used : DbSession(jodd.db.DbSession) JDateTime(jodd.datetime.JDateTime)

Example 5 with JDateTime

use of jodd.datetime.JDateTime in project jodd by oblac.

the class EMLParserTest method testParseEML.

@Test
public void testParseEML() throws FileNotFoundException, MessagingException {
    File emlFile = new File(testDataRoot, "example.eml");
    ReceivedEmail email = EMLParser.create().parse(emlFile);
    assertEquals("Example <from@example.com>", email.getFrom().toString());
    assertEquals("to@example.com", email.getTo()[0].toString());
    assertEquals("test!", email.getSubject());
    // the time is specified in GMT zone
    JDateTime jdt = new JDateTime(2010, 3, 27, 12, 11, 21, 0);
    jdt.changeTimeZone(TimeZone.getTimeZone("GMT"), TimeZone.getDefault());
    // compare
    assertEquals(jdt.convertToDate(), email.getSentDate());
    Map<String, String> headers = email.getAllHeaders();
    assertEquals("1.0", headers.get("MIME-Version"));
    List<EmailMessage> messages = email.getAllMessages();
    assertEquals(2, messages.size());
    EmailMessage msg1 = messages.get(0);
    assertEquals("Test", msg1.getContent().trim());
    assertEquals("text/plain", msg1.getMimeType());
    assertEquals("us-ascii", msg1.getEncoding());
    EmailMessage msg2 = messages.get(1);
    assertTrue(msg2.getContent().contains("Test<o:p>"));
    assertEquals("text/html", msg2.getMimeType());
    assertEquals("us-ascii", msg2.getEncoding());
    List<EmailAttachment> attachments = email.getAttachments();
    assertNull(attachments);
    List<ReceivedEmail> attachedMessages = email.getAttachedMessages();
    assertNotNull(attachedMessages);
    assertEquals(1, attachedMessages.size());
    email = attachedMessages.get(0);
    // attached message
    assertEquals("Example <from@example.com>", email.getFrom().toString());
    assertEquals("to@example.com", email.getTo()[0].toString());
    assertEquals("test", email.getSubject());
    jdt = new JDateTime(2010, 3, 27, 12, 9, 46, 0);
    jdt.changeTimeZone(TimeZone.getTimeZone("GMT"), TimeZone.getDefault());
    assertEquals(jdt.convertToDate(), email.getSentDate());
    headers = email.getAllHeaders();
    assertEquals("1.0", headers.get("MIME-Version"));
    messages = email.getAllMessages();
    assertEquals(2, messages.size());
    msg1 = messages.get(0);
    assertEquals("test", msg1.getContent().trim());
    assertEquals("text/plain", msg1.getMimeType());
    assertEquals("us-ascii", msg1.getEncoding());
    msg2 = messages.get(1);
    assertTrue(msg2.getContent().contains("test</TITLE>"));
    assertEquals("text/html", msg2.getMimeType());
    assertEquals("us-ascii", msg2.getEncoding());
    attachments = email.getAttachments();
    assertNull(attachments);
    attachedMessages = email.getAttachedMessages();
    assertNull(attachedMessages);
}
Also used : JDateTime(jodd.datetime.JDateTime) File(java.io.File) Test(org.junit.Test)

Aggregations

JDateTime (jodd.datetime.JDateTime)38 Test (org.junit.Test)28 Calendar (java.util.Calendar)10 GregorianCalendar (java.util.GregorianCalendar)8 Date (java.util.Date)6 Timestamp (java.sql.Timestamp)5 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)2 Time (java.sql.Time)2 DbSession (jodd.db.DbSession)2 CellReference (org.apache.poi.hssf.util.CellReference)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 File (java.io.File)1 Date (java.sql.Date)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 DateTimeStamp (jodd.datetime.DateTimeStamp)1 DbThreadSession (jodd.db.DbThreadSession)1 Area (jodd.json.model.cat.Area)1 Event (jodd.json.model.cat.Event)1