mirror of https://github.com/apache/lucene.git
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:
parent
3118a1cdb9
commit
646d9646cb
|
@ -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.
|
||||
|
@ -37,6 +38,8 @@ import java.util.Date;
|
|||
*/
|
||||
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 {
|
|||
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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue