Skip to content

Commit 16bf492

Browse files
committed
修复 #12 #13
1 parent 73fe1da commit 16bf492

File tree

4 files changed

+83
-22
lines changed

4 files changed

+83
-22
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using PetaTest;
5+
using ToolGood.Algorithm;
6+
7+
namespace ToolGood.Algorithm2.Test.Tests
8+
{
9+
[TestFixture]
10+
public class IssuesTest
11+
{
12+
[Test]
13+
public void issues_12()
14+
{
15+
AlgorithmEngine engine = new AlgorithmEngine();
16+
var dt = engine.TryEvaluate("Year(44406)=2021", false);
17+
Assert.AreEqual(dt, true);
18+
dt = engine.TryEvaluate("MONTH(44406)=7", false);
19+
Assert.AreEqual(dt, true);
20+
dt = engine.TryEvaluate("DAY(44406)=29", false);
21+
Assert.AreEqual(dt, true);
22+
}
23+
24+
25+
[Test]
26+
public void issues_13()
27+
{
28+
AlgorithmEngine engine = new AlgorithmEngine();
29+
var dt = engine.TryEvaluate("days360(date(2020,5,31),date(2023,12,15))", 0);
30+
Assert.AreEqual(dt, 1275);
31+
}
32+
33+
}
34+
}

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ public Operand VisitYEAR_fun(mathParser.YEAR_funContext context)
18231823
{
18241824
var firstValue = this.Visit(context.expr()).ToMyDate("Function YEAR parameter is error!");
18251825
if (firstValue.IsError) { return firstValue; }
1826-
if (firstValue.DateValue.Year==null) {
1826+
if (firstValue.DateValue.Year == null) {
18271827
return Operand.Error("Function YEAR is error!");
18281828
}
18291829
return Operand.Create(firstValue.DateValue.Year.Value);
@@ -1949,7 +1949,13 @@ public Operand VisitDATEDIF_fun(mathParser.DATEDIF_funContext context)
19491949
#region md
19501950
var mo = endMyDate.Day - startMyDate.Day;
19511951
if (mo < 0) {
1952-
var days = new DateTime(startMyDate.Year, startMyDate.Month + 1, 1).AddDays(-1).Day;
1952+
int days;
1953+
if (startMyDate.Month==12) {
1954+
days = new DateTime(startMyDate.Year+1, 1, 1).AddDays(-1).Day;
1955+
} else {
1956+
days = new DateTime(startMyDate.Year, startMyDate.Month + 1, 1).AddDays(-1).Day;
1957+
1958+
}
19531959
mo += days;
19541960
}
19551961
return Operand.Create((mo));
@@ -1989,19 +1995,39 @@ public Operand VisitDAYS360_fun(mathParser.DAYS360_funContext context)
19891995
if (endMyDate.Day == 31) days += 30;
19901996
if (startMyDate.Day == 31) days -= 30;
19911997
} else {
1992-
if (startMyDate.Day == new DateTime(startMyDate.Year, startMyDate.Month + 1, 1).AddDays(-1).Day) {
1993-
days -= 30;
1998+
if (startMyDate.Month == 12) {
1999+
if (startMyDate.Day == new DateTime(startMyDate.Year + 1, 1, 1).AddDays(-1).Day) {
2000+
days -= 30;
2001+
} else {
2002+
days -= startMyDate.Day;
2003+
}
19942004
} else {
1995-
days -= startMyDate.Day;
2005+
if (startMyDate.Day == new DateTime(startMyDate.Year, startMyDate.Month + 1, 1).AddDays(-1).Day) {
2006+
days -= 30;
2007+
} else {
2008+
days -= startMyDate.Day;
2009+
}
19962010
}
1997-
if (endMyDate.Day == new DateTime(endMyDate.Year, endMyDate.Month + 1, 1).AddDays(-1).Day) {
1998-
if (startMyDate.Day < 30) {
1999-
days += 31;
2011+
if (endMyDate.Month == 12) {
2012+
if (endMyDate.Day == new DateTime(endMyDate.Year+1, 1, 1).AddDays(-1).Day) {
2013+
if (startMyDate.Day < 30) {
2014+
days += 31;
2015+
} else {
2016+
days += 30;
2017+
}
20002018
} else {
2001-
days += 30;
2019+
days += endMyDate.Day;
20022020
}
20032021
} else {
2004-
days += endMyDate.Day;
2022+
if (endMyDate.Day == new DateTime(endMyDate.Year, endMyDate.Month + 1, 1).AddDays(-1).Day) {
2023+
if (startMyDate.Day < 30) {
2024+
days += 31;
2025+
} else {
2026+
days += 30;
2027+
}
2028+
} else {
2029+
days += endMyDate.Day;
2030+
}
20052031
}
20062032
}
20072033
return Operand.Create(days);
@@ -3117,11 +3143,11 @@ public Operand VisitREGEX_fun(mathParser.REGEX_funContext context)
31173143
var secondValue = args[1].ToText("Function REGEX parameter 2 is error!");
31183144
if (secondValue.IsError) { return secondValue; }
31193145

3120-
var b = Regex.Match(firstValue.TextValue, secondValue.TextValue);
3121-
if (b.Success == false) {
3122-
return Operand.Error("Function REGEX is error!");
3123-
}
3124-
return Operand.Create(b.Value);
3146+
var b = Regex.Match(firstValue.TextValue, secondValue.TextValue);
3147+
if (b.Success == false) {
3148+
return Operand.Error("Function REGEX is error!");
3149+
}
3150+
return Operand.Create(b.Value);
31253151
}
31263152
public Operand VisitREGEXREPALCE_fun(mathParser.REGEXREPALCE_funContext context)
31273153
{
@@ -3213,7 +3239,7 @@ public Operand VisitSHA512_fun(mathParser.SHA512_funContext context)
32133239
}
32143240
return Operand.Error("Function SHA512 is error!");
32153241
}
3216-
3242+
32173243
public Operand VisitCRC32_fun(mathParser.CRC32_funContext context)
32183244
{
32193245
var args = new List<Operand>(); int index = 1;
@@ -3275,7 +3301,7 @@ public Operand VisitHMACSHA256_fun(mathParser.HMACSHA256_funContext context)
32753301
}
32763302
var t = Hash.GetHmacSha256String(encoding.GetBytes(args[0].TextValue), args[1].TextValue);
32773303
return Operand.Create(t);
3278-
} catch (Exception) { }
3304+
} catch (Exception) { }
32793305
return Operand.Error("Function HMACSHA256 is error!");
32803306
}
32813307
public Operand VisitHMACSHA512_fun(mathParser.HMACSHA512_funContext context)
@@ -3291,7 +3317,7 @@ public Operand VisitHMACSHA512_fun(mathParser.HMACSHA512_funContext context)
32913317
}
32923318
var t = Hash.GetHmacSha512String(encoding.GetBytes(args[0].TextValue), args[1].TextValue);
32933319
return Operand.Create(t);
3294-
} catch (Exception) { }
3320+
} catch (Exception) { }
32953321
return Operand.Error("Function HMACSHA512 is error!");
32963322
}
32973323
public Operand VisitTRIMSTART_fun(mathParser.TRIMSTART_funContext context)

csharp/ToolGood.Algorithm2/MyDate.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public MyDate(double num)
8787
{
8888
int days = (int)num;
8989
if (days > 365) {
90-
var start = DateTime.MinValue.AddDays((int)days);
90+
var start = new DateTime(1900, 1, 1).AddDays(((int)days) - 2);
9191
Year = start.Year;
9292
Month = start.Month;
9393
Day = start.Day;
@@ -316,9 +316,10 @@ public static implicit operator MyDate(double days)
316316

317317
public static implicit operator double(MyDate MyDate)
318318
{
319-
if (MyDate.Year != null && MyDate.Year>1900) {
319+
if (MyDate.Year != null && MyDate.Year > 1900) {
320320
var dt = new DateTime((MyDate.Year ?? 0), (MyDate.Month ?? 0), (MyDate.Day ?? 0), MyDate.Hour, MyDate.Minute, MyDate.Second);
321-
double days = (double)(dt - DateTime.MinValue).TotalDays;
321+
//double days = (double)(dt - DateTime.MinValue).TotalDays;
322+
double days = (double)(dt - new DateTime(1900, 1, 1)).TotalDays + 2;
322323
days += (MyDate.Hour + (MyDate.Minute + MyDate.Second / 60.0) / 60) / 24;
323324
return days;
324325
}

csharp/ToolGood.Algorithm2/ToolGood.Algorithm2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Product>ToolGood.Algorithm</Product>
2121
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2222
<SignAssembly>true</SignAssembly>
23-
<Version>2.2.0.0</Version>
23+
<Version>2.2.0.1</Version>
2424
<AssemblyOriginatorKeyFile>ToolGood.Algorithm.snk</AssemblyOriginatorKeyFile>
2525
<DelaySign>false</DelaySign>
2626
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\ToolGood.Algorithm.xml</DocumentationFile>

0 commit comments

Comments
 (0)