Skip to content

Add date functions #12

New issue

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

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

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ packages
project.lock.json
.vs
/Math expression eval/UnitTest/UnitTest.csproj.user
Math expression eval/ConsoleApp/Properties/PublishProfiles/FolderProfile.pubxml
14 changes: 14 additions & 0 deletions Math expression eval/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\org.matheval\org.matheval.csproj" />
</ItemGroup>

</Project>
185 changes: 185 additions & 0 deletions Math expression eval/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using org.matheval;


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);
////expression = new("second('03/30/2022')");
////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);

////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();
//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("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("datedif('01/01/2023','01/01/2024','day')");
//val = expression.Eval();
//Console.WriteLine(val);
//expression = new("datedif('01/01/2023','04/08/2023','day')");
//val = expression.Eval();
//Console.WriteLine(val);

//expression = new("datedif('01/01/2023','03/03/2023','month')");
//val = expression.Eval();
//Console.WriteLine(val);
//expression = new("datedif('01/01/2023','04/25/2024','month')");
//val = expression.Eval();
//Console.WriteLine(val);

//expression = new("datedif('01/01/2023','07/01/2023','year')");
//val = expression.Eval();
//Console.WriteLine(val);
//expression = new("datedif('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);
10 changes: 8 additions & 2 deletions Math expression eval/Math expression eval.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
73 changes: 73 additions & 0 deletions Math expression eval/UnitTest/Afe_CommonTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

67 changes: 67 additions & 0 deletions Math expression eval/UnitTest/DateDifFunctionTest.cs
Original file line number Diff line number Diff line change
@@ -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 DateDifFunctionTest
{
[TestMethod]
public void DayDifference()
{
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("datedif('01/01/2023','01/01/2024','day')");
Assert.AreEqual((decimal)365, expression.Eval());

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("datedif('01/01/2023','03/03/2023','month')");
Assert.AreEqual((decimal)2, expression.Eval());

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("datedif('01/01/2023','07/01/2023','year')");
Assert.AreEqual((decimal).50, expression.Eval());

expression = new Expression("datedif('01/01/2023','04/25/2025','year')");
Assert.AreEqual((decimal)2.25, expression.Eval());
}
}
}

Loading