use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class JavaJodaTimeDuellingTests method testSamePrinterOutputWithTimeZone.
public void testSamePrinterOutputWithTimeZone() {
String format = "strict_date_optional_time";
String dateInput = "2017-02-01T08:02:00.000-01:00";
DateFormatter javaFormatter = DateFormatter.forPattern(format);
TemporalAccessor javaDate = javaFormatter.parse(dateInput);
DateFormatter jodaFormatter = Joda.forPattern(format);
DateTime dateTime = jodaFormatter.parseJoda(dateInput);
String javaDateString = javaFormatter.withZone(ZoneOffset.ofHours(-1)).format(javaDate);
String jodaDateString = jodaFormatter.withZone(ZoneOffset.ofHours(-1)).formatJoda(dateTime);
String message = String.format(Locale.ROOT, "expected string representation to be equal for format [%s]: joda [%s], java [%s]", format, jodaDateString, javaDateString);
assertThat(message, javaDateString, is(jodaDateString));
}
use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class JavaJodaTimeDuellingTests method testIso8601Parsers.
// the iso 8601 parser is available via Joda.forPattern(), so we have to test this slightly differently
public void testIso8601Parsers() {
String format = "iso8601";
org.joda.time.format.DateTimeFormatter isoFormatter = ISODateTimeFormat.dateTimeParser().withZone(DateTimeZone.UTC);
JodaDateFormatter jodaFormatter = new JodaDateFormatter(format, isoFormatter, isoFormatter);
DateFormatter javaFormatter = DateFormatter.forPattern(format);
assertSameDate("2018-10-10", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10+0430", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11-08:00", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11Z", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12+0100", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12.123", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12.123Z", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12.123+0000", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12,123", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12,123Z", format, jodaFormatter, javaFormatter);
assertSameDate("2018-10-10T10:11:12,123+05:30", format, jodaFormatter, javaFormatter);
}
use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class JavaJodaTimeDuellingTests method assertSameDate.
private void assertSameDate(String input, String format, Locale locale) {
DateFormatter jodaFormatter = Joda.forPattern(format).withLocale(locale);
DateFormatter javaFormatter = DateFormatter.forPattern(format).withLocale(locale);
assertSameDate(input, format, jodaFormatter, javaFormatter);
}
use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class JavaJodaTimeDuellingTests method testDateFormatterWithLocale.
public void testDateFormatterWithLocale() {
Locale locale = randomLocale(random());
String pattern = randomBoolean() ? "strict_date_optional_time||date_time" : "date_time||strict_date_optional_time";
DateFormatter formatter = DateFormatter.forPattern(pattern).withLocale(locale);
assertThat(formatter.pattern(), is(pattern));
assertThat(formatter.locale(), is(locale));
}
use of org.opensearch.common.time.DateFormatter in project OpenSearch by opensearch-project.
the class OpenSearchTestCaseTests method testRandomDateFormatterPattern.
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/61496")
public void testRandomDateFormatterPattern() {
DateFormatter formatter = DateFormatter.forPattern(randomDateFormatterPattern());
/*
* Make sure it doesn't crash trying to format some dates and
* that round tripping through millis doesn't lose any information.
* Interestingly, round tripping through a string *can* lose
* information because not all date formats spit out milliseconds.
* Hell, not all of them spit out the time of day at all!
* But going from text back to millis back to text should
* be fine!
*/
String formatted = formatter.formatMillis(randomLongBetween(0, 2_000_000_000_000L));
String formattedAgain = formatter.formatMillis(formatter.parseMillis(formatted));
assertThat(formattedAgain, equalTo(formatted));
}
Aggregations