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
This commit is contained in:
Daniel Naber 2006-02-05 18:17:02 +00:00
parent 3118a1cdb9
commit 646d9646cb
2 changed files with 29 additions and 4 deletions

View File

@ -20,6 +20,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone;
/** /**
* Provides support for converting dates to strings and vice-versa. * 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. * is set to <code>Resolution.DAY</code> or lower.
*/ */
public class DateTools { public class DateTools {
private final static TimeZone GMT = TimeZone.getTimeZone("GMT");
private DateTools() {} private DateTools() {}
@ -46,7 +49,7 @@ public class DateTools {
* @param resolution the desired resolution, see * @param resolution the desired resolution, see
* {@link #round(Date, DateTools.Resolution)} * {@link #round(Date, DateTools.Resolution)}
* @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter, * @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) { public static String dateToString(Date date, Resolution resolution) {
return timeToString(date.getTime(), resolution); return timeToString(date.getTime(), resolution);
@ -59,10 +62,10 @@ public class DateTools {
* @param resolution the desired resolution, see * @param resolution the desired resolution, see
* {@link #round(long, DateTools.Resolution)} * {@link #round(long, DateTools.Resolution)}
* @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter, * @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) { 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 //protected in JDK's prior to 1.4
//cal.setTimeInMillis(round(time, resolution)); //cal.setTimeInMillis(round(time, resolution));
@ -70,6 +73,7 @@ public class DateTools {
cal.setTime(new Date(round(time, resolution))); cal.setTime(new Date(round(time, resolution)));
SimpleDateFormat sdf = new SimpleDateFormat(); SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(GMT);
String pattern = null; String pattern = null;
if (resolution == Resolution.YEAR) { if (resolution == Resolution.YEAR) {
pattern = "yyyy"; pattern = "yyyy";
@ -135,6 +139,7 @@ public class DateTools {
else else
throw new ParseException("Input is not valid date string: " + dateString, 0); throw new ParseException("Input is not valid date string: " + dateString, 0);
SimpleDateFormat sdf = new SimpleDateFormat(pattern); SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setTimeZone(GMT);
Date date = sdf.parse(dateString); Date date = sdf.parse(dateString);
return date; 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 * set to 0 or 1, expressed as milliseconds since January 1, 1970, 00:00:00 GMT
*/ */
public static long round(long time, Resolution resolution) { 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 // protected in JDK's prior to 1.4
//cal.setTimeInMillis(time); //cal.setTimeInMillis(time);

View File

@ -4,6 +4,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -58,6 +59,7 @@ public class TestDateTools extends TestCase {
cal.set(1970, 0, 1, // year=1970, month=january, day=1 cal.set(1970, 0, 1, // year=1970, month=january, day=1
0, 0, 0); // hour, minute, second 0, 0, 0); // hour, minute, second
cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.MILLISECOND, 0);
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
assertEquals(cal.getTime().getTime(), time); assertEquals(cal.getTime().getTime(), time);
cal.set(1980, 1, 2, // year=1980, month=february, day=2 cal.set(1980, 1, 2, // year=1980, month=february, day=2
11, 5, 0); // hour, minute, second 11, 5, 0); // hour, minute, second
@ -68,6 +70,7 @@ public class TestDateTools extends TestCase {
public void testDateAndTimetoString() throws ParseException { public void testDateAndTimetoString() throws ParseException {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.set(2004, 1, 3, // year=2004, month=february(!), day=3 cal.set(2004, 1, 3, // year=2004, month=february(!), day=3
22, 8, 56); // hour, minute, second 22, 8, 56); // hour, minute, second
cal.set(Calendar.MILLISECOND, 333); cal.set(Calendar.MILLISECOND, 333);
@ -131,6 +134,7 @@ public class TestDateTools extends TestCase {
public void testRound() { public void testRound() {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.set(2004, 1, 3, // year=2004, month=february(!), day=3 cal.set(2004, 1, 3, // year=2004, month=february(!), day=3
22, 8, 56); // hour, minute, second 22, 8, 56); // hour, minute, second
cal.set(Calendar.MILLISECOND, 333); cal.set(Calendar.MILLISECOND, 333);
@ -168,7 +172,23 @@ public class TestDateTools extends TestCase {
private String isoFormat(Date date) { private String isoFormat(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return sdf.format(date); 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);
}
}
} }