BigQueryAvroUtils { @VisibleForTesting static String formatTimestamp(Long timestampMicro) { long timestampSec = timestampMicro / 1_000_000; long micros = timestampMicro % 1_000_000; if (micros < 0) { micros += 1_000_000; timestampSec -= 1; } String dayAndTime = DATE_AND_SECONDS_FORMATTER.print(timestampSec * 1000); if (micros == 0) { return String.format("%s UTC", dayAndTime); } return String.format("%s.%06d UTC", dayAndTime, micros); }    }
@Test public void testFormatTimestamp() { assertThat( BigQueryAvroUtils.formatTimestamp(1452062291123456L), equalTo("2016-01-06 06:38:11.123456 UTC")); }
@Test public void testFormatTimestampLeadingZeroesOnMicros() { assertThat( BigQueryAvroUtils.formatTimestamp(1452062291000456L), equalTo("2016-01-06 06:38:11.000456 UTC")); }
@Test public void testFormatTimestampTrailingZeroesOnMicros() { assertThat( BigQueryAvroUtils.formatTimestamp(1452062291123000L), equalTo("2016-01-06 06:38:11.123000 UTC")); }
@Test public void testFormatTimestampNegative() { assertThat(BigQueryAvroUtils.formatTimestamp(-1L), equalTo("1969-12-31 23:59:59.999999 UTC")); assertThat( BigQueryAvroUtils.formatTimestamp(-100_000L), equalTo("1969-12-31 23:59:59.900000 UTC")); assertThat(BigQueryAvroUtils.formatTimestamp(-1_000_000L), equalTo("1969-12-31 23:59:59 UTC")); assertThat( BigQueryAvroUtils.formatTimestamp(-(1969L * 365 + 477) * 86400 * 1_000_000), equalTo("0001-01-01 00:00:00 UTC")); }