Skip to content

Commit 495defd

Browse files
author
linzhijun
committed
添加 时间截转化
1 parent fad14e4 commit 495defd

File tree

12 files changed

+7565
-5396
lines changed

12 files changed

+7565
-5396
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public void DATEVALUE_Test()
1818
dt = engine.TryEvaluate("DATEVALUE('2016-01-01')", DateTime.MinValue);
1919
Assert.AreEqual(dt, new DateTime(2016, 1, 1));
2020

21+
// chinese time
22+
dt = engine.TryEvaluate("DATEVALUE('1691234899000',0)", DateTime.Now);
23+
Assert.AreEqual(dt.ToLocalTime(), new DateTime(2023, 8, 5, 19, 28, 19));
24+
25+
// chinese time
26+
dt = engine.TryEvaluate("DATEVALUE('1691234899',0)", DateTime.Now);
27+
Assert.AreEqual(dt.ToLocalTime(), new DateTime(2023, 8, 5, 19, 28, 19));
2128

2229
}
2330
[Test]

csharp/ToolGood.Algorithm2.Test/ToolGood.Algorithm2.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.1" />
9+
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

csharp/ToolGood.Algorithm2/AlgorithmEngine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public class AlgorithmEngine
3030
/// 是否忽略大小写
3131
/// </summary>
3232
public bool IgnoreCase { get; private set; }
33+
/// <summary>
34+
/// 使用 本地时间, 影响 时间截转化
35+
/// </summary>
36+
public bool UseLocalTime { get; set; } = false;
3337

3438
private ProgContext _context;
3539
private readonly Dictionary<string, Operand> _tempdict;
@@ -368,6 +372,7 @@ public Operand Evaluate()
368372
visitor.GetParameter += GetDiyParameterInside;
369373
visitor.excelIndex = UseExcelIndex ? 1 : 0;
370374
visitor.DiyFunction += ExecuteDiyFunction;
375+
visitor.useLocalTime = UseLocalTime;
371376
return visitor.Visit(_context);
372377
}
373378
#endregion

csharp/ToolGood.Algorithm2/Internals/DiyNameVisitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,5 +1157,10 @@ public object VisitADDHOURS_fun([NotNull] mathParser.ADDHOURS_funContext context
11571157
{
11581158
return VisitChildren(context);
11591159
}
1160+
1161+
public object VisitTIMESTAMP_fun([NotNull] mathParser.TIMESTAMP_funContext context)
1162+
{
1163+
return VisitChildren(context);
1164+
}
11601165
}
11611166
}

csharp/ToolGood.Algorithm2/Internals/MathSplitVisitor.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace ToolGood.Algorithm.Internals
88
{
9-
sealed class MathSplitVisitor : AbstractParseTreeVisitor<ConditionTree>, ImathVisitor<ConditionTree>
9+
sealed class MathSplitVisitor : AbstractParseTreeVisitor<ConditionTree>, ImathVisitor<ConditionTree>
1010
{
1111
public ConditionTree VisitProg(mathParser.ProgContext context)
1212
{
@@ -22,7 +22,7 @@ public ConditionTree VisitAndOr_fun(mathParser.AndOr_funContext context)
2222
} else {
2323
tree.Type = ConditionTreeType.Or;
2424
}
25-
var exprs= context.expr();
25+
var exprs = context.expr();
2626

2727
tree.Nodes.Add(exprs[0].Accept(this));
2828
tree.Nodes.Add(exprs[1].Accept(this));
@@ -1161,5 +1161,10 @@ public ConditionTree VisitADDHOURS_fun([NotNull] mathParser.ADDHOURS_funContext
11611161
{
11621162
return Visit_fun(context);
11631163
}
1164+
1165+
public ConditionTree VisitTIMESTAMP_fun([NotNull] mathParser.TIMESTAMP_funContext context)
1166+
{
1167+
return Visit_fun(context);
1168+
}
11641169
}
11651170
}

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class MathVisitor : AbstractParseTreeVisitor<Operand>, ImathVisitor<Operand>
2222
public event Func<string, Operand> GetParameter;
2323
public event Func<string, List<Operand>, Operand> DiyFunction;
2424
public int excelIndex;
25+
public bool useLocalTime;
2526

2627
#region base
2728

@@ -1172,8 +1173,8 @@ public virtual Operand VisitMROUND_fun(mathParser.MROUND_funContext context)
11721173
public virtual Operand VisitRAND_fun(mathParser.RAND_funContext context)
11731174
{
11741175
#if NETSTANDARD2_1
1175-
var tick = DateTime.Now.Ticks;
1176-
Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
1176+
var tick = DateTime.Now.Ticks;
1177+
Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
11771178
#else
11781179
Random rand = Random.Shared;
11791180
#endif
@@ -1188,8 +1189,8 @@ public virtual Operand VisitRANDBETWEEN_fun(mathParser.RANDBETWEEN_funContext co
11881189
var secondValue = args[1];
11891190

11901191
#if NETSTANDARD2_1
1191-
var tick = DateTime.Now.Ticks;
1192-
Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
1192+
var tick = DateTime.Now.Ticks;
1193+
Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
11931194
#else
11941195
Random rand = Random.Shared;
11951196
#endif
@@ -1728,14 +1729,79 @@ private static string F_base_ToChineseRMB(decimal x)
17281729

17291730
public virtual Operand VisitDATEVALUE_fun(mathParser.DATEVALUE_funContext context)
17301731
{
1731-
var firstValue = context.expr().Accept(this).ToText("Function DATEVALUE parameter is error!");
1732-
if (firstValue.IsError) { return firstValue; }
1733-
1734-
if (DateTime.TryParse(firstValue.TextValue, cultureInfo, DateTimeStyles.None, out DateTime dt)) {
1735-
return Operand.Create(dt);
1732+
var args = new List<Operand>();
1733+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
1734+
int type = 1; // 文本转时间
1735+
if (args.Count == 2) {
1736+
var secondValue = args[1].ToNumber("Function DATEVALUE parameter 2 is error!");
1737+
if (secondValue.IsError) { return secondValue; }
1738+
type = secondValue.IntValue;
1739+
}
1740+
if (type == 0) {
1741+
if (args[0].Type == OperandType.TEXT) {
1742+
if (DateTime.TryParse(args[0].TextValue, cultureInfo, DateTimeStyles.None, out DateTime time)) {
1743+
return Operand.Create(time);
1744+
}
1745+
}
1746+
var firstValue = args[0].ToNumber("Function DATEVALUE parameter 1 is error!");
1747+
if (firstValue.LongValue <= 2958465L) { // 9999-12-31 日时间在excel的数字为 2958465
1748+
return firstValue.ToMyDate();
1749+
}
1750+
if (firstValue.LongValue <= 253402232399L) { // 9999-12-31 12:59:59 日时间 转 时间截 为 253402232399L,
1751+
var time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(firstValue.LongValue);
1752+
if (useLocalTime) { return Operand.Create(time.ToLocalTime()); }
1753+
return Operand.Create(time);
1754+
}
1755+
// 注:时间截 253402232399 ms 转时间 为 1978-01-12 05:30:32
1756+
var time2 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(firstValue.LongValue);
1757+
if (useLocalTime) { return Operand.Create(time2.ToLocalTime()); }
1758+
return Operand.Create(time2);
1759+
} else if (type == 1) {
1760+
var firstValue = args[0].ToText("Function DATEVALUE parameter 1 is error!");
1761+
if (firstValue.IsError) { return firstValue; }
1762+
if (DateTime.TryParse(firstValue.TextValue, cultureInfo, DateTimeStyles.None, out DateTime dt)) {
1763+
return Operand.Create(dt);
1764+
}
1765+
} else if (type == 2) {
1766+
return args[0].ToNumber("Function DATEVALUE parameter is error!").ToMyDate();
1767+
} else if (type == 3) {
1768+
var firstValue = args[0].ToNumber("Function DATEVALUE parameter 1 is error!");
1769+
var time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(firstValue.LongValue);
1770+
if (useLocalTime) { return Operand.Create(time.ToLocalTime()); }
1771+
return Operand.Create(time);
1772+
} else if (type == 4) {
1773+
var firstValue = args[0].ToNumber("Function DATEVALUE parameter 1 is error!");
1774+
var time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(firstValue.LongValue);
1775+
if (useLocalTime) { return Operand.Create(time.ToLocalTime()); }
1776+
return Operand.Create(time);
17361777
}
17371778
return Operand.Error("Function DATEVALUE parameter is error!");
17381779
}
1780+
public Operand VisitTIMESTAMP_fun(mathParser.TIMESTAMP_funContext context)
1781+
{
1782+
var args = new List<Operand>();
1783+
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
1784+
int type = 0; // 毫秒
1785+
if (args.Count == 2) {
1786+
var secondValue = args[1].ToNumber("Function TIMESTAMP parameter 2 is error!");
1787+
if (secondValue.IsError) { return secondValue; }
1788+
type = secondValue.IntValue;
1789+
}
1790+
DateTime firstValue;
1791+
if (useLocalTime) {
1792+
firstValue = args[0].ToMyDate("Function TIMESTAMP parameter 1 is error!").DateValue.ToDateTime(DateTimeKind.Local).ToUniversalTime();
1793+
} else {
1794+
firstValue = args[0].ToMyDate("Function TIMESTAMP parameter 1 is error!").DateValue.ToDateTime(DateTimeKind.Utc);
1795+
}
1796+
if (type == 0) {
1797+
var ms = (firstValue - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
1798+
return Operand.Create(ms);
1799+
} else if (type == 1) {
1800+
var s = (firstValue - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
1801+
return Operand.Create(s);
1802+
}
1803+
return Operand.Error("Function TIMESTAMP parameter is error!");
1804+
}
17391805
public virtual Operand VisitTIMEVALUE_fun(mathParser.TIMEVALUE_funContext context)
17401806
{
17411807
var firstValue = context.expr().Accept(this).ToText("Function TIMEVALUE parameter is error!");
@@ -3905,6 +3971,8 @@ public virtual Operand VisitDiyFunction_fun(mathParser.DiyFunction_funContext co
39053971

39063972

39073973

3974+
3975+
39083976
#endregion
39093977
}
39103978
}

csharp/ToolGood.Algorithm2/MyDate.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ public DateTime ToDateTime()
234234
return new DateTime(Year ?? 0, Month ?? 0, Day ?? 0, Hour, Minute, Second);
235235
}
236236
/// <summary>
237+
/// 转DateTime
238+
/// </summary>
239+
/// <returns></returns>
240+
public DateTime ToDateTime(DateTimeKind dateTimeKind)
241+
{
242+
return new DateTime(Year ?? 0, Month ?? 0, Day ?? 0, Hour, Minute, Second, dateTimeKind);
243+
}
244+
/// <summary>
237245
/// 转TimeSpan
238246
/// </summary>
239247
/// <returns></returns>

csharp/ToolGood.Algorithm2/ToolGood.Algorithm2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
<ItemGroup>
32-
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.12.0" />
32+
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.13.0" />
3333
</ItemGroup>
3434

3535

0 commit comments

Comments
 (0)