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

[BUG] The date-processor doesn't parse times in AM/PM format #4564

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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 @@ -131,10 +131,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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading