LUCENE-4199: allow setting of tz used for date range parsing

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4199@1359149 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-07-09 13:12:41 +00:00
parent e4b8dc640e
commit 5fea62f7c8
5 changed files with 31 additions and 3 deletions

View File

@ -71,6 +71,7 @@ public abstract class QueryParserBase {
float fuzzyMinSim = FuzzyQuery.defaultMinSimilarity;
int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;
Locale locale = Locale.getDefault();
TimeZone timeZone = TimeZone.getDefault();
// the default date resolution
DateTools.Resolution dateResolution = null;
@ -329,6 +330,14 @@ public abstract class QueryParserBase {
public Locale getLocale() {
return locale;
}
public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
}
public TimeZone getTimeZone() {
return timeZone;
}
/**
* Sets the default date resolution used by RangeQueries for fields for which no
@ -678,7 +687,7 @@ public abstract class QueryParserBase {
// The user can only specify the date, not the time, so make sure
// the time is set to the latest possible time of that date to really
// include all documents:
Calendar cal = Calendar.getInstance(locale);
Calendar cal = Calendar.getInstance(timeZone, locale);
cal.setTime(d2);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);

View File

@ -19,6 +19,7 @@ package org.apache.lucene.queryparser.flexible.standard;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.TooManyListenersException;
import org.apache.lucene.analysis.Analyzer;
@ -344,6 +345,14 @@ public class StandardQueryParser extends QueryParserHelper {
return getQueryConfigHandler().get(ConfigurationKeys.LOCALE);
}
public void setTimeZone(TimeZone timeZone) {
getQueryConfigHandler().set(ConfigurationKeys.TIMEZONE, timeZone);
}
public TimeZone getTimeZone() {
return getQueryConfigHandler().get(ConfigurationKeys.TIMEZONE);
}
/**
* Sets the default slop for phrases. If zero, then exact phrase matches are
* required. Default value is zero.

View File

@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.DateTools;
@ -101,6 +102,8 @@ public class StandardQueryConfigHandler extends QueryConfigHandler {
*/
final public static ConfigurationKey<Locale> LOCALE = ConfigurationKey.newInstance();
final public static ConfigurationKey<TimeZone> TIMEZONE = ConfigurationKey.newInstance();
/**
* Key used to set the {@link RewriteMethod} used when creating queries
*

View File

@ -90,7 +90,7 @@ public class NumericQueryNode extends QueryNodeImpl implements
* @return the value converte to {@link String} and escaped
*/
protected CharSequence getTermEscaped(EscapeQuerySyntax escaper) {
return escaper.escape(NumberFormat.getNumberInstance().format(this.value),
return escaper.escape(numberFormat.format(this.value),
Locale.ROOT, Type.NORMAL);
}

View File

@ -22,6 +22,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.DateTools.Resolution;
@ -76,6 +77,12 @@ public class TermRangeQueryNodeProcessor extends QueryNodeProcessorImpl {
locale = Locale.getDefault();
}
TimeZone timeZone = getQueryConfigHandler().get(ConfigurationKeys.TIMEZONE);
if (timeZone == null) {
timeZone = TimeZone.getDefault();
}
CharSequence field = termRangeNode.getField();
String fieldStr = null;
@ -114,7 +121,7 @@ public class TermRangeQueryNodeProcessor extends QueryNodeProcessorImpl {
// the time is set to the latest possible time of that date to
// really
// include all documents:
Calendar cal = Calendar.getInstance(locale);
Calendar cal = Calendar.getInstance(timeZone, locale);
cal.setTime(d2);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);