]> source.dussan.org Git - jackcess.git/commitdiff
add support for weekdayname function
authorJames Ahlborn <jtahlborn@yahoo.com>
Wed, 26 Sep 2018 02:08:37 +0000 (02:08 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Wed, 26 Sep 2018 02:08:37 +0000 (02:08 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1201 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

index 2168ceb6bfbd0089ea05e7b1a3403533790ae67a..c8ffc0ffc77d97f4b2867c4b27f3934cd30a70b4 100644 (file)
@@ -19,6 +19,7 @@ package com.healthmarketscience.jackcess.impl.expr;
 
 import java.math.BigDecimal;
 import java.text.DateFormat;
+import java.text.DateFormatSymbols;
 import java.util.Calendar;
 import java.util.Date;
 
@@ -218,27 +219,41 @@ public class DefaultDateFunctions
       if(param1 == null) {
         return null;
       }
-      int day = nonNullToCalendarField(ctx, param1, Calendar.DAY_OF_WEEK);
+      int dayOfWeek = nonNullToCalendarField(ctx, param1, Calendar.DAY_OF_WEEK);
 
-      // vbSunday (default)
-      int firstDay = 1;
+      int firstDay = getFirstDayParam(params, 1);
+
+      return ValueSupport.toValue(dayOfWeekToWeekDay(dayOfWeek, firstDay));
+    }
+  });
+
+  public static final Function WEEKDAYNAME = registerFunc(new FuncVar("WeekdayName", 1, 3) {
+    @Override
+    protected Value evalVar(EvalContext ctx, Value[] params) {
+      Value param1 = params[0];
+      if(param1 == null) {
+        return null;
+      }
+      int weekday = param1.getAsLongInt();
+
+      boolean abbreviate = false;
       if(params.length > 1) {
-        firstDay = params[1].getAsLongInt();
-        if(firstDay == 0) {
-          // 0 == vbUseSystem, so we will use the default "sunday"
-          firstDay = 1;
-        }
+        abbreviate = params[1].getAsBoolean();
       }
 
-      // 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;
+      int firstDay = getFirstDayParam(params, 2);
 
-      return ValueSupport.toValue(day);
+      int dayOfWeek = weekDayToDayOfWeek(weekday, firstDay);
+
+      DateFormatSymbols syms = ctx.createDateFormat(
+          ctx.getTemporalConfig().getDateFormat()).getDateFormatSymbols();
+      String[] weekdayNames = (abbreviate ?
+                               syms.getShortWeekdays() : syms.getWeekdays());
+      // note, the array is 1 based
+      return ValueSupport.toValue(weekdayNames[dayOfWeek]);
     }
   });
 
-
   private static int nonNullToCalendarField(EvalContext ctx, Value param,
                                             int field) {
     return nonNullToCalendar(ctx, param).get(field);
@@ -290,7 +305,8 @@ public class DefaultDateFunctions
     boolean hasDate = (dateOnly(dd) != 0.0d);
     boolean hasTime = (timeOnly(dd) != 0.0d);
 
-    Value.Type type = (hasDate ? (hasTime ? Value.Type.DATE_TIME : Value.Type.DATE) :
+    Value.Type type = (hasDate ? (hasTime ? Value.Type.DATE_TIME :
+                                  Value.Type.DATE) :
                        Value.Type.TIME);
     DateFormat fmt = ValueSupport.getDateFormatForType(ctx, type);
     return ValueSupport.toValue(type, dd, fmt);
@@ -317,4 +333,29 @@ public class DefaultDateFunctions
   private static double currentTimeDouble(DateFormat fmt) {
     return ColumnImpl.toDateDouble(System.currentTimeMillis(), fmt.getCalendar());
   }
+
+  private static int dayOfWeekToWeekDay(int day, int firstDay) {
+    // shift all the values to 0 based to calculate the correct value, then
+    // back to 1 based to return the result
+    return (((day - 1) - (firstDay - 1) + 7) % 7) + 1;
+  }
+
+  private static int weekDayToDayOfWeek(int weekday, int firstDay) {
+    // shift all the values to 0 based to calculate the correct value, then
+    // back to 1 based to return the result
+    return (((firstDay - 1) + (weekday - 1)) % 7) + 1;
+  }
+
+  private static int getFirstDayParam(Value[] params, int idx) {
+    // vbSunday (default)
+    int firstDay = 1;
+    if(params.length > idx) {
+      firstDay = params[idx].getAsLongInt();
+      if(firstDay == 0) {
+        // 0 == vbUseSystem, so we will use the default "sunday"
+        firstDay = 1;
+      }
+    }
+    return firstDay;
+  }
 }
index e8aa234fc9d7a96605f7d24d34a0886785d9477b..6a36199145139588d4976b00ceb8ff2d9a886dcc 100644 (file)
@@ -243,6 +243,12 @@ public class DefaultFunctionsTest extends TestCase
 
     assertEquals(7, eval("=Weekday(#11/22/2003#)"));
     assertEquals(3, eval("=Weekday(#11/22/2003#, 5)"));
+    assertEquals(1, eval("=Weekday(#11/22/2003#, 7)"));
+
+    assertEquals("Sunday", eval("=WeekdayName(1)"));
+    assertEquals("Sun", eval("=WeekdayName(1,True)"));
+    assertEquals("Tuesday", eval("=WeekdayName(1,False,3)"));
+    assertEquals("Thu", eval("=WeekdayName(3,True,3)"));
 
     assertTrue(((String)eval("=CStr(Date())"))
                  .matches("\\d{1,2}/\\d{1,2}/\\d{4}"));