Skip to content

Commit

Permalink
add a new file Workday.txt and handling blank, error values, unexpect…
Browse files Browse the repository at this point in the history
…ed type and coercions in Workday
  • Loading branch information
m365solutioninsights committed Feb 13, 2025
1 parent 6aa54e0 commit f95f319
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 34 deletions.
6 changes: 5 additions & 1 deletion src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,15 @@ internal static class TexlStrings
public static StringGetter DateTimeValueArg2 = (b) => StringResources.Get("DateTimeValueArg2", b);

public static StringGetter AboutDateAdd = (b) => StringResources.Get("AboutDateAdd", b);
public static StringGetter AboutWorkday = (b) => StringResources.Get("AboutWorkday", b);
public static StringGetter DateAddArg1 = (b) => StringResources.Get("DateAddArg1", b);
public static StringGetter DateAddArg2 = (b) => StringResources.Get("DateAddArg2", b);
public static StringGetter DateAddArg3 = (b) => StringResources.Get("DateAddArg3", b);

public static StringGetter AboutWorkday = (b) => StringResources.Get("AboutWorkday", b);
public static StringGetter WorkdayArg1 = (b) => StringResources.Get("WorkdayArg1", b);
public static StringGetter WorkdayArg2 = (b) => StringResources.Get("WorkdayArg2", b);
public static StringGetter WorkdayArg3 = (b) => StringResources.Get("WorkdayArg3", b);

public static StringGetter AboutDateAddT = (b) => StringResources.Get("AboutDateAddT", b);
public static StringGetter DateAddTArg1 = (b) => StringResources.Get("DateAddTArg1", b);
public static StringGetter DateAddTArg2 = (b) => StringResources.Get("DateAddTArg2", b);
Expand Down
25 changes: 6 additions & 19 deletions src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,9 @@ public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DTyp

return fValid;
}
}
// Workday(timestamp: d, delta: n : d
}

// Workday(timestamp: d, delta: n) : d
internal sealed class WorkdayFunction : BuiltinFunction
{
public override bool IsSelfContained => true;
Expand All @@ -634,20 +634,7 @@ public WorkdayFunction()

public override IEnumerable<TexlStrings.StringGetter[]> GetSignatures()
{
yield return new[] { TexlStrings.DateAddArg1, TexlStrings.DateAddArg2 };
}

public override IEnumerable<string> GetRequiredEnumNames()
{
return new List<string>() { LanguageConstants.TimeUnitEnumString };
}

// This method returns true if there are special suggestions for a particular parameter of the function.
public override bool HasSuggestionsForParam(int argumentIndex)
{
Contracts.Assert(argumentIndex >= 0);

return argumentIndex == 2;
yield return new[] { TexlStrings.WorkdayArg1, TexlStrings.WorkdayArg2 };
}

public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DType[] argTypes, IErrorContainer errors, out DType returnType, out Dictionary<TexlNode, DType> nodeToCoercedTypeMap)
Expand All @@ -666,9 +653,9 @@ public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DTyp

if (fValid)
{
if (type0.Kind == DKind.Date || type0.Kind == DKind.DateTime)
if (type0.Kind == DKind.Date || type0.Kind == DKind.DateTime || type0.Kind == DKind.Time)
{
// Arg0 should be a DateTime or Date.
// Arg0 should be a Time, DateTime or Date.
returnType = type0;
}
else if (nodeToCoercedTypeMap != null && nodeToCoercedTypeMap.TryGetValue(args[0], out var coercedType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ static Library()
BuiltinFunctionsCore.Workday,
StandardErrorHandling<FormulaValue>(
BuiltinFunctionsCore.Workday.Name,
expandArguments: InsertDefaultValues(outputArgsCount: 3, fillWith: new BlankValue(IRContext.NotInSource(FormulaType.Blank))),
expandArguments: InsertDefaultValues(outputArgsCount: 2, fillWith: new BlankValue(IRContext.NotInSource(FormulaType.Blank))),
replaceBlankValues: ReplaceBlankWith(
new DateTimeValue(IRContext.NotInSource(FormulaType.DateTime), _epoch),
new NumberValue(IRContext.NotInSource(FormulaType.Number), 0)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public static FormulaValue Workday(EvalVisitor runner, EvalVisitorContext contex
}
else
{
throw new NotImplementedException();
//throw new NotImplementedException();
return CommonErrors.RuntimeTypeMismatch(args[1].IRContext);
}

var useUtcConversion = NeedToConvertToUtc(runner, dateTime, timeUnit);
Expand Down Expand Up @@ -230,18 +231,7 @@ public static FormulaValue Workday(EvalVisitor runner, EvalVisitorContext contex

dateTime = MakeValidDateTime(runner, dateTime, timeZoneInfo);

if (irContext.ResultType._type.Kind == Core.Types.DKind.Date)
{
return new DateValue(irContext, dateTime);
}
else if (irContext.ResultType._type.Kind == Core.Types.DKind.Time)
{
return new TimeValue(irContext, dateTime.Subtract(_epoch));
}
else
{
return new DateTimeValue(irContext, dateTime);
}
return new DateValue(irContext, dateTime);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
>> Workday(Date(2024,9,13), 10)
Date(2024,9,27)

>> Workday(Date(2024,9,13), -10)
Date(2024,8,30)

>> Workday(45565,1)
Date(2024,10,1)

>> Workday(Date(2024,9,30), "true")
<Error: The value 'true' cannot be converted to a number.>

>> DateAdd(false, 1)
Error 8-13: Invalid argument type (Boolean). Expecting a DateTime value instead.
Error 0-7: The function 'DateAdd' has some invalid arguments.

>> Workday("false",1)
<Error: The date or time value cannot be parsed.>

>> Workday([Date(2000,1,1)],1)
Error 8-24: Invalid argument type (Table). Expecting a DateTime value instead.

Error 0-7: The function 'Workday' has some invalid arguments.
>> Workday(null,1)
Error 8-12: Use of a reserved word that is currently not supported.
Error 8-12: Invalid argument type (Error). Expecting a DateTime value instead.
Error 0-7: The function 'Workday' has some invalid arguments.

>> Workday(Date(2024,9,30), null)
Error 25-29: Use of a reserved word that is currently not supported.
Error 25-29: Invalid argument type (Error). Expecting a Number value instead.
Error 0-7: The function 'Workday' has some invalid arguments.

>> Workday(Date(2024,9,30), [])
Error 25-27: Invalid argument type (Table). Expecting a Number value instead.
Error 0-7: The function 'Workday' has some invalid arguments.

>> Workday(,)
Error 8-9: Unexpected characters. Characters are used in the formula in an unexpected way.
Error 9-10: Unexpected characters. Characters are used in the formula in an unexpected way.
Error 10-10: Unexpected characters. The formula contains 'Eof' where 'ParenClose' is expected.
Error 8-9: Invalid argument type (Error). Expecting a DateTime value instead.
Error 9-10: Invalid argument type (Error). Expecting a Number value instead.
Error 0-7: The function 'Workday' has some invalid arguments.

>> Workday(DateTimeValue(\"1 Jan 2015\"), 2))
Error 22-23: Unexpected characters. Characters are used in the formula in an unexpected way.
Error 23-36: Expected operator. We expect an operator such as +, *, or & at this point in the formula.
Error 36-37: Unexpected characters. Characters are used in the formula in an unexpected way.
Error 0-42: Invalid number of arguments: received 1, expected 2.
2 changes: 2 additions & 0 deletions src/tests/Microsoft.PowerFx.Core.Tests.Shared/TexlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public void TexlDateOverloads_Negative(string script)
[InlineData("DateAdd(\"2000-01-01\", 1)", "d")] // Coercion on date argument from string
[InlineData("DateAdd(45678, 1)", "d")] // Coercion on date argument from number
[InlineData("DateAdd(Time(12,34,56), 1)", "T")] // Coercion on date argument from time
[InlineData("Workday(Date(2024,9,13),999)", "D")]
[InlineData("Workday(Date(2024,9,13),-101)", "D")]
public void TexlDateAdd(string script, string expectedType)
{
Assert.True(DType.TryParse(expectedType, out var type));
Expand Down

0 comments on commit f95f319

Please sign in to comment.