Skip to content

Commit

Permalink
Remove extra validation when DateTimeValue(uo)
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-joyle committed Aug 14, 2024
1 parent 7c7e66b commit 3852c83
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ namespace Microsoft.PowerFx.Functions
{
internal static partial class Library
{
private static bool IsValidDateTimeUO(string s)
{
return Regex.IsMatch(s, @"^[0-9]{4,4}-[0-1][0-9]-[0-3][0-9](T[0-2][0-9]:[0-5][0-9]:[0-5][0-9](\.[0-9]{0,7})?Z?)?$");
}

public static FormulaValue Index_UO(IRContext irContext, FormulaValue[] args)
{
var arg0 = (UntypedObjectValue)args[0];
Expand Down Expand Up @@ -476,7 +471,7 @@ public static FormulaValue DateValue_UO(EvalVisitor runner, EvalVisitorContext c
{
var s = impl.GetString();

if (IsValidDateTimeUO(s) && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTime datetime))
if (DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTime datetime))
{
var timeZoneInfo = runner.TimeZoneInfo;
datetime = MakeValidDateTime(runner, datetime, timeZoneInfo);
Expand Down Expand Up @@ -520,7 +515,7 @@ public static FormulaValue DateTimeValue_UO(EvalVisitor runner, EvalVisitorConte
{
var s = impl.GetString();

if (IsValidDateTimeUO(s) && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTime datetime))
if (DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTime datetime))
{
datetime = MakeValidDateTime(runner, datetime, runner.TimeZoneInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,13 @@ Error({Kind:ErrorKind.InvalidArgument})
Error({Kind:ErrorKind.InvalidArgument})

>> DateValue(ParseJSON("""2011-1-15"""))
Error({Kind:ErrorKind.InvalidArgument})
Date(2011,1,15)

>> DateValue(ParseJSON("""2011-01"""))
Error({Kind:ErrorKind.InvalidArgument})
Date(2011,1,1)

>> DateValue(ParseJSON("""2011-1-15""")) = DateValue("2011-1-15")
true

>> DateValue(ParseJSON("""2011"""))
Error({Kind:ErrorKind.InvalidArgument})
Expand All @@ -266,6 +269,9 @@ Error({Kind:ErrorKind.InvalidArgument})
>> DateDiff(DateTimeValue(ParseJSON("""2011-01-15T00:00:00""")), DateTimeValue(ParseJSON("""2011-01-30""")))
15

>> DateDiff(DateTimeValue(ParseJSON("""8/13/2024 9:10 AM""")), DateTimeValue(ParseJSON("""8/14/2024 9:10 AM""")))
1

>> DateTimeValue(ParseJSON("""2011-01-15T08:0:00.000Z"""))
Error({Kind:ErrorKind.InvalidArgument})

Expand Down

0 comments on commit 3852c83

Please sign in to comment.