private Date getDate(Object value) { if (value == null) { return null; } if (value instanceof Date) { return ((Date) value); } else { DateMathParser parser = new DateMathParser(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER); try { return new Date(parser.parse(value.toString(), new LongSupplier() { public long getAsLong() { return System.currentTimeMillis(); } })); } catch (ElasticsearchParseException e) { logger.warn("unable to parse date " + value.toString(), e); } } return null; }
@Override public long parseLong(String value, final SearchContext searchContext) { final Callable<Long> now = new Callable<Long>() { @Override public Long call() throws Exception { return searchContext.nowInMillis(); } }; return parser.parse(value, now, false, timezone); }
DateMathParser getForceDateParser() { // pkg private for testing if (this.format != null) { return new DateMathParser(this.format); } return null; }
public long parse(String text, Callable<Long> now, boolean roundUp, DateTimeZone timeZone) { long time; String mathString; if (text.startsWith("now")) { try { time = now.call(); } catch (Exception e) { throw new ElasticsearchParseException("could not read the current timestamp", e); } mathString = text.substring("now".length()); } else { int index = text.indexOf("||"); if (index == -1) { return parseDateTime(text, timeZone, roundUp); } time = parseDateTime(text.substring(0, index), timeZone, false); mathString = text.substring(index + 2); } return parseMath(mathString, time, roundUp, timeZone); }
} else if ("time_zone".equals(currentFieldName)) { try { qpSettings.timeZone(DateMathParser.parseZone(parser.text())); } catch (IllegalArgumentException e) { throw new QueryParsingException(parseContext.index(), "[query_string] time_zone [" + parser.text() + "] is unknown");
public long parse(String text, LongSupplier now) { return parse(text, now, false, null); }
public DateTime(FormatDateTimeFormatter formatter, DateTimeZone timeZone) { this.formatter = Objects.requireNonNull(formatter); this.timeZone = Objects.requireNonNull(timeZone); this.parser = new DateMathParser(formatter); }
public long parse(String text, LongSupplier now, boolean roundUp, DateTimeZone timeZone) { long time; String mathString; if (text.startsWith("now")) { try { time = now.getAsLong(); } catch (Exception e) { throw new ElasticsearchParseException("could not read the current timestamp", e); } mathString = text.substring("now".length()); } else { int index = text.indexOf("||"); if (index == -1) { return parseDateTime(text, timeZone, roundUp); } time = parseDateTime(text.substring(0, index), timeZone, false); mathString = text.substring(index + 2); } return parseMath(mathString, time, roundUp, timeZone); }
@Override public Query rangeQuery(String field, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, ShapeRelation relation, @Nullable DateTimeZone timeZone, @Nullable DateMathParser parser, QueryShardContext context) { DateTimeZone zone = (timeZone == null) ? DateTimeZone.UTC : timeZone; DateMathParser dateMathParser = (parser == null) ? new DateMathParser(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER) : parser; Long low = lowerTerm == null ? Long.MIN_VALUE : dateMathParser.parse(lowerTerm instanceof BytesRef ? ((BytesRef) lowerTerm).utf8ToString() : lowerTerm.toString(), context::nowInMillis, false, zone); Long high = upperTerm == null ? Long.MAX_VALUE : dateMathParser.parse(upperTerm instanceof BytesRef ? ((BytesRef) upperTerm).utf8ToString() : upperTerm.toString(), context::nowInMillis, false, zone); return super.rangeQuery(field, low, high, includeLower, includeUpper, relation, zone, dateMathParser, context); } @Override
@Override public long parseLong(String value, boolean roundUp, LongSupplier now) { return parser.parse(value, now, roundUp, timeZone); }
public void setDateTimeFormatter(FormatDateTimeFormatter dateTimeFormatter) { checkIfFrozen(); this.dateTimeFormatter = dateTimeFormatter; this.dateMathParser = new DateMathParser(dateTimeFormatter); }
public long parse(String text, LongSupplier now, boolean roundUp, DateTimeZone timeZone) { long time; String mathString; if (text.startsWith("now")) { try { time = now.getAsLong(); } catch (Exception e) { throw new ElasticsearchParseException("could not read the current timestamp", e); } mathString = text.substring("now".length()); } else { int index = text.indexOf("||"); if (index == -1) { return parseDateTime(text, timeZone, roundUp); } time = parseDateTime(text.substring(0, index), timeZone, false); mathString = text.substring(index + 2); } return parseMath(mathString, time, roundUp, timeZone); }
@Override public Query rangeQuery(String field, boolean hasDocValues, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, ShapeRelation relation, @Nullable DateTimeZone timeZone, @Nullable DateMathParser parser, QueryShardContext context) { DateTimeZone zone = (timeZone == null) ? DateTimeZone.UTC : timeZone; DateMathParser dateMathParser = (parser == null) ? new DateMathParser(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER) : parser; Long low = lowerTerm == null ? Long.MIN_VALUE : dateMathParser.parse(lowerTerm instanceof BytesRef ? ((BytesRef) lowerTerm).utf8ToString() : lowerTerm.toString(), context::nowInMillis, false, zone); Long high = upperTerm == null ? Long.MAX_VALUE : dateMathParser.parse(upperTerm instanceof BytesRef ? ((BytesRef) upperTerm).utf8ToString() : upperTerm.toString(), context::nowInMillis, false, zone); return super.rangeQuery(field, hasDocValues, low, high, includeLower, includeUpper, relation, zone, dateMathParser, context); } @Override
public long parse(String text, LongSupplier now) { return parse(text, now, false, null); }
public void setDateTimeFormatter(FormatDateTimeFormatter dateTimeFormatter) { checkIfFrozen(); this.dateTimeFormatter = dateTimeFormatter; this.dateMathParser = new DateMathParser(dateTimeFormatter); }
DateMathParser dateMathParser = new DateMathParser(formatter); long millis = dateMathParser.parse(mathExpression, context::getStartTime, false, timeZone);
public long parse(String text, Callable<Long> now) { return parse(text, now, false, null); }
public void setDateTimeFormatter(FormatDateTimeFormatter dateTimeFormatter) { checkIfFrozen(); this.dateTimeFormatter = dateTimeFormatter; this.dateMathParser = new DateMathParser(dateTimeFormatter); }
DateMathParser dateMathParser = new DateMathParser(formatter); long millis = dateMathParser.parse(mathExpression, new Callable<Long>() { @Override public Long call() throws Exception {
private Number parse(DateMathParser dateMathParser, String dateStr) { return dateMathParser.parse(dateStr, () -> {throw new IllegalArgumentException("now is not used at indexing time");}); } @Override