Search in sources :

Example 16 with FormatDateTimeFormatter

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

the class SimpleJodaTests method testThatNegativeEpochsCanBeParsed.

public void testThatNegativeEpochsCanBeParsed() {
    // problem: negative epochs can be arbitrary in size...
    boolean parseMilliSeconds = randomBoolean();
    FormatDateTimeFormatter formatter = Joda.forPattern(parseMilliSeconds ? "epoch_millis" : "epoch_second");
    DateTime dateTime = formatter.parser().parseDateTime("-10000");
    assertThat(dateTime.getYear(), is(1969));
    assertThat(dateTime.getMonthOfYear(), is(12));
    assertThat(dateTime.getDayOfMonth(), is(31));
    if (parseMilliSeconds) {
        // utc timezone, +2 offset due to CEST
        assertThat(dateTime.getHourOfDay(), is(23));
        assertThat(dateTime.getMinuteOfHour(), is(59));
        assertThat(dateTime.getSecondOfMinute(), is(50));
    } else {
        // utc timezone, +2 offset due to CEST
        assertThat(dateTime.getHourOfDay(), is(21));
        assertThat(dateTime.getMinuteOfHour(), is(13));
        assertThat(dateTime.getSecondOfMinute(), is(20));
    }
    // every negative epoch must be parsed, no matter if exact the size or bigger
    if (parseMilliSeconds) {
        formatter.parser().parseDateTime("-100000000");
        formatter.parser().parseDateTime("-999999999999");
        formatter.parser().parseDateTime("-1234567890123");
        formatter.parser().parseDateTime("-1234567890123456789");
    } else {
        formatter.parser().parseDateTime("-100000000");
        formatter.parser().parseDateTime("-1234567890");
        formatter.parser().parseDateTime("-1234567890123456");
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) MutableDateTime(org.joda.time.MutableDateTime)

Example 17 with FormatDateTimeFormatter

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

the class SimpleJodaTests method testThatRootObjectParsingIsStrict.

public void testThatRootObjectParsingIsStrict() throws Exception {
    String[] datesThatWork = new String[] { "2014/10/10", "2014/10/10 12:12:12", "2014-05-05", "2014-05-05T12:12:12.123Z" };
    String[] datesThatShouldNotWork = new String[] { "5-05-05", "2014-5-05", "2014-05-5", "2014-05-05T1:12:12.123Z", "2014-05-05T12:1:12.123Z", "2014-05-05T12:12:1.123Z", "4/10/10", "2014/1/10", "2014/10/1", "2014/10/10 1:12:12", "2014/10/10 12:1:12", "2014/10/10 12:12:1" };
    // good case
    for (String date : datesThatWork) {
        boolean dateParsingSuccessful = false;
        for (FormatDateTimeFormatter dateTimeFormatter : RootObjectMapper.Defaults.DYNAMIC_DATE_TIME_FORMATTERS) {
            try {
                dateTimeFormatter.parser().parseMillis(date);
                dateParsingSuccessful = true;
                break;
            } catch (Exception e) {
            }
        }
        if (!dateParsingSuccessful) {
            fail("Parsing for date " + date + " in root object mapper failed, but shouldnt");
        }
    }
    // bad case
    for (String date : datesThatShouldNotWork) {
        for (FormatDateTimeFormatter dateTimeFormatter : RootObjectMapper.Defaults.DYNAMIC_DATE_TIME_FORMATTERS) {
            try {
                dateTimeFormatter.parser().parseMillis(date);
                fail(String.format(Locale.ROOT, "Expected exception when parsing date %s in root mapper", date));
            } catch (Exception e) {
            }
        }
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 18 with FormatDateTimeFormatter

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

the class SimpleJodaTests method testMultipleFormats.

public void testMultipleFormats() {
    FormatDateTimeFormatter formatter = Joda.forPattern("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd");
    long millis = formatter.parser().parseMillis("1970/01/01 00:00:00");
    assertThat("1970/01/01 00:00:00", is(formatter.printer().print(millis)));
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 19 with FormatDateTimeFormatter

use of org.elasticsearch.common.joda.FormatDateTimeFormatter in project elasticsearch-jdbc by jprante.

the class StandardContext method writeState.

protected void writeState() {
    String statefile = settings.get("statefile");
    if (statefile == null || source == null || source.getMetric() == null) {
        return;
    }
    try {
        File file = new File(statefile);
        if (file.getParentFile() != null && !file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists() || file.canWrite()) {
            Writer writer = new FileWriter(statefile);
            FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
            Settings.Builder settingsBuilder = Settings.settingsBuilder().put(settings).put("metrics.lastexecutionstart", formatter.printer().print(source.getMetric().getLastExecutionStart())).put("metrics.lastexecutionend", formatter.printer().print(source.getMetric().getLastExecutionEnd())).put("metrics.counter", source.getMetric().getCounter());
            XContentBuilder builder = jsonBuilder().prettyPrint().startObject().field("type", "jdbc").field("jdbc").map(settingsBuilder.build().getAsStructuredMap()).endObject();
            writer.write(builder.string());
            writer.close();
            if (file.length() > 0) {
                logger.info("state persisted to {}", statefile);
            } else {
                logger.error("state file truncated!");
            }
        } else {
            logger.warn("can't write to {}", statefile);
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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