@@ -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}
0 commit comments