Skip to content

Commit

Permalink
Merge pull request #7 from reportportal/EPMRPP-93396
Browse files Browse the repository at this point in the history
EPMRPP-93396 || Refactor MultiFormatDateDeserializer
  • Loading branch information
pbortnik authored Aug 20, 2024
2 parents 923562e + 4ada762 commit d8de030
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Arrays;
import java.util.List;

/**
* Deserialization class for parsing incoming dates of different formats.
Expand All @@ -38,7 +42,20 @@ public class MultiFormatDateDeserializer extends JsonDeserializer<Instant> {
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("UTC"));

private static final DateTimeFormatter LOCAL_DATE_TIME_MS_FORMAT =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZone(ZoneId.of("UTC"));
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

private static final DateTimeFormatter LOCAL_DATE_TIME_MS_FORMAT_DATE =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXX");

private static final List<DateTimeFormatter> PREDEFINED_FORMATS = Arrays.asList(
DateTimeFormatter.RFC_1123_DATE_TIME,
DateTimeFormatter.ISO_OFFSET_DATE_TIME,
DateTimeFormatter.ISO_DATE_TIME,
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
TIMESTAMP_FORMAT,
LOCAL_DATE_TIME_MS_FORMAT,
LOCAL_DATE_TIME_MS_FORMAT_DATE
);

@Override
public Instant deserialize(JsonParser parser, DeserializationContext context) throws IOException {
Expand All @@ -47,7 +64,6 @@ public Instant deserialize(JsonParser parser, DeserializationContext context) th
if (parser.getText() == null) {
return Instant.ofEpochMilli(longDate);
}
// ignore
} catch (Exception e) {
// ignore
}
Expand All @@ -59,16 +75,17 @@ public Instant deserialize(JsonParser parser, DeserializationContext context) th
}

String strDate = parser.getText();
DateTimeFormatter formatter =
new DateTimeFormatterBuilder().appendOptional(DateTimeFormatter.RFC_1123_DATE_TIME)
.appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
.appendOptional(DateTimeFormatter.ISO_DATE_TIME)
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.appendOptional(TIMESTAMP_FORMAT)
.appendOptional(LOCAL_DATE_TIME_MS_FORMAT)
.toFormatter();

return LocalDateTime.from(formatter.parse(strDate)).toInstant(ZoneOffset.UTC);

for (DateTimeFormatter formatter : PREDEFINED_FORMATS) {
try {
TemporalAccessor parsedDate = formatter.parseBest(strDate, ZonedDateTime::from,
LocalDateTime::from);
return parsedDate instanceof ZonedDateTime ? ((ZonedDateTime) parsedDate).toInstant()
: ((LocalDateTime) parsedDate).toInstant(ZoneOffset.UTC);
} catch (DateTimeParseException e) {
// Exception means the text could not be parsed with this formatter, continue with next formatter
}
}
throw new IOException("Unable to parse date: " + strDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -44,10 +45,12 @@ class MultiFormatDateDeserializerTest {
@ParameterizedTest
@ValueSource(strings = {
"2024-03-01T20:24:09.930987Z",
"2024-03-01T20:24:09.930987654Z",
"2024-03-01T20:24:09.930987654",
"2024-03-01T20:24:09.930Z",
"2024-03-01T20:24:09.930",
"2024-03-01T20:24:09.930+00:00",
"2024-03-01T19:24:09.930-01:00",
"2024-03-01T23:24:09.930+0300",
"1709324649930"
})
void deserializeDates(String strDate) throws IOException {
Expand Down

0 comments on commit d8de030

Please sign in to comment.