Apache Lucene open-source search software
DateTools needs to use UTC for correct collation (LUCENE-491), patch by John Haxby
Daniel Naber646d9646cbb2
diff --git a/src/java/org/apache/lucene/document/DateTools.java b/src/java/org/apache/lucene/document/DateTools.javaindex d1095ed1837..3f8f9ed02a1 100644--- a/src/java/org/apache/lucene/document/DateTools.java+++ b/src/java/org/apache/lucene/document/DateTools.java@@ -20,6 +20,7 @@ import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;+import java.util.TimeZone;/*** Provides support for converting dates to strings and vice-versa.@@ -36,6 +37,8 @@ import java.util.Date;* is set to <code>Resolution.DAY</code> or lower.*/public class DateTools {++ private final static TimeZone GMT = TimeZone.getTimeZone("GMT");private DateTools() {}@@ -46,7 +49,7 @@ public class DateTools {* @param resolution the desired resolution, see* {@link #round(Date, DateTools.Resolution)}* @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,- * depeding on <code>resolution</code>+ * depeding on <code>resolution</code>; using UTC as timezone*/public static String dateToString(Date date, Resolution resolution) {return timeToString(date.getTime(), resolution);@@ -59,10 +62,10 @@ public class DateTools {* @param resolution the desired resolution, see* {@link #round(long, DateTools.Resolution)}* @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,- * depeding on <code>resolution</code>+ * depeding on <code>resolution</code>; using UTC as timezone*/public static String timeToString(long time, Resolution resolution) {- Calendar cal = Calendar.getInstance();+ Calendar cal = Calendar.getInstance(GMT);//protected in JDK's prior to 1.4//cal.setTimeInMillis(round(time, resolution));@@ -70,6 +73,7 @@ public class DateTools {cal.setTime(new Date(round(time, resolution)));SimpleDateFormat sdf = new SimpleDateFormat();+ sdf.setTimeZone(GMT);String pattern = null;if (resolution == Resolution.YEAR) {pattern = "yyyy";@@ -135,6 +139,7 @@ public class DateTools {elsethrow new ParseException("Input is not valid date string: " + dateString, 0);SimpleDateFormat sdf = new SimpleDateFormat(pattern);+ sdf.setTimeZone(GMT);Date date = sdf.parse(dateString);return date;}@@ -163,7 +168,7 @@ public class DateTools {* set to 0 or 1, expressed as milliseconds since January 1, 1970, 00:00:00 GMT*/public static long round(long time, Resolution resolution) {- Calendar cal = Calendar.getInstance();+ Calendar cal = Calendar.getInstance(GMT);// protected in JDK's prior to 1.4//cal.setTimeInMillis(time);diff --git a/src/test/org/apache/lucene/document/TestDateTools.java b/src/test/org/apache/lucene/document/TestDateTools.javaindex 2a22f369b02..1e06c6ed218 100644--- a/src/test/org/apache/lucene/document/TestDateTools.java+++ b/src/test/org/apache/lucene/document/TestDateTools.java@@ -4,6 +4,7 @@ import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;+import java.util.TimeZone;import junit.framework.TestCase;@@ -58,6 +59,7 @@ public class TestDateTools extends TestCase {cal.set(1970, 0, 1, // year=1970, month=january, day=10, 0, 0); // hour, minute, secondcal.set(Calendar.MILLISECOND, 0);+ cal.setTimeZone(TimeZone.getTimeZone("GMT"));assertEquals(cal.getTime().getTime(), time);cal.set(1980, 1, 2, // year=1980, month=february, day=211, 5, 0); // hour, minute, second@@ -68,6 +70,7 @@ public class TestDateTools extends TestCase {public void testDateAndTimetoString() throws ParseException {Calendar cal = Calendar.getInstance();+ cal.setTimeZone(TimeZone.getTimeZone("GMT"));cal.set(2004, 1, 3, // year=2004, month=february(!), day=322, 8, 56); // hour, minute, secondcal.set(Calendar.MILLISECOND, 333);@@ -131,6 +134,7 @@ public class TestDateTools extends TestCase {public void testRound() {Calendar cal = Calendar.getInstance();+ cal.setTimeZone(TimeZone.getTimeZone("GMT"));cal.set(2004, 1, 3, // year=2004, month=february(!), day=322, 8, 56); // hour, minute, secondcal.set(Calendar.MILLISECOND, 333);@@ -168,7 +172,23 @@ public class TestDateTools extends TestCase {private String isoFormat(Date date) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");+ sdf.setTimeZone(TimeZone.getTimeZone("GMT"));return sdf.format(date);}+ public void testDateToolsUTC() throws Exception {+ // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London+ long time = 1130630400;+ try {+ TimeZone.setDefault(TimeZone.getTimeZone(/* "GMT" */ "Europe/London"));+ String d1 = DateTools.dateToString(new Date(time*1000), DateTools.Resolution.MINUTE);+ String d2 = DateTools.dateToString(new Date((time+3600)*1000), DateTools.Resolution.MINUTE);+ assertFalse("different times", d1.equals(d2));+ assertEquals("midnight", DateTools.stringToTime(d1), time*1000);+ assertEquals("later", DateTools.stringToTime(d2), (time+3600)*1000);+ } finally {+ TimeZone.setDefault(null);+ }+ }+}