Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/95996.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 95996
summary: Fixing `DateProcessor` when the format is `epoch_millis`
area: Ingest Node
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale
// fill the rest of the date up with the parsed date
if (accessor.isSupported(ChronoField.YEAR) == false
&& accessor.isSupported(ChronoField.YEAR_OF_ERA) == false
&& accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false) {
&& accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false
&& accessor.isSupported(ChronoField.INSTANT_SECONDS) == false) {
int year = LocalDate.now(ZoneOffset.UTC).getYear();
ZonedDateTime newTime = Instant.EPOCH.atZone(ZoneOffset.UTC).withYear(year);
for (ChronoField field : FIELDS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.common.time.FormatNames;
import org.elasticsearch.test.ESTestCase;

import java.time.ZoneId;
Expand Down Expand Up @@ -133,6 +134,19 @@ public void testDefaultHourDefaultedToTimezoneOverride() {
assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 01, 01, 0, 0, 0, 0, timezone)));
}

public void testParseAllFormatNames() {
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
for (FormatNames formatName : FormatNames.values()) {
String name = formatName.getName();
DateFormatter formatter = DateFormatter.forPattern(name);
String formattedInput = formatter.format(now);
DateFormat dateFormat = DateFormat.fromString(name);
ZonedDateTime parsed = dateFormat.getFunction(name, ZoneOffset.UTC, Locale.ROOT).apply(formattedInput);
String formattedOutput = formatter.format(parsed);
assertThat(name, formattedOutput, equalTo(formattedInput));
}
}

public void testParseUnixMs() {
assertThat(
DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null).apply("1000500").toInstant().toEpochMilli(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ public void testUnix() {
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("1970-01-01T00:16:40.500Z"));
}

public void testEpochMillis() {
DateProcessor dateProcessor = new DateProcessor(
randomAlphaOfLength(10),
null,
templatize(ZoneOffset.UTC),
templatize(randomLocale(random())),
"date_as_string",
List.of("epoch_millis"),
"date_as_date"
);
Map<String, Object> document = new HashMap<>();
document.put("date_as_string", "1683701716065");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
dateProcessor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2023-05-10T06:55:16.065Z"));

document = new HashMap<>();
document.put("date_as_string", 1683701716065L);
ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
dateProcessor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2023-05-10T06:55:16.065Z"));
}

public void testInvalidTimezone() {
DateProcessor processor = new DateProcessor(
randomAlphaOfLength(10),
Expand Down