Skip to content

Commit 96a2ab0

Browse files
authored
Fix fractional seconds for strict_date_optional_time (#41871)
The fractional seconds portion of strict_date_optional_time was accidentally copied from the printer, which always prints at least 3 fractional digits. This commit fixes the formatter to allow 1 or 2 fractional seconds. closes #41633
1 parent 070b4f7 commit 96a2ab0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

server/src/main/java/org/elasticsearch/common/time/DateFormatters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public class DateFormatters {
110110
.appendLiteral(':')
111111
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
112112
.optionalStart()
113-
.appendFraction(NANO_OF_SECOND, 3, 9, true)
113+
.appendFraction(NANO_OF_SECOND, 1, 9, true)
114114
.optionalEnd()
115115
.optionalEnd()
116116
.optionalStart()

server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,44 @@ public void test0MillisAreFormatted() {
260260
String formatted = formatter.formatMillis(clock.millis());
261261
assertThat(formatted, is("2019-02-08T11:43:00.000Z"));
262262
}
263+
264+
public void testFractionalSeconds() {
265+
DateFormatter formatter = DateFormatters.forPattern("strict_date_optional_time");
266+
{
267+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.1Z"));
268+
assertThat(instant.getNano(), is(100_000_000));
269+
}
270+
{
271+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.12Z"));
272+
assertThat(instant.getNano(), is(120_000_000));
273+
}
274+
{
275+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.123Z"));
276+
assertThat(instant.getNano(), is(123_000_000));
277+
}
278+
{
279+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.1234Z"));
280+
assertThat(instant.getNano(), is(123_400_000));
281+
}
282+
{
283+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.12345Z"));
284+
assertThat(instant.getNano(), is(123_450_000));
285+
}
286+
{
287+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.123456Z"));
288+
assertThat(instant.getNano(), is(123_456_000));
289+
}
290+
{
291+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.1234567Z"));
292+
assertThat(instant.getNano(), is(123_456_700));
293+
}
294+
{
295+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.12345678Z"));
296+
assertThat(instant.getNano(), is(123_456_780));
297+
}
298+
{
299+
Instant instant = Instant.from(formatter.parse("2019-05-06T14:52:37.123456789Z"));
300+
assertThat(instant.getNano(), is(123_456_789));
301+
}
302+
}
263303
}

0 commit comments

Comments
 (0)