Move testToUtc test to DateFormattersTests #38698 Backport #38610

The test was relying on toString in ZonedDateTime which is different to
what is formatted by strict_date_time when milliseconds are 0
The method is just delegating to dateFormatter, so that scenario should
be covered there.

closes #38359
Backport #38610
This commit is contained in:
Przemyslaw Gomulka 2019-02-11 11:34:25 +01:00 committed by GitHub
parent 7e10592940
commit ab9e2f2e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 15 deletions

View File

@ -21,8 +21,10 @@ package org.elasticsearch.common.time;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import java.time.Clock;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
@ -96,6 +98,8 @@ public class DateFormattersTests extends ESTestCase {
assertThat(e.getMessage(), is("failed to parse date field [1234.1234567890] with format [epoch_second]")); assertThat(e.getMessage(), is("failed to parse date field [1234.1234567890] with format [epoch_second]"));
} }
public void testEpochMilliParsersWithDifferentFormatters() { public void testEpochMilliParsersWithDifferentFormatters() {
DateFormatter formatter = DateFormatter.forPattern("strict_date_optional_time||epoch_millis"); DateFormatter formatter = DateFormatter.forPattern("strict_date_optional_time||epoch_millis");
TemporalAccessor accessor = formatter.parse("123"); TemporalAccessor accessor = formatter.parse("123");
@ -248,4 +252,12 @@ public class DateFormattersTests extends ESTestCase {
assertThat(roundupParser.getLocale(), is(locale)); assertThat(roundupParser.getLocale(), is(locale));
assertThat(formatter.locale(), is(locale)); assertThat(formatter.locale(), is(locale));
} }
public void test0MillisAreFormatted() {
DateFormatter formatter = DateFormatter.forPattern("strict_date_time");
Clock clock = Clock.fixed(ZonedDateTime.of(2019, 02, 8, 11, 43, 00, 0,
ZoneOffset.UTC).toInstant(), ZoneOffset.UTC);
String formatted = formatter.formatMillis(clock.millis());
assertThat(formatted, is("2019-02-08T11:43:00.000Z"));
}
} }

View File

@ -16,9 +16,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Objects; import java.util.Objects;
/** /**
@ -26,7 +24,7 @@ import java.util.Objects;
*/ */
public abstract class MonitoringDoc implements ToXContentObject { public abstract class MonitoringDoc implements ToXContentObject {
private static final DateFormatter dateTimeFormatter = DateFormatter.forPattern("strict_date_time"); private static final DateFormatter dateTimeFormatter = DateFormatter.forPattern("strict_date_time").withZone(ZoneOffset.UTC);
private final String cluster; private final String cluster;
private final long timestamp; private final long timestamp;
private final long intervalMillis; private final long intervalMillis;
@ -126,9 +124,7 @@ public abstract class MonitoringDoc implements ToXContentObject {
* @return a string representing the timestamp * @return a string representing the timestamp
*/ */
public static String toUTC(final long timestamp) { public static String toUTC(final long timestamp) {
ZonedDateTime zonedDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC); return dateTimeFormatter.formatMillis(timestamp);
return dateTimeFormatter.format(zonedDateTime);
} }
/** /**

View File

@ -26,8 +26,6 @@ import org.elasticsearch.xpack.monitoring.collector.shards.ShardMonitoringDoc;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -166,13 +164,6 @@ public abstract class BaseMonitoringDocTestCase<T extends MonitoringDoc> extends
} }
} }
public void testToUTC() {
final long timestamp = System.currentTimeMillis();
final String expected = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC).toString();
assertEquals(expected, MonitoringDoc.toUTC(timestamp));
}
public void testMonitoringNodeConstructor() { public void testMonitoringNodeConstructor() {
final String id = randomAlphaOfLength(5); final String id = randomAlphaOfLength(5);
final String name = randomAlphaOfLengthBetween(3, 10); final String name = randomAlphaOfLengthBetween(3, 10);