@Override public DateMathParser toDateMathParser() { return new JodaDateMathParser(this); } }
@Override public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { if (hasMilliSecondPrecision) { out.append(String.valueOf(getDateTimeMillis(partial))); } else { out.append(String.valueOf(getDateTimeMillis(partial) / 1000)); } }
@Override public String toString() { return "[index=" + index + ", deleteDate=" + Joda.getStrictStandardDateFormatter().formatMillis(deleteDateInMillis) + "]"; }
return new JodaDateFormatter(input, ISODateTimeFormat.dateOptionalTimeParser().withLocale(Locale.ROOT).withZone(DateTimeZone.UTC), ISODateTimeFormat.dateTime().withLocale(Locale.ROOT).withZone(DateTimeZone.UTC)); formatter = ISODateTimeFormat.yearMonthDay(); } else if ("epoch_second".equals(input)) { formatter = new DateTimeFormatterBuilder().append(new EpochTimePrinter(false), new EpochTimeParser(false)).toFormatter(); } else if ("epoch_millis".equals(input)) { formatter = new DateTimeFormatterBuilder().append(new EpochTimePrinter(true), new EpochTimeParser(true)).toFormatter(); return new JodaDateFormatter(input, StrictISODateTimeFormat.dateOptionalTimeParser().withLocale(Locale.ROOT).withZone(DateTimeZone.UTC), StrictISODateTimeFormat.dateTime().withLocale(Locale.ROOT).withZone(DateTimeZone.UTC)); formatter = forPattern(input).parser; } else { DateTimeFormatter dateTimeFormatter = null; for (int i = 0; i < formats.length; i++) { JodaDateFormatter currentFormatter = forPattern(formats[i]); DateTimeFormatter currentParser = currentFormatter.parser; if (dateTimeFormatter == null) { maybeLogJodaDeprecation(input); formatter = DateTimeFormat.forPattern(input); } catch (IllegalArgumentException e) { return new JodaDateFormatter(input, formatter, formatter);
public DateMath(String format, DateTimeZone timezone) { this(new DateMathParser(Joda.forPattern(format)), timezone); }
static DateFormatter forPattern(String input) { if (Strings.hasLength(input) == false) { throw new IllegalArgumentException("No date pattern provided"); } if (input.startsWith("8") == false) { return Joda.forPattern(input); } // force java 8 date format List<DateFormatter> formatters = new ArrayList<>(); for (String pattern : Strings.delimitedListToStringArray(input.substring(1), "||")) { if (Strings.hasLength(pattern) == false) { throw new IllegalArgumentException("Cannot have empty element in multi date format pattern: " + input); } formatters.add(DateFormatters.forPattern(pattern)); } if (formatters.size() == 1) { return formatters.get(0); } return new DateFormatters.MergedDateFormatter(input, formatters); } }
/** * Gets the format field to parse the from/to fields */ public String format() { return this.format == null ? null : this.format.format(); }
@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); }
public static DateFormatter getStrictStandardDateFormatter() { // 2014/10/10 DateTimeFormatter shortFormatter = new DateTimeFormatterBuilder() .appendFixedDecimal(DateTimeFieldType.year(), 4) .appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2) .appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2) .toFormatter() .withZoneUTC(); // 2014/10/10 12:12:12 DateTimeFormatter longFormatter = new DateTimeFormatterBuilder() .appendFixedDecimal(DateTimeFieldType.year(), 4) .appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2) .appendLiteral('/') .appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2) .appendLiteral(' ') .appendFixedSignedDecimal(DateTimeFieldType.hourOfDay(), 2) .appendLiteral(':') .appendFixedSignedDecimal(DateTimeFieldType.minuteOfHour(), 2) .appendLiteral(':') .appendFixedSignedDecimal(DateTimeFieldType.secondOfMinute(), 2) .toFormatter() .withZoneUTC(); DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(longFormatter.withZone(DateTimeZone.UTC).getPrinter(), new DateTimeParser[]{longFormatter.getParser(), shortFormatter.getParser(), new EpochTimeParser(true)}); DateTimeFormatter formatter = builder.toFormatter().withLocale(Locale.ROOT).withZone(DateTimeZone.UTC); return new JodaDateFormatter("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis", formatter, formatter); }
@Override public long parse(String text, LongSupplier now, boolean roundUp, ZoneId tz) { final DateTimeZone timeZone = tz == null ? null : DateUtils.zoneIdToDateTimeZone(tz); 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); }
DateMathParser getForceDateParser() { // pkg private for testing if (this.format != null) { return new DateMathParser(this.format); } return null; }
boolean isPositive = text.startsWith("-") == false; int firstDotIndex = text.indexOf('.'); boolean isTooLong = (firstDotIndex == -1 ? text.length() : firstDotIndex) > estimateParsedLength();
JodaDateFormatter formatter = new JodaDateFormatter(dateFormatterPattern, parser, parser); DateMathParser dateMathParser = formatter.toDateMathParser(); long millis = dateMathParser.parse(mathExpression, context::getStartTime, false, DateUtils.dateTimeZoneToZoneId(timeZone)); String time = formatter.formatMillis(millis); beforePlaceHolderSb.append(time); inPlaceHolderSb = new StringBuilder();
@Override public DateFormatter withZone(ZoneId zoneId) { DateTimeZone timeZone = DateUtils.zoneIdToDateTimeZone(zoneId); DateTimeFormatter parser = this.parser.withZone(timeZone); DateTimeFormatter printer = this.printer.withZone(timeZone); return new JodaDateFormatter(pattern, parser, printer); }
private long parseDateTime(String value, DateTimeZone timeZone, boolean roundUpIfNoTime) { DateTimeFormatter parser = dateTimeFormatter.parser; if (timeZone != null) { parser = parser.withZone(timeZone); } try { MutableDateTime date; // We use 01/01/1970 as a base date so that things keep working with date // fields that are filled with times without dates if (roundUpIfNoTime) { date = new MutableDateTime(1970, 1, 1, 23, 59, 59, 999, DateTimeZone.UTC); } else { date = new MutableDateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC); } final int end = parser.parseInto(date, value, 0); if (end < 0) { int position = ~end; throw new IllegalArgumentException("Parse failure at index [" + position + "] of [" + value + "]"); } else if (end != value.length()) { throw new IllegalArgumentException("Unrecognized chars at the end of [" + value + "]: [" + value.substring(end) + "]"); } return date.getMillis(); } catch (IllegalArgumentException e) { throw new ElasticsearchParseException("failed to parse date field [{}] with format [{}]", e, value, dateTimeFormatter.pattern()); } }
DateMathParser getForceDateParser() { // pkg private for testing if (this.format != null) { return this.format.toDateMathParser(); } return null; }
public Timestamp(boolean enabled, String format, String defaultTimestamp, Boolean ignoreMissing) { this.enabled = enabled; this.format = format; this.dateTimeFormatter = Joda.forPattern(format); this.defaultTimestamp = defaultTimestamp; this.ignoreMissing = ignoreMissing; }
@Override public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { if (hasMilliSecondPrecision) { buf.append(String.valueOf(getDateTimeMillis(partial))); } else { buf.append(String.valueOf(getDateTimeMillis(partial) / 1000)); } }
/** * Gets the format field to parse the from/to fields */ public String format() { return this.format == null ? null : this.format.format(); }
@Override public DateFormatter withLocale(Locale locale) { DateTimeFormatter parser = this.parser.withLocale(locale); DateTimeFormatter printer = this.printer.withLocale(locale); return new JodaDateFormatter(pattern, parser, printer); }