From b2aba90126da7421be9d30656354dda71d0de301 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Wed, 25 Jul 2018 02:30:33 +0000 Subject: [PATCH] add some date function tests git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1185 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../impl/expr/DefaultDateFunctions.java | 19 +++++++++++---- .../impl/expr/DefaultFunctionsTest.java | 23 +++++++++++++++++++ .../impl/expr/ExpressionatorTest.java | 6 ++--- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java index e60c956..7bfb256 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java @@ -155,9 +155,8 @@ public class DefaultDateFunctions public static final Function YEAR = registerFunc(new Func1NullIsNull("Year") { @Override protected Value eval1(EvalContext ctx, Value param1) { - // convert from 0 based to 1 based value return BuiltinOperators.toValue( - nonNullToCalendarField(ctx, param1, Calendar.YEAR) + 1); + nonNullToCalendarField(ctx, param1, Calendar.YEAR)); } }); @@ -186,11 +185,21 @@ public class DefaultDateFunctions return null; } int day = nonNullToCalendarField(ctx, param1, Calendar.DAY_OF_WEEK); + + // vbSunday (default) + int firstDay = 1; if(params.length > 1) { - // TODO handle first day of week - // int firstDay = params[1].getAsLong(); - throw new UnsupportedOperationException(); + firstDay = params[1].getAsLongInt(); + if(firstDay == 0) { + // 0 == vbUseSystem, so we will use the default "sunday" + firstDay = 1; + } } + + // shift all the values to 0 based to calculate the correct value, then + // back to 1 based to return the result + day = (((day - 1) - (firstDay - 1) + 7) % 7) + 1; + return BuiltinOperators.toValue(day); } }); diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java index 58d99e0..461f153 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -213,6 +213,29 @@ public class DefaultFunctionsTest extends TestCase assertEquals(-4, eval("=Round(-4, 2)")); } + public void testDateFuncs() throws Exception + { + assertEquals("1/2/2003", eval("=CStr(DateValue(#01/02/2003 7:00:00 AM#))")); + assertEquals("07:00:00 AM", eval("=CStr(TimeValue(#01/02/2003 7:00:00 AM#))")); + + assertEquals(2003, eval("=Year(#01/02/2003 7:00:00 AM#)")); + assertEquals(1, eval("=Month(#01/02/2003 7:00:00 AM#)")); + assertEquals(2, eval("=Day(#01/02/2003 7:00:00 AM#)")); + + assertEquals(7, eval("=Hour(#01/02/2003 7:10:27 AM#)")); + assertEquals(19, eval("=Hour(#01/02/2003 7:10:27 PM#)")); + assertEquals(10, eval("=Minute(#01/02/2003 7:10:27 AM#)")); + assertEquals(27, eval("=Second(#01/02/2003 7:10:27 AM#)")); + + assertEquals(7, eval("=Weekday(#11/22/2003#)")); + assertEquals(3, eval("=Weekday(#11/22/2003#, 5)")); + + assertTrue(((String)eval("=CStr(Date())")) + .matches("\\d{1,2}/\\d{1,2}/\\d{4}")); + assertTrue(((String)eval("=CStr(Time())")) + .matches("\\d{1,2}:\\d{1,2}:\\d{2} (AM|PM)")); + } + public void testFinancialFuncs() throws Exception { assertEquals("-9.57859403981317", diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java index 5f6d9c2..8e077b0 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java @@ -315,11 +315,11 @@ public class ExpressionatorTest extends TestCase public void testDateArith() throws Exception { - assertEquals(new Date(1041508800000L), eval("=#01/02/2003# + #7:00:00#")); - assertEquals(new Date(1041458400000L), eval("=#01/02/2003# - #7:00:00#")); + assertEquals(new Date(1041508800000L), eval("=#01/02/2003# + #7:00:00 AM#")); + assertEquals(new Date(1041458400000L), eval("=#01/02/2003# - #7:00:00 AM#")); assertEquals(new Date(1044680400000L), eval("=#01/02/2003# + '37'")); assertEquals(new Date(1044680400000L), eval("='37' + #01/02/2003#")); - assertEquals(new Date(1041508800000L), eval("=#01/02/2003 7:00:00#")); + assertEquals(new Date(1041508800000L), eval("=#01/02/2003 7:00:00 AM#")); } public void testNull() throws Exception -- 2.39.5