Search in sources :

Example 1 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class ExtendedBoundsTests method unparsed.

/**
     * Convert an extended bounds in parsed for into one in unparsed form.
     */
public static ExtendedBounds unparsed(ExtendedBounds template) {
    // It'd probably be better to randomize the formatter
    FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
    String minAsStr = template.getMin() == null ? null : formatter.printer().print(new Instant(template.getMin()));
    String maxAsStr = template.getMax() == null ? null : formatter.printer().print(new Instant(template.getMax()));
    return new ExtendedBounds(minAsStr, maxAsStr);
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) Instant(org.joda.time.Instant)

Example 2 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method assertDateFormatParsingThrowingException.

private void assertDateFormatParsingThrowingException(String pattern, String invalidDate) {
    try {
        FormatDateTimeFormatter formatter = Joda.forPattern(pattern);
        DateTimeFormatter parser = formatter.parser();
        parser.parseMillis(invalidDate);
        fail(String.format(Locale.ROOT, "Expected parsing exception for pattern [%s] with date [%s], but did not happen", pattern, invalidDate));
    } catch (IllegalArgumentException e) {
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 3 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testIsoVsCustom.

public void testIsoVsCustom() {
    DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
    long millis = formatter.parseMillis("1970-01-01T00:00:00");
    assertThat(millis, equalTo(0L));
    formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").withZone(DateTimeZone.UTC);
    millis = formatter.parseMillis("1970/01/01 00:00:00");
    assertThat(millis, equalTo(0L));
    FormatDateTimeFormatter formatter2 = Joda.forPattern("yyyy/MM/dd HH:mm:ss");
    millis = formatter2.parser().parseMillis("1970/01/01 00:00:00");
    assertThat(millis, equalTo(0L));
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 4 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testForInvalidDatesInEpochSecond.

public void testForInvalidDatesInEpochSecond() {
    FormatDateTimeFormatter formatter = Joda.forPattern("epoch_second");
    try {
        formatter.parser().parseDateTime(randomFrom("invalid date", "12345678901234567", "12345678901234567890"));
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Invalid format"));
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 5 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method assertValidDateFormatParsing.

private void assertValidDateFormatParsing(String pattern, String dateToParse, String expectedDate) {
    FormatDateTimeFormatter formatter = Joda.forPattern(pattern);
    assertThat(formatter.printer().print(formatter.parser().parseMillis(dateToParse)), is(expectedDate));
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Aggregations

FormatDateTimeFormatter (org.elasticsearch.common.joda.FormatDateTimeFormatter)19 DateTime (org.joda.time.DateTime)3 LocalDateTime (org.joda.time.LocalDateTime)3 MutableDateTime (org.joda.time.MutableDateTime)3 Settings (org.elasticsearch.common.settings.Settings)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 InetAddress (java.net.InetAddress)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexSettings (org.elasticsearch.index.IndexSettings)1 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)1 DocValueFormat (org.elasticsearch.search.DocValueFormat)1 SearchParseException (org.elasticsearch.search.SearchParseException)1 SearchContext (org.elasticsearch.search.internal.SearchContext)1 Instant (org.joda.time.Instant)1