From 5534a60fa0b0a3016a71ec720ee2a7e6d8922322 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Wed, 7 Oct 2020 14:12:04 +0200 Subject: [PATCH] strict_date_optional_time_nanos with width 1 on nanos part (#63117) (#63387) This formatter should allow parsing fraction of a second with minimum width of 1. The same is allowed for strict_date_optional_time closes #61357 --- .../org/elasticsearch/common/time/DateFormatters.java | 4 ++-- .../elasticsearch/common/time/DateFormattersTests.java | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java index b8425baa017..1f67b90db2e 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -159,11 +159,11 @@ public class DateFormatters { .appendLiteral('T') .append(STRICT_HOUR_MINUTE_SECOND_FORMATTER) .optionalStart() - .appendFraction(NANO_OF_SECOND, 3, 9, true) + .appendFraction(NANO_OF_SECOND, 1, 9, true) .optionalEnd() .optionalStart() .appendLiteral(',') - .appendFraction(NANO_OF_SECOND, 3, 9, false) + .appendFraction(NANO_OF_SECOND, 1, 9, false) .optionalEnd() .optionalStart() .appendZoneOrOffsetId() diff --git a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java index b7da30a18f0..bfcb0b65b17 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java @@ -145,6 +145,15 @@ public class DateFormattersTests extends ESTestCase { assertThat(first, is(second)); } + public void testNanoOfSecondWidth() throws Exception { + ZonedDateTime first = DateFormatters.from( + DateFormatters.forPattern("strict_date_optional_time_nanos").parse("1970-01-01T00:00:00.1")); + assertThat(first.getNano(), is(100000000)); + ZonedDateTime second = DateFormatters.from( + DateFormatters.forPattern("strict_date_optional_time_nanos").parse("1970-01-01T00:00:00.000000001")); + assertThat(second.getNano(), is(1)); + } + public void testLocales() { assertThat(DateFormatters.forPattern("strict_date_optional_time").locale(), is(Locale.ROOT)); Locale locale = randomLocale(random());