diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2018-09-26 02:08:37 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2018-09-26 02:08:37 +0000 |
commit | 0dea2079f785d3f1b70a2a2263c12b3e511ad94d (patch) | |
tree | aea840ec131f2e768f837f8180e13c8cf2abe6b1 | |
parent | b9dddc4eed7c6a059c83aebd056a95bcfeff7dd5 (diff) | |
download | jackcess-0dea2079f785d3f1b70a2a2263c12b3e511ad94d.tar.gz jackcess-0dea2079f785d3f1b70a2a2263c12b3e511ad94d.zip |
add support for weekdayname function
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1201 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java | 69 | ||||
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java | 6 |
2 files changed, 61 insertions, 14 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 2168ceb..c8ffc0f 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java @@ -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; + } } 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 e8aa234..6a36199 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -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}")); |