diff --git a/data-prepper-plugins/date-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessor.java b/data-prepper-plugins/date-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessor.java index 328f8294bc..a2260a8694 100644 --- a/data-prepper-plugins/date-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessor.java +++ b/data-prepper-plugins/date-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessor.java @@ -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()); diff --git a/data-prepper-plugins/date-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessorTests.java b/data-prepper-plugins/date-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessorTests.java index 91299dc4b3..27a9af6a60 100644 --- a/data-prepper-plugins/date-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessorTests.java +++ b/data-prepper-plugins/date-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/date/DateProcessorTests.java @@ -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 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 testData = getTestData(); + String formattedExpectedTime = expectedDateTime.format(DateTimeFormatter.ofPattern(pattern)); + testData.put("logDate", formattedExpectedTime); + + final Record record = buildRecordWithEvent(testData); + final List> processedRecords = (List>) 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();