Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Parsing timestamps fails when producing messages #803

Merged
merged 2 commits into from
Sep 7, 2021
Merged
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
27 changes: 14 additions & 13 deletions src/main/java/org/akhq/utils/AvroSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import java.math.MathContext;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.time.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import java.util.stream.Collectors;

Expand All @@ -44,10 +45,10 @@ public class AvroSerializer {
protected static final String DATE_FORMAT = "yyyy-MM-dd[XXX]";
protected static final String TIME_FORMAT = "HH:mm[:ss][.SSSSSS][XXX]";
protected static final DateTimeFormatter DATETIME_FORMAT = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"))
.appendOptional(DateTimeFormatter.ofPattern(":ss"))
.appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true)
.appendOptional(DateTimeFormatter.ofPattern("XXX"))
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.parseLenient()
.appendOffsetId()
.toFormatter();

public static GenericRecord recordSerializer(Map<String, Object> record, Schema schema) {
Expand Down Expand Up @@ -176,6 +177,8 @@ private static Long timestampMicrosSerializer(Object data, Schema schema, Schema
}
} else if (data instanceof Long) {
value = Instant.ofEpochSecond(0, (Long) data * 1000);
} else if (data instanceof Integer) {
value = Instant.ofEpochSecond(0, ((Integer) data).longValue() * 1000);
} else {
value = (Instant) data;
}
Expand All @@ -198,6 +201,8 @@ private static Long timestampMillisSerializer(Object data, Schema schema, Schema
}
} else if (data instanceof Long) {
value = Instant.ofEpochMilli((Long) data);
} else if (data instanceof Integer) {
value = Instant.ofEpochMilli(((Integer) data).longValue());
} else {
value = (Instant) data;
}
Expand All @@ -210,12 +215,8 @@ private static Long timestampMillisSerializer(Object data, Schema schema, Schema
}

protected static Instant parseDateTime(String data) {
try {
return ZonedDateTime.parse(data, DATETIME_FORMAT).toInstant();
} catch (DateTimeParseException e) {
LocalDateTime localDateTime = LocalDateTime.parse(data, DATETIME_FORMAT);
return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
}
TimeZone tz = TimeZone.getDefault();
return DATETIME_FORMAT.withZone(tz.toZoneId()).parse(data, Instant::from);
}

private static Long timeMicrosDeserializer(Object data, Schema schema, Schema.Type primitiveType, LogicalType logicalType) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/akhq/utils/AvroSerializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,48 @@ void testParseDateTime_micros_offset() {
Instant.parse("2021-07-16T13:30:12.345678Z"));
}

@Test
void testParseDateTime_micros_offset_short() {
assertEquals(AvroSerializer.parseDateTime("2021-07-16T21:30:12.345678+08"),
Instant.parse("2021-07-16T13:30:12.345678Z"));
}

@Test
void testParseDateTime_millis_offset() {
assertEquals(AvroSerializer.parseDateTime("2021-07-16T21:30:12.345+08:00"),
Instant.parse("2021-07-16T13:30:12.345Z"));
}

@Test
void testParseDateTime_millis_offset_short() {
assertEquals(AvroSerializer.parseDateTime("2021-07-16T21:30:12.345+08"),
Instant.parse("2021-07-16T13:30:12.345Z"));
}

@Test
void testParseDateTime_seconds_offset() {
assertEquals(AvroSerializer.parseDateTime("2021-07-16T21:30:12+08:00"),
Instant.parse("2021-07-16T13:30:12Z"));
}

@Test
void testParseDateTime_seconds_offset_short() {
assertEquals(AvroSerializer.parseDateTime("2021-07-16T21:30:12+08"),
Instant.parse("2021-07-16T13:30:12Z"));
}

@Test
void testParseDateTime_minutes_offset() {
assertEquals(AvroSerializer.parseDateTime("2021-07-16T21:30+08:00"),
Instant.parse("2021-07-16T13:30:00Z"));
}

@Test
void testParseDateTime_minutes_offset_short() {
assertEquals(AvroSerializer.parseDateTime("2021-07-16T21:30+08"),
Instant.parse("2021-07-16T13:30:00Z"));
}

}

@Nested
Expand Down