Skip to content

Commit

Permalink
- Fixed timezone parsing when input starts with '+'sign. Fixes issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn van Groningen authored and kimchy committed Aug 7, 2012
1 parent 37e7a54 commit 195e586
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Expand Up @@ -183,7 +183,8 @@ private long parseOffset(String offset) throws IOException {
if (offset.charAt(0) == '-') {
return -TimeValue.parseTimeValue(offset.substring(1), null).millis();
}
return TimeValue.parseTimeValue(offset, null).millis();
int beginIndex = offset.charAt(0) == '+' ? 1 : 0;
return TimeValue.parseTimeValue(offset.substring(beginIndex), null).millis();
}

private DateTimeZone parseZone(XContentParser parser, XContentParser.Token token) throws IOException {
Expand All @@ -193,9 +194,10 @@ private DateTimeZone parseZone(XContentParser parser, XContentParser.Token token
String text = parser.text();
int index = text.indexOf(':');
if (index != -1) {
int beginIndex = text.charAt(0) == '+' ? 1 : 0;
// format like -02:30
return DateTimeZone.forOffsetHoursMinutes(
Integer.parseInt(text.substring(0, index)),
Integer.parseInt(text.substring(beginIndex, index)),
Integer.parseInt(text.substring(index + 1))
);
} else {
Expand Down
Expand Up @@ -1478,6 +1478,73 @@ public void testDateHistoFacets() throws Exception {
}
}

@Test
// https://github.com/elasticsearch/elasticsearch/issues/2141
public void testDateHistoFacets_preZoneBug() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client.admin().indices().prepareCreate("test").execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();

client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("date", "2009-03-05T23:31:01")
.field("num", 1)
.endObject()).execute().actionGet();
client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet();

client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("date", "2009-03-05T18:01:01")
.field("num", 2)
.endObject()).execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();

client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("date", "2009-03-05T22:01:01")
.field("num", 3)
.endObject()).execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();


for (int i = 0; i < numberOfRuns(); i++) {
SearchResponse searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(dateHistogramFacet("stats1").field("date").interval("day").preZone("+02:00"))
.addFacet(dateHistogramFacet("stats2").field("date").valueField("num").interval("day").preZone("+01:30"))
.execute().actionGet();

if (searchResponse.failedShards() > 0) {
logger.warn("Failed shards:");
for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {
logger.warn("-> {}", shardSearchFailure);
}
}
assertThat(searchResponse.failedShards(), equalTo(0));

// time zone causes the dates to shift by 2:00
DateHistogramFacet facet = searchResponse.facets().facet("stats1");
assertThat(facet.name(), equalTo("stats1"));
assertThat(facet.entries().size(), equalTo(2));
assertThat(facet.entries().get(0).time(), equalTo(utcTimeInMillis("2009-03-05")));
assertThat(facet.entries().get(0).count(), equalTo(1l));
assertThat(facet.entries().get(1).time(), equalTo(utcTimeInMillis("2009-03-06")));
assertThat(facet.entries().get(1).count(), equalTo(2l));

// time zone causes the dates to shift by 1:30
facet = searchResponse.facets().facet("stats2");
assertThat(facet.name(), equalTo("stats2"));
assertThat(facet.entries().size(), equalTo(2));
assertThat(facet.entries().get(0).time(), equalTo(utcTimeInMillis("2009-03-05")));
assertThat(facet.entries().get(0).count(), equalTo(2l));
assertThat(facet.entries().get(0).total(), equalTo(5d));
assertThat(facet.entries().get(1).time(), equalTo(utcTimeInMillis("2009-03-06")));
assertThat(facet.entries().get(1).count(), equalTo(1l));
assertThat(facet.entries().get(1).total(), equalTo(1d));
}
}

@Test
public void testTermsStatsFacets() throws Exception {
try {
Expand Down

0 comments on commit 195e586

Please sign in to comment.