@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)); }
case INT32: return Integer.toString(Bytes.getInt(value)); case INT64: return Long.toString(Bytes.getLong(value)); case UNIXTIME_MICROS: return TimestampUtil.timestampToString(Bytes.getLong(value)); case FLOAT: return Float.toString(Bytes.getFloat(value)); case DOUBLE: return Double.toString(Bytes.getDouble(value));
/** * Creates a new comparison predicate on a timestamp column. * @param column the column schema * @param op the comparison operation * @param value the value to compare against */ public static KuduPredicate newComparisonPredicate(ColumnSchema column, ComparisonOp op, Timestamp value) { checkColumn(column, Type.UNIXTIME_MICROS); long micros = TimestampUtil.timestampToMicros(value); return newComparisonPredicate(column, op, 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) { 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); }
/** * Add a Timestamp for the specified column. * * Note: Timestamp instances with nanosecond precision are truncated to microseconds. * * @param columnIndex the column's index in the schema * @param val value to add * @throws IllegalArgumentException if the value doesn't match the column's type * @throws IllegalStateException if the row was already applied * @throws IndexOutOfBoundsException if the column doesn't exist */ public void addTimestamp(int columnIndex, Timestamp val) { checkNotFrozen(); ColumnSchema column = schema.getColumnByIndex(columnIndex); checkColumn(column, Type.UNIXTIME_MICROS); long micros = TimestampUtil.timestampToMicros(val); Bytes.setLong(rowAlloc, micros, getPositionInRowAllocAndSetBitSet(columnIndex)); }
/** * 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); }
@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); } }
break; case UNIXTIME_MICROS: { buf.append(TimestampUtil.timestampToString(getLong(i))); } break; case STRING:
return; case UNIXTIME_MICROS: sb.append(TimestampUtil.timestampToString( Bytes.getLong(rowAlloc, schema.getColumnOffset(idx)))); return;
StringBuilder expectedRow = new StringBuilder(); expectedRow.append(String.format("UNIXTIME_MICROS key=%s, UNIXTIME_MICROS c1=", TimestampUtil.timestampToString(timestamps.get(i)))); if (i % 2 == 1) { expectedRow.append(TimestampUtil.timestampToString(timestamps.get(i))); } else { expectedRow.append("NULL");