From 2a6e6866bd6a25f469c07ba0766edec6272e93ac Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Tue, 30 May 2017 09:42:26 -0700 Subject: [PATCH] Fix floating-point error when DateProcessor parses UNIX (#24947) DateProcessor's DateFormat UNIX format parser resulted in a floating point rounding error when parsing certain stringed epoch times. Now Double.parseDouble is used, preserving the intented input. --- .../main/java/org/elasticsearch/ingest/common/DateFormat.java | 2 +- .../java/org/elasticsearch/ingest/common/DateFormatTests.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java index 9ead2d05f7d..bf664afb407 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java @@ -38,7 +38,7 @@ enum DateFormat { Unix { @Override Function getFunction(String format, DateTimeZone timezone, Locale locale) { - return (date) -> new DateTime((long)(Float.parseFloat(date) * 1000), timezone); + return (date) -> new DateTime((long)(Double.parseDouble(date) * 1000), timezone); } }, UnixMs { diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java index 886630dbbd9..415ee872093 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java @@ -50,6 +50,10 @@ public class DateFormatTests extends ESTestCase { assertThat(DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null).apply("1000.5").getMillis(), equalTo(1000500L)); } + public void testParseUnixWithMsPrecision() { + assertThat(DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null).apply("1495718015").getMillis(), equalTo(1495718015000L)); + } + public void testParseISO8601() { assertThat(DateFormat.Iso8601.getFunction(null, DateTimeZone.UTC, null).apply("2001-01-01T00:00:00-0800").getMillis(), equalTo(978336000000L));