Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public Long toLong(Instant instant, Schema schema, LogicalType type) {

if (seconds < 0 && nanos > 0) {
long micros = Math.multiplyExact(seconds + 1, 1_000_000_000L);
long adjustment = nanos - 1_000_000;
long adjustment = nanos - 1_000_000_000L;

return Math.addExact(micros, adjustment);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.avro.data.TimeConversions.TimeMillisConversion;
import org.apache.avro.data.TimeConversions.TimestampMicrosConversion;
import org.apache.avro.data.TimeConversions.TimestampMillisConversion;
import org.apache.avro.data.TimeConversions.TimestampNanosConversion;
import org.apache.avro.reflect.ReflectData;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -44,6 +45,7 @@ public class TestTimeConversions {
public static Schema TIME_MICROS_SCHEMA;
public static Schema TIMESTAMP_MILLIS_SCHEMA;
public static Schema TIMESTAMP_MICROS_SCHEMA;
public static Schema TIMESTAMP_NANOS_SCHEMA;

@BeforeAll
public static void createSchemas() {
Expand All @@ -54,6 +56,8 @@ public static void createSchemas() {
.addToSchema(Schema.create(Schema.Type.LONG));
TestTimeConversions.TIMESTAMP_MICROS_SCHEMA = LogicalTypes.timestampMicros()
.addToSchema(Schema.create(Schema.Type.LONG));
TestTimeConversions.TIMESTAMP_NANOS_SCHEMA = LogicalTypes.timestampNanos()
.addToSchema(Schema.create(Schema.Type.LONG));
}

@Test
Expand Down Expand Up @@ -191,6 +195,21 @@ void timestampMicrosConversion() throws Exception {
"Pre 1970 date should be correct");
}

@Test
void timestampNanosConversionBeforeEpochWithPositiveNanos() {
TimestampNanosConversion conversion = new TimestampNanosConversion();

// < Epoch
long Dec_31_1969_23_59_59_999_999_999_instant = -1L;
Instant Dec_31_1969_23_59_59_999_999_999 = ZonedDateTime.of(1969, 12, 31, 23, 59, 59, 999_999_999, ZoneOffset.UTC)
.toInstant();

assertEquals(Dec_31_1969_23_59_59_999_999_999, conversion.fromLong(Dec_31_1969_23_59_59_999_999_999_instant,
TIMESTAMP_NANOS_SCHEMA, LogicalTypes.timestampNanos()), "Pre 1970 nanos date should be correct");
assertEquals(Dec_31_1969_23_59_59_999_999_999_instant, (long) conversion.toLong(Dec_31_1969_23_59_59_999_999_999,
TIMESTAMP_NANOS_SCHEMA, LogicalTypes.timestampNanos()), "Pre 1970 nanos date should be correct");
}

@Test
void dynamicSchemaWithDateConversion() throws ClassNotFoundException {
Schema schema = getReflectedSchemaByName("java.time.LocalDate", new TimeConversions.DateConversion());
Expand Down