Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds calendar functions #2469

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,14 @@ static Library()
returnBehavior: ReturnBehavior.AlwaysEvaluateAndReturnResult,
targetFunction: Month)
},
{
BuiltinFunctionsCore.MonthsLong,
NoErrorHandling(CalendarMonthsLong)
},
{
BuiltinFunctionsCore.MonthsShort,
NoErrorHandling(CalendarMonthsShort)
},
{
BuiltinFunctionsCore.Not,
StandardErrorHandling<BooleanValue>(
Expand Down Expand Up @@ -1863,6 +1871,14 @@ static Library()
returnBehavior: ReturnBehavior.AlwaysEvaluateAndReturnResult,
targetFunction: Weekday)
},
{
BuiltinFunctionsCore.WeekdaysLong,
NoErrorHandling(CalendarWeekdaysLong)
},
{
BuiltinFunctionsCore.WeekdaysShort,
NoErrorHandling(CalendarWeekdaysShort)
},
{
BuiltinFunctionsCore.WeekNum,
StandardErrorHandling<FormulaValue>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,48 @@ private static double WeekStartDay(double startOfWeek)
return 0;
}

private static FormulaValue CalendarMonthsLong(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args)
{
var rows = Enumerable
.Range(1, 12)
.Select(month => new DateTime(2024, month, 2).ToString("MMMM", runner.CultureInfo))
.Select(monthName => new StringValue(IRContext.NotInSource(FormulaType.String), monthName));

return new InMemoryTableValue(irContext, StandardTableNodeRecords(irContext, rows.ToArray(), forceSingleColumn: true));
}

private static FormulaValue CalendarMonthsShort(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args)
{
var rows = Enumerable
.Range(1, 12)
.Select(month => new DateTime(2024, month, 2).ToString("MMM", runner.CultureInfo).TrimEnd('.'))
.Select(monthName => new StringValue(IRContext.NotInSource(FormulaType.String), monthName));

return new InMemoryTableValue(irContext, StandardTableNodeRecords(irContext, rows.ToArray(), forceSingleColumn: true));
}

private static FormulaValue CalendarWeekdaysLong(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args)
{
var anySunday = new DateTime(2024, 6, 2);
var rows = Enumerable
.Range(0, 7)
.Select(weekday => anySunday.AddDays(weekday).ToString("dddd", runner.CultureInfo))
.Select(weekdayName => new StringValue(IRContext.NotInSource(FormulaType.String), weekdayName));

return new InMemoryTableValue(irContext, StandardTableNodeRecords(irContext, rows.ToArray(), forceSingleColumn: true));
}

private static FormulaValue CalendarWeekdaysShort(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args)
{
var anySunday = new DateTime(2024, 6, 2);
var rows = Enumerable
.Range(0, 7)
.Select(weekday => anySunday.AddDays(weekday).ToString("ddd", runner.CultureInfo).TrimEnd('.'))
.Select(weekdayName => new StringValue(IRContext.NotInSource(FormulaType.String), weekdayName));

return new InMemoryTableValue(irContext, StandardTableNodeRecords(irContext, rows.ToArray(), forceSingleColumn: true));
}

private static double GetDoubleFromFormulaValue(FormulaValue fv, double defaultDouble = 0)
{
switch (fv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ private static AsyncFunctionPtr NoErrorHandling(
};
}

// A wrapper for a function with no error handling behavior whatsoever.
private static AsyncFunctionPtr NoErrorHandling(
Func<EvalVisitor, EvalVisitorContext, IRContext, FormulaValue[], FormulaValue> targetFunction)
{
return (visitor, context, irContext, args) =>
{
var result = targetFunction(visitor, context, irContext, args);
return new ValueTask<FormulaValue>(result);
};
}

// A wrapper for a function with no error handling behavior whatsoever.
private static AsyncFunctionPtr NoErrorHandling(
Func<EvalVisitor, EvalVisitorContext, IRContext, FormulaValue[], ValueTask<FormulaValue>> targetFunction)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
>> Language()
"en-US"
"en-US"

>> Calendar.MonthsLong()
Table({Value:"January"},{Value:"February"},{Value:"March"},{Value:"April"},{Value:"May"},{Value:"June"},{Value:"July"},{Value:"August"},{Value:"September"},{Value:"October"},{Value:"November"},{Value:"December"})

>> Calendar.MonthsShort()
Table({Value:"Jan"},{Value:"Feb"},{Value:"Mar"},{Value:"Apr"},{Value:"May"},{Value:"Jun"},{Value:"Jul"},{Value:"Aug"},{Value:"Sep"},{Value:"Oct"},{Value:"Nov"},{Value:"Dec"})

>> Calendar.WeekdaysLong()
Table({Value:"Sunday"},{Value:"Monday"},{Value:"Tuesday"},{Value:"Wednesday"},{Value:"Thursday"},{Value:"Friday"},{Value:"Saturday"})

>> Calendar.WeekdaysShort()
Table({Value:"Sun"},{Value:"Mon"},{Value:"Tue"},{Value:"Wed"},{Value:"Thu"},{Value:"Fri"},{Value:"Sat"})
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
"Minute",
"Mod",
"Month",
"MonthsLong",
"MonthsShort",
"Not",
"Now",
"OptionSetInfo",
Expand Down Expand Up @@ -145,6 +147,8 @@
"Value",
"VarP",
"Weekday",
"WeekdaysLong",
"WeekdaysShort",
"WeekNum",
"With",
"Year"
Expand Down
Loading
Loading