Search in sources :

Example 26 with DateFormatter

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));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateFormatter(org.opensearch.common.time.DateFormatter) Matchers.containsString(org.hamcrest.Matchers.containsString) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime)

Example 27 with DateFormatter

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);
}
Also used : DateFormatter(org.opensearch.common.time.DateFormatter) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 28 with DateFormatter

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);
}
Also used : DateFormatter(org.opensearch.common.time.DateFormatter)

Example 29 with DateFormatter

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));
}
Also used : Locale(java.util.Locale) DateFormatter(org.opensearch.common.time.DateFormatter) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 30 with DateFormatter

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));
}
Also used : DateFormatter(org.opensearch.common.time.DateFormatter)

Aggregations

DateFormatter (org.opensearch.common.time.DateFormatter)34 ZonedDateTime (java.time.ZonedDateTime)9 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ZoneId (java.time.ZoneId)7 SearchResponse (org.opensearch.action.search.SearchResponse)6 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)6 ArrayList (java.util.ArrayList)5 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)4 DateMathParser (org.opensearch.common.time.DateMathParser)4 AggregationBuilders.dateHistogram (org.opensearch.search.aggregations.AggregationBuilders.dateHistogram)4 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)4 DateTime (org.joda.time.DateTime)3 QueryShardContext (org.opensearch.index.query.QueryShardContext)3 Instant (java.time.Instant)2 ZoneOffset (java.time.ZoneOffset)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 BinaryDocValuesRangeQuery (org.apache.lucene.queries.BinaryDocValuesRangeQuery)2 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2