Skip to content

Commit 24d5a0d

Browse files
committed
添加 日期操作
1 parent 7ce8c1f commit 24d5a0d

File tree

12 files changed

+2772
-2122
lines changed

12 files changed

+2772
-2122
lines changed

csharp/ToolGood.Algorithm2.MathParserReplace/Program.cs

Lines changed: 0 additions & 97 deletions
This file was deleted.

csharp/ToolGood.Algorithm2.MathParserReplace/ToolGood.Algorithm2.MathParserReplace.csproj

Lines changed: 0 additions & 8 deletions
This file was deleted.

csharp/ToolGood.Algorithm2.Test/AlgorithmEngine/AlgorithmEngineTest_dateTime.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,29 @@ public void WEEKNUM_test()
203203
Assert.AreEqual(dt, 1);
204204
}
205205

206+
[Test]
207+
public void Add_test()
208+
{
209+
AlgorithmEngine engine = new AlgorithmEngine();
210+
var dt = engine.TryEvaluate("'2000-02-01'.addYears(1).year()", 0);
211+
Assert.AreEqual(dt, 2001);
212+
213+
dt = engine.TryEvaluate("'2000-02-01'.AddMonths(1).Month()", 0);
214+
Assert.AreEqual(dt, 3);
215+
216+
dt = engine.TryEvaluate("'2000-02-01'.AddDays(1).Day()", 0);
217+
Assert.AreEqual(dt, 2);
218+
219+
dt = engine.TryEvaluate("'2000-02-01 12:05:06'.AddHours(1).Hour()", 0);
220+
Assert.AreEqual(dt, 13);
221+
222+
dt = engine.TryEvaluate("'2000-02-01 12:05:06'.AddMinutes(1).Minute()", 0);
223+
Assert.AreEqual(dt, 6);
224+
225+
dt = engine.TryEvaluate("'2000-02-01 12:05:06'.AddSeconds(1).Second()", 0);
226+
Assert.AreEqual(dt, 7);
227+
228+
}
206229

207230
}
208231
}

csharp/ToolGood.Algorithm2/Internals/DiyNameVisitor.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Antlr4.Runtime.Tree;
1+
using Antlr4.Runtime.Misc;
2+
using Antlr4.Runtime.Tree;
23
using System;
34

45
namespace ToolGood.Algorithm.Internals
@@ -1126,5 +1127,35 @@ public object VisitYEAR_fun(mathParser.YEAR_funContext context)
11261127
{
11271128
return VisitChildren(context);
11281129
}
1129-
}
1130+
1131+
public object VisitADDMONTHS_fun([NotNull] mathParser.ADDMONTHS_funContext context)
1132+
{
1133+
return VisitChildren(context);
1134+
}
1135+
1136+
public object VisitADDYEARS_fun([NotNull] mathParser.ADDYEARS_funContext context)
1137+
{
1138+
return VisitChildren(context);
1139+
}
1140+
1141+
public object VisitADDSECONDS_fun([NotNull] mathParser.ADDSECONDS_funContext context)
1142+
{
1143+
return VisitChildren(context);
1144+
}
1145+
1146+
public object VisitADDMINUTES_fun([NotNull] mathParser.ADDMINUTES_funContext context)
1147+
{
1148+
return VisitChildren(context);
1149+
}
1150+
1151+
public object VisitADDDAYS_fun([NotNull] mathParser.ADDDAYS_funContext context)
1152+
{
1153+
return VisitChildren(context);
1154+
}
1155+
1156+
public object VisitADDHOURS_fun([NotNull] mathParser.ADDHOURS_funContext context)
1157+
{
1158+
return VisitChildren(context);
1159+
}
1160+
}
11301161
}

csharp/ToolGood.Algorithm2/Internals/MathSplitVisitor.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Antlr4.Runtime;
2+
using Antlr4.Runtime.Misc;
23
using Antlr4.Runtime.Tree;
34
using System.Collections.Generic;
45
using System.Text;
@@ -1131,6 +1132,34 @@ public ConditionTree VisitYEAR_fun(mathParser.YEAR_funContext context)
11311132
return Visit_fun(context);
11321133
}
11331134

1135+
public ConditionTree VisitADDMONTHS_fun([NotNull] mathParser.ADDMONTHS_funContext context)
1136+
{
1137+
return Visit_fun(context);
1138+
}
1139+
1140+
public ConditionTree VisitADDYEARS_fun([NotNull] mathParser.ADDYEARS_funContext context)
1141+
{
1142+
return Visit_fun(context);
1143+
}
1144+
1145+
public ConditionTree VisitADDSECONDS_fun([NotNull] mathParser.ADDSECONDS_funContext context)
1146+
{
1147+
return Visit_fun(context);
1148+
}
11341149

1150+
public ConditionTree VisitADDMINUTES_fun([NotNull] mathParser.ADDMINUTES_funContext context)
1151+
{
1152+
return Visit_fun(context);
1153+
}
1154+
1155+
public ConditionTree VisitADDDAYS_fun([NotNull] mathParser.ADDDAYS_funContext context)
1156+
{
1157+
return Visit_fun(context);
1158+
}
1159+
1160+
public ConditionTree VisitADDHOURS_fun([NotNull] mathParser.ADDHOURS_funContext context)
1161+
{
1162+
return Visit_fun(context);
1163+
}
11351164
}
11361165
}

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,90 @@ public virtual Operand VisitWEEKNUM_fun(mathParser.WEEKNUM_funContext context)
20982098
return Operand.Create(week);
20992099
}
21002100

2101+
public virtual Operand VisitADDMONTHS_fun(mathParser.ADDMONTHS_funContext context)
2102+
{
2103+
var args = new List<Operand>();
2104+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
2105+
2106+
var firstValue = args[0].ToMyDate("Function AddMonths parameter 1 is error!");
2107+
if (firstValue.IsError) { return firstValue; };
2108+
var secondValue = args[1].ToNumber("Function AddMonths parameter 2 is error!");
2109+
if (secondValue.IsError) { return secondValue; };
2110+
2111+
var date = firstValue.DateValue.AddMonths(secondValue.IntValue);
2112+
return Operand.Create(date);
2113+
}
2114+
2115+
public virtual Operand VisitADDYEARS_fun(mathParser.ADDYEARS_funContext context)
2116+
{
2117+
var args = new List<Operand>();
2118+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
2119+
2120+
var firstValue = args[0].ToMyDate("Function AddYears parameter 1 is error!");
2121+
if (firstValue.IsError) { return firstValue; };
2122+
var secondValue = args[1].ToNumber("Function AddYears parameter 2 is error!");
2123+
if (secondValue.IsError) { return secondValue; };
2124+
2125+
var date = firstValue.DateValue.AddYears(secondValue.IntValue);
2126+
return Operand.Create(date);
2127+
}
2128+
2129+
public virtual Operand VisitADDSECONDS_fun(mathParser.ADDSECONDS_funContext context)
2130+
{
2131+
var args = new List<Operand>();
2132+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
2133+
2134+
var firstValue = args[0].ToMyDate("Function AddSeconds parameter 1 is error!");
2135+
if (firstValue.IsError) { return firstValue; };
2136+
var secondValue = args[1].ToNumber("Function AddSeconds parameter 2 is error!");
2137+
if (secondValue.IsError) { return secondValue; };
2138+
2139+
var date = firstValue.DateValue.AddSeconds(secondValue.IntValue);
2140+
return Operand.Create(date);
2141+
}
2142+
2143+
public virtual Operand VisitADDMINUTES_fun(mathParser.ADDMINUTES_funContext context)
2144+
{
2145+
var args = new List<Operand>();
2146+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
2147+
2148+
var firstValue = args[0].ToMyDate("Function AddMinutes parameter 1 is error!");
2149+
if (firstValue.IsError) { return firstValue; };
2150+
var secondValue = args[1].ToNumber("Function AddMinutes parameter 2 is error!");
2151+
if (secondValue.IsError) { return secondValue; };
2152+
2153+
var date = firstValue.DateValue.AddMinutes(secondValue.IntValue);
2154+
return Operand.Create(date);
2155+
}
2156+
2157+
public virtual Operand VisitADDDAYS_fun(mathParser.ADDDAYS_funContext context)
2158+
{
2159+
var args = new List<Operand>();
2160+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
2161+
2162+
var firstValue = args[0].ToMyDate("Function AddDays parameter 1 is error!");
2163+
if (firstValue.IsError) { return firstValue; };
2164+
var secondValue = args[1].ToNumber("Function AddDays parameter 2 is error!");
2165+
if (secondValue.IsError) { return secondValue; };
2166+
2167+
var date = firstValue.DateValue.AddDays(secondValue.IntValue);
2168+
return Operand.Create(date);
2169+
}
2170+
2171+
public virtual Operand VisitADDHOURS_fun(mathParser.ADDHOURS_funContext context)
2172+
{
2173+
var args = new List<Operand>();
2174+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
2175+
2176+
var firstValue = args[0].ToMyDate("Function AddHours parameter 1 is error!");
2177+
if (firstValue.IsError) { return firstValue; };
2178+
var secondValue = args[1].ToNumber("Function AddHours parameter 2 is error!");
2179+
if (secondValue.IsError) { return secondValue; };
2180+
2181+
var date = firstValue.DateValue.AddHours(secondValue.IntValue);
2182+
return Operand.Create(date);
2183+
}
2184+
21012185
#endregion
21022186

21032187
#region sum
@@ -3819,6 +3903,8 @@ public virtual Operand VisitDiyFunction_fun(mathParser.DiyFunction_funContext co
38193903
return Operand.Error("DiyFunction is error!");
38203904
}
38213905

3906+
3907+
38223908
#endregion
38233909
}
38243910
}

csharp/ToolGood.Algorithm2/ToolGood.Algorithm2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<Product>ToolGood.Algorithm</Product>
2222
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2323
<SignAssembly>true</SignAssembly>
24-
<Version>3.3.0.1</Version>
24+
<Version>3.4.0.0</Version>
2525
<AssemblyOriginatorKeyFile>ToolGood.Algorithm.snk</AssemblyOriginatorKeyFile>
2626
<DelaySign>false</DelaySign>
2727
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\ToolGood.Algorithm.xml</DocumentationFile>

0 commit comments

Comments
 (0)