]> source.dussan.org Git - jackcess.git/commitdiff
add some date function tests
authorJames Ahlborn <jtahlborn@yahoo.com>
Wed, 25 Jul 2018 02:30:33 +0000 (02:30 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Wed, 25 Jul 2018 02:30:33 +0000 (02:30 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1185 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/ExpressionatorTest.java

index e60c956c106f828bcdf6613c63fbad43f79a2f32..7bfb256e7f98386bcae84d0c57d71018126938fa 100644 (file)
@@ -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);
     }
   });
index 58d99e0d5b5cb2b3d96ef43d18ef0722a3beeaff..461f1537687aaeba2a4a6f72d25da22e9de3728c 100644 (file)
@@ -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",
index 5f6d9c26c095ef297075b2174d1a01e831344306..8e077b0d7f3c317f167921c74f257a84dc67f9f5 100644 (file)
@@ -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