From 646d9646cbb2184d98b7977106672454dc1bd005 Mon Sep 17 00:00:00 2001 From: Daniel Naber Date: Sun, 5 Feb 2006 18:17:02 +0000 Subject: [PATCH] DateTools needs to use UTC for correct collation (LUCENE-491), patch by John Haxby git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@375070 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/document/DateTools.java | 13 ++++++++---- .../apache/lucene/document/TestDateTools.java | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/lucene/document/DateTools.java b/src/java/org/apache/lucene/document/DateTools.java index 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 Resolution.DAY 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 yyyyMMddHHmmssSSS or shorter, - * depeding on resolution + * depeding on resolution; 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 yyyyMMddHHmmssSSS or shorter, - * depeding on resolution + * depeding on resolution; 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 { else throw 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.java index 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=1 0, 0, 0); // hour, minute, second cal.set(Calendar.MILLISECOND, 0); + cal.setTimeZone(TimeZone.getTimeZone("GMT")); assertEquals(cal.getTime().getTime(), time); cal.set(1980, 1, 2, // year=1980, month=february, day=2 11, 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=3 22, 8, 56); // hour, minute, second cal.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=3 22, 8, 56); // hour, minute, second cal.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); + } + } + }