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");
}
}
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) {
}
}
}
}
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)));
}
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);
}
}
Aggregations