Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
19e3035
Core: Move aggs/mapping code over to java time
spinscale Sep 20, 2018
baf7acd
run test on java9 only
spinscale Sep 20, 2018
efe5661
fix compilation error
spinscale Sep 20, 2018
b856795
fix randomized test to use java time
spinscale Sep 20, 2018
443bdea
silence another test for now on java8 due to BWC issues without syste…
spinscale Sep 20, 2018
995855d
Merge branch 'master' into java-time
spinscale Sep 21, 2018
739ca1f
Merge branch 'master' into java-time
spinscale Sep 24, 2018
af7abc8
Merge branch 'master' into java-time
spinscale Sep 25, 2018
e6d61d2
Merge branch 'master' into java-time
spinscale Sep 26, 2018
a464a05
Merge branch 'master' into java-time
spinscale Sep 27, 2018
79f2ebb
Merge branch 'master' into java-time
spinscale Sep 27, 2018
a9de009
fix wrong casting
spinscale Sep 27, 2018
c9b7e22
Merge branch 'master' into java-time
spinscale Sep 28, 2018
5d34a06
fix import
spinscale Sep 28, 2018
121b208
Merge branch 'master' into java-time
spinscale Sep 28, 2018
4ab9cc2
fix compilation errors
spinscale Sep 28, 2018
13301c1
Merge branch 'master' into java-time
spinscale Sep 28, 2018
8f4564e
fix unit test
spinscale Sep 28, 2018
720caf8
Merge branch 'master' into java-time
spinscale Oct 1, 2018
26905c9
Merge branch 'master' into java-time
spinscale Oct 1, 2018
7b88806
Merge branch 'master' into java-time
spinscale Oct 2, 2018
d9b241c
Merge branch 'master' into java-time
spinscale Oct 2, 2018
3f8e10a
fix compilation errors
spinscale Oct 2, 2018
3df911f
fix tests
spinscale Oct 2, 2018
a1987ef
remove unused imports
spinscale Oct 2, 2018
8cd9cfa
Merge branch 'master' into java-time
spinscale Oct 3, 2018
d8af35d
Merge branch 'master' into java-time
spinscale Oct 4, 2018
5e50374
Merge branch 'master' into java-time
spinscale Oct 5, 2018
50e86b5
Merge branch 'master' into java-time
spinscale Oct 6, 2018
c58040f
Merge branch 'master' into java-time
spinscale Oct 8, 2018
80f7e69
fix test by not using root locale
spinscale Oct 8, 2018
3f74ffb
Merge branch 'master' into java-time
spinscale Oct 9, 2018
a8fd14c
Merge branch 'master' into java-time
spinscale Oct 10, 2018
ae50fcc
Merge branch 'master' into java-time
spinscale Oct 11, 2018
0d8c7e3
Add floating point parsing of epoch millis
spinscale Oct 11, 2018
fc99549
Merge branch 'master' into java-time
spinscale Oct 15, 2018
dee1b8e
catch both exceptions, fixes tests properly
spinscale Oct 15, 2018
113eb93
add another test for epoch millis date formatter
spinscale Oct 16, 2018
2861b7c
Merge branch 'master' into java-time
spinscale Oct 16, 2018
390ca93
Merge branch 'master' into java-time
spinscale Oct 16, 2018
dab811d
remove some TODOs
spinscale Oct 16, 2018
982c418
Merge branch 'master' into java-time
spinscale Oct 17, 2018
f56a596
Merge branch 'master' into java-time
spinscale Oct 17, 2018
7bdc090
Merge branch 'master' into java-time
spinscale Oct 18, 2018
418444f
Merge branch 'master' into java-time
spinscale Oct 23, 2018
87c49d1
remove more TODOs
spinscale Oct 23, 2018
5735433
fix HLRC compilation error
spinscale Oct 23, 2018
64cae08
Merge branch 'master' into java-time
spinscale Oct 24, 2018
01d7bdf
Merge branch 'master' into java-time
spinscale Oct 24, 2018
603be5d
Merge branch 'master' into java-time
spinscale Oct 25, 2018
8fbf1f1
remove import
spinscale Oct 25, 2018
e5b0cd0
Merge branch 'master' into java-time
spinscale Oct 25, 2018
50dd0ff
Merge branch 'master' into java-time
spinscale Oct 26, 2018
f49b2dd
Merge branch 'master' into java-time
spinscale Oct 26, 2018
2ce818d
Merge branch 'master' into java-time
spinscale Oct 29, 2018
a0c24af
fix checkstyle
spinscale Oct 29, 2018
b087623
Merge branch 'master' into java-time
spinscale Oct 31, 2018
e76d474
Merge branch 'master' into java-time
spinscale Nov 2, 2018
594dcc9
Merge branch 'master' into java-time
spinscale Nov 2, 2018
2a4ef79
fix range query test
spinscale Nov 2, 2018
436af3d
Merge branch 'master' into java-time
spinscale Nov 2, 2018
1e83827
Merge branch 'master' into java-time
spinscale Nov 5, 2018
1a96c92
Merge branch 'master' into java-time
spinscale Nov 6, 2018
ca0344b
Merge branch 'master' into java-time
spinscale Nov 7, 2018
1d7f1f2
Merge branch 'master' into java-time
spinscale Nov 7, 2018
cc80caf
fix compilation errors
spinscale Nov 7, 2018
7e44ddf
Merge branch 'master' into java-time
spinscale Nov 8, 2018
a2b5ef5
Merge branch 'master' into java-time
spinscale Nov 9, 2018
fb2810b
fix compilation
spinscale Nov 9, 2018
89e5fd1
fix tests
spinscale Nov 9, 2018
9421ce6
Merge branch 'master' into java-time
spinscale Nov 26, 2018
4cf8463
removed import
spinscale Nov 26, 2018
568c1b5
Merge branch 'master' into java-time
spinscale Nov 26, 2018
1edd15c
Merge branch 'master' into java-time
pgomulka Nov 27, 2018
3b90c7b
Core: Migrating from joda to java.time. ML package
pgomulka Nov 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.io.IOException;

public final class WatchStatusDateParser {

private static final FormatDateTimeFormatter FORMATTER = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER;
private static final FormatDateTimeFormatter FORMATTER = Joda.forPattern("strict_date_optional_time||epoch_millis");

private WatchStatusDateParser() {
// Prevent instantiation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -302,7 +302,7 @@ public Object valueForDisplay(Object value) {
}

@Override
public DocValueFormat docValueFormat(String format, DateTimeZone timeZone) {
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName()
+ "] does not support custom time zones");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
import org.elasticsearch.index.fielddata.plain.DocValuesIndexFieldData;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.DocValueFormat;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -229,7 +229,7 @@ public BytesRef parseBytesRef(String value) {
};

@Override
public DocValueFormat docValueFormat(final String format, final DateTimeZone timeZone) {
public DocValueFormat docValueFormat(final String format, final ZoneId timeZone) {
return COLLATE_FORMAT;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.common.time.JavaDateMathParser;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -913,18 +914,19 @@ String resolveExpression(String expression, final Context context) {
int formatPatternTimeZoneSeparatorIndex = patternAndTZid.indexOf(TIME_ZONE_BOUND);
if (formatPatternTimeZoneSeparatorIndex != -1) {
dateFormatterPattern = patternAndTZid.substring(0, formatPatternTimeZoneSeparatorIndex);
timeZone = ZoneId.of(patternAndTZid.substring(formatPatternTimeZoneSeparatorIndex + 1));
timeZone = DateUtils.of(patternAndTZid.substring(formatPatternTimeZoneSeparatorIndex + 1));
} else {
dateFormatterPattern = patternAndTZid;
timeZone = ZoneOffset.UTC;
}
dateFormatter = DateFormatters.forPattern(dateFormatterPattern);
}

DateFormatter formatter = dateFormatter.withZone(timeZone);
DateMathParser dateMathParser = new JavaDateMathParser(formatter);
long millis = dateMathParser.parse(mathExpression, context::getStartTime, false, timeZone);
Instant instant = dateMathParser.parse(mathExpression, context::getStartTime, false, timeZone);

String time = formatter.format(Instant.ofEpochMilli(millis));
String time = formatter.format(instant);
beforePlaceHolderSb.append(time);
inPlaceHolderSb = new StringBuilder();
inPlaceHolder = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().before(Version.V_6_0_0_alpha1)) {
// timestamp
out.writeBoolean(false); // enabled
out.writeString(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format());
out.writeString(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.pattern());
out.writeOptionalString("now"); // 5.x default
out.writeOptionalBoolean(null);
}
Expand Down
19 changes: 15 additions & 4 deletions server/src/main/java/org/elasticsearch/common/Rounding.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.elasticsearch.common;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
Expand Down Expand Up @@ -367,8 +368,13 @@ public long nextRoundingValue(long utcMillis) {
@Override
public void innerWriteTo(StreamOutput out) throws IOException {
out.writeByte(unit.getId());
String tz = ZoneOffset.UTC.equals(timeZone) ? "UTC" : timeZone.getId(); // stay joda compatible
out.writeString(tz);
if (out.getVersion().onOrAfter(Version.V_7_0_0)) {
out.writeString(timeZone.getId());
} else {
// stay joda compatible
String tz = ZoneOffset.UTC.equals(timeZone) ? "UTC" : timeZone.getId();
out.writeString(tz);
}
}

@Override
Expand Down Expand Up @@ -490,8 +496,13 @@ public long nextRoundingValue(long time) {
@Override
public void innerWriteTo(StreamOutput out) throws IOException {
out.writeVLong(interval);
String tz = ZoneOffset.UTC.equals(timeZone) ? "UTC" : timeZone.getId(); // stay joda compatible
out.writeString(tz);
if (out.getVersion().onOrAfter(Version.V_7_0_0)) {
out.writeString(timeZone.getId());
} else {
// stay joda compatible
String tz = ZoneOffset.UTC.equals(timeZone) ? "UTC" : timeZone.getId();
out.writeString(tz);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,23 @@ public DateTimeZone readOptionalTimeZone() throws IOException {
return null;
}

/**
* Read a {@linkplain DateTimeZone}.
*/
public ZoneId readZoneId() throws IOException {
return ZoneId.of(readString());
}

/**
* Read an optional {@linkplain ZoneId}.
*/
public ZoneId readOptionalZoneId() throws IOException {
if (readBoolean()) {
return ZoneId.of(readString());
}
return null;
}

public int[] readIntArray() throws IOException {
int length = readArraySize();
int[] values = new int[length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.nio.file.FileSystemLoopException;
import java.nio.file.NoSuchFileException;
import java.nio.file.NotDirectoryException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -677,7 +678,6 @@ public final <K, V> void writeMap(final Map<K, V> map, final Writer<K> keyWriter
writers.put(ZonedDateTime.class, (o, v) -> {
o.writeByte((byte) 23);
final ZonedDateTime zonedDateTime = (ZonedDateTime) v;
zonedDateTime.getZone().getId();
o.writeString(zonedDateTime.getZone().getId());
o.writeLong(zonedDateTime.toInstant().toEpochMilli());
});
Expand Down Expand Up @@ -974,6 +974,13 @@ public void writeTimeZone(DateTimeZone timeZone) throws IOException {
writeString(timeZone.getID());
}

/**
* Write a {@linkplain ZoneId} to the stream.
*/
public void writeZoneId(ZoneId timeZone) throws IOException {
writeString(timeZone.getId());
}

/**
* Write an optional {@linkplain DateTimeZone} to the stream.
*/
Expand All @@ -986,6 +993,18 @@ public void writeOptionalTimeZone(@Nullable DateTimeZone timeZone) throws IOExce
}
}

/**
* Write an optional {@linkplain ZoneId} to the stream.
*/
public void writeOptionalZoneId(@Nullable ZoneId timeZone) throws IOException {
if (timeZone == null) {
writeBoolean(false);
} else {
writeBoolean(true);
writeZoneId(timeZone);
}
}

/**
* Writes a list of {@link Streamable} objects
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormatter;

import java.time.Instant;
import java.time.ZoneId;
import java.util.Objects;
import java.util.function.LongSupplier;
Expand All @@ -41,7 +42,7 @@ public class JodaDateMathParser implements DateMathParser {

private final FormatDateTimeFormatter dateTimeFormatter;

public JodaDateMathParser(FormatDateTimeFormatter dateTimeFormatter) {
JodaDateMathParser(FormatDateTimeFormatter dateTimeFormatter) {
Objects.requireNonNull(dateTimeFormatter);
this.dateTimeFormatter = dateTimeFormatter;
}
Expand All @@ -50,7 +51,7 @@ public JodaDateMathParser(FormatDateTimeFormatter dateTimeFormatter) {
// if it has been used. For instance, the request cache does not cache requests that make
// use of `now`.
@Override
public long parse(String text, LongSupplier now, boolean roundUp, ZoneId tz) {
public Instant parse(String text, LongSupplier now, boolean roundUp, ZoneId tz) {
final DateTimeZone timeZone = tz == null ? null : DateUtils.zoneIdToDateTimeZone(tz);
long time;
String mathString;
Expand All @@ -64,13 +65,13 @@ public long parse(String text, LongSupplier now, boolean roundUp, ZoneId tz) {
} else {
int index = text.indexOf("||");
if (index == -1) {
return parseDateTime(text, timeZone, roundUp);
return Instant.ofEpochMilli(parseDateTime(text, timeZone, roundUp));
}
time = parseDateTime(text.substring(0, index), timeZone, false);
mathString = text.substring(index + 2);
}

return parseMath(mathString, time, roundUp, timeZone);
return Instant.ofEpochMilli(parseMath(mathString, time, roundUp, timeZone));
}

private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone) throws ElasticsearchParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@

package org.elasticsearch.common.time;

import org.elasticsearch.ElasticsearchParseException;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.LongSupplier;
import java.util.stream.Collectors;

public interface DateFormatter {
Expand Down Expand Up @@ -94,6 +99,13 @@ public interface DateFormatter {
*/
DateFormatter parseDefaulting(Map<TemporalField, Long> fields);

/**
* Create a DateMathParser from the existing formatter
*
* @return The DateMathParser object
*/
DateMathParser toDateMathParser();

/**
* Merge several date formatters into a single one. Useful if you need to have several formatters with
* different formats act as one, for example when you specify a
Expand All @@ -106,6 +118,34 @@ static DateFormatter merge(DateFormatter ... formatters) {
return new MergedDateFormatter(formatters);
}

class MergedDateMathParser implements DateMathParser {

private final DateMathParser[] parsers;

MergedDateMathParser(DateFormatter ... formatters) {
this.parsers = Arrays.stream(formatters)
.map(DateFormatter::toDateMathParser)
.collect(Collectors.toList()).toArray(new DateMathParser[0]);
}

@Override
public Instant parse(String text, LongSupplier now, boolean roundUp, ZoneId tz) {
ElasticsearchParseException failure = null;
for (DateMathParser parser : parsers) {
try {
return parser.parse(text, now, roundUp, tz);
} catch (ElasticsearchParseException e) {
if (failure == null) {
failure = e;
} else {
failure.addSuppressed(e);
}
}
}
throw failure;
}
}

class MergedDateFormatter implements DateFormatter {

private final String format;
Expand All @@ -118,11 +158,11 @@ class MergedDateFormatter implements DateFormatter {

@Override
public TemporalAccessor parse(String input) {
DateTimeParseException failure = null;
ElasticsearchParseException failure = null;
for (DateFormatter formatter : formatters) {
try {
return formatter.parse(input);
} catch (DateTimeParseException e) {
} catch (ElasticsearchParseException e) {
if (failure == null) {
failure = e;
} else {
Expand Down Expand Up @@ -163,9 +203,31 @@ public ZoneId getZone() {
return formatters[0].getZone();
}

@Override
public DateMathParser toDateMathParser() {
return new MergedDateMathParser(formatters);
}

@Override
public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) {
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.parseDefaulting(fields)).toArray(DateFormatter[]::new));
}

@Override
public int hashCode() {
return Objects.hash(getLocale(), format);
}

@Override
public boolean equals(Object obj) {
if (obj.getClass().equals(this.getClass()) == false) {
return false;
}
MergedDateFormatter other = (MergedDateFormatter) obj;

return Objects.equals(pattern(), other.pattern()) &&
Objects.equals(getLocale(), other.getLocale()) &&
Objects.equals(getZone(), other.getZone());
}
}
}
Loading