Add DateTime ctors without timezone to forbidden APIs

Using DateTime with default timezone is asking for trouble and should
be added to forbidden APIs
This commit is contained in:
Simon Willnauer 2015-06-18 09:45:20 +02:00
parent 2fa56899d6
commit 2a63249441
8 changed files with 24 additions and 9 deletions

View File

@ -29,6 +29,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.text.StringAndBytesText;
import org.elasticsearch.common.text.Text;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -398,7 +399,8 @@ public abstract class StreamInput extends InputStream {
case 12:
return new Date(readLong());
case 13:
return new DateTime(readLong());
final String timeZoneId = readString();
return new DateTime(readLong(), DateTimeZone.forID(timeZoneId));
case 14:
return readBytesReference();
case 15:

View File

@ -373,6 +373,7 @@ public abstract class StreamOutput extends OutputStream {
writeLong(((Date) value).getTime());
} else if (value instanceof ReadableInstant) {
writeByte((byte) 13);
writeString(((ReadableInstant) value).getZone().getID());
writeLong(((ReadableInstant) value).getMillis());
} else if (value instanceof BytesReference) {
writeByte((byte) 14);

View File

@ -35,6 +35,7 @@ import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBui
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.io.BufferedWriter;
import java.io.IOException;
@ -172,7 +173,7 @@ public class BasicScriptBenchmark {
out.write("\n\n\n\n");
out.write("######################################\n");
out.write("# Octave script for plotting results\n");
String filename = "scriptScoreBenchmark" + new DateTime().toString();
String filename = "scriptScoreBenchmark" + new DateTime(DateTimeZone.UTC).toString();
out.write("#Call '" + args[0] + "' from the command line. The plot is then in " + filename + "\n\n");
out.write("handleArray = [];\n tagArray = [];\n plot([]);\n hold on;\n");

View File

@ -232,8 +232,8 @@ public class SimpleDateMappingTests extends ElasticsearchSingleNodeTest {
} finally {
SearchContext.removeCurrent();
}
assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(11).millis()).getMillis()));
assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(10).millis()).getMillis()));
assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(11).millis(), DateTimeZone.UTC).getMillis()));
assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(10).millis(), DateTimeZone.UTC).getMillis()));
}
public void testDayWithoutYearFormat() throws Exception {
@ -258,8 +258,8 @@ public class SimpleDateMappingTests extends ElasticsearchSingleNodeTest {
} finally {
SearchContext.removeCurrent();
}
assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(35).millis()).getMillis()));
assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(34).millis()).getMillis()));
assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(35).millis(), DateTimeZone.UTC).getMillis()));
assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(34).millis(), DateTimeZone.UTC).getMillis()));
}
public void testIgnoreMalformedOption() throws Exception {

View File

@ -32,6 +32,7 @@ import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
import org.elasticsearch.test.TestSearchContext;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Test;
@ -107,7 +108,7 @@ public class IndexQueryParserFilterDateRangeTimezoneTests extends ElasticsearchS
// Min value was 2012-01-01 (UTC) so we need to remove one hour
DateTime min = DateTime.parse("2012-01-01T00:00:00.000+01:00");
// Max value is when we started the test. So it should be some ms from now
DateTime max = new DateTime(startDate);
DateTime max = new DateTime(startDate, DateTimeZone.UTC);
assertThat(((NumericRangeQuery) parsedQuery).getMin().longValue(), is(min.getMillis()));

View File

@ -35,6 +35,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import java.util.ArrayList;
@ -78,7 +79,7 @@ public class MinDocCountTests extends AbstractTermsTests {
longTerm = randomInt(cardinality * 2);
} while (!longTerms.add(longTerm));
double doubleTerm = longTerm * Math.PI;
String dateTerm = DateTimeFormat.forPattern("yyyy-MM-dd").print(new DateTime(2014, 1, ((int) longTerm % 20) + 1, 0, 0));
String dateTerm = DateTimeFormat.forPattern("yyyy-MM-dd").print(new DateTime(2014, 1, ((int) longTerm % 20) + 1, 0, 0, DateTimeZone.UTC));
final int frequency = randomBoolean() ? 1 : randomIntBetween(2, 20);
for (int j = 0; j < frequency; ++j) {
indexRequests.add(client().prepareIndex("idx", "type").setSource(jsonBuilder()

View File

@ -35,6 +35,7 @@ import org.elasticsearch.index.query.functionscore.gauss.GaussDecayFunctionBuild
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import java.io.IOException;
@ -532,7 +533,7 @@ public class DecayFunctionScoreTests extends ElasticsearchIntegrationTest {
@Test
public void testDateWithoutOrigin() throws Exception {
DateTime dt = new DateTime();
DateTime dt = new DateTime(DateTimeZone.UTC);
assertAcked(prepareCreate("test").addMapping(
"type1",

View File

@ -45,3 +45,11 @@ java.nio.file.FileSystems#getDefault() @ use PathUtils.getDefault instead.
@defaultMessage Specify a location for the temp file/directory instead.
java.nio.file.Files#createTempDirectory(java.lang.String,java.nio.file.attribute.FileAttribute[])
java.nio.file.Files#createTempFile(java.lang.String,java.lang.String,java.nio.file.attribute.FileAttribute[])
@defaultMessage Constructing a DateTime without a time zone is dangerous - use DateTime(DateTimeZone.getDefault()) if you really want the default timezone
org.joda.time.DateTime#<init>()
org.joda.time.DateTime#<init>(long)
org.joda.time.DateTime#<init>(int, int, int, int, int)
org.joda.time.DateTime#<init>(int, int, int, int, int, int)
org.joda.time.DateTime#<init>(int, int, int, int, int, int, int)
org.joda.time.DateTime#now()