From b2ae74d23fb67865720e5df2a69a7cf46be38e07 Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Fri, 3 Aug 2012 18:01:01 +0000 Subject: [PATCH] FIXED - task 586: Only one parse date format is attempted from list in toDate(format1,format2) http://code.google.com/p/google-refine/issues/detail?id=586 git-svn-id: http://google-refine.googlecode.com/svn/trunk@2520 7d457c2a-affb-35e4-300a-418c747d4874 --- .../google/refine/expr/functions/ToDate.java | 10 ++++++++-- .../strings/ToFromConversionTests.java | 17 ++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/main/src/com/google/refine/expr/functions/ToDate.java b/main/src/com/google/refine/expr/functions/ToDate.java index 6d8ace3f9..5ecad0368 100644 --- a/main/src/com/google/refine/expr/functions/ToDate.java +++ b/main/src/com/google/refine/expr/functions/ToDate.java @@ -106,12 +106,17 @@ public class ToDate implements Function { continue; } String format = (String) args[i]; - SimpleDateFormat formatter = new SimpleDateFormat(format); + SimpleDateFormat formatter; + try { + formatter = new SimpleDateFormat(format); + } catch (IllegalArgumentException e) { + return new EvalError("Unknown date format"); + } Date date = null; try { date = formatter.parse(o1); } catch (java.text.ParseException e) { - return new EvalError("Cannot parse to date"); + continue; } if (date != null) { GregorianCalendar c = new GregorianCalendar(); @@ -119,6 +124,7 @@ public class ToDate implements Function { return c; } } + return new EvalError("Unable to parse as date"); } return null; diff --git a/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java b/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java index 11615ed39..0d5443e32 100644 --- a/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java +++ b/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java @@ -44,6 +44,8 @@ import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.google.refine.expr.EvalError; +import com.google.refine.expr.util.CalendarParser; +import com.google.refine.expr.util.CalendarParserException; import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.grel.Function; import com.google.refine.tests.RefineTest; @@ -92,13 +94,6 @@ public class ToFromConversionTests extends RefineTest { } } - @Test - public void testFromError() { - Assert.assertTrue(invoke("fromError") instanceof EvalError); - Assert.assertTrue(invoke("fromError", (Object)null) instanceof EvalError); - Assert.assertTrue(invoke("fromError", "string") instanceof EvalError); - Assert.assertEquals(invoke("fromError", new EvalError("message")),"message"); - } @Test public void testToNumber() { @@ -127,13 +122,17 @@ public class ToFromConversionTests extends RefineTest { } @Test - public void testToDate() { + public void testToDate() throws CalendarParserException { // Assert.assertTrue(invoke("toDate") instanceof EvalError); Assert.assertNull(invoke("toDate")); Assert.assertTrue(invoke("toDate", (Object) null) instanceof EvalError); Assert.assertTrue(invoke("toDate", 1.0) instanceof EvalError); + Assert.assertTrue(invoke("toDate", "2012-03-01","xxx") instanceof EvalError); // bad format string Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof GregorianCalendar); -// Assert.assertEquals(invoke("toDate", "2012-03-01"),new GregorianCalendar(2012,3,1)); + Assert.assertEquals(invoke("toDate", "2012-03-01"),CalendarParser.parse("2012-03-01")); + Assert.assertEquals(invoke("toDate", "2012-03-01","yyyy-MM-dd"),CalendarParser.parse("2012-03-01")); + // Multiple format strings should get tried sequentially until one succeeds or all are exhausted + Assert.assertEquals(invoke("toDate", "2012-03-01","MMM","yyyy-MM-dd"), CalendarParser.parse("2012-03-01")); // Date // Calendar // String