diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java b/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java index 2afc70685..7e616a0bc 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java @@ -5,6 +5,10 @@ import java.util.Properties; import org.json.JSONException; import org.json.JSONWriter; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.GregorianCalendar; + import com.metaweb.gridworks.expr.util.CalendarParser; import com.metaweb.gridworks.expr.util.CalendarParserException; import com.metaweb.gridworks.gel.Function; @@ -12,33 +16,61 @@ import com.metaweb.gridworks.gel.Function; public class ToDate implements Function { public Object call(Properties bindings, Object[] args) { - if (args.length == 1 || args.length == 2) { - Object o1 = args[0]; - if (o1 != null && o1 instanceof String) { - boolean month_first = true; - if (args.length == 2) { - Object o2 = args[1]; - if (o2 != null && o2 instanceof Boolean) { - month_first = ((Boolean) o2).booleanValue(); - } + if (args.length == 0) { + // missing value, can this happen? + } + if (!(args[0] instanceof String)) { + // ignore cell values that aren't strings + return null; + } + String o1 = (String) args[0]; + + // "o, boolean month_first (optional)" + if (args.length == 1 || (args.length == 2 && args[1] instanceof Boolean)) { + boolean month_first = true; + if (args.length == 2) { + month_first = (Boolean) args[1]; + } + try { + return CalendarParser.parse( o1, (month_first) ? CalendarParser.MM_DD_YY : CalendarParser.DD_MM_YY); + } catch (CalendarParserException e) { + // do something about + } + } + + // "o, format1, format2 (optional), ..." + if (args.length>=2) { + for (int i=1;i