@Test public void testTimestampConversion() throws Exception { Timestamp epoch = new Timestamp(0); assertEquals(0, TimestampUtil.timestampToMicros(epoch)); assertEquals(epoch, TimestampUtil.microsToTimestamp(0)); Timestamp t1 = new Timestamp(0); t1.setNanos(123456000); assertEquals(123456, TimestampUtil.timestampToMicros(t1)); assertEquals(t1, TimestampUtil.microsToTimestamp(123456)); SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); iso8601.setTimeZone(TimeZone.getTimeZone("UTC")); Timestamp t3 = new Timestamp(iso8601.parse("1923-12-01T00:44:36.876").getTime()); t3.setNanos(876544000); assertEquals(-1454368523123456L, TimestampUtil.timestampToMicros(t3)); assertEquals(t3, TimestampUtil.microsToTimestamp(-1454368523123456L)); }
@Test public void testNonZuluTimestampConversion() throws Exception { SimpleDateFormat cst = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); cst.setTimeZone(TimeZone.getTimeZone("America/Chicago")); String timeString = "2016-08-19T12:12:12.121"; Timestamp timestamp = new Timestamp(cst.parse(timeString).getTime()); long toMicros = TimestampUtil.timestampToMicros(timestamp); Timestamp fromMicros = TimestampUtil.microsToTimestamp(toMicros); String formattedCST = cst.format(fromMicros); assertEquals(1471626732121000L, toMicros); assertEquals(timestamp, fromMicros); assertEquals(timeString, formattedCST); SimpleDateFormat pst = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); pst.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); String formattedPST = pst.format(fromMicros); assertEquals("2016-08-19T10:12:12.121", formattedPST); SimpleDateFormat utc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); utc.setTimeZone(TimeZone.getTimeZone("UTC")); String formattedUTC = utc.format(fromMicros); assertEquals("2016-08-19T17:12:12.121", formattedUTC); } }
/** * Get the specified column's Timestamp. * * @param columnIndex Column index in the schema * @return a Timestamp * @throws IllegalArgumentException if the column is null, is unset, * or if the type doesn't match the column's type * @throws IndexOutOfBoundsException if the column doesn't exist */ public Timestamp getTimestamp(int columnIndex) { checkColumn(schema.getColumnByIndex(columnIndex), Type.UNIXTIME_MICROS); checkColumnExists(schema.getColumnByIndex(columnIndex)); checkValue(columnIndex); long micros = Bytes.getLong(rowAlloc, schema.getColumnOffset(columnIndex)); return TimestampUtil.microsToTimestamp(micros); }
/** * Get the specified column's Timestamp. * * @param columnIndex Column index in the schema * @return a Timestamp * @throws IllegalArgumentException if the column is null, is unset, * or if the type doesn't match the column's type * @throws IndexOutOfBoundsException if the column doesn't exist */ public Timestamp getTimestamp(int columnIndex) { checkValidColumn(columnIndex); checkNull(columnIndex); checkType(columnIndex, Type.UNIXTIME_MICROS); ColumnSchema column = schema.getColumnByIndex(columnIndex); long micros = Bytes.getLong(this.rowData.getRawArray(), this.rowData.getRawOffset() + getCurrentRowDataOffsetForColumn(columnIndex)); return TimestampUtil.microsToTimestamp(micros); }