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));
}
});
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);
}
});
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",
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