Skip to content

Commit b16fb69

Browse files
author
Christoph Büscher
committed
DateMath: Fix using time zone when rounding.
Currently rounding in DateMathParser This always done in UTC, even when another time zone is specified. This is fixed by passing the time zone down to the rounding logic when it is specified. Closes #9814 Closes #9885
1 parent 3e32dd9 commit b16fb69

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/org/elasticsearch/common/joda/DateMathParser.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ public long parse(String text, Callable<Long> now, boolean roundUp, DateTimeZone
7676
}
7777
}
7878

79-
return parseMath(mathString, time, roundUp);
79+
return parseMath(mathString, time, roundUp, timeZone);
8080
}
8181

82-
private long parseMath(String mathString, long time, boolean roundUp) throws ElasticsearchParseException {
83-
MutableDateTime dateTime = new MutableDateTime(time, DateTimeZone.UTC);
82+
private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone) throws ElasticsearchParseException {
83+
if (timeZone == null) {
84+
timeZone = DateTimeZone.UTC;
85+
}
86+
MutableDateTime dateTime = new MutableDateTime(time, timeZone);
8487
for (int i = 0; i < mathString.length(); ) {
8588
char c = mathString.charAt(i++);
8689
final boolean round;

src/test/java/org/elasticsearch/common/joda/DateMathParserTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,26 @@ public void testRounding() {
146146
assertDateMathEquals("2014-11-18||/y", "2014-12-31T23:59:59.999", 0, true, null);
147147
assertDateMathEquals("2014||/y", "2014-01-01", 0, false, null);
148148
assertDateMathEquals("2014-01-01T00:00:00.001||/y", "2014-12-31T23:59:59.999", 0, true, null);
149+
// rounding should also take into account time zone
150+
assertDateMathEquals("2014-11-18||/y", "2013-12-31T23:00:00.000Z", 0, false, DateTimeZone.forID("CET"));
151+
assertDateMathEquals("2014-11-18||/y", "2014-12-31T22:59:59.999Z", 0, true, DateTimeZone.forID("CET"));
149152

150153
assertDateMathEquals("2014-11-18||/M", "2014-11-01", 0, false, null);
151154
assertDateMathEquals("2014-11-18||/M", "2014-11-30T23:59:59.999", 0, true, null);
152155
assertDateMathEquals("2014-11||/M", "2014-11-01", 0, false, null);
153156
assertDateMathEquals("2014-11||/M", "2014-11-30T23:59:59.999", 0, true, null);
157+
assertDateMathEquals("2014-11-18||/M", "2014-10-31T23:00:00.000Z", 0, false, DateTimeZone.forID("CET"));
158+
assertDateMathEquals("2014-11-18||/M", "2014-11-30T22:59:59.999Z", 0, true, DateTimeZone.forID("CET"));
154159

155160
assertDateMathEquals("2014-11-18T14||/w", "2014-11-17", 0, false, null);
156161
assertDateMathEquals("2014-11-18T14||/w", "2014-11-23T23:59:59.999", 0, true, null);
157162
assertDateMathEquals("2014-11-18||/w", "2014-11-17", 0, false, null);
158163
assertDateMathEquals("2014-11-18||/w", "2014-11-23T23:59:59.999", 0, true, null);
164+
assertDateMathEquals("2014-11-18||/w", "2014-11-16T23:00:00.000Z", 0, false, DateTimeZone.forID("+01:00"));
165+
assertDateMathEquals("2014-11-18||/w", "2014-11-17T01:00:00.000Z", 0, false, DateTimeZone.forID("-01:00"));
166+
assertDateMathEquals("2014-11-18||/w", "2014-11-16T23:00:00.000Z", 0, false, DateTimeZone.forID("CET"));
167+
assertDateMathEquals("2014-11-18||/w", "2014-11-23T22:59:59.999Z", 0, true, DateTimeZone.forID("CET"));
168+
assertDateMathEquals("2014-07-22||/w", "2014-07-20T22:00:00.000Z", 0, false, DateTimeZone.forID("CET")); // with DST
159169

160170
assertDateMathEquals("2014-11-18T14||/d", "2014-11-18", 0, false, null);
161171
assertDateMathEquals("2014-11-18T14||/d", "2014-11-18T23:59:59.999", 0, true, null);
@@ -181,7 +191,7 @@ public void testRounding() {
181191
assertDateMathEquals("2014-11-18T14:27:32||/s", "2014-11-18T14:27:32", 0, false, null);
182192
assertDateMathEquals("2014-11-18T14:27:32||/s", "2014-11-18T14:27:32.999", 0, true, null);
183193
}
184-
194+
185195
public void testTimestamps() {
186196
assertDateMathEquals("1418248078000", "2014-12-10T21:47:58.000");
187197

0 commit comments

Comments
 (0)