|
21 | 21 |
|
22 | 22 | import org.elasticsearch.test.ESTestCase; |
23 | 23 |
|
| 24 | +import java.time.Instant; |
24 | 25 | import java.time.ZoneId; |
25 | 26 | import java.time.ZonedDateTime; |
26 | 27 | import java.time.format.DateTimeParseException; |
@@ -56,6 +57,42 @@ public void testEpochMilliParser() { |
56 | 57 | assertSameFormat(formatter, 1); |
57 | 58 | } |
58 | 59 |
|
| 60 | + // this is not in the duelling tests, because the epoch second parser in joda time drops the milliseconds after the comma |
| 61 | + // but is able to parse the rest |
| 62 | + // as this feature is supported it also makes sense to make it exact |
| 63 | + public void testEpochSecondParser() { |
| 64 | + DateFormatter formatter = DateFormatters.forPattern("epoch_second"); |
| 65 | + |
| 66 | + assertThat(Instant.from(formatter.parse("1234.567")).toEpochMilli(), is(1234567L)); |
| 67 | + assertThat(Instant.from(formatter.parse("1234.")).getNano(), is(0)); |
| 68 | + assertThat(Instant.from(formatter.parse("1234.")).getEpochSecond(), is(1234L)); |
| 69 | + assertThat(Instant.from(formatter.parse("1234.1")).getNano(), is(100_000_000)); |
| 70 | + assertThat(Instant.from(formatter.parse("1234.12")).getNano(), is(120_000_000)); |
| 71 | + assertThat(Instant.from(formatter.parse("1234.123")).getNano(), is(123_000_000)); |
| 72 | + assertThat(Instant.from(formatter.parse("1234.1234")).getNano(), is(123_400_000)); |
| 73 | + assertThat(Instant.from(formatter.parse("1234.12345")).getNano(), is(123_450_000)); |
| 74 | + assertThat(Instant.from(formatter.parse("1234.123456")).getNano(), is(123_456_000)); |
| 75 | + assertThat(Instant.from(formatter.parse("1234.1234567")).getNano(), is(123_456_700)); |
| 76 | + assertThat(Instant.from(formatter.parse("1234.12345678")).getNano(), is(123_456_780)); |
| 77 | + assertThat(Instant.from(formatter.parse("1234.123456789")).getNano(), is(123_456_789)); |
| 78 | + DateTimeParseException e = expectThrows(DateTimeParseException.class, () -> formatter.parse("1234.1234567890")); |
| 79 | + assertThat(e.getMessage(), is("too much granularity after dot [1234.1234567890]")); |
| 80 | + e = expectThrows(DateTimeParseException.class, () -> formatter.parse("1234.123456789013221")); |
| 81 | + assertThat(e.getMessage(), is("too much granularity after dot [1234.123456789013221]")); |
| 82 | + e = expectThrows(DateTimeParseException.class, () -> formatter.parse("abc")); |
| 83 | + assertThat(e.getMessage(), is("invalid number [abc]")); |
| 84 | + e = expectThrows(DateTimeParseException.class, () -> formatter.parse("1234.abc")); |
| 85 | + assertThat(e.getMessage(), is("invalid number [1234.abc]")); |
| 86 | + |
| 87 | + // different zone, should still yield the same output, as epoch is time zone independent |
| 88 | + ZoneId zoneId = randomZone(); |
| 89 | + DateFormatter zonedFormatter = formatter.withZone(zoneId); |
| 90 | + |
| 91 | + assertThatSameDateTime(formatter, zonedFormatter, randomLongBetween(-100_000_000, 100_000_000)); |
| 92 | + assertSameFormat(formatter, randomLongBetween(-100_000_000, 100_000_000)); |
| 93 | + assertThat(formatter.format(Instant.ofEpochSecond(1234, 567_000_000)), is("1234.567")); |
| 94 | + } |
| 95 | + |
59 | 96 | public void testEpochMilliParsersWithDifferentFormatters() { |
60 | 97 | DateFormatter formatter = DateFormatters.forPattern("strict_date_optional_time||epoch_millis"); |
61 | 98 | TemporalAccessor accessor = formatter.parse("123"); |
|
0 commit comments