Skip to content

Commit

Permalink
[BUG] The date-processor doesn't parse times in AM/PM format (opensea…
Browse files Browse the repository at this point in the history
…rch-project#4564)

* Fixed parsing of times in AM/PM format

Signed-off-by: Federico Brignola <160846842+federicobrignola@users.noreply.github.com>

* Fix parsing of time with hours in the format (clock-time-of-day 1-24)

Signed-off-by: Federico Brignola <160846842+FedericoBrignola@users.noreply.github.com>

---------

Signed-off-by: Federico Brignola <160846842+federicobrignola@users.noreply.github.com>
Signed-off-by: Federico Brignola <160846842+FedericoBrignola@users.noreply.github.com>
FedericoBrignola authored Nov 12, 2024
1 parent 5616462 commit 597465a
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -134,10 +134,12 @@ private DateTimeFormatter getSourceFormatter(final String pattern) {
.appendPattern(pattern)
.parseDefaulting(ChronoField.MONTH_OF_YEAR, localDateForDefaultValues.getMonthValue())
.parseDefaulting(ChronoField.DAY_OF_MONTH, localDateForDefaultValues.getDayOfMonth())
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0);

if(!pattern.contains("a") && !pattern.contains("k"))
dateTimeFormatterBuilder.parseDefaulting(ChronoField.HOUR_OF_DAY, 0);

if (!(pattern.contains("y") || pattern.contains("u")))
dateTimeFormatterBuilder.parseDefaulting(ChronoField.YEAR_OF_ERA, localDateForDefaultValues.getYear());

Original file line number Diff line number Diff line change
@@ -539,6 +539,36 @@ void match_without_year_test(String pattern) {
verify(dateProcessingMatchSuccessCounter, times(1)).increment();
}

@ParameterizedTest
@ValueSource(strings = {"hh:mm:ss a", "KK:mm:ss a", "kk:mm:ss", "HH:mm:ss"})
void match_with_different_hours_formats(String pattern) {
when(mockDateMatch.getKey()).thenReturn("logDate");
when(mockDateMatch.getPatterns()).thenReturn(Collections.singletonList(pattern));

List<DateProcessorConfig.DateMatch> dateMatches = Collections.singletonList(mockDateMatch);
when(mockDateProcessorConfig.getMatch()).thenReturn(dateMatches);
when(mockDateProcessorConfig.getSourceZoneId()).thenReturn(ZoneId.systemDefault());
when(mockDateProcessorConfig.getDestinationZoneId()).thenReturn(ZoneId.systemDefault());
when(mockDateProcessorConfig.getSourceLocale()).thenReturn(Locale.ROOT);
when(mockDateProcessorConfig.getToOriginationMetadata()).thenReturn(true);

dateProcessor = createObjectUnderTest();

Map<String, Object> testData = getTestData();
String formattedExpectedTime = expectedDateTime.format(DateTimeFormatter.ofPattern(pattern));
testData.put("logDate", formattedExpectedTime);

final Record<Event> record = buildRecordWithEvent(testData);
final List<Record<Event>> processedRecords = (List<Record<Event>>) dateProcessor.doExecute(Collections.singletonList(record));

Event event = processedRecords.get(0).getData();
Assertions.assertNotNull(event.getMetadata().getExternalOriginationTime());
Assertions.assertNotNull(event.getEventHandle().getExternalOriginationTime());
ZonedDateTime expectedZonedDatetime = expectedDateTime.atZone(mockDateProcessorConfig.getSourceZoneId()).truncatedTo(ChronoUnit.SECONDS);
Assertions.assertEquals(expectedZonedDatetime, event.getMetadata().getExternalOriginationTime().atZone(mockDateProcessorConfig.getSourceZoneId()));
verify(dateProcessingMatchSuccessCounter, times(1)).increment();
}

@Test
void date_processor_catches_exceptions_instead_of_throwing() {
final String dateWhen = UUID.randomUUID().toString();

0 comments on commit 597465a

Please sign in to comment.