From cfda7968eea42cdc7846e91f2fcce91d9c08d4e8 Mon Sep 17 00:00:00 2001 From: Andy Edmonds Date: Tue, 24 Jan 2023 12:54:42 -0500 Subject: [PATCH 01/14] Update target frameworks and add console app for testing --- Math expression eval/ConsoleApp/ConsoleApp.csproj | 14 ++++++++++++++ Math expression eval/ConsoleApp/Program.cs | 5 +++++ Math expression eval/Math expression eval.sln | 10 ++++++++-- Math expression eval/UnitTest/UnitTest.csproj | 2 +- .../org.matheval/org.matheval.csproj | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 Math expression eval/ConsoleApp/ConsoleApp.csproj create mode 100644 Math expression eval/ConsoleApp/Program.cs diff --git a/Math expression eval/ConsoleApp/ConsoleApp.csproj b/Math expression eval/ConsoleApp/ConsoleApp.csproj new file mode 100644 index 0000000..7ecd3b7 --- /dev/null +++ b/Math expression eval/ConsoleApp/ConsoleApp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs new file mode 100644 index 0000000..7a7ea66 --- /dev/null +++ b/Math expression eval/ConsoleApp/Program.cs @@ -0,0 +1,5 @@ +using org.matheval; + +Expression expression = new("1+1"); +var val = expression.Eval(); +Console.WriteLine(val); \ No newline at end of file diff --git a/Math expression eval/Math expression eval.sln b/Math expression eval/Math expression eval.sln index 0236dfe..58f58b8 100644 --- a/Math expression eval/Math expression eval.sln +++ b/Math expression eval/Math expression eval.sln @@ -1,12 +1,14 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30907.101 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33205.214 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "org.matheval", "org.matheval\org.matheval.csproj", "{85774930-C0E9-432C-A23F-015FB26F3897}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTest", "UnitTest\UnitTest.csproj", "{3FFECB40-1C8E-4362-9476-BEBB4EE329BD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{E88BE99B-5A05-49F3-97D0-AC12D5A5F5A7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {3FFECB40-1C8E-4362-9476-BEBB4EE329BD}.Debug|Any CPU.Build.0 = Debug|Any CPU {3FFECB40-1C8E-4362-9476-BEBB4EE329BD}.Release|Any CPU.ActiveCfg = Release|Any CPU {3FFECB40-1C8E-4362-9476-BEBB4EE329BD}.Release|Any CPU.Build.0 = Release|Any CPU + {E88BE99B-5A05-49F3-97D0-AC12D5A5F5A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E88BE99B-5A05-49F3-97D0-AC12D5A5F5A7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E88BE99B-5A05-49F3-97D0-AC12D5A5F5A7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E88BE99B-5A05-49F3-97D0-AC12D5A5F5A7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Math expression eval/UnitTest/UnitTest.csproj b/Math expression eval/UnitTest/UnitTest.csproj index b9dc132..1cefac0 100644 --- a/Math expression eval/UnitTest/UnitTest.csproj +++ b/Math expression eval/UnitTest/UnitTest.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1; + netcoreapp2.1;net7.0 false diff --git a/Math expression eval/org.matheval/org.matheval.csproj b/Math expression eval/org.matheval/org.matheval.csproj index 677a79e..39b3f02 100644 --- a/Math expression eval/org.matheval/org.matheval.csproj +++ b/Math expression eval/org.matheval/org.matheval.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1;netcoreapp3.1;net35;net40;net45;net46;net461;net462;net47;net471;net472;net48;netstandard2.0 + netstandard2.0;netstandard2.1 Library Matheval org.matheval From 9ec491bc3ef35a1e8050b65a842c1f385011e792 Mon Sep 17 00:00:00 2001 From: Andy Edmonds Date: Tue, 24 Jan 2023 15:11:21 -0500 Subject: [PATCH 02/14] year function --- Math expression eval/ConsoleApp/Program.cs | 3 +- .../org.matheval/Common/Afe_Common.cs | 23 +++++++ .../Functions/Impl/yearFunction.cs | 60 +++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 Math expression eval/org.matheval/Functions/Impl/yearFunction.cs diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index 7a7ea66..aea4486 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -1,5 +1,6 @@ using org.matheval; -Expression expression = new("1+1"); +var date = DateTime.Now; +Expression expression = new Expression("year('2021-03-30')"); var val = expression.Eval(); Console.WriteLine(val); \ No newline at end of file diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index dd4c207..83839b1 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -606,6 +606,11 @@ public static class Afe_Common /// public const string Const_Find = "find"; + /// + /// Function Year + /// + public const string Const_Year = "year"; + #endregion @@ -764,6 +769,24 @@ public static decimal ToDecimal(object value, CultureInfo cultureInfo) } } + /// + /// TODO poner default invariant culture. + /// + /// + /// + /// + public static decimal ToInteger(object value, CultureInfo cultureInfo) + { + if (value is int) + { + return (int)value; + } + else + { + return Convert.ToInt32(value, cultureInfo); + } + } + /// /// Connvert object to string /// TODO poner default invariant culture. diff --git a/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs new file mode 100644 index 0000000..1b14daa --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs @@ -0,0 +1,60 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the year from a date + /// + public class yearFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List + { + new FunctionDef(Afe_Common.Const_Lower, new System.Type[] { typeof(string) }, typeof(int), 1) + }; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + string dateString = Afe_Common.ToString(args[Afe_Common.Const_Key_One].ToString(), dc.WorkingCulture); + DateTime date = DateTime.Parse(dateString, dc.WorkingCulture); + return Afe_Common.ToInteger(date.Year, dc.WorkingCulture); + } + } +} From f3d1560fb1ba2b3e14565bd91afe9f7259c92933 Mon Sep 17 00:00:00 2001 From: Andy Edmonds Date: Tue, 24 Jan 2023 16:35:30 -0500 Subject: [PATCH 03/14] date function --- Math expression eval/ConsoleApp/Program.cs | 3 +- .../org.matheval/Common/Afe_Common.cs | 50 ++++++++------- .../Functions/Impl/dateFunction.cs | 62 +++++++++++++++++++ .../Functions/Impl/yearFunction.cs | 2 +- 4 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 Math expression eval/org.matheval/Functions/Impl/dateFunction.cs diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index aea4486..de41754 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -1,6 +1,5 @@ using org.matheval; -var date = DateTime.Now; -Expression expression = new Expression("year('2021-03-30')"); +Expression expression = new("date(2022, 3, 30)"); var val = expression.Eval(); Console.WriteLine(val); \ No newline at end of file diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index 83839b1..fe9c074 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -231,28 +231,18 @@ public static class Afe_Common /// /// Funtion TODAY /// - public const string Const_TODAY = "TODAY"; + public const string Const_TODAY = "today"; /// /// Funtion left /// public const string Const_LEFT = "left"; - /// - /// Function Second - /// - public const string Const_Second = "second"; - /// /// Function code /// public const string Const_Code = "code"; - /// - /// Function hour - /// - public const string Const_Hour = "hour"; - /// /// Function int /// @@ -263,12 +253,6 @@ public static class Afe_Common /// public const string Const_Isblank = "isblank"; - /// - /// Function Minute - /// - public const string Const_Minute = "minute"; - - /// /// Function proper /// @@ -585,12 +569,6 @@ public static class Afe_Common /// public const string Const_Round = "round"; - - /// - /// Function time - /// - public const string Const_Time = "time"; - /// /// Function Exp /// @@ -606,11 +584,37 @@ public static class Afe_Common /// public const string Const_Find = "find"; + /// + /// Function Date + /// + public const string Const_Date = "date"; + public const string Const_DateValue = "datevalue"; + public const string Const_Time = "time"; + public const string Const_Second = "second"; + public const string Const_Minute = "minute"; + public const string Const_Hour = "hour"; + public const string Const_Day = "day"; + public const string Const_Month = "month"; + /// /// Function Year /// public const string Const_Year = "year"; + public const string Const_Now = "now"; + + /// + /// Function Today + /// + public const string Const_Today = "today"; + public const string Const_EDate = "edate"; + public const string Const_Weekday = "weekday"; + public const string Const_WeekNum = "weeknum"; + public const string Const_Workday = "workday"; + public const string Const_NetWorkdays = "networkdays"; + public const string Const_EOMonth = "eomonth"; + public const string Const_DateDif = "datedif"; + public const string Const_Days = "days"; #endregion diff --git a/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs b/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs new file mode 100644 index 0000000..a01420e --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs @@ -0,0 +1,62 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the year from a date + /// + public class dateFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List + { + new FunctionDef(Afe_Common.Const_Date, new Type[] { typeof(decimal), typeof(decimal), typeof(decimal) }, typeof(string), 3) + }; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var year = Afe_Common.ToInteger(args[Afe_Common.Const_Key_One], dc.WorkingCulture); + var month = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); + var day = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Three], dc.WorkingCulture); + var dateString = year.ToString(dc.WorkingCulture) + "-" + month.ToString(dc.WorkingCulture) + "-" + day.ToString(dc.WorkingCulture); + return Afe_Common.ToString(DateTime.Parse(dateString, dc.WorkingCulture).Date, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs index 1b14daa..97950cb 100644 --- a/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs @@ -40,7 +40,7 @@ public List GetInfo() { return new List { - new FunctionDef(Afe_Common.Const_Lower, new System.Type[] { typeof(string) }, typeof(int), 1) + new FunctionDef(Afe_Common.Const_Year, new Type[] { typeof(string) }, typeof(int), 1) }; } From 0203b3367591a7d2c41c018851a82106f68344c4 Mon Sep 17 00:00:00 2001 From: Andy Edmonds Date: Mon, 27 Feb 2023 10:59:02 -0500 Subject: [PATCH 04/14] clean up date function --- Math expression eval/org.matheval/Common/Afe_Common.cs | 2 +- .../org.matheval/Functions/Impl/dateFunction.cs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index fe9c074..cab60e3 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -779,7 +779,7 @@ public static decimal ToDecimal(object value, CultureInfo cultureInfo) /// /// /// - public static decimal ToInteger(object value, CultureInfo cultureInfo) + public static int ToInteger(object value, CultureInfo cultureInfo) { if (value is int) { diff --git a/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs b/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs index a01420e..38ce074 100644 --- a/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs @@ -55,8 +55,7 @@ public Object Execute(Dictionary args, ExpressionContext dc) var year = Afe_Common.ToInteger(args[Afe_Common.Const_Key_One], dc.WorkingCulture); var month = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); var day = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Three], dc.WorkingCulture); - var dateString = year.ToString(dc.WorkingCulture) + "-" + month.ToString(dc.WorkingCulture) + "-" + day.ToString(dc.WorkingCulture); - return Afe_Common.ToString(DateTime.Parse(dateString, dc.WorkingCulture).Date, dc.WorkingCulture); + return Afe_Common.ToString(new DateTime(year, month, day), dc.WorkingCulture); } } } From 6deaee9880e84f58672f74bb159d9d24685421af Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Fri, 3 Mar 2023 17:16:24 -0500 Subject: [PATCH 05/14] Weekend check point --- .vscode/launch.json | 26 ++++ .vscode/tasks.json | 41 ++++++ Math expression eval/ConsoleApp/Program.cs | 121 ++++++++++++++++++ .../org.matheval/Common/Afe_Common.cs | 42 +++++- .../Functions/Impl/SecondFunction.cs | 58 +++++++++ .../Functions/Impl/datevalueFunction.cs | 61 +++++++++ .../Functions/Impl/dayFunction.cs | 58 +++++++++ .../Functions/Impl/edateFunction .cs | 60 +++++++++ .../Functions/Impl/hourFunction.cs | 58 +++++++++ .../Functions/Impl/minuteFunction.cs | 58 +++++++++ .../Functions/Impl/monthFunction.cs | 58 +++++++++ .../Functions/Impl/networkdaysFunction.cs | 86 +++++++++++++ .../Functions/Impl/nowFunction.cs | 55 ++++++++ .../Functions/Impl/timeFunction.cs | 60 +++++++++ .../Functions/Impl/weekdayFunction .cs | 83 ++++++++++++ .../Functions/Impl/weeknumFunction.cs | 60 +++++++++ .../Functions/Impl/workdayFunction.cs | 72 +++++++++++ 17 files changed, 1055 insertions(+), 2 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/dayFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/edateFunction .cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/hourFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/monthFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/nowFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/timeFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..07a6339 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/Math expression eval/ConsoleApp/bin/Debug/net7.0/ConsoleApp.dll", + "args": [], + "cwd": "${workspaceFolder}/Math expression eval/ConsoleApp", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..594ec39 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Math expression eval/ConsoleApp/ConsoleApp.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Math expression eval/ConsoleApp/ConsoleApp.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/Math expression eval/ConsoleApp/ConsoleApp.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index de41754..5c46300 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -1,5 +1,126 @@ using org.matheval; +Console.WriteLine("date test"); Expression expression = new("date(2022, 3, 30)"); var val = expression.Eval(); +Console.WriteLine(val); +/* +Console.WriteLine("datevalue test"); +expression = new("datevalue('2023-01-15')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("datevalue('2023/01/15')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("datevalue('12-25-2022')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("datevalue('12/25/2022')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("Time test"); +expression = new("time(10,15,32)"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("time(23,59,59)"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("time(1111,59,59)"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("second test"); +expression = new("second('01:23:20')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("second('03/30/2022 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("minute test"); +expression = new("minute('01:23:20')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("minute('03/30/2022 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("hour test"); +expression = new("hour('01:23:20')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("hour('03/30/2022 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("day test"); +expression = new("day('03/15/2022')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("day('05/30/2022 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("month test"); +expression = new("month('03/15/2022')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("month('05/30/2022 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("now test"); +expression = new("now()"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("edate test"); +expression = new("edate('05/30/2022 04:50:45',5)"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("edate('05/30/2022',20)"); +val = expression.Eval(); +Console.WriteLine(val); + + +Console.WriteLine("weekday test"); +DateTime date = new DateTime(); +for (int i = 0; i < 8; i++) +{ + date = DateTime.UtcNow.AddDays(i); + expression = new("weekday('" + date.ToString() + "')"); + val = expression.Eval(); + Console.WriteLine(date.DayOfWeek +": "+ val.ToString()); +} + +Console.WriteLine("weeknum test"); +expression = new("weeknum('01/01/2022 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("weeknum('12/20/2022')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("workday test"); +expression = new("workday('01/01/2023 04:50:45',3)"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("workday('01/01/2023',20)"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("workday('01/01/2023',99)"); +val = expression.Eval(); +Console.WriteLine(val); +*/ + +Console.WriteLine("workday test"); +expression = new("networkdays('01/01/2023 04:50:45','01/25/2023 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("networkdays('01/01/2023 04:50:45','02/25/2023 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("networkdays('01/01/2023 04:50:45','01/01/2023 04:50:45')"); +val = expression.Eval(); Console.WriteLine(val); \ No newline at end of file diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index cab60e3..ff0d591 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -25,6 +25,7 @@ THE SOFTWARE. using System.Collections; using System.Collections.Generic; using System.Globalization; +using System.Linq.Expressions; using System.Text.RegularExpressions; namespace org.matheval.Common @@ -588,12 +589,33 @@ public static class Afe_Common /// Function Date /// public const string Const_Date = "date"; + /// + /// Function DateValue + /// public const string Const_DateValue = "datevalue"; + /// + /// Function Time + /// public const string Const_Time = "time"; + /// + /// Function Time + /// public const string Const_Second = "second"; + /// + /// Function Time + /// public const string Const_Minute = "minute"; + /// + /// Function Time + /// public const string Const_Hour = "hour"; + /// + /// Function Time + /// public const string Const_Day = "day"; + /// + /// Function Time + /// public const string Const_Month = "month"; /// @@ -601,16 +623,34 @@ public static class Afe_Common /// public const string Const_Year = "year"; + /// + /// Function Year + /// public const string Const_Now = "now"; /// /// Function Today /// public const string Const_Today = "today"; + /// + /// Function Today + /// public const string Const_EDate = "edate"; + /// + /// Function Today + /// public const string Const_Weekday = "weekday"; + /// + /// Function Today + /// public const string Const_WeekNum = "weeknum"; + /// + /// Function Today + /// public const string Const_Workday = "workday"; + /// + /// Function Today + /// public const string Const_NetWorkdays = "networkdays"; public const string Const_EOMonth = "eomonth"; public const string Const_DateDif = "datedif"; @@ -810,8 +850,6 @@ public static string ToString(object value, CultureInfo cultureInfo) } } - - /// /// Check object instance of List /// diff --git a/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs new file mode 100644 index 0000000..fcb7ac4 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs @@ -0,0 +1,58 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the second component of a Time, Datetime + /// + public class secondFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + return Afe_Common.ToString(dateTime.Second, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs new file mode 100644 index 0000000..2cf5ba1 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs @@ -0,0 +1,61 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace org.matheval.Functions +{ + /// + /// Returns the year from a date + /// + public class datevalueFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List + { + new FunctionDef(Afe_Common.Const_DateValue, new Type[] { typeof(string) }, typeof(string), 1) + }; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateString = Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture); + return Afe_Common.ToString(DateTime.Parse(dateString), dc.WorkingCulture); + + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs new file mode 100644 index 0000000..063a04a --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs @@ -0,0 +1,58 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the day component of a Date, Datetime + /// + public class dayFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + return Afe_Common.ToString(dateTime.Day, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/edateFunction .cs b/Math expression eval/org.matheval/Functions/Impl/edateFunction .cs new file mode 100644 index 0000000..c795253 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/edateFunction .cs @@ -0,0 +1,60 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Add month to Date, Datime + /// + public class edateFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List + { + new FunctionDef(Afe_Common.Const_Date, new Type[] { typeof(string), typeof(decimal) }, typeof(string), 2) + }; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var date = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var month = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); + return Afe_Common.ToString(date.AddMonths(month), dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs new file mode 100644 index 0000000..3c58e8e --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs @@ -0,0 +1,58 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the hour component of a Time, Datetime + /// + public class hourFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + return Afe_Common.ToString(dateTime.Hour, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs new file mode 100644 index 0000000..8f1fd29 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs @@ -0,0 +1,58 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the minute component of a Time, Datetime + /// + public class minuteFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + return Afe_Common.ToString(dateTime.Minute, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs b/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs new file mode 100644 index 0000000..f85059d --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs @@ -0,0 +1,58 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the month component of a Date, Datetime + /// + public class monthFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + return Afe_Common.ToString(dateTime.Month, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs new file mode 100644 index 0000000..ec4651d --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs @@ -0,0 +1,86 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace org.matheval.Functions +{ + /// + /// Returns the day of the week corresponding to a date. The day is given as an integer, ranging from 1 (Sunday) to 7 (Saturday) + /// + public class networkdaysFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string),typeof(string) }, typeof(string), 2)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var endDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_Two], dc.WorkingCulture)); + TimeSpan span = endDate - startDate; + + //adjusted from https://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates + int businessDays = span.Days + 1; + int fullWeekCount = businessDays / 7; + // find out if there are weekends + if (businessDays > fullWeekCount * 7) + { + int firstDayOfWeek = startDate.DayOfWeek == DayOfWeek.Sunday + ? 7 : (int)startDate.DayOfWeek; + int lastDayOfWeek = endDate.DayOfWeek == DayOfWeek.Sunday + ? 7 : (int)endDate.DayOfWeek; + if (lastDayOfWeek < firstDayOfWeek) + lastDayOfWeek += 7; + if (firstDayOfWeek <= 6) + { + if (lastDayOfWeek >= 7) + businessDays -= 2; + else if (lastDayOfWeek >= 6) + businessDays -= 1; + } + else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7) + businessDays -= 1; + } + //subtract the weekends during the full weeks in the interval + businessDays -= fullWeekCount + fullWeekCount; + return Afe_Common.ToInteger(businessDays, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/nowFunction.cs b/Math expression eval/org.matheval/Functions/Impl/nowFunction.cs new file mode 100644 index 0000000..880d443 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/nowFunction.cs @@ -0,0 +1,55 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the current Datetime based on a GMT calendar + /// + public class nowFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List { new FunctionDef(Afe_Common.Const_Not, new System.Type[] { }, typeof(Boolean), 0) }; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + return DateTime.UtcNow; + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/timeFunction.cs b/Math expression eval/org.matheval/Functions/Impl/timeFunction.cs new file mode 100644 index 0000000..f2fc8ed --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/timeFunction.cs @@ -0,0 +1,60 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Constructs a Time instance from Integer representations of the hour, minute, and second + /// + public class timeFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(decimal), typeof(decimal), typeof(decimal)}, typeof(string), 3)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var hours = Afe_Common.ToInteger(args[Afe_Common.Const_Key_One], dc.WorkingCulture); + var minutes = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); + var seconds = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Three], dc.WorkingCulture); + return Afe_Common.ToString(new TimeSpan(hours, minutes, seconds), dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs b/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs new file mode 100644 index 0000000..8cfe028 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs @@ -0,0 +1,83 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the day of the week corresponding to a date. The day is given as an integer, ranging from 1 (Sunday) to 7 (Saturday) + /// + public class weekdayFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + int day = 0; + switch (dateTime.DayOfWeek.ToString()) + { + case "Sunday": + day = 1; + break; + case "Monday": + day = 2; + break; + case "Tuesday": + day = 3; + break; + case "Wednesday": + day = 4; + break; + case "Thursday": + day = 5; + break; + case "Friday": + day = 6; + break; + case "Saturday": + day = 7; + break; + } + return Afe_Common.ToInteger(day, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs b/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs new file mode 100644 index 0000000..a40a59f --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs @@ -0,0 +1,60 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace org.matheval.Functions +{ + /// + /// Returns the day of the week corresponding to a date. The day is given as an integer, ranging from 1 (Sunday) to 7 (Saturday) + /// + public class weeknumFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var weekNum = dc.WorkingCulture.Calendar.GetWeekOfYear(dateTime, dc.WorkingCulture.DateTimeFormat.CalendarWeekRule, dc.WorkingCulture.DateTimeFormat.FirstDayOfWeek); + return Afe_Common.ToInteger(weekNum, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs new file mode 100644 index 0000000..edfac21 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs @@ -0,0 +1,72 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace org.matheval.Functions +{ + /// + /// Returns a number that represents a date that is the indicated number of working days before or after a date (the starting date). + /// + public class workdayFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string),typeof(decimal)}, typeof(string), 2)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var days = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); + DateTime finalDate = startDate; + int i = 0; + do + { + var dayOfWeek = startDate.DayOfWeek.ToString(); + if (dayOfWeek != "Saturday" && dayOfWeek != "Sunday") + { + finalDate = finalDate.AddDays(1); + i++; + } + startDate = startDate.AddDays(1); + } while (i < days); + return Afe_Common.ToString(finalDate, dc.WorkingCulture); + } + } +} From 00a438c45b1e7ec1758008a36a98cae11d5b5b5f Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Mon, 6 Mar 2023 11:13:42 -0500 Subject: [PATCH 06/14] Three New Functions for Date work --- Math expression eval/ConsoleApp/Program.cs | 55 +++++++++++++- .../org.matheval/Common/Afe_Common.cs | 10 ++- .../Functions/Impl/datediffFunction.cs | 75 +++++++++++++++++++ .../Functions/Impl/daysFunction.cs | 59 +++++++++++++++ .../{edateFunction .cs => edateFunction.cs} | 0 .../Functions/Impl/eomonthFunction.cs | 60 +++++++++++++++ .../Functions/Impl/workdayFunction.cs | 2 +- 7 files changed, 256 insertions(+), 5 deletions(-) create mode 100644 Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs create mode 100644 Math expression eval/org.matheval/Functions/Impl/daysFunction.cs rename Math expression eval/org.matheval/Functions/Impl/{edateFunction .cs => edateFunction.cs} (100%) create mode 100644 Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index 5c46300..b916a30 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -112,9 +112,8 @@ expression = new("workday('01/01/2023',99)"); val = expression.Eval(); Console.WriteLine(val); -*/ -Console.WriteLine("workday test"); +Console.WriteLine("networkday test"); expression = new("networkdays('01/01/2023 04:50:45','01/25/2023 04:50:45')"); val = expression.Eval(); Console.WriteLine(val); @@ -123,4 +122,54 @@ Console.WriteLine(val); expression = new("networkdays('01/01/2023 04:50:45','01/01/2023 04:50:45')"); val = expression.Eval(); -Console.WriteLine(val); \ No newline at end of file +Console.WriteLine(val); + +Console.WriteLine("eomonth test"); +expression = new("eomonth('01/01/2023 04:50:45',2)"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("eomonth('01/01/2023',2)"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("eomonth('01/01/2023 04:50:45',11)"); +val = expression.Eval(); +Console.WriteLine(val); + + + +Console.WriteLine("datediff test"); +expression = new("datediff('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("datediff('01/01/2023','01/01/2024','day')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("datediff('01/01/2023','04/08/2023','day')"); +val = expression.Eval(); +Console.WriteLine(val); + +expression = new("datediff('01/01/2023','03/03/2023','month')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("datediff('01/01/2023','04/25/2024','month')"); +val = expression.Eval(); +Console.WriteLine(val); + +expression = new("datediff('01/01/2023','08/02/2023','year')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("datediff('01/01/2023','04/25/2025','year')"); +val = expression.Eval(); +Console.WriteLine(val); + +Console.WriteLine("days test"); +expression = new("days('01/01/2023 04:50:45','01/05/2023 04:50:45')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("days('01/01/2023','01/01/2024')"); +val = expression.Eval(); +Console.WriteLine(val); +expression = new("days('01/01/2023','04/08/2023')"); +val = expression.Eval(); +Console.WriteLine(val); +*/ \ No newline at end of file diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index ff0d591..029ef56 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -25,7 +25,6 @@ THE SOFTWARE. using System.Collections; using System.Collections.Generic; using System.Globalization; -using System.Linq.Expressions; using System.Text.RegularExpressions; namespace org.matheval.Common @@ -652,8 +651,17 @@ public static class Afe_Common /// Function Today /// public const string Const_NetWorkdays = "networkdays"; + /// + /// Function Today + /// public const string Const_EOMonth = "eomonth"; + /// + /// Function Today + /// public const string Const_DateDif = "datedif"; + /// + /// Function Today + /// public const string Const_Days = "days"; #endregion diff --git a/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs new file mode 100644 index 0000000..7839edc --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs @@ -0,0 +1,75 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the day component of a Date, Datetime + /// + public class datediffFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string), typeof(string), typeof(string)}, typeof(string), 3)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var endDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_Two], dc.WorkingCulture)); + var unit = Afe_Common.ToString(args[Afe_Common.Const_Key_Three], dc.WorkingCulture); + return Afe_Common.ToString(SubtractDates(startDate, endDate, unit), dc.WorkingCulture); + } + + private static string SubtractDates(DateTime startDate, DateTime endDate, string unit) + { + switch (unit) + { + case "day": + return endDate.Subtract(startDate).TotalDays.ToString(); + case "month": + return (((endDate.Year - startDate.Year) * 12) + endDate.Month - startDate.Month).ToString(); + case "year": + return ((decimal)(((endDate.Year - startDate.Year) * 12) + endDate.Month - startDate.Month) / 12).ToString(".##"); + default: + return null; + } + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs new file mode 100644 index 0000000..ef1e2f4 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs @@ -0,0 +1,59 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; + +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Returns the day component of a Date, Datetime + /// + public class daysFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List{ + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string), typeof(string)}, typeof(string), 2)}; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var endDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_Two], dc.WorkingCulture)); + return Afe_Common.ToString(endDate.Subtract(startDate).TotalDays, dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/edateFunction .cs b/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs similarity index 100% rename from Math expression eval/org.matheval/Functions/Impl/edateFunction .cs rename to Math expression eval/org.matheval/Functions/Impl/edateFunction.cs diff --git a/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs new file mode 100644 index 0000000..c5c6ac2 --- /dev/null +++ b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs @@ -0,0 +1,60 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using org.matheval.Common; +using System; +using System.Collections.Generic; + +namespace org.matheval.Functions +{ + /// + /// Extracts a given number of characters + /// from the middle of a supplied text string + /// MID("abcd",1,2) -> ab + /// + public class eomonthFunction : IFunction + { + /// + /// Get Information + /// + /// FunctionDefs + public List GetInfo() + { + return new List { new FunctionDef(Afe_Common.Const_MID, new System.Type[] { typeof(string), typeof(decimal) }, typeof(string), 2) }; + } + + /// + /// Execute + /// + /// args + /// dc + /// Value + public Object Execute(Dictionary args, ExpressionContext dc) + { + var date = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var months = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); + var dateMonths = date.AddMonths(months); + return Afe_Common.ToString(new DateTime(dateMonths.Year, dateMonths.Month, DateTime.DaysInMonth(dateMonths.Year, dateMonths.Month)), dc.WorkingCulture); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs index edfac21..94e0332 100644 --- a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs @@ -66,7 +66,7 @@ public Object Execute(Dictionary args, ExpressionContext dc) } startDate = startDate.AddDays(1); } while (i < days); - return Afe_Common.ToString(finalDate, dc.WorkingCulture); + return Afe_Common.ToInteger(finalDate, dc.WorkingCulture); } } } From 72f2d430f0d65aeebec176ffde4d23f666cc21ab Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Mon, 6 Mar 2023 17:37:48 -0500 Subject: [PATCH 07/14] EOD checkpoint - Fix return types, Unit tests --- Math expression eval/ConsoleApp/Program.cs | 8 +-- .../UnitTest/dateFunctionTest.cs | 39 +++++++++++ .../UnitTest/datediffFunctionTest.cs | 67 +++++++++++++++++++ .../UnitTest/datevalueFunctionTest.cs | 42 ++++++++++++ .../UnitTest/dayFunctionTest.cs | 43 ++++++++++++ .../UnitTest/daysFunctionTest.cs | 42 ++++++++++++ .../UnitTest/edateFunctionTest.cs | 44 ++++++++++++ .../UnitTest/eomonthFunctionTest.cs | 43 ++++++++++++ .../UnitTest/hourFunctionTest.cs | 41 ++++++++++++ .../UnitTest/networkdayFunctionTest.cs | 43 ++++++++++++ .../UnitTest/nowFunctionTest.cs | 40 +++++++++++ .../UnitTest/secondFunctionTest.cs | 43 ++++++++++++ .../UnitTest/timeFunctionTest.cs | 39 +++++++++++ .../UnitTest/weekdayFunctionTest.cs | 46 +++++++++++++ .../UnitTest/weeknumberFunctionTest.cs | 43 ++++++++++++ .../UnitTest/workdayFunctionTest.cs | 42 ++++++++++++ .../UnitTest/yearFunctionTest.cs | 39 +++++++++++ .../Functions/Impl/SecondFunction.cs | 2 +- .../Functions/Impl/datediffFunction.cs | 2 +- .../Functions/Impl/dayFunction.cs | 2 +- .../Functions/Impl/daysFunction.cs | 2 +- .../Functions/Impl/hourFunction.cs | 2 +- .../Functions/Impl/workdayFunction.cs | 8 +-- 23 files changed, 707 insertions(+), 15 deletions(-) create mode 100644 Math expression eval/UnitTest/dateFunctionTest.cs create mode 100644 Math expression eval/UnitTest/datediffFunctionTest.cs create mode 100644 Math expression eval/UnitTest/datevalueFunctionTest.cs create mode 100644 Math expression eval/UnitTest/dayFunctionTest.cs create mode 100644 Math expression eval/UnitTest/daysFunctionTest.cs create mode 100644 Math expression eval/UnitTest/edateFunctionTest.cs create mode 100644 Math expression eval/UnitTest/eomonthFunctionTest.cs create mode 100644 Math expression eval/UnitTest/hourFunctionTest.cs create mode 100644 Math expression eval/UnitTest/networkdayFunctionTest.cs create mode 100644 Math expression eval/UnitTest/nowFunctionTest.cs create mode 100644 Math expression eval/UnitTest/secondFunctionTest.cs create mode 100644 Math expression eval/UnitTest/timeFunctionTest.cs create mode 100644 Math expression eval/UnitTest/weekdayFunctionTest.cs create mode 100644 Math expression eval/UnitTest/weeknumberFunctionTest.cs create mode 100644 Math expression eval/UnitTest/workdayFunctionTest.cs create mode 100644 Math expression eval/UnitTest/yearFunctionTest.cs diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index b916a30..f06be2f 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -4,7 +4,8 @@ Expression expression = new("date(2022, 3, 30)"); var val = expression.Eval(); Console.WriteLine(val); -/* + + Console.WriteLine("datevalue test"); expression = new("datevalue('2023-01-15')"); val = expression.Eval(); @@ -135,8 +136,6 @@ val = expression.Eval(); Console.WriteLine(val); - - Console.WriteLine("datediff test"); expression = new("datediff('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); val = expression.Eval(); @@ -155,7 +154,7 @@ val = expression.Eval(); Console.WriteLine(val); -expression = new("datediff('01/01/2023','08/02/2023','year')"); +expression = new("datediff('01/01/2023','07/01/2023','year')"); val = expression.Eval(); Console.WriteLine(val); expression = new("datediff('01/01/2023','04/25/2025','year')"); @@ -172,4 +171,3 @@ expression = new("days('01/01/2023','04/08/2023')"); val = expression.Eval(); Console.WriteLine(val); -*/ \ No newline at end of file diff --git a/Math expression eval/UnitTest/dateFunctionTest.cs b/Math expression eval/UnitTest/dateFunctionTest.cs new file mode 100644 index 0000000..7155129 --- /dev/null +++ b/Math expression eval/UnitTest/dateFunctionTest.cs @@ -0,0 +1,39 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class dateFunctionTest + { + [TestMethod] + public void ConvertDates() + { + Expression expr1 = new Expression("date(2022, 3, 30)"); + Assert.AreEqual("03/30/2022 00:00:00", expr1.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/datediffFunctionTest.cs b/Math expression eval/UnitTest/datediffFunctionTest.cs new file mode 100644 index 0000000..72f2ae1 --- /dev/null +++ b/Math expression eval/UnitTest/datediffFunctionTest.cs @@ -0,0 +1,67 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class datediffFunctionTest + { + [TestMethod] + public void DayDifference() + { + Expression expression = new Expression("datediff('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); + Assert.AreEqual((decimal)35, expression.Eval()); + + expression = new Expression("datediff('01/01/2023','01/01/2024','day')"); + Assert.AreEqual((decimal)365, expression.Eval()); + + expression = new Expression("datediff('01/01/2023','04/11/2023','day')"); + Assert.AreEqual((decimal)100, expression.Eval()); + } + + [TestMethod] + public void MonthDifference() + { + Expression expression = new Expression("datediff('01/01/2023','03/03/2023','month')"); + Assert.AreEqual((decimal)2, expression.Eval()); + + expression = new Expression("datediff('01/01/2023','04/25/2024','month')"); + Assert.AreEqual((decimal)15, expression.Eval()); + } + + [TestMethod] + public void YearDifference() + { + Expression expression = new Expression("datediff('01/01/2023','07/01/2023','year')"); + Assert.AreEqual((decimal).50, expression.Eval()); + + expression = new Expression("datediff('01/01/2023','04/25/2025','year')"); + Assert.AreEqual((decimal)2.25, expression.Eval()); + } + } +} + diff --git a/Math expression eval/UnitTest/datevalueFunctionTest.cs b/Math expression eval/UnitTest/datevalueFunctionTest.cs new file mode 100644 index 0000000..0c56982 --- /dev/null +++ b/Math expression eval/UnitTest/datevalueFunctionTest.cs @@ -0,0 +1,42 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class datevalueFunctionTest + { + [TestMethod] + public void DateValueTest() + { + Expression epression = new Expression("datevalue('2023-01-01')"); + Assert.AreEqual("01/01/2023 00:00:00", epression.Eval()); + + epression = new Expression("datevalue('2023/09/22')"); + Assert.AreEqual("09/22/2023 00:00:00", epression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/dayFunctionTest.cs b/Math expression eval/UnitTest/dayFunctionTest.cs new file mode 100644 index 0000000..6bd9073 --- /dev/null +++ b/Math expression eval/UnitTest/dayFunctionTest.cs @@ -0,0 +1,43 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class dayFunctionTest + { + [TestMethod] + public void ReturnDays() + { + Expression expression = new Expression("day('03/15/2022')"); + Assert.AreEqual(15, expression.Eval()); + + expression = new Expression("day('05/30/2022 04:50:45')"); + Assert.AreEqual(30, expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/daysFunctionTest.cs b/Math expression eval/UnitTest/daysFunctionTest.cs new file mode 100644 index 0000000..0f9fed3 --- /dev/null +++ b/Math expression eval/UnitTest/daysFunctionTest.cs @@ -0,0 +1,42 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class daysFunctionTest + { + [TestMethod] + public void ReturnDaysBetweenDates() + { + Expression expression = new Expression("days('01/01/2023 04:50:45','01/05/2023 04:50:45')"); + Assert.AreEqual(4, expression.Eval()); + + expression = new Expression("days('01/01/2023','01/01/2024')"); + Assert.AreEqual(365, expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/edateFunctionTest.cs b/Math expression eval/UnitTest/edateFunctionTest.cs new file mode 100644 index 0000000..89a27ed --- /dev/null +++ b/Math expression eval/UnitTest/edateFunctionTest.cs @@ -0,0 +1,44 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class edateFunctionTest + { + [TestMethod] + public void ReturnDate() + { + Expression expression = new Expression("edate('05/30/2022 04:50:45',5)"); + Assert.AreEqual("10/30/2022 04:50:45", expression.Eval()); + + expression = new Expression("edate('05/30/2022',15)"); + Assert.AreEqual("08/30/2023 00:00:00", expression.Eval()); + } + } +} + diff --git a/Math expression eval/UnitTest/eomonthFunctionTest.cs b/Math expression eval/UnitTest/eomonthFunctionTest.cs new file mode 100644 index 0000000..992f6fc --- /dev/null +++ b/Math expression eval/UnitTest/eomonthFunctionTest.cs @@ -0,0 +1,43 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class eomonthFunctionTest + { + [TestMethod] + public void ReturnEndOfMonth() + { + + Expression expression = new Expression("eomonth('01/01/2023',2)"); + Assert.AreEqual("03/31/2023 00:00:00", expression.Eval()); + + expression = new Expression("eomonth('01/01/2023',12)"); + Assert.AreEqual("01/31/2024 00:00:00", expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/hourFunctionTest.cs b/Math expression eval/UnitTest/hourFunctionTest.cs new file mode 100644 index 0000000..3ad970c --- /dev/null +++ b/Math expression eval/UnitTest/hourFunctionTest.cs @@ -0,0 +1,41 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class hourFunctionTest + { + [TestMethod] + public void ReturnHour() + { + Expression expression = new Expression("hour('01:23:20')"); + Assert.AreEqual(01, expression.Eval()); + expression = new Expression("hour('03/30/2022 04:50:45')"); + Assert.AreEqual(04, expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/networkdayFunctionTest.cs b/Math expression eval/UnitTest/networkdayFunctionTest.cs new file mode 100644 index 0000000..d786b9b --- /dev/null +++ b/Math expression eval/UnitTest/networkdayFunctionTest.cs @@ -0,0 +1,43 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class networkdayFunctionTest + { + [TestMethod] + public void ReturnWorkDays() + { + Expression expression = new Expression("networkdays('01/01/2023 04:50:45','01/22/2023 04:50:45')"); + Assert.AreEqual(15, expression.Eval()); + + expression = new Expression("networkdays('01/01/2023 04:50:45','01/01/2023 04:50:45')"); + Assert.AreEqual(0, expression.Eval()); + + } + } +} diff --git a/Math expression eval/UnitTest/nowFunctionTest.cs b/Math expression eval/UnitTest/nowFunctionTest.cs new file mode 100644 index 0000000..817d16b --- /dev/null +++ b/Math expression eval/UnitTest/nowFunctionTest.cs @@ -0,0 +1,40 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class nowFunctionTest + { + [TestMethod] + public void ReturnCurrentDate() + { + Expression expression = new Expression("now()"); + Assert.AreEqual(DateTime.UtcNow.Date, DateTime.Parse(expression.Eval().ToString()).Date); + } + } +} diff --git a/Math expression eval/UnitTest/secondFunctionTest.cs b/Math expression eval/UnitTest/secondFunctionTest.cs new file mode 100644 index 0000000..1c2ac37 --- /dev/null +++ b/Math expression eval/UnitTest/secondFunctionTest.cs @@ -0,0 +1,43 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class secondFunctionTest + { + [TestMethod] + public void ReturnSeconds() + { + Expression expression = new Expression("second('01:23:20')"); + Assert.AreEqual(20, expression.Eval()); + + expression = new Expression("second('03/30/2022 04:05:45')"); + Assert.AreEqual(45, expression.Eval()); + + } + } +} diff --git a/Math expression eval/UnitTest/timeFunctionTest.cs b/Math expression eval/UnitTest/timeFunctionTest.cs new file mode 100644 index 0000000..94978da --- /dev/null +++ b/Math expression eval/UnitTest/timeFunctionTest.cs @@ -0,0 +1,39 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class timeFunctionTest + { + [TestMethod] + public void ConvertDates() + { + Expression expression = new Expression("time(10,15,32)"); + Assert.AreEqual("10:15:32", expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/weekdayFunctionTest.cs b/Math expression eval/UnitTest/weekdayFunctionTest.cs new file mode 100644 index 0000000..cd93048 --- /dev/null +++ b/Math expression eval/UnitTest/weekdayFunctionTest.cs @@ -0,0 +1,46 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class weekdayFunctionTest + { + [TestMethod] + public void ReturnWeekDay() + { + DateTime date = new DateTime(2023,01,01);//sunday + Expression expression = new Expression(); + for (int i = 1; i <= 7; i++) + { + expression = new Expression("weekday('" + date.ToString() + "')"); + Assert.AreEqual(i, expression.Eval()); + date = date.AddDays(1); + } + } + } +} diff --git a/Math expression eval/UnitTest/weeknumberFunctionTest.cs b/Math expression eval/UnitTest/weeknumberFunctionTest.cs new file mode 100644 index 0000000..7b4a957 --- /dev/null +++ b/Math expression eval/UnitTest/weeknumberFunctionTest.cs @@ -0,0 +1,43 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class weeknumberFunctionTest + { + [TestMethod] + public void ReturnWeekNumber() + { + Expression expression = new Expression("weeknum('01/01/2022 04:50:45')"); + Assert.AreEqual(1, expression.Eval()); + + expression = new Expression("weeknum('12/20/2022 04:50:45')"); + Assert.AreEqual(52, expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/workdayFunctionTest.cs b/Math expression eval/UnitTest/workdayFunctionTest.cs new file mode 100644 index 0000000..ba1a43a --- /dev/null +++ b/Math expression eval/UnitTest/workdayFunctionTest.cs @@ -0,0 +1,42 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class workdayFunctionTest + { + [TestMethod] + public void ReturnWorkDayDate() + { + Expression expression = new Expression("workday('01/01/2023 04:50:45',3)"); + Assert.AreEqual("01/05/2023 04:50:45", expression.Eval()); + + expression = new Expression("workday('01/01/2023',7)"); + Assert.AreEqual("01/11/2023 00:00:00", expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/yearFunctionTest.cs b/Math expression eval/UnitTest/yearFunctionTest.cs new file mode 100644 index 0000000..afe6cd3 --- /dev/null +++ b/Math expression eval/UnitTest/yearFunctionTest.cs @@ -0,0 +1,39 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; + +namespace UnitTest +{ + [TestClass] + public class yearFunctionTest + { + [TestMethod] + public void ReturnYear() + { + Expression expr1 = new Expression("year('05/30/2022 04:50:45')"); + Assert.AreEqual(2022, expr1.Eval()); + } + } +} diff --git a/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs index fcb7ac4..9f2c6c1 100644 --- a/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() public Object Execute(Dictionary args, ExpressionContext dc) { var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - return Afe_Common.ToString(dateTime.Second, dc.WorkingCulture); + return Afe_Common.ToInteger(dateTime.Second, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs index 7839edc..e5a72c5 100644 --- a/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs @@ -54,7 +54,7 @@ public Object Execute(Dictionary args, ExpressionContext dc) var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); var endDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_Two], dc.WorkingCulture)); var unit = Afe_Common.ToString(args[Afe_Common.Const_Key_Three], dc.WorkingCulture); - return Afe_Common.ToString(SubtractDates(startDate, endDate, unit), dc.WorkingCulture); + return Afe_Common.ToDecimal(SubtractDates(startDate, endDate, unit), dc.WorkingCulture); } private static string SubtractDates(DateTime startDate, DateTime endDate, string unit) diff --git a/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs index 063a04a..4246d40 100644 --- a/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() public Object Execute(Dictionary args, ExpressionContext dc) { var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - return Afe_Common.ToString(dateTime.Day, dc.WorkingCulture); + return Afe_Common.ToInteger(dateTime.Day, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs index ef1e2f4..bc0ba1c 100644 --- a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs @@ -53,7 +53,7 @@ public Object Execute(Dictionary args, ExpressionContext dc) { var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); var endDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_Two], dc.WorkingCulture)); - return Afe_Common.ToString(endDate.Subtract(startDate).TotalDays, dc.WorkingCulture); + return Afe_Common.ToInteger(endDate.Subtract(startDate).TotalDays, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs index 3c58e8e..330d0d1 100644 --- a/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() public Object Execute(Dictionary args, ExpressionContext dc) { var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - return Afe_Common.ToString(dateTime.Hour, dc.WorkingCulture); + return Afe_Common.ToInteger(dateTime.Hour, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs index 94e0332..b538dfe 100644 --- a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs @@ -54,19 +54,17 @@ public Object Execute(Dictionary args, ExpressionContext dc) { var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); var days = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); - DateTime finalDate = startDate; int i = 0; - do + while (i < days) { var dayOfWeek = startDate.DayOfWeek.ToString(); if (dayOfWeek != "Saturday" && dayOfWeek != "Sunday") { - finalDate = finalDate.AddDays(1); i++; } startDate = startDate.AddDays(1); - } while (i < days); - return Afe_Common.ToInteger(finalDate, dc.WorkingCulture); + } + return Afe_Common.ToString(startDate, dc.WorkingCulture); } } } From 9788fecf722fd39db5e4e9809b21cee5fdfaefca Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Tue, 7 Mar 2023 09:37:06 -0500 Subject: [PATCH 08/14] Fix some return types, Add unit tests for Minute/Month/Today, Make WorkDayFunction handle negative workdays, Rename all the things --- Math expression eval/ConsoleApp/Program.cs | 3 ++ .../UnitTest/MinuteFunctionTest.cs | 43 +++++++++++++++++++ .../UnitTest/MonthFunctionTest.cs | 43 +++++++++++++++++++ .../UnitTest/TodayFunctionTest.cs | 40 +++++++++++++++++ .../UnitTest/dateFunctionTest.cs | 4 +- .../UnitTest/datediffFunctionTest.cs | 2 +- .../UnitTest/datevalueFunctionTest.cs | 4 +- .../UnitTest/dayFunctionTest.cs | 2 +- .../UnitTest/daysFunctionTest.cs | 4 +- .../UnitTest/edateFunctionTest.cs | 14 +++++- .../UnitTest/eomonthFunctionTest.cs | 5 +-- .../UnitTest/hourFunctionTest.cs | 4 +- .../UnitTest/networkdayFunctionTest.cs | 5 +-- .../UnitTest/nowFunctionTest.cs | 2 +- .../UnitTest/secondFunctionTest.cs | 2 +- .../UnitTest/timeFunctionTest.cs | 4 +- .../UnitTest/weekdayFunctionTest.cs | 4 +- .../UnitTest/weeknumberFunctionTest.cs | 4 +- .../UnitTest/workdayFunctionTest.cs | 13 +++++- .../UnitTest/yearFunctionTest.cs | 2 +- .../Functions/Impl/minuteFunction.cs | 2 +- .../Functions/Impl/monthFunction.cs | 2 +- .../Functions/Impl/workdayFunction.cs | 10 ++--- 23 files changed, 182 insertions(+), 36 deletions(-) create mode 100644 Math expression eval/UnitTest/MinuteFunctionTest.cs create mode 100644 Math expression eval/UnitTest/MonthFunctionTest.cs create mode 100644 Math expression eval/UnitTest/TodayFunctionTest.cs diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index f06be2f..7db0cf4 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -107,6 +107,9 @@ expression = new("workday('01/01/2023 04:50:45',3)"); val = expression.Eval(); Console.WriteLine(val); +expression = new("workday('01/01/2023 04:50:45',-3)"); +val = expression.Eval(); +Console.WriteLine(val); expression = new("workday('01/01/2023',20)"); val = expression.Eval(); Console.WriteLine(val); diff --git a/Math expression eval/UnitTest/MinuteFunctionTest.cs b/Math expression eval/UnitTest/MinuteFunctionTest.cs new file mode 100644 index 0000000..f546fc3 --- /dev/null +++ b/Math expression eval/UnitTest/MinuteFunctionTest.cs @@ -0,0 +1,43 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class MinuteFunctionTest + { + [TestMethod] + public void ReturnMinutes() + { + Expression expression = new Expression("minute('01:23:20')"); ; + Assert.AreEqual(23, expression.Eval()); + + expression = new Expression("minute('03/30/2022 04:50:45')"); + Assert.AreEqual(50, expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/MonthFunctionTest.cs b/Math expression eval/UnitTest/MonthFunctionTest.cs new file mode 100644 index 0000000..8e351b7 --- /dev/null +++ b/Math expression eval/UnitTest/MonthFunctionTest.cs @@ -0,0 +1,43 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class MonthFunctionTest + { + [TestMethod] + public void ReturnMonths() + { + Expression expression = new Expression("month('03/15/2022')"); + Assert.AreEqual(03, expression.Eval()); + + expression = new Expression("month('05/30/2022 04:50:45')"); + Assert.AreEqual(05, expression.Eval()); + } + } +} diff --git a/Math expression eval/UnitTest/TodayFunctionTest.cs b/Math expression eval/UnitTest/TodayFunctionTest.cs new file mode 100644 index 0000000..0ecf81d --- /dev/null +++ b/Math expression eval/UnitTest/TodayFunctionTest.cs @@ -0,0 +1,40 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using System; + +namespace UnitTest +{ + [TestClass] + public class TodayFunctionTest + { + [TestMethod] + public void ReturnCurrentDate() + { + Expression expression = new Expression("Today()"); + Assert.AreEqual(DateTime.Today.Date, DateTime.Parse(expression.Eval().ToString()).Date); + } + } +} diff --git a/Math expression eval/UnitTest/dateFunctionTest.cs b/Math expression eval/UnitTest/dateFunctionTest.cs index 7155129..b2e1ee8 100644 --- a/Math expression eval/UnitTest/dateFunctionTest.cs +++ b/Math expression eval/UnitTest/dateFunctionTest.cs @@ -27,10 +27,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class dateFunctionTest + public class DateFunctionTest { [TestMethod] - public void ConvertDates() + public void CreateDate() { Expression expr1 = new Expression("date(2022, 3, 30)"); Assert.AreEqual("03/30/2022 00:00:00", expr1.Eval()); diff --git a/Math expression eval/UnitTest/datediffFunctionTest.cs b/Math expression eval/UnitTest/datediffFunctionTest.cs index 72f2ae1..a5c41d1 100644 --- a/Math expression eval/UnitTest/datediffFunctionTest.cs +++ b/Math expression eval/UnitTest/datediffFunctionTest.cs @@ -28,7 +28,7 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class datediffFunctionTest + public class DateDiffFunctionTest { [TestMethod] public void DayDifference() diff --git a/Math expression eval/UnitTest/datevalueFunctionTest.cs b/Math expression eval/UnitTest/datevalueFunctionTest.cs index 0c56982..cf00631 100644 --- a/Math expression eval/UnitTest/datevalueFunctionTest.cs +++ b/Math expression eval/UnitTest/datevalueFunctionTest.cs @@ -27,10 +27,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class datevalueFunctionTest + public class DateValueFunctionTest { [TestMethod] - public void DateValueTest() + public void CreateDateFromString() { Expression epression = new Expression("datevalue('2023-01-01')"); Assert.AreEqual("01/01/2023 00:00:00", epression.Eval()); diff --git a/Math expression eval/UnitTest/dayFunctionTest.cs b/Math expression eval/UnitTest/dayFunctionTest.cs index 6bd9073..ded3f12 100644 --- a/Math expression eval/UnitTest/dayFunctionTest.cs +++ b/Math expression eval/UnitTest/dayFunctionTest.cs @@ -28,7 +28,7 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class dayFunctionTest + public class DayFunctionTest { [TestMethod] public void ReturnDays() diff --git a/Math expression eval/UnitTest/daysFunctionTest.cs b/Math expression eval/UnitTest/daysFunctionTest.cs index 0f9fed3..9879035 100644 --- a/Math expression eval/UnitTest/daysFunctionTest.cs +++ b/Math expression eval/UnitTest/daysFunctionTest.cs @@ -27,10 +27,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class daysFunctionTest + public class DaysFunctionTest { [TestMethod] - public void ReturnDaysBetweenDates() + public void DaysBetweenDates() { Expression expression = new Expression("days('01/01/2023 04:50:45','01/05/2023 04:50:45')"); Assert.AreEqual(4, expression.Eval()); diff --git a/Math expression eval/UnitTest/edateFunctionTest.cs b/Math expression eval/UnitTest/edateFunctionTest.cs index 89a27ed..e7c96f4 100644 --- a/Math expression eval/UnitTest/edateFunctionTest.cs +++ b/Math expression eval/UnitTest/edateFunctionTest.cs @@ -28,10 +28,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class edateFunctionTest + public class EDateFunctionTest { [TestMethod] - public void ReturnDate() + public void AddMonthsToDate() { Expression expression = new Expression("edate('05/30/2022 04:50:45',5)"); Assert.AreEqual("10/30/2022 04:50:45", expression.Eval()); @@ -39,6 +39,16 @@ public void ReturnDate() expression = new Expression("edate('05/30/2022',15)"); Assert.AreEqual("08/30/2023 00:00:00", expression.Eval()); } + + [TestMethod] + public void SubtractMonthsFromDate() + { + Expression expression = new Expression("edate('05/30/2022 04:50:45',-5)"); + Assert.AreEqual("12/30/2021 04:50:45", expression.Eval()); + + expression = new Expression("edate('05/30/2022',-1)"); + Assert.AreEqual("04/30/2022 00:00:00", expression.Eval()); + } } } diff --git a/Math expression eval/UnitTest/eomonthFunctionTest.cs b/Math expression eval/UnitTest/eomonthFunctionTest.cs index 992f6fc..4b2d1da 100644 --- a/Math expression eval/UnitTest/eomonthFunctionTest.cs +++ b/Math expression eval/UnitTest/eomonthFunctionTest.cs @@ -27,12 +27,11 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class eomonthFunctionTest + public class EMonthFunctionTest { [TestMethod] - public void ReturnEndOfMonth() + public void EndOfMonth() { - Expression expression = new Expression("eomonth('01/01/2023',2)"); Assert.AreEqual("03/31/2023 00:00:00", expression.Eval()); diff --git a/Math expression eval/UnitTest/hourFunctionTest.cs b/Math expression eval/UnitTest/hourFunctionTest.cs index 3ad970c..5ed13d6 100644 --- a/Math expression eval/UnitTest/hourFunctionTest.cs +++ b/Math expression eval/UnitTest/hourFunctionTest.cs @@ -27,10 +27,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class hourFunctionTest + public class HourFunctionTest { [TestMethod] - public void ReturnHour() + public void ReturnHours() { Expression expression = new Expression("hour('01:23:20')"); Assert.AreEqual(01, expression.Eval()); diff --git a/Math expression eval/UnitTest/networkdayFunctionTest.cs b/Math expression eval/UnitTest/networkdayFunctionTest.cs index d786b9b..2740692 100644 --- a/Math expression eval/UnitTest/networkdayFunctionTest.cs +++ b/Math expression eval/UnitTest/networkdayFunctionTest.cs @@ -27,17 +27,16 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class networkdayFunctionTest + public class NetWorkDayFunctionTest { [TestMethod] - public void ReturnWorkDays() + public void CalculateWorkDaysBetweenDates() { Expression expression = new Expression("networkdays('01/01/2023 04:50:45','01/22/2023 04:50:45')"); Assert.AreEqual(15, expression.Eval()); expression = new Expression("networkdays('01/01/2023 04:50:45','01/01/2023 04:50:45')"); Assert.AreEqual(0, expression.Eval()); - } } } diff --git a/Math expression eval/UnitTest/nowFunctionTest.cs b/Math expression eval/UnitTest/nowFunctionTest.cs index 817d16b..225bac3 100644 --- a/Math expression eval/UnitTest/nowFunctionTest.cs +++ b/Math expression eval/UnitTest/nowFunctionTest.cs @@ -28,7 +28,7 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class nowFunctionTest + public class NowFunctionTest { [TestMethod] public void ReturnCurrentDate() diff --git a/Math expression eval/UnitTest/secondFunctionTest.cs b/Math expression eval/UnitTest/secondFunctionTest.cs index 1c2ac37..34a0f4e 100644 --- a/Math expression eval/UnitTest/secondFunctionTest.cs +++ b/Math expression eval/UnitTest/secondFunctionTest.cs @@ -27,7 +27,7 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class secondFunctionTest + public class SecondFunctionTest { [TestMethod] public void ReturnSeconds() diff --git a/Math expression eval/UnitTest/timeFunctionTest.cs b/Math expression eval/UnitTest/timeFunctionTest.cs index 94978da..9d72436 100644 --- a/Math expression eval/UnitTest/timeFunctionTest.cs +++ b/Math expression eval/UnitTest/timeFunctionTest.cs @@ -27,10 +27,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class timeFunctionTest + public class TimeFunctionTest { [TestMethod] - public void ConvertDates() + public void ReturnTime() { Expression expression = new Expression("time(10,15,32)"); Assert.AreEqual("10:15:32", expression.Eval()); diff --git a/Math expression eval/UnitTest/weekdayFunctionTest.cs b/Math expression eval/UnitTest/weekdayFunctionTest.cs index cd93048..0fc1e86 100644 --- a/Math expression eval/UnitTest/weekdayFunctionTest.cs +++ b/Math expression eval/UnitTest/weekdayFunctionTest.cs @@ -28,10 +28,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class weekdayFunctionTest + public class WeekDayFunctionTest { [TestMethod] - public void ReturnWeekDay() + public void ReturnDayOfWeekNumber() { DateTime date = new DateTime(2023,01,01);//sunday Expression expression = new Expression(); diff --git a/Math expression eval/UnitTest/weeknumberFunctionTest.cs b/Math expression eval/UnitTest/weeknumberFunctionTest.cs index 7b4a957..7b5ca1b 100644 --- a/Math expression eval/UnitTest/weeknumberFunctionTest.cs +++ b/Math expression eval/UnitTest/weeknumberFunctionTest.cs @@ -28,10 +28,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class weeknumberFunctionTest + public class WeekNumberFunctionTest { [TestMethod] - public void ReturnWeekNumber() + public void CalculatesWeekNumber() { Expression expression = new Expression("weeknum('01/01/2022 04:50:45')"); Assert.AreEqual(1, expression.Eval()); diff --git a/Math expression eval/UnitTest/workdayFunctionTest.cs b/Math expression eval/UnitTest/workdayFunctionTest.cs index ba1a43a..5e78682 100644 --- a/Math expression eval/UnitTest/workdayFunctionTest.cs +++ b/Math expression eval/UnitTest/workdayFunctionTest.cs @@ -27,10 +27,10 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class workdayFunctionTest + public class WorkDayFunctionTest { [TestMethod] - public void ReturnWorkDayDate() + public void ReturnDatePositiveWorkDay() { Expression expression = new Expression("workday('01/01/2023 04:50:45',3)"); Assert.AreEqual("01/05/2023 04:50:45", expression.Eval()); @@ -38,5 +38,14 @@ public void ReturnWorkDayDate() expression = new Expression("workday('01/01/2023',7)"); Assert.AreEqual("01/11/2023 00:00:00", expression.Eval()); } + [TestMethod] + public void ReturnDateNegativeWorkDay() + { + Expression expression = new Expression("workday('01/01/2023 04:50:45',-3)"); + Assert.AreEqual("12/27/2022 04:50:45", expression.Eval()); + + expression = new Expression("workday('01/01/2023',-7)"); + Assert.AreEqual("12/21/2022 00:00:00", expression.Eval()); + } } } diff --git a/Math expression eval/UnitTest/yearFunctionTest.cs b/Math expression eval/UnitTest/yearFunctionTest.cs index afe6cd3..1eb2021 100644 --- a/Math expression eval/UnitTest/yearFunctionTest.cs +++ b/Math expression eval/UnitTest/yearFunctionTest.cs @@ -27,7 +27,7 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class yearFunctionTest + public class YearFunctionTest { [TestMethod] public void ReturnYear() diff --git a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs index 8f1fd29..116f56c 100644 --- a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() public Object Execute(Dictionary args, ExpressionContext dc) { var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - return Afe_Common.ToString(dateTime.Minute, dc.WorkingCulture); + return Afe_Common.ToInteger(dateTime.Minute, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs b/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs index f85059d..b43d765 100644 --- a/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() public Object Execute(Dictionary args, ExpressionContext dc) { var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - return Afe_Common.ToString(dateTime.Month, dc.WorkingCulture); + return Afe_Common.ToInteger(dateTime.Month, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs index b538dfe..9df1397 100644 --- a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs @@ -52,19 +52,19 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var date = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); var days = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); int i = 0; - while (i < days) + while (i < Math.Abs(days)) { - var dayOfWeek = startDate.DayOfWeek.ToString(); + var dayOfWeek = date.DayOfWeek.ToString(); if (dayOfWeek != "Saturday" && dayOfWeek != "Sunday") { i++; } - startDate = startDate.AddDays(1); + date = days >= 0 ? date.AddDays(1) : date.AddDays(-1); } - return Afe_Common.ToString(startDate, dc.WorkingCulture); + return Afe_Common.ToString(date, dc.WorkingCulture); } } } From 632baa12e63e43d896cc81b1afda482a459ae251 Mon Sep 17 00:00:00 2001 From: Andy Edmonds Date: Tue, 7 Mar 2023 14:48:17 -0500 Subject: [PATCH 09/14] fix issue with nesting now() inside minute() --- Math expression eval/ConsoleApp/Program.cs | 339 +++++++++--------- .../Functions/Impl/minuteFunction.cs | 2 +- .../Functions/Impl/nowFunction.cs | 4 +- 3 files changed, 172 insertions(+), 173 deletions(-) diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index 7db0cf4..84baff5 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -1,176 +1,175 @@ using org.matheval; -Console.WriteLine("date test"); -Expression expression = new("date(2022, 3, 30)"); +Expression expression = new("minute(now())"); var val = expression.Eval(); Console.WriteLine(val); -Console.WriteLine("datevalue test"); -expression = new("datevalue('2023-01-15')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("datevalue('2023/01/15')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("datevalue('12-25-2022')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("datevalue('12/25/2022')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("Time test"); -expression = new("time(10,15,32)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("time(23,59,59)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("time(1111,59,59)"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("second test"); -expression = new("second('01:23:20')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("second('03/30/2022 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("minute test"); -expression = new("minute('01:23:20')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("minute('03/30/2022 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("hour test"); -expression = new("hour('01:23:20')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("hour('03/30/2022 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("day test"); -expression = new("day('03/15/2022')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("day('05/30/2022 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("month test"); -expression = new("month('03/15/2022')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("month('05/30/2022 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("now test"); -expression = new("now()"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("edate test"); -expression = new("edate('05/30/2022 04:50:45',5)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("edate('05/30/2022',20)"); -val = expression.Eval(); -Console.WriteLine(val); - - -Console.WriteLine("weekday test"); -DateTime date = new DateTime(); -for (int i = 0; i < 8; i++) -{ - date = DateTime.UtcNow.AddDays(i); - expression = new("weekday('" + date.ToString() + "')"); - val = expression.Eval(); - Console.WriteLine(date.DayOfWeek +": "+ val.ToString()); -} - -Console.WriteLine("weeknum test"); -expression = new("weeknum('01/01/2022 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("weeknum('12/20/2022')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("workday test"); -expression = new("workday('01/01/2023 04:50:45',3)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("workday('01/01/2023 04:50:45',-3)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("workday('01/01/2023',20)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("workday('01/01/2023',99)"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("networkday test"); -expression = new("networkdays('01/01/2023 04:50:45','01/25/2023 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("networkdays('01/01/2023 04:50:45','02/25/2023 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("networkdays('01/01/2023 04:50:45','01/01/2023 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("eomonth test"); -expression = new("eomonth('01/01/2023 04:50:45',2)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("eomonth('01/01/2023',2)"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("eomonth('01/01/2023 04:50:45',11)"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("datediff test"); -expression = new("datediff('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("datediff('01/01/2023','01/01/2024','day')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("datediff('01/01/2023','04/08/2023','day')"); -val = expression.Eval(); -Console.WriteLine(val); - -expression = new("datediff('01/01/2023','03/03/2023','month')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("datediff('01/01/2023','04/25/2024','month')"); -val = expression.Eval(); -Console.WriteLine(val); - -expression = new("datediff('01/01/2023','07/01/2023','year')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("datediff('01/01/2023','04/25/2025','year')"); -val = expression.Eval(); -Console.WriteLine(val); - -Console.WriteLine("days test"); -expression = new("days('01/01/2023 04:50:45','01/05/2023 04:50:45')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("days('01/01/2023','01/01/2024')"); -val = expression.Eval(); -Console.WriteLine(val); -expression = new("days('01/01/2023','04/08/2023')"); -val = expression.Eval(); -Console.WriteLine(val); +//Console.WriteLine("datevalue test"); +//expression = new("datevalue('2023-01-15')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("datevalue('2023/01/15')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("datevalue('12-25-2022')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("datevalue('12/25/2022')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("Time test"); +//expression = new("time(10,15,32)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("time(23,59,59)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("time(1111,59,59)"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("second test"); +//expression = new("second('01:23:20')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("second('03/30/2022 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("minute test"); +//expression = new("minute('01:23:20')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("minute('03/30/2022 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("hour test"); +//expression = new("hour('01:23:20')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("hour('03/30/2022 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("day test"); +//expression = new("day('03/15/2022')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("day('05/30/2022 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("month test"); +//expression = new("month('03/15/2022')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("month('05/30/2022 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("now test"); +//expression = new("now()"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("edate test"); +//expression = new("edate('05/30/2022 04:50:45',5)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("edate('05/30/2022',20)"); +//val = expression.Eval(); +//Console.WriteLine(val); + + +//Console.WriteLine("weekday test"); +//DateTime date = new DateTime(); +//for (int i = 0; i < 8; i++) +//{ +// date = DateTime.UtcNow.AddDays(i); +// expression = new("weekday('" + date.ToString() + "')"); +// val = expression.Eval(); +// Console.WriteLine(date.DayOfWeek +": "+ val.ToString()); +//} + +//Console.WriteLine("weeknum test"); +//expression = new("weeknum('01/01/2022 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("weeknum('12/20/2022')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("workday test"); +//expression = new("workday('01/01/2023 04:50:45',3)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("workday('01/01/2023 04:50:45',-3)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("workday('01/01/2023',20)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("workday('01/01/2023',99)"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("networkday test"); +//expression = new("networkdays('01/01/2023 04:50:45','01/25/2023 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("networkdays('01/01/2023 04:50:45','02/25/2023 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("networkdays('01/01/2023 04:50:45','01/01/2023 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("eomonth test"); +//expression = new("eomonth('01/01/2023 04:50:45',2)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("eomonth('01/01/2023',2)"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("eomonth('01/01/2023 04:50:45',11)"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("datediff test"); +//expression = new("datediff('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("datediff('01/01/2023','01/01/2024','day')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("datediff('01/01/2023','04/08/2023','day')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//expression = new("datediff('01/01/2023','03/03/2023','month')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("datediff('01/01/2023','04/25/2024','month')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//expression = new("datediff('01/01/2023','07/01/2023','year')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("datediff('01/01/2023','04/25/2025','year')"); +//val = expression.Eval(); +//Console.WriteLine(val); + +//Console.WriteLine("days test"); +//expression = new("days('01/01/2023 04:50:45','01/05/2023 04:50:45')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("days('01/01/2023','01/01/2024')"); +//val = expression.Eval(); +//Console.WriteLine(val); +//expression = new("days('01/01/2023','04/08/2023')"); +//val = expression.Eval(); +//Console.WriteLine(val); diff --git a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs index 116f56c..25db5c2 100644 --- a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs @@ -40,7 +40,7 @@ public class minuteFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/nowFunction.cs b/Math expression eval/org.matheval/Functions/Impl/nowFunction.cs index 880d443..6b1c11c 100644 --- a/Math expression eval/org.matheval/Functions/Impl/nowFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/nowFunction.cs @@ -38,7 +38,7 @@ public class nowFunction : IFunction /// FunctionDefs public List GetInfo() { - return new List { new FunctionDef(Afe_Common.Const_Not, new System.Type[] { }, typeof(Boolean), 0) }; + return new List { new FunctionDef(Afe_Common.Const_Now, null, typeof(string), 0) }; } /// @@ -49,7 +49,7 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - return DateTime.UtcNow; + return DateTime.UtcNow.ToString(); } } } From 6b6ce64cf1f9415eadb7beaecb28c8909625b5a4 Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Tue, 7 Mar 2023 16:33:51 -0500 Subject: [PATCH 10/14] Fix CopyPaste issues, and use proper return type --- .gitignore | 1 + Math expression eval/org.matheval/Common/Afe_Common.cs | 4 ---- .../org.matheval/Functions/Impl/SecondFunction.cs | 2 +- .../Impl/{datediffFunction.cs => datedifFunction.cs} | 4 ++-- .../org.matheval/Functions/Impl/dayFunction.cs | 2 +- .../org.matheval/Functions/Impl/daysFunction.cs | 2 +- .../org.matheval/Functions/Impl/edateFunction.cs | 2 +- .../org.matheval/Functions/Impl/eomonthFunction.cs | 2 +- .../org.matheval/Functions/Impl/hourFunction.cs | 2 +- .../org.matheval/Functions/Impl/minuteFunction.cs | 2 +- .../org.matheval/Functions/Impl/monthFunction.cs | 2 +- .../org.matheval/Functions/Impl/networkdaysFunction.cs | 2 +- .../org.matheval/Functions/Impl/timeFunction.cs | 2 +- .../org.matheval/Functions/Impl/weekdayFunction .cs | 2 +- .../org.matheval/Functions/Impl/weeknumFunction.cs | 2 +- .../org.matheval/Functions/Impl/workdayFunction.cs | 2 +- .../org.matheval/Functions/Impl/yearFunction.cs | 2 +- 17 files changed, 17 insertions(+), 20 deletions(-) rename Math expression eval/org.matheval/Functions/Impl/{datediffFunction.cs => datedifFunction.cs} (93%) diff --git a/.gitignore b/.gitignore index 6203922..c3b5ef4 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ packages project.lock.json .vs /Math expression eval/UnitTest/UnitTest.csproj.user +Math expression eval/ConsoleApp/Properties/PublishProfiles/FolderProfile.pubxml diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index 029ef56..f27d9de 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -627,10 +627,6 @@ public static class Afe_Common /// public const string Const_Now = "now"; - /// - /// Function Today - /// - public const string Const_Today = "today"; /// /// Function Today /// diff --git a/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs index 9f2c6c1..5fdf1b2 100644 --- a/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs @@ -40,7 +40,7 @@ public class secondFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + new FunctionDef(Afe_Common.Const_Second, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs similarity index 93% rename from Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs rename to Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs index e5a72c5..73d88ab 100644 --- a/Math expression eval/org.matheval/Functions/Impl/datediffFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs @@ -31,7 +31,7 @@ namespace org.matheval.Functions /// /// Returns the day component of a Date, Datetime /// - public class datediffFunction : IFunction + public class datedifFunction : IFunction { /// /// Get Information @@ -40,7 +40,7 @@ public class datediffFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string), typeof(string), typeof(string)}, typeof(string), 3)}; + new FunctionDef(Afe_Common.Const_DateDif, new System.Type[]{typeof(string), typeof(string), typeof(string)}, typeof(decimal), 3)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs index 4246d40..4732650 100644 --- a/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs @@ -40,7 +40,7 @@ public class dayFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + new FunctionDef(Afe_Common.Const_Day, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs index bc0ba1c..0d794b1 100644 --- a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs @@ -40,7 +40,7 @@ public class daysFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string), typeof(string)}, typeof(string), 2)}; + new FunctionDef(Afe_Common.Const_Days, new System.Type[]{typeof(string), typeof(string)}, typeof(decimal), 2)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs b/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs index c795253..b1f5c4e 100644 --- a/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs @@ -40,7 +40,7 @@ public List GetInfo() { return new List { - new FunctionDef(Afe_Common.Const_Date, new Type[] { typeof(string), typeof(decimal) }, typeof(string), 2) + new FunctionDef(Afe_Common.Const_EDate, new Type[] { typeof(string), typeof(decimal) }, typeof(string), 2) }; } diff --git a/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs index c5c6ac2..0364d25 100644 --- a/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs @@ -40,7 +40,7 @@ public class eomonthFunction : IFunction /// FunctionDefs public List GetInfo() { - return new List { new FunctionDef(Afe_Common.Const_MID, new System.Type[] { typeof(string), typeof(decimal) }, typeof(string), 2) }; + return new List { new FunctionDef(Afe_Common.Const_EOMonth, new System.Type[] { typeof(string), typeof(decimal) }, typeof(string), 2) }; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs index 330d0d1..94fddeb 100644 --- a/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs @@ -40,7 +40,7 @@ public class hourFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + new FunctionDef(Afe_Common.Const_Hour, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs index 25db5c2..b7f9bd0 100644 --- a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs @@ -40,7 +40,7 @@ public class minuteFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; + new FunctionDef(Afe_Common.Const_Minute, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs b/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs index b43d765..7cc4a55 100644 --- a/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/monthFunction.cs @@ -40,7 +40,7 @@ public class monthFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + new FunctionDef(Afe_Common.Const_Month, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs index ec4651d..e40fd75 100644 --- a/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs @@ -41,7 +41,7 @@ public class networkdaysFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string),typeof(string) }, typeof(string), 2)}; + new FunctionDef(Afe_Common.Const_NetWorkdays, new System.Type[]{typeof(string),typeof(string) }, typeof(decimal), 2)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/timeFunction.cs b/Math expression eval/org.matheval/Functions/Impl/timeFunction.cs index f2fc8ed..d649713 100644 --- a/Math expression eval/org.matheval/Functions/Impl/timeFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/timeFunction.cs @@ -40,7 +40,7 @@ public class timeFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(decimal), typeof(decimal), typeof(decimal)}, typeof(string), 3)}; + new FunctionDef(Afe_Common.Const_Time, new System.Type[]{typeof(decimal), typeof(decimal), typeof(decimal)}, typeof(string), 3)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs b/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs index 8cfe028..ccd59d6 100644 --- a/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs +++ b/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs @@ -40,7 +40,7 @@ public class weekdayFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + new FunctionDef(Afe_Common.Const_Weekday, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs b/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs index a40a59f..7b27fc1 100644 --- a/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs @@ -41,7 +41,7 @@ public class weeknumFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string)}, typeof(string), 1)}; + new FunctionDef(Afe_Common.Const_WeekNum, new System.Type[]{typeof(string)}, typeof(decimal), 1)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs index 9df1397..48c81bb 100644 --- a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs @@ -41,7 +41,7 @@ public class workdayFunction : IFunction public List GetInfo() { return new List{ - new FunctionDef(Afe_Common.Const_Tan, new System.Type[]{typeof(string),typeof(decimal)}, typeof(string), 2)}; + new FunctionDef(Afe_Common.Const_Workday, new System.Type[]{typeof(string),typeof(decimal)}, typeof(string), 2)}; } /// diff --git a/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs index 97950cb..9236e70 100644 --- a/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs @@ -40,7 +40,7 @@ public List GetInfo() { return new List { - new FunctionDef(Afe_Common.Const_Year, new Type[] { typeof(string) }, typeof(int), 1) + new FunctionDef(Afe_Common.Const_Year, new Type[] { typeof(string) }, typeof(decimal), 1) }; } From 7de8da21d51720ed93f29d4c541b78b138d1681f Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Wed, 8 Mar 2023 10:13:23 -0500 Subject: [PATCH 11/14] Update summaries in functions --- .../org.matheval/Functions/Impl/dateFunction.cs | 2 +- .../org.matheval/Functions/Impl/datedifFunction.cs | 2 +- .../org.matheval/Functions/Impl/datevalueFunction.cs | 2 +- .../org.matheval/Functions/Impl/daysFunction.cs | 2 +- .../org.matheval/Functions/Impl/eomonthFunction.cs | 4 +--- .../org.matheval/Functions/Impl/networkdaysFunction.cs | 2 +- 6 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs b/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs index 38ce074..a687b8e 100644 --- a/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/dateFunction.cs @@ -28,7 +28,7 @@ THE SOFTWARE. namespace org.matheval.Functions { /// - /// Returns the year from a date + /// Constructs a Date from Integer representations of the year, month (1=Jan), and day /// public class dateFunction : IFunction { diff --git a/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs index 73d88ab..50f3369 100644 --- a/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs @@ -29,7 +29,7 @@ THE SOFTWARE. namespace org.matheval.Functions { /// - /// Returns the day component of a Date, Datetime + /// Calculates the number of days, months, or years between two dates. /// public class datedifFunction : IFunction { diff --git a/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs index 2cf5ba1..49413db 100644 --- a/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs @@ -29,7 +29,7 @@ THE SOFTWARE. namespace org.matheval.Functions { /// - /// Returns the year from a date + /// Constructs a Date from String representations of the year–month–day /// public class datevalueFunction : IFunction { diff --git a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs index 0d794b1..f400262 100644 --- a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs @@ -29,7 +29,7 @@ THE SOFTWARE. namespace org.matheval.Functions { /// - /// Returns the day component of a Date, Datetime + /// Returns the number of days between two dates /// public class daysFunction : IFunction { diff --git a/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs index 0364d25..2dab48b 100644 --- a/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs @@ -28,9 +28,7 @@ THE SOFTWARE. namespace org.matheval.Functions { /// - /// Extracts a given number of characters - /// from the middle of a supplied text string - /// MID("abcd",1,2) -> ab + // Calculates the last day of the month after adding a specified number of months to a date, datetime /// public class eomonthFunction : IFunction { diff --git a/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs index e40fd75..c05be44 100644 --- a/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/networkdaysFunction.cs @@ -30,7 +30,7 @@ THE SOFTWARE. namespace org.matheval.Functions { /// - /// Returns the day of the week corresponding to a date. The day is given as an integer, ranging from 1 (Sunday) to 7 (Saturday) + /// Returns the number of whole workdays between two dates /// public class networkdaysFunction : IFunction { From ce25f6ed1752c84a48d028b6c9442f748caad5c4 Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Wed, 8 Mar 2023 10:17:26 -0500 Subject: [PATCH 12/14] Update DateDiff Unit test --- ...iffFunctionTest.cs => DateDifFunctionTest.cs} | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename Math expression eval/UnitTest/{datediffFunctionTest.cs => DateDifFunctionTest.cs} (73%) diff --git a/Math expression eval/UnitTest/datediffFunctionTest.cs b/Math expression eval/UnitTest/DateDifFunctionTest.cs similarity index 73% rename from Math expression eval/UnitTest/datediffFunctionTest.cs rename to Math expression eval/UnitTest/DateDifFunctionTest.cs index a5c41d1..48fcee2 100644 --- a/Math expression eval/UnitTest/datediffFunctionTest.cs +++ b/Math expression eval/UnitTest/DateDifFunctionTest.cs @@ -28,38 +28,38 @@ THE SOFTWARE. namespace UnitTest { [TestClass] - public class DateDiffFunctionTest + public class DateDifFunctionTest { [TestMethod] public void DayDifference() { - Expression expression = new Expression("datediff('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); + Expression expression = new Expression("datedif('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); Assert.AreEqual((decimal)35, expression.Eval()); - expression = new Expression("datediff('01/01/2023','01/01/2024','day')"); + expression = new Expression("datedif('01/01/2023','01/01/2024','day')"); Assert.AreEqual((decimal)365, expression.Eval()); - expression = new Expression("datediff('01/01/2023','04/11/2023','day')"); + expression = new Expression("datedif('01/01/2023','04/11/2023','day')"); Assert.AreEqual((decimal)100, expression.Eval()); } [TestMethod] public void MonthDifference() { - Expression expression = new Expression("datediff('01/01/2023','03/03/2023','month')"); + Expression expression = new Expression("datedif('01/01/2023','03/03/2023','month')"); Assert.AreEqual((decimal)2, expression.Eval()); - expression = new Expression("datediff('01/01/2023','04/25/2024','month')"); + expression = new Expression("datedif('01/01/2023','04/25/2024','month')"); Assert.AreEqual((decimal)15, expression.Eval()); } [TestMethod] public void YearDifference() { - Expression expression = new Expression("datediff('01/01/2023','07/01/2023','year')"); + Expression expression = new Expression("datedif('01/01/2023','07/01/2023','year')"); Assert.AreEqual((decimal).50, expression.Eval()); - expression = new Expression("datediff('01/01/2023','04/25/2025','year')"); + expression = new Expression("datedif('01/01/2023','04/25/2025','year')"); Assert.AreEqual((decimal)2.25, expression.Eval()); } } From 452c1d90280fe44ad7377da61a0b2c390427d0f3 Mon Sep 17 00:00:00 2001 From: MartinCarter Date: Thu, 9 Mar 2023 09:44:30 -0500 Subject: [PATCH 13/14] Add ToDateTIme and TimeValidation to Afe_Common, update all Date parameter functions, New Afe_CommonTest unit test class --- Math expression eval/ConsoleApp/Program.cs | 36 +++++---- .../UnitTest/Afe_CommonTests.cs | 73 +++++++++++++++++++ .../org.matheval/Common/Afe_Common.cs | 43 ++++++++++- .../Functions/Impl/SecondFunction.cs | 2 +- .../Functions/Impl/datedifFunction.cs | 4 +- .../Functions/Impl/datevalueFunction.cs | 4 +- .../Functions/Impl/dayFunction.cs | 2 +- .../Functions/Impl/daysFunction.cs | 4 +- .../Functions/Impl/edateFunction.cs | 4 +- .../Functions/Impl/eomonthFunction.cs | 4 +- .../Functions/Impl/hourFunction.cs | 2 +- .../Functions/Impl/minuteFunction.cs | 2 +- .../Functions/Impl/weekdayFunction .cs | 2 +- .../Functions/Impl/weeknumFunction.cs | 2 +- .../Functions/Impl/workdayFunction.cs | 2 +- .../Functions/Impl/yearFunction.cs | 5 +- 16 files changed, 155 insertions(+), 36 deletions(-) create mode 100644 Math expression eval/UnitTest/Afe_CommonTests.cs diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index 84baff5..3dc89ae 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -1,8 +1,9 @@ -using org.matheval; +//using org.matheval; -Expression expression = new("minute(now())"); -var val = expression.Eval(); -Console.WriteLine(val); + +//Expression expression = new("minute(now())"); +//var val = expression.Eval(); +//Console.WriteLine(val); //Console.WriteLine("datevalue test"); @@ -37,6 +38,10 @@ //expression = new("second('03/30/2022 04:50:45')"); //val = expression.Eval(); //Console.WriteLine(val); +////expression = new("second('03/30/2022')"); +////val = expression.Eval(); +////Console.WriteLine(val); + //Console.WriteLine("minute test"); //expression = new("minute('01:23:20')"); @@ -46,6 +51,10 @@ //val = expression.Eval(); //Console.WriteLine(val); +////expression = new("minute('03/30/2022')"); +////val = expression.Eval(); +////Console.WriteLine(val); + //Console.WriteLine("hour test"); //expression = new("hour('01:23:20')"); //val = expression.Eval(); @@ -62,6 +71,7 @@ //val = expression.Eval(); //Console.WriteLine(val); + //Console.WriteLine("month test"); //expression = new("month('03/15/2022')"); //val = expression.Eval(); @@ -91,7 +101,7 @@ // date = DateTime.UtcNow.AddDays(i); // expression = new("weekday('" + date.ToString() + "')"); // val = expression.Eval(); -// Console.WriteLine(date.DayOfWeek +": "+ val.ToString()); +// Console.WriteLine(date.DayOfWeek + ": " + val.ToString()); //} //Console.WriteLine("weeknum test"); @@ -138,28 +148,28 @@ //val = expression.Eval(); //Console.WriteLine(val); -//Console.WriteLine("datediff test"); -//expression = new("datediff('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); +//Console.WriteLine("datedif test"); +//expression = new("datedif('01/01/2023 04:50:45','02/05/2023 04:50:45','day')"); //val = expression.Eval(); //Console.WriteLine(val); -//expression = new("datediff('01/01/2023','01/01/2024','day')"); +//expression = new("datedif('01/01/2023','01/01/2024','day')"); //val = expression.Eval(); //Console.WriteLine(val); -//expression = new("datediff('01/01/2023','04/08/2023','day')"); +//expression = new("datedif('01/01/2023','04/08/2023','day')"); //val = expression.Eval(); //Console.WriteLine(val); -//expression = new("datediff('01/01/2023','03/03/2023','month')"); +//expression = new("datedif('01/01/2023','03/03/2023','month')"); //val = expression.Eval(); //Console.WriteLine(val); -//expression = new("datediff('01/01/2023','04/25/2024','month')"); +//expression = new("datedif('01/01/2023','04/25/2024','month')"); //val = expression.Eval(); //Console.WriteLine(val); -//expression = new("datediff('01/01/2023','07/01/2023','year')"); +//expression = new("datedif('01/01/2023','07/01/2023','year')"); //val = expression.Eval(); //Console.WriteLine(val); -//expression = new("datediff('01/01/2023','04/25/2025','year')"); +//expression = new("datedif('01/01/2023','04/25/2025','year')"); //val = expression.Eval(); //Console.WriteLine(val); diff --git a/Math expression eval/UnitTest/Afe_CommonTests.cs b/Math expression eval/UnitTest/Afe_CommonTests.cs new file mode 100644 index 0000000..6ca18ab --- /dev/null +++ b/Math expression eval/UnitTest/Afe_CommonTests.cs @@ -0,0 +1,73 @@ +/* + The MIT License + + Copyright (c) 2021 MathEval.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using org.matheval; +using org.matheval.Common; +using System; +using System.Globalization; + +namespace UnitTest +{ + [TestClass] + public class Afe_CommonTests + { + [TestMethod] + public void ReturnsDateTime() + { + CultureInfo dc = new CultureInfo("en-US"); + var dateTimeString = "2023-01-01"; + Assert.IsInstanceOfType(Afe_Common.ToDateTime(dateTimeString, dc), typeof(DateTime)); + + dateTimeString = "2023-01-01 12:12:12"; + Assert.IsInstanceOfType(Afe_Common.ToDateTime(dateTimeString, dc), typeof(DateTime)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void ToDateTimeArgumentException() + { + CultureInfo dc = new CultureInfo("en-US"); + Afe_Common.ToDateTime("12:12:12", dc); + } + + [TestMethod] + public void TimeValidationReturnsDateTime() + { + DateTime dateTime= DateTime.Parse("2023-01-01 12:12:12"); + Assert.AreEqual(Afe_Common.TimeValidation(dateTime), dateTime); + + dateTime = DateTime.Parse("12:12:12"); + Assert.AreEqual(Afe_Common.TimeValidation(dateTime), dateTime); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TimeValidationTimeArgumentException() + { + DateTime dateTime = DateTime.Parse("2023-01-01"); + Afe_Common.TimeValidation(dateTime); + } + } +} + diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index f27d9de..4cece79 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -836,11 +836,34 @@ public static int ToInteger(object value, CultureInfo cultureInfo) } /// - /// Connvert object to string - /// TODO poner default invariant culture. + /// connvert object to string + /// todo poner default invariant culture. /// /// - /// + /// + /// + public static DateTime ToDateTime(object value, CultureInfo cultureInfo) + { + if(value is DateTime) + { + return (DateTime)value; + } + else if (TimeSpan.TryParse(value.ToString(), out _)) + { + throw new ArgumentException("Date is Invalid"); + } + else + { + return Convert.ToDateTime(value, cultureInfo); + } + } + + /// + /// connvert object to string + /// todo poner default invariant culture. + /// + /// + /// /// public static string ToString(object value, CultureInfo cultureInfo) { @@ -934,6 +957,20 @@ public static int DateDif(DateTime startDate, DateTime endDate, string unit) throw new Exception("Please set M or D or Y for UNIT param"); } + /// + /// DateDif + /// + /// dateTime + /// Blank Time Check + /// + public static DateTime TimeValidation (DateTime dateTime) + { + if (dateTime.ToString("HH:mm:ss") == "00:00:00") + { + throw new ArgumentException("Time is Invalid"); + } + return dateTime; + } #endregion #region Enum diff --git a/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs index 5fdf1b2..07adbd5 100644 --- a/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/SecondFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() public Object Execute(Dictionary args, ExpressionContext dc) { var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - return Afe_Common.ToInteger(dateTime.Second, dc.WorkingCulture); + return Afe_Common.ToInteger(Afe_Common.TimeValidation(dateTime).Second, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs index 50f3369..640480f 100644 --- a/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/datedifFunction.cs @@ -51,8 +51,8 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - var endDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_Two], dc.WorkingCulture)); + var startDate = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); + var endDate = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); var unit = Afe_Common.ToString(args[Afe_Common.Const_Key_Three], dc.WorkingCulture); return Afe_Common.ToDecimal(SubtractDates(startDate, endDate, unit), dc.WorkingCulture); } diff --git a/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs b/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs index 49413db..c93ccf8 100644 --- a/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/datevalueFunction.cs @@ -53,8 +53,8 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var dateString = Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture); - return Afe_Common.ToString(DateTime.Parse(dateString), dc.WorkingCulture); + var dateTime = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); + return Afe_Common.ToString(dateTime, dc.WorkingCulture); } } diff --git a/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs index 4732650..c248cac 100644 --- a/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/dayFunction.cs @@ -51,7 +51,7 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var dateTime = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); return Afe_Common.ToInteger(dateTime.Day, dc.WorkingCulture); } } diff --git a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs index f400262..fba547e 100644 --- a/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/daysFunction.cs @@ -51,8 +51,8 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var startDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - var endDate = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_Two], dc.WorkingCulture)); + var startDate = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); + var endDate = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); return Afe_Common.ToInteger(endDate.Subtract(startDate).TotalDays, dc.WorkingCulture); } } diff --git a/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs b/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs index b1f5c4e..b0074a6 100644 --- a/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/edateFunction.cs @@ -52,9 +52,9 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var date = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var dateTime = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); var month = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); - return Afe_Common.ToString(date.AddMonths(month), dc.WorkingCulture); + return Afe_Common.ToString(dateTime.AddMonths(month), dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs index 2dab48b..cb7ac52 100644 --- a/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/eomonthFunction.cs @@ -49,9 +49,9 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var date = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var dateTime = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); var months = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); - var dateMonths = date.AddMonths(months); + var dateMonths = dateTime.AddMonths(months); return Afe_Common.ToString(new DateTime(dateMonths.Year, dateMonths.Month, DateTime.DaysInMonth(dateMonths.Year, dateMonths.Month)), dc.WorkingCulture); } } diff --git a/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs index 94fddeb..2299aa9 100644 --- a/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/hourFunction.cs @@ -51,7 +51,7 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var dateTime = Afe_Common.TimeValidation(DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture))); return Afe_Common.ToInteger(dateTime.Hour, dc.WorkingCulture); } } diff --git a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs index b7f9bd0..6eb663c 100644 --- a/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/minuteFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() public Object Execute(Dictionary args, ExpressionContext dc) { var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); - return Afe_Common.ToInteger(dateTime.Minute, dc.WorkingCulture); + return Afe_Common.ToInteger(Afe_Common.TimeValidation(dateTime).Minute, dc.WorkingCulture); } } } diff --git a/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs b/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs index ccd59d6..b1652f8 100644 --- a/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs +++ b/Math expression eval/org.matheval/Functions/Impl/weekdayFunction .cs @@ -51,7 +51,7 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var dateTime = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); int day = 0; switch (dateTime.DayOfWeek.ToString()) { diff --git a/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs b/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs index 7b27fc1..69b8a02 100644 --- a/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/weeknumFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var dateTime = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var dateTime = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); var weekNum = dc.WorkingCulture.Calendar.GetWeekOfYear(dateTime, dc.WorkingCulture.DateTimeFormat.CalendarWeekRule, dc.WorkingCulture.DateTimeFormat.FirstDayOfWeek); return Afe_Common.ToInteger(weekNum, dc.WorkingCulture); } diff --git a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs index 48c81bb..a174c59 100644 --- a/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/workdayFunction.cs @@ -52,7 +52,7 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - var date = DateTime.Parse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture)); + var date = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One], dc.WorkingCulture); var days = Afe_Common.ToInteger(args[Afe_Common.Const_Key_Two], dc.WorkingCulture); int i = 0; while (i < Math.Abs(days)) diff --git a/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs index 9236e70..1af8187 100644 --- a/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs +++ b/Math expression eval/org.matheval/Functions/Impl/yearFunction.cs @@ -52,9 +52,8 @@ public List GetInfo() /// Value public Object Execute(Dictionary args, ExpressionContext dc) { - string dateString = Afe_Common.ToString(args[Afe_Common.Const_Key_One].ToString(), dc.WorkingCulture); - DateTime date = DateTime.Parse(dateString, dc.WorkingCulture); - return Afe_Common.ToInteger(date.Year, dc.WorkingCulture); + var dateTime = Afe_Common.ToDateTime(args[Afe_Common.Const_Key_One].ToString(), dc.WorkingCulture); + return Afe_Common.ToInteger(dateTime.Year, dc.WorkingCulture); } } } From 22ec3f427d19570b0c840032be22fe277631c73c Mon Sep 17 00:00:00 2001 From: Andy Edmonds Date: Thu, 9 Mar 2023 15:26:56 -0500 Subject: [PATCH 14/14] clean out user files --- .vscode/launch.json | 26 -------------- .vscode/tasks.json | 41 ---------------------- Math expression eval/ConsoleApp/Program.cs | 8 ++--- 3 files changed, 4 insertions(+), 71 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 07a6339..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - // Use IntelliSense to find out which attributes exist for C# debugging - // Use hover for the description of the existing attributes - // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md - "name": ".NET Core Launch (console)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/Math expression eval/ConsoleApp/bin/Debug/net7.0/ConsoleApp.dll", - "args": [], - "cwd": "${workspaceFolder}/Math expression eval/ConsoleApp", - // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console - "console": "internalConsole", - "stopAtEntry": false - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach" - } - ] -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 594ec39..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "command": "dotnet", - "type": "process", - "args": [ - "build", - "${workspaceFolder}/Math expression eval/ConsoleApp/ConsoleApp.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "publish", - "command": "dotnet", - "type": "process", - "args": [ - "publish", - "${workspaceFolder}/Math expression eval/ConsoleApp/ConsoleApp.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "watch", - "command": "dotnet", - "type": "process", - "args": [ - "watch", - "run", - "--project", - "${workspaceFolder}/Math expression eval/ConsoleApp/ConsoleApp.csproj" - ], - "problemMatcher": "$msCompile" - } - ] -} \ No newline at end of file diff --git a/Math expression eval/ConsoleApp/Program.cs b/Math expression eval/ConsoleApp/Program.cs index 3dc89ae..11ae821 100644 --- a/Math expression eval/ConsoleApp/Program.cs +++ b/Math expression eval/ConsoleApp/Program.cs @@ -1,9 +1,9 @@ -//using org.matheval; +using org.matheval; -//Expression expression = new("minute(now())"); -//var val = expression.Eval(); -//Console.WriteLine(val); +Expression expression = new("minute(now())"); +var val = expression.Eval(); +Console.WriteLine(val); //Console.WriteLine("datevalue test");