ISSUE-99 thanks to knut.forkalsrud for providing the patch!

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1238 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2010-08-31 18:56:35 +00:00
parent 8bff031c1c
commit 3648883e0c

View File

@ -1,6 +1,6 @@
package com.google.gridworks.expr.functions.strings;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
@ -20,14 +20,14 @@ public class Diff implements Function {
if (o1 != null && o2 != null) {
if (o1 instanceof String && o2 instanceof String) {
return StringUtils.difference((String) o1,(String) o2);
} else if (o1 instanceof Calendar && args.length == 3) {
} else if (o1 instanceof Date && args.length == 3) {
Object o3 = args[3];
if (o3 != null && o3 instanceof String) {
try {
String unit = ((String) o3).toLowerCase();
Calendar c1 = (Calendar) o1;
Calendar c2 = (o2 instanceof Calendar) ? (Calendar) o2 : CalendarParser.parse((o2 instanceof String) ? (String) o2 : o2.toString());
long delta = (c1.getTimeInMillis() - c2.getTimeInMillis()) / 1000;
Date c1 = (Date) o1;
Date c2 = (o2 instanceof Date) ? (Date) o2 : CalendarParser.parse((o2 instanceof String) ? (String) o2 : o2.toString()).getTime();
long delta = (c1.getTime() - c2.getTime()) / 1000;
if ("seconds".equals(unit)) return delta;
delta /= 60;
if ("minutes".equals(unit)) return delta;
@ -48,9 +48,7 @@ public class Diff implements Function {
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
public void write(JSONWriter writer, Properties options) throws JSONException {
writer.object();
writer.key("description"); writer.value("For strings, returns the portion where they differ. For dates, it returns the difference in given time units");
writer.key("params"); writer.value("o1, o2, time unit (optional)");