Skip to content

Commit c354ca7

Browse files
author
linzhijun
committed
添加 UseLocalTime
1 parent 7839902 commit c354ca7

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

csharp/ToolGood.Algorithm2/AlgorithmEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ public String GetSimplifiedFormula(String formula)
732732
visitor.GetParameter += GetDiyParameterInside;
733733
visitor.excelIndex = UseExcelIndex ? 1 : 0;
734734
visitor.DiyFunction += ExecuteDiyFunction;
735+
visitor.useLocalTime = UseLocalTime;
735736

736737
Operand obj = visitor.Visit(_context);
737738
obj = obj.ToText("It can't be converted to String!");

csharp/ToolGood.Algorithm2/AlgorithmEngineEx.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public class AlgorithmEngineEx
4444
/// 跳过公式错误
4545
/// </summary>
4646
public bool JumpFormulaError { get; set; } = false;
47+
/// <summary>
48+
/// 使用 本地时间, 影响 时间截转化
49+
/// </summary>
50+
public bool UseLocalTime { get; set; } = false;
4751
private readonly Dictionary<string, Operand> _tempdict;
4852

4953

@@ -181,6 +185,7 @@ private Operand EvaluateCategory(ProgContext context)
181185
visitor.GetParameter += GetDiyParameterInside;
182186
visitor.excelIndex = UseExcelIndex ? 1 : 0;
183187
visitor.DiyFunction += ExecuteDiyFunction;
188+
visitor.useLocalTime = UseLocalTime;
184189
return visitor.Visit(context);
185190
} catch (Exception ex) {
186191
LastError = ex.Message + "\r\n" + ex.StackTrace;
@@ -713,7 +718,10 @@ public DateTime TryEvaluateCategory(string categoryName, DateTime def)
713718
LastError = obj.ErrorMsg;
714719
return def;
715720
}
716-
return (DateTime)obj.DateValue;
721+
if (UseLocalTime) {
722+
return obj.DateValue.ToDateTime(DateTimeKind.Local);
723+
}
724+
return obj.DateValue.ToDateTime(DateTimeKind.Utc);
717725
} catch (Exception ex) {
718726
LastError = ex.Message + "\r\n" + ex.StackTrace;
719727
}
@@ -1014,7 +1022,10 @@ public DateTime TryEvaluate(string exp, DateTime def)
10141022
LastError = obj.ErrorMsg;
10151023
return def;
10161024
}
1017-
return (DateTime)obj.DateValue;
1025+
if (UseLocalTime) {
1026+
return obj.DateValue.ToDateTime(DateTimeKind.Local);
1027+
}
1028+
return obj.DateValue.ToDateTime(DateTimeKind.Utc);
10181029
} catch (Exception ex) {
10191030
LastError = ex.Message + "\r\n" + ex.StackTrace;
10201031
}
@@ -1082,7 +1093,7 @@ public String GetSimplifiedFormula(String formula)
10821093
visitor.GetParameter += GetDiyParameterInside;
10831094
visitor.excelIndex = UseExcelIndex ? 1 : 0;
10841095
visitor.DiyFunction += ExecuteDiyFunction;
1085-
1096+
visitor.useLocalTime=UseLocalTime;
10861097
Operand obj = visitor.Visit(_context);
10871098
obj = obj.ToText("It can't be converted to String!");
10881099
if (obj.IsError) {

csharp/ToolGood.Algorithm2/AlgorithmEngineHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ private static HashSet<string> GetLexerSet()
236236
lexerSet.Add("ADDHOURS");
237237
lexerSet.Add("ADDMINUTES");
238238
lexerSet.Add("ADDSECONDS");
239+
lexerSet.Add("TIMESTAMP");
239240
_lexerSet = lexerSet;
240241
}
241242
return _lexerSet;

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class AlgorithmEngine {
3535
* 是否忽略大小写
3636
*/
3737
public final boolean IgnoreCase;
38-
public boolean UseLocalTime=false;
38+
public boolean UseLocalTime = false;
3939
private ProgContext _context;
4040
private final Map<String, Operand> _tempdict;
4141

@@ -229,7 +229,7 @@ public Operand Evaluate() throws Exception {
229229
visitor.DiyFunction = f -> {
230230
return ExecuteDiyFunction(f.Name, f.OperandList);
231231
};
232-
visitor.useLocalTime=UseLocalTime;
232+
visitor.useLocalTime = UseLocalTime;
233233
return visitor.visit(_context);
234234
}
235235

@@ -395,6 +395,7 @@ public String GetSimplifiedFormula(final String formula) {
395395
visitor.DiyFunction = f -> {
396396
return ExecuteDiyFunction(f.Name, f.OperandList);
397397
};
398+
visitor.useLocalTime = UseLocalTime;
398399
Operand obj = visitor.visit(_context);
399400
obj = obj.ToText("It can't be converted to String!");
400401
if (obj.IsError()) {

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngineEx.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
package toolgood.algorithm;
22

3-
import java.math.BigDecimal;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
import java.util.Map;
7-
import java.util.TreeMap;
8-
93
import org.antlr.v4.runtime.CharStreams;
104
import org.antlr.v4.runtime.CommonTokenStream;
115
import org.joda.time.DateTime;
12-
13-
import toolgood.algorithm.internals.AntlrCharStream;
14-
import toolgood.algorithm.internals.AntlrErrorListener;
15-
import toolgood.algorithm.internals.CharUtil;
16-
import toolgood.algorithm.internals.ConditionCacheInfo;
17-
import toolgood.algorithm.internals.MathSimplifiedFormulaVisitor;
18-
import toolgood.algorithm.internals.MathVisitor;
6+
import org.joda.time.DateTimeZone;
7+
import toolgood.algorithm.internals.*;
198
import toolgood.algorithm.litJson.JsonData;
209
import toolgood.algorithm.litJson.JsonMapper;
2110
import toolgood.algorithm.math.mathLexer;
2211
import toolgood.algorithm.math.mathParser;
2312
import toolgood.algorithm.math.mathParser.ProgContext;
2413

14+
import java.math.BigDecimal;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.TreeMap;
19+
2520
/**
2621
* 算法引擎 扩展版
2722
* 支持 一层算法 内套 一层算法
@@ -52,6 +47,10 @@ public class AlgorithmEngineEx {
5247
* 跳过公式错误
5348
*/
5449
public Boolean JumpFormulaError = false;
50+
/**
51+
* 使用本地时区
52+
*/
53+
public Boolean UseLocalTime = false;
5554
private final Map<String, Operand> _tempdict;
5655
private ConditionCache MultiConditionCache;
5756

@@ -125,7 +124,7 @@ public void ClearParameters() {
125124

126125
/**
127126
* 执行
128-
*
127+
*
129128
* @param categoryName
130129
* @return
131130
*/
@@ -175,7 +174,7 @@ public Operand EvaluateCategory(String categoryName) {
175174

176175
/**
177176
* 查找备注
178-
*
177+
*
179178
* @param categoryName
180179
* @return
181180
* @throws Exception
@@ -369,6 +368,7 @@ public double TryEvaluateCategory(final String categoryName, final double defval
369368
}
370369
return defvalue;
371370
}
371+
372372
public long TryEvaluateCategory(final String categoryName, final long defvalue) {
373373
try {
374374
Operand obj = EvaluateCategory(categoryName);
@@ -425,7 +425,10 @@ public DateTime TryEvaluateCategory(final String categoryName, final DateTime de
425425
LastError = obj.ErrorMsg();
426426
return defvalue;
427427
}
428-
return obj.DateValue().ToDateTime();
428+
if (UseLocalTime) {
429+
return obj.DateValue().ToDateTime(DateTimeZone.getDefault());
430+
}
431+
return obj.DateValue().ToDateTime(DateTimeZone.UTC);
429432
} catch (final Exception ex) {
430433
LastError = ex.getMessage();
431434
}
@@ -446,6 +449,7 @@ public MyDate TryEvaluateCategory(final String categoryName, final MyDate defval
446449
}
447450
return defvalue;
448451
}
452+
449453
public BigDecimal TryEvaluate(final String exp, final BigDecimal defvalue) {
450454
try {
451455
Operand obj = Evaluate(exp);
@@ -490,6 +494,7 @@ public double TryEvaluate(final String exp, final double defvalue) {
490494
}
491495
return defvalue;
492496
}
497+
493498
public long TryEvaluate(final String exp, final long defvalue) {
494499
try {
495500
Operand obj = Evaluate(exp);
@@ -546,7 +551,10 @@ public DateTime TryEvaluate(final String exp, final DateTime defvalue) {
546551
LastError = obj.ErrorMsg();
547552
return defvalue;
548553
}
549-
return obj.DateValue().ToDateTime();
554+
if (UseLocalTime) {
555+
return obj.DateValue().ToDateTime(DateTimeZone.getDefault());
556+
}
557+
return obj.DateValue().ToDateTime(DateTimeZone.UTC);
550558
} catch (final Exception ex) {
551559
LastError = ex.getMessage();
552560
}
@@ -582,6 +590,7 @@ private Operand EvaluateCategory(ProgContext context) {
582590
visitor.DiyFunction = f -> {
583591
return ExecuteDiyFunction(f.Name, f.OperandList);
584592
};
593+
visitor.useLocalTime = UseLocalTime;
585594
return visitor.visit(context);
586595
} catch (Exception ex) {
587596
LastError = ex.getMessage();
@@ -591,9 +600,8 @@ private Operand EvaluateCategory(ProgContext context) {
591600

592601
/**
593602
* 获取简化公式
594-
*
603+
*
595604
* @param formula 公式
596-
*
597605
*/
598606
public String GetSimplifiedFormula(final String formula) {
599607
try {
@@ -610,6 +618,7 @@ public String GetSimplifiedFormula(final String formula) {
610618
visitor.DiyFunction = f -> {
611619
return ExecuteDiyFunction(f.Name, f.OperandList);
612620
};
621+
visitor.useLocalTime = UseLocalTime;
613622
Operand obj = visitor.visit(_context);
614623
obj = obj.ToText("It can't be converted to String!");
615624
if (obj.IsError()) {
@@ -626,7 +635,7 @@ public String GetSimplifiedFormula(final String formula) {
626635

627636
/**
628637
* 计算公式
629-
*
638+
*
630639
* @param formula 公式
631640
* @param splitChar 分隔符
632641
* @return
@@ -641,7 +650,7 @@ public String EvaluateFormula(String formula, Character splitChar) {
641650

642651
/**
643652
* 计算公式
644-
*
653+
*
645654
* @param formula 公式
646655
* @param splitChars 分隔符
647656
* @return

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngineHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ private static Set<String> GetLexerSet() {
243243
lexerSet.add("ADDHOURS");
244244
lexerSet.add("ADDMINUTES");
245245
lexerSet.add("ADDSECONDS");
246+
lexerSet.add("TIMESTAMP");
246247
_lexerSet = lexerSet;
247248
}
248249
return _lexerSet;

0 commit comments

Comments
 (0)