|
@@ -20,11 +20,14 @@
|
|
|
package org.elasticsearch.common.joda;
|
|
|
|
|
|
import org.elasticsearch.ElasticsearchParseException;
|
|
|
+import org.elasticsearch.ExceptionsHelper;
|
|
|
import org.elasticsearch.test.ElasticsearchTestCase;
|
|
|
import org.joda.time.DateTimeZone;
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
+
|
|
|
public class DateMathParserTests extends ElasticsearchTestCase {
|
|
|
FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
|
|
|
DateMathParser parser = new DateMathParser(formatter, TimeUnit.MILLISECONDS);
|
|
@@ -34,16 +37,19 @@ public class DateMathParserTests extends ElasticsearchTestCase {
|
|
|
}
|
|
|
|
|
|
void assertDateMathEquals(String toTest, String expected, long now, boolean roundUp, DateTimeZone timeZone) {
|
|
|
- DateMathParser parser = new DateMathParser(Joda.forPattern("dateOptionalTime"), TimeUnit.MILLISECONDS);
|
|
|
- long gotMillis = parser.parse(toTest, now, roundUp, null);
|
|
|
+ long gotMillis = parser.parse(toTest, now, roundUp, timeZone);
|
|
|
+ assertDateEquals(gotMillis, toTest, expected);
|
|
|
+ }
|
|
|
+
|
|
|
+ void assertDateEquals(long gotMillis, String original, String expected) {
|
|
|
long expectedMillis = parser.parse(expected, 0);
|
|
|
if (gotMillis != expectedMillis) {
|
|
|
fail("Date math not equal\n" +
|
|
|
- "Original : " + toTest + "\n" +
|
|
|
- "Parsed : " + formatter.printer().print(gotMillis) + "\n" +
|
|
|
- "Expected : " + expected + "\n" +
|
|
|
- "Expected milliseconds : " + expectedMillis + "\n" +
|
|
|
- "Actual milliseconds : " + gotMillis + "\n");
|
|
|
+ "Original : " + original + "\n" +
|
|
|
+ "Parsed : " + formatter.printer().print(gotMillis) + "\n" +
|
|
|
+ "Expected : " + expected + "\n" +
|
|
|
+ "Expected milliseconds : " + expectedMillis + "\n" +
|
|
|
+ "Actual milliseconds : " + gotMillis + "\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -56,6 +62,23 @@ public class DateMathParserTests extends ElasticsearchTestCase {
|
|
|
assertDateMathEquals("2014-05-30T20:21:35", "2014-05-30T20:21:35.000");
|
|
|
assertDateMathEquals("2014-05-30T20:21:35.123", "2014-05-30T20:21:35.123");
|
|
|
}
|
|
|
+
|
|
|
+ public void testRoundingDoesNotAffectExactDate() {
|
|
|
+ assertDateMathEquals("2014-11-12T22:55:00Z", "2014-11-12T22:55:00Z", 0, true, null);
|
|
|
+ assertDateMathEquals("2014-11-12T22:55:00Z", "2014-11-12T22:55:00Z", 0, false, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testTimezone() {
|
|
|
+ // timezone works within date format
|
|
|
+ assertDateMathEquals("2014-05-30T20:21+02:00", "2014-05-30T18:21:00.000");
|
|
|
+
|
|
|
+ // but also externally
|
|
|
+ assertDateMathEquals("2014-05-30T20:21", "2014-05-30T18:21:00.000", 0, false, DateTimeZone.forID("+02:00"));
|
|
|
+
|
|
|
+ // and timezone in the date has priority
|
|
|
+ assertDateMathEquals("2014-05-30T20:21+03:00", "2014-05-30T17:21:00.000", 0, false, DateTimeZone.forID("-08:00"));
|
|
|
+ assertDateMathEquals("2014-05-30T20:21Z", "2014-05-30T20:21:00.000", 0, false, DateTimeZone.forID("-08:00"));
|
|
|
+ }
|
|
|
|
|
|
public void testBasicMath() {
|
|
|
assertDateMathEquals("2014-11-18||+y", "2015-11-18");
|
|
@@ -81,6 +104,10 @@ public class DateMathParserTests extends ElasticsearchTestCase {
|
|
|
assertDateMathEquals("2014-11-18T14:27:32||+60s", "2014-11-18T14:28:32");
|
|
|
assertDateMathEquals("2014-11-18T14:27:32||-3600s", "2014-11-18T13:27:32");
|
|
|
}
|
|
|
+
|
|
|
+ public void testLenientEmptyMath() {
|
|
|
+ assertDateMathEquals("2014-05-30T20:21||", "2014-05-30T20:21:00.000");
|
|
|
+ }
|
|
|
|
|
|
public void testMultipleAdjustments() {
|
|
|
assertDateMathEquals("2014-11-18||+1M-1M", "2014-11-18");
|
|
@@ -97,13 +124,16 @@ public class DateMathParserTests extends ElasticsearchTestCase {
|
|
|
assertDateMathEquals("now+M", "2014-12-18T14:27:32", now, false, null);
|
|
|
assertDateMathEquals("now-2d", "2014-11-16T14:27:32", now, false, null);
|
|
|
assertDateMathEquals("now/m", "2014-11-18T14:27", now, false, null);
|
|
|
+
|
|
|
+ // timezone does not affect now
|
|
|
+ assertDateMathEquals("now/m", "2014-11-18T14:27", now, false, DateTimeZone.forID("+02:00"));
|
|
|
}
|
|
|
|
|
|
public void testRounding() {
|
|
|
assertDateMathEquals("2014-11-18||/y", "2014-01-01", 0, false, null);
|
|
|
assertDateMathEquals("2014-11-18||/y", "2014-12-31T23:59:59.999", 0, true, null);
|
|
|
assertDateMathEquals("2014||/y", "2014-01-01", 0, false, null);
|
|
|
- assertDateMathEquals("2014||/y", "2014-12-31T23:59:59.999", 0, true, null);
|
|
|
+ assertDateMathEquals("2014-01-01T00:00:00.001||/y", "2014-12-31T23:59:59.999", 0, true, null);
|
|
|
|
|
|
assertDateMathEquals("2014-11-18||/M", "2014-11-01", 0, false, null);
|
|
|
assertDateMathEquals("2014-11-18||/M", "2014-11-30T23:59:59.999", 0, true, null);
|
|
@@ -140,20 +170,47 @@ public class DateMathParserTests extends ElasticsearchTestCase {
|
|
|
assertDateMathEquals("2014-11-18T14:27:32||/s", "2014-11-18T14:27:32.999", 0, true, null);
|
|
|
}
|
|
|
|
|
|
- void assertParseException(String msg, String date) {
|
|
|
+ public void testTimestamps() {
|
|
|
+ assertDateMathEquals("1418248078000", "2014-12-10T21:47:58.000");
|
|
|
+
|
|
|
+ // timezone does not affect timestamps
|
|
|
+ assertDateMathEquals("1418248078000", "2014-12-10T21:47:58.000", 0, false, DateTimeZone.forID("-08:00"));
|
|
|
+
|
|
|
+ // datemath still works on timestamps
|
|
|
+ assertDateMathEquals("1418248078000||/m", "2014-12-10T21:47:00.000");
|
|
|
+
|
|
|
+ // also check other time units
|
|
|
+ DateMathParser parser = new DateMathParser(Joda.forPattern("dateOptionalTime"), TimeUnit.SECONDS);
|
|
|
+ long datetime = parser.parse("1418248078", 0);
|
|
|
+ assertDateEquals(datetime, "1418248078", "2014-12-10T21:47:58.000");
|
|
|
+
|
|
|
+ // a timestamp before 10000 is a year
|
|
|
+ assertDateMathEquals("9999", "9999-01-01T00:00:00.000");
|
|
|
+ // 10000 is the first timestamp
|
|
|
+ assertDateMathEquals("10000", "1970-01-01T00:00:10.000");
|
|
|
+ // but 10000 with T is still a date format
|
|
|
+ assertDateMathEquals("10000T", "10000-01-01T00:00:00.000");
|
|
|
+ }
|
|
|
+
|
|
|
+ void assertParseException(String msg, String date, String exc) {
|
|
|
try {
|
|
|
parser.parse(date, 0);
|
|
|
fail("Date: " + date + "\n" + msg);
|
|
|
} catch (ElasticsearchParseException e) {
|
|
|
- // expected
|
|
|
+ assertThat(ExceptionsHelper.detailedMessage(e).contains(exc), equalTo(true));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void testIllegalMathFormat() {
|
|
|
- assertParseException("Expected date math unsupported operator exception", "2014-11-18||*5");
|
|
|
- assertParseException("Expected date math incompatible rounding exception", "2014-11-18||/2m");
|
|
|
- assertParseException("Expected date math illegal unit type exception", "2014-11-18||+2a");
|
|
|
- assertParseException("Expected date math truncation exception", "2014-11-18||+12");
|
|
|
- assertParseException("Expected date math truncation exception", "2014-11-18||-");
|
|
|
+ assertParseException("Expected date math unsupported operator exception", "2014-11-18||*5", "operator not supported");
|
|
|
+ assertParseException("Expected date math incompatible rounding exception", "2014-11-18||/2m", "rounding");
|
|
|
+ assertParseException("Expected date math illegal unit type exception", "2014-11-18||+2a", "unit [a] not supported");
|
|
|
+ assertParseException("Expected date math truncation exception", "2014-11-18||+12", "truncated");
|
|
|
+ assertParseException("Expected date math truncation exception", "2014-11-18||-", "truncated");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testIllegalDateFormat() {
|
|
|
+ assertParseException("Expected bad timestamp exception", Long.toString(Long.MAX_VALUE) + "0", "timestamp");
|
|
|
+ assertParseException("Expected bad date format exception", "123bogus", "with format");
|
|
|
}
|
|
|
}
|