Skip to content

Commit 05478a7

Browse files
author
linzhijun
committed
java 添加 时间截 转化
1 parent 4a4649c commit 05478a7

File tree

14 files changed

+5041
-4713
lines changed

14 files changed

+5041
-4713
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public void DATEVALUE_Test()
2626
dt = engine.TryEvaluate("DATEVALUE('1691234899',0)", DateTime.Now);
2727
Assert.AreEqual(dt.ToLocalTime(), new DateTime(2023, 8, 5, 19, 28, 19));
2828

29+
30+
engine.UseLocalTime = true;
31+
// chinese time
32+
dt = engine.TryEvaluate("DATEVALUE('1691234899000',0)", DateTime.Now);
33+
Assert.AreEqual(dt, new DateTime(2023, 8, 5, 19, 28, 19));
34+
35+
// chinese time
36+
dt = engine.TryEvaluate("DATEVALUE('1691234899',0)", DateTime.Now);
37+
Assert.AreEqual(dt, new DateTime(2023, 8, 5, 19, 28, 19));
2938
}
3039
[Test]
3140
public void TIMESTAMP_Test()

csharp/ToolGood.Algorithm2/AlgorithmEngine.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,10 @@ public DateTime TryEvaluate(string exp, DateTime def)
659659
LastError = obj.ErrorMsg;
660660
return def;
661661
}
662-
return (DateTime)obj.DateValue;
662+
if (UseLocalTime) {
663+
return obj.DateValue.ToDateTime(DateTimeKind.Local);
664+
}
665+
return obj.DateValue.ToDateTime(DateTimeKind.Utc);
663666
}
664667
} catch (Exception ex) {
665668
LastError = ex.Message + "\r\n" + ex.StackTrace;

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,7 @@ public virtual Operand VisitDATEVALUE_fun(mathParser.DATEVALUE_funContext contex
17311731
{
17321732
var args = new List<Operand>();
17331733
foreach (var item in context.expr()) { var aa = item.Accept(this); if (aa.IsError) { return aa; } args.Add(aa); }
1734-
int type = 1; // 文本转时间
1734+
int type = 0;
17351735
if (args.Count == 2) {
17361736
var secondValue = args[1].ToNumber("Function DATEVALUE parameter 2 is error!");
17371737
if (secondValue.IsError) { return secondValue; }

csharp/ToolGood.Algorithm2/MyDate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ internal string ToString(string f)
231231
/// <returns></returns>
232232
public DateTime ToDateTime()
233233
{
234-
return new DateTime(Year ?? 0, Month ?? 0, Day ?? 0, Hour, Minute, Second);
234+
return new DateTime(Year ?? 0, Month ?? 0, Day ?? 0, Hour, Minute, Second, DateTimeKind.Utc);
235235
}
236236
/// <summary>
237237
/// 转DateTime

java/toolgood.algorithm/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
<dependency>
5757
<groupId>org.antlr</groupId>
5858
<artifactId>antlr4-runtime</artifactId>
59-
<version>4.12.0</version>
59+
<version>4.13.0</version>
6060
</dependency>
6161
<dependency>
6262
<groupId>joda-time</groupId>
6363
<artifactId>joda-time</artifactId>
64-
<version>2.12.2</version>
64+
<version>2.12.5</version>
6565
</dependency>
6666
<dependency>
6767
<groupId>org.apache.commons</groupId>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.antlr.v4.runtime.CommonTokenStream;
55
import org.antlr.v4.runtime.RecognitionException;
66
import org.joda.time.DateTime;
7+
import org.joda.time.DateTimeZone;
78
import toolgood.algorithm.internals.*;
89
import toolgood.algorithm.litJson.JsonData;
910
import toolgood.algorithm.litJson.JsonMapper;
@@ -34,6 +35,7 @@ public class AlgorithmEngine {
3435
* 是否忽略大小写
3536
*/
3637
public final boolean IgnoreCase;
38+
public boolean UseLocalTime=false;
3739
private ProgContext _context;
3840
private final Map<String, Operand> _tempdict;
3941

@@ -223,9 +225,11 @@ public Operand Evaluate() throws Exception {
223225
return null;
224226
};
225227
visitor.excelIndex = UseExcelIndex ? 1 : 0;
228+
226229
visitor.DiyFunction = f -> {
227230
return ExecuteDiyFunction(f.Name, f.OperandList);
228231
};
232+
visitor.useLocalTime=UseLocalTime;
229233
return visitor.visit(_context);
230234
}
231235

@@ -343,7 +347,10 @@ public DateTime TryEvaluate(final String exp, final DateTime defvalue) {
343347
LastError = obj.ErrorMsg();
344348
return defvalue;
345349
}
346-
return obj.DateValue().ToDateTime();
350+
if (UseLocalTime) {
351+
return obj.DateValue().ToDateTime(DateTimeZone.getDefault());
352+
}
353+
return obj.DateValue().ToDateTime(DateTimeZone.UTC);
347354
}
348355
} catch (final Exception ex) {
349356
LastError = ex.getMessage();

java/toolgood.algorithm/src/main/java/toolgood/algorithm/MyDate.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ public String toString(String f) {
249249
public DateTime ToDateTime() {
250250
return new DateTime(Year, Month, Day, Hour, Minute, Second, DateTimeZone.UTC);
251251
}
252+
public DateTime ToDateTime(DateTimeZone zoo) {
253+
return new DateTime(Year, Month, Day, Hour, Minute, Second, zoo);
254+
}
252255

253256
public int DayOfWeek() {
254257
return new DateTime(Year, Month, Day, 0, 0, 0, DateTimeZone.UTC).dayOfWeek().get();

java/toolgood.algorithm/src/main/java/toolgood/algorithm/internals/DiyNameVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ public Object visitLARGE_fun(LARGE_funContext context) {
680680
return visitChildren(context);
681681
}
682682

683+
@Override
684+
public Object visitTIMESTAMP_fun(TIMESTAMP_funContext context) {
685+
return visitChildren(context);
686+
}
687+
683688
@Override
684689
public Object visitVALUE_fun(VALUE_funContext context) {
685690

java/toolgood.algorithm/src/main/java/toolgood/algorithm/internals/MathSplitVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,11 @@ public ConditionTree visitLARGE_fun(mathParser.LARGE_funContext context) {
573573
return visit_fun(context);
574574
}
575575

576+
@Override
577+
public ConditionTree visitTIMESTAMP_fun(mathParser.TIMESTAMP_funContext context) {
578+
return visit_fun(context);
579+
}
580+
576581
@Override
577582
public ConditionTree visitVALUE_fun(mathParser.VALUE_funContext context) {
578583
return visit_fun(context);

java/toolgood.algorithm/src/main/java/toolgood/algorithm/internals/MathVisitor.java

Lines changed: 154 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class MathVisitor extends AbstractParseTreeVisitor<Operand> implements ma
3535
public Function<String, Operand> GetParameter;
3636
public Function<MyFunction, Operand> DiyFunction;
3737
public int excelIndex;
38+
public boolean useLocalTime;
3839

3940
public Operand visitProg(final ProgContext context) {
4041
return context.expr().accept(this);
@@ -2347,17 +2348,108 @@ private String F_base_ToChineseRMB(final BigDecimal numberOfMoney) {
23472348
}
23482349

23492350
public Operand visitDATEVALUE_fun(final DATEVALUE_funContext context) {
2350-
final Operand firstValue = context.expr().accept(this).ToText("Function DATEVALUE parameter is error!");
2351-
if (firstValue.IsError()) {
2352-
return firstValue;
2351+
final List<Operand> args = new ArrayList<Operand>();
2352+
for (final ExprContext item : context.expr()) {
2353+
final Operand aa = item.accept(this);
2354+
if (aa.IsError()) {
2355+
return aa;
2356+
}
2357+
args.add(aa);
23532358
}
2354-
MyDate date = MyDate.parse(firstValue.TextValue());
2355-
if (date != null) {
2356-
return Operand.Create(date);
2359+
int type = 0;
2360+
if (args.size() == 2) {
2361+
Operand secondValue = args.get(1).ToNumber("Function DATEVALUE parameter 2 is error!");
2362+
if (secondValue.IsError()) {
2363+
return secondValue;
2364+
}
2365+
type = secondValue.IntValue();
2366+
}
2367+
if (type == 0) {
2368+
if (args.get(0).Type() == OperandType.TEXT) {
2369+
MyDate date = MyDate.parse(args.get(0).TextValue());
2370+
if (date != null) {
2371+
return Operand.Create(date);
2372+
}
2373+
}
2374+
Operand firstValue = args.get(0).ToNumber("Function DATEVALUE parameter 1 is error!");
2375+
if (firstValue.LongValue() <= 2958465L) { // 9999-12-31 日时间在excel的数字为 2958465
2376+
return firstValue.ToDate("Function DATEVALUE parameter 1 is error!");
2377+
}
2378+
if (firstValue.LongValue() <= 253402232399L) { // 9999-12-31 12:59:59 日时间 转 时间截 为 253402232399L,
2379+
DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeZone.UTC).plus (firstValue.LongValue()*1000);
2380+
if (useLocalTime) {
2381+
return Operand.Create(time.toDateTime(DateTimeZone.getDefault()));
2382+
}
2383+
return Operand.Create(time);
2384+
}
2385+
// 注:时间截 253402232399 ms 转时间 为 1978-01-12 05:30:32
2386+
DateTime time2 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeZone.UTC).plus(firstValue.LongValue());
2387+
if (useLocalTime) {
2388+
return Operand.Create(time2.toDateTime(DateTimeZone.getDefault()));
2389+
}
2390+
return Operand.Create(time2);
2391+
} else if (type == 1) {
2392+
Operand firstValue = args.get(0).ToText("Function DATEVALUE parameter 1 is error!");
2393+
if (firstValue.IsError()) {
2394+
return firstValue;
2395+
}
2396+
MyDate date = MyDate.parse(firstValue.TextValue());
2397+
if (date != null) {
2398+
return Operand.Create(date);
2399+
}
2400+
} else if (type == 2) {
2401+
return args.get(0).ToNumber("Function DATEVALUE parameter is error!").ToDate("");
2402+
} else if (type == 3) {
2403+
Operand firstValue = args.get(0).ToNumber("Function DATEVALUE parameter 1 is error!");
2404+
DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeZone.UTC).plus(firstValue.LongValue());
2405+
if (useLocalTime) {
2406+
return Operand.Create(time.toDateTime(DateTimeZone.getDefault()));
2407+
}
2408+
return Operand.Create(time);
2409+
} else if (type == 4) {
2410+
Operand firstValue = args.get(0).ToNumber("Function DATEVALUE parameter 1 is error!");
2411+
DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeZone.UTC).plus(firstValue.LongValue()*1000);
2412+
if (useLocalTime) {
2413+
return Operand.Create(time.toDateTime(DateTimeZone.getDefault()));
2414+
}
2415+
return Operand.Create(time);
23572416
}
23582417
return Operand.Error("Function DATEVALUE parameter is error!");
23592418
}
23602419

2420+
@Override
2421+
public Operand visitTIMESTAMP_fun(TIMESTAMP_funContext context) {
2422+
final List<Operand> args = new ArrayList<Operand>();
2423+
for (final ExprContext item : context.expr()) {
2424+
final Operand aa = item.accept(this);
2425+
if (aa.IsError()) {
2426+
return aa;
2427+
}
2428+
args.add(aa);
2429+
}
2430+
int type = 0; // 毫秒
2431+
if (args.size() == 2) {
2432+
Operand secondValue = args.get(1).ToNumber("Function TIMESTAMP parameter 2 is error!");
2433+
if (secondValue.IsError()) {
2434+
return secondValue;
2435+
}
2436+
type = secondValue.IntValue();
2437+
}
2438+
DateTime firstValue;
2439+
if (useLocalTime) {
2440+
firstValue = args.get(0).ToDate("Function TIMESTAMP parameter 1 is error!").DateValue().ToDateTime(DateTimeZone.getDefault());
2441+
} else {
2442+
firstValue = args.get(0).ToDate("Function TIMESTAMP parameter 1 is error!").DateValue().ToDateTime(DateTimeZone.UTC);
2443+
}
2444+
if (type == 0) {
2445+
firstValue.getMillis();
2446+
return Operand.Create(firstValue.getMillis());
2447+
} else if (type == 1) {
2448+
return Operand.Create(firstValue.getMillis() / 1000);
2449+
}
2450+
return Operand.Error("Function TIMESTAMP parameter is error!");
2451+
}
2452+
23612453
public Operand visitTIMEVALUE_fun(final TIMEVALUE_funContext context) {
23622454
final Operand firstValue = context.expr().accept(this).ToText("Function TIMEVALUE parameter is error!");
23632455
if (firstValue.IsError()) {
@@ -2854,11 +2946,17 @@ public Operand visitADDYEARS_fun(ADDYEARS_funContext context) {
28542946
}
28552947

28562948
Operand firstValue = args.get(0).ToDate("Function AddYears parameter 1 is error!");
2857-
if (firstValue.IsError()) { return firstValue; };
2949+
if (firstValue.IsError()) {
2950+
return firstValue;
2951+
}
2952+
;
28582953
Operand secondValue = args.get(1).ToNumber("Function AddYears parameter 2 is error!");
2859-
if (secondValue.IsError()) { return secondValue; };
2954+
if (secondValue.IsError()) {
2955+
return secondValue;
2956+
}
2957+
;
28602958

2861-
MyDate date= firstValue.DateValue().AddYears(secondValue.IntValue());
2959+
MyDate date = firstValue.DateValue().AddYears(secondValue.IntValue());
28622960
return Operand.Create(date);
28632961
}
28642962

@@ -2874,13 +2972,20 @@ public Operand visitADDMONTHS_fun(ADDMONTHS_funContext context) {
28742972
}
28752973

28762974
Operand firstValue = args.get(0).ToDate("Function AddMonths parameter 1 is error!");
2877-
if (firstValue.IsError()) { return firstValue; };
2975+
if (firstValue.IsError()) {
2976+
return firstValue;
2977+
}
2978+
;
28782979
Operand secondValue = args.get(1).ToNumber("Function AddMonths parameter 2 is error!");
2879-
if (secondValue.IsError()) { return secondValue; };
2980+
if (secondValue.IsError()) {
2981+
return secondValue;
2982+
}
2983+
;
28802984

2881-
MyDate date= firstValue.DateValue().AddMonths(secondValue.IntValue());
2985+
MyDate date = firstValue.DateValue().AddMonths(secondValue.IntValue());
28822986
return Operand.Create(date);
28832987
}
2988+
28842989
@Override
28852990
public Operand visitADDDAYS_fun(ADDDAYS_funContext context) {
28862991
final List<Operand> args = new ArrayList<Operand>();
@@ -2893,11 +2998,17 @@ public Operand visitADDDAYS_fun(ADDDAYS_funContext context) {
28932998
}
28942999

28953000
Operand firstValue = args.get(0).ToDate("Function AddDays parameter 1 is error!");
2896-
if (firstValue.IsError()) { return firstValue; };
3001+
if (firstValue.IsError()) {
3002+
return firstValue;
3003+
}
3004+
;
28973005
Operand secondValue = args.get(1).ToNumber("Function AddDays parameter 2 is error!");
2898-
if (secondValue.IsError()) { return secondValue; };
3006+
if (secondValue.IsError()) {
3007+
return secondValue;
3008+
}
3009+
;
28993010

2900-
MyDate date= firstValue.DateValue().AddDays(secondValue.IntValue());
3011+
MyDate date = firstValue.DateValue().AddDays(secondValue.IntValue());
29013012
return Operand.Create(date);
29023013
}
29033014

@@ -2913,11 +3024,17 @@ public Operand visitADDHOURS_fun(ADDHOURS_funContext context) {
29133024
}
29143025

29153026
Operand firstValue = args.get(0).ToDate("Function AddHours parameter 1 is error!");
2916-
if (firstValue.IsError()) { return firstValue; };
3027+
if (firstValue.IsError()) {
3028+
return firstValue;
3029+
}
3030+
;
29173031
Operand secondValue = args.get(1).ToNumber("Function AddHours parameter 2 is error!");
2918-
if (secondValue.IsError()) { return secondValue; };
3032+
if (secondValue.IsError()) {
3033+
return secondValue;
3034+
}
3035+
;
29193036

2920-
MyDate date= firstValue.DateValue().AddHours(secondValue.IntValue());
3037+
MyDate date = firstValue.DateValue().AddHours(secondValue.IntValue());
29213038
return Operand.Create(date);
29223039
}
29233040

@@ -2933,11 +3050,17 @@ public Operand visitADDMINUTES_fun(ADDMINUTES_funContext context) {
29333050
}
29343051

29353052
Operand firstValue = args.get(0).ToDate("Function AddMinutes parameter 1 is error!");
2936-
if (firstValue.IsError()) { return firstValue; };
3053+
if (firstValue.IsError()) {
3054+
return firstValue;
3055+
}
3056+
;
29373057
Operand secondValue = args.get(1).ToNumber("Function AddMinutes parameter 2 is error!");
2938-
if (secondValue.IsError()) { return secondValue; };
3058+
if (secondValue.IsError()) {
3059+
return secondValue;
3060+
}
3061+
;
29393062

2940-
MyDate date= firstValue.DateValue().AddMinutes(secondValue.IntValue());
3063+
MyDate date = firstValue.DateValue().AddMinutes(secondValue.IntValue());
29413064
return Operand.Create(date);
29423065
}
29433066

@@ -2953,11 +3076,17 @@ public Operand visitADDSECONDS_fun(ADDSECONDS_funContext context) {
29533076
}
29543077

29553078
Operand firstValue = args.get(0).ToDate("Function AddSeconds parameter 1 is error!");
2956-
if (firstValue.IsError()) { return firstValue; };
3079+
if (firstValue.IsError()) {
3080+
return firstValue;
3081+
}
3082+
;
29573083
Operand secondValue = args.get(1).ToNumber("Function AddSeconds parameter 2 is error!");
2958-
if (secondValue.IsError()) { return secondValue; };
3084+
if (secondValue.IsError()) {
3085+
return secondValue;
3086+
}
3087+
;
29593088

2960-
MyDate date= firstValue.DateValue().AddSeconds(secondValue.IntValue());
3089+
MyDate date = firstValue.DateValue().AddSeconds(secondValue.IntValue());
29613090
return Operand.Create(date);
29623091
}
29633092

@@ -3131,6 +3260,7 @@ public Operand visitLARGE_fun(final LARGE_funContext context) {
31313260
return Operand.Create(list.get(list.size() - 1 - (secondValue.IntValue() - excelIndex)));
31323261
}
31333262

3263+
31343264
public Operand visitSMALL_fun(final SMALL_funContext context) {
31353265
final List<Operand> args = new ArrayList<Operand>();
31363266
for (final ExprContext item : context.expr()) {

0 commit comments

Comments
 (0)