Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DateTimeLocal.cs
1using System;
2using System.Collections.Generic;
7
9{
14 {
23 : base(new ScriptNode[] { String }, argumentTypes1Scalar, Start, Length, Expression)
24 {
25 }
26
38 {
39 }
40
56 {
57 }
58
73 ScriptNode MSecond, int Start, int Length, Expression Expression)
75 {
76 }
77
81 public override string FunctionName => nameof(DateTimeLocal);
82
86 public override string[] DefaultArgumentNames
87 {
88 get { return new string[] { "Year", "Month", "Day", "Hour", "Minute", "Second", "MSecond" }; }
89 }
90
98 {
99 int i, c = this.Arguments.Length;
100
101 if (c == 1)
102 {
103 object Obj = Arguments[0].AssociatedObjectValue;
104
105 if (Obj is System.DateTime TP)
106 return new DateTimeValue(TP.ToLocalTime());
107 else if (Obj is System.DateTimeOffset TPO)
108 return new DateTimeValue(TPO.ToLocalTime().DateTime);
109 else if (Obj is long L)
110 return new DateTimeValue(DateTime.FromInteger(L, DateTimeKind.Local));
111 else if (Obj is double Dbl)
112 return new DateTimeValue(DateTime.FromInteger((long)Dbl, DateTimeKind.Local));
113 else
114 {
115 string s = Obj?.ToString() ?? string.Empty;
116
117 if (DateTime.TryParse(s, out TP))
118 {
119 if (TP.Kind != DateTimeKind.Local)
120 TP = new System.DateTime(TP.Year, TP.Month, TP.Day, TP.Hour, TP.Minute, TP.Second, TP.Millisecond, DateTimeKind.Local);
121
122 return new DateTimeValue(TP);
123 }
124 else
125 throw new ScriptRuntimeException("Unable to parse DateTime value: " + s, this);
126 }
127 }
128
129 double[] d = new double[c];
130 DoubleNumber n;
131
132 for (i = 0; i < c; i++)
133 {
134 n = Arguments[i] as DoubleNumber;
135 if (n is null)
136 throw new ScriptRuntimeException("Expected number arguments.", this);
137
138 d[i] = n.Value;
139 }
140
141 switch (c)
142 {
143 case 3:
144 return new DateTimeValue(new System.DateTime((int)d[0], (int)d[1], (int)d[2], 0, 0, 0, DateTimeKind.Local));
145
146 case 6:
147 return new DateTimeValue(new System.DateTime((int)d[0], (int)d[1], (int)d[2], (int)d[3], (int)d[4], (int)d[5], DateTimeKind.Local));
148
149 case 7:
150 return new DateTimeValue(new System.DateTime((int)d[0], (int)d[1], (int)d[2], (int)d[3], (int)d[4], (int)d[5], (int)d[6], DateTimeKind.Local));
151
152 default:
153 throw new ScriptRuntimeException("Invalid number of parameters.", this);
154 }
155 }
156
163 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
164 {
165 object Obj = CheckAgainst.AssociatedObjectValue;
166
167 if (!(Obj is System.DateTime TP))
168 {
169 if (Obj is double d)
170 TP = DateTime.FromInteger((long)d, DateTimeKind.Local);
171 else
172 {
173 string s = CheckAgainst.AssociatedObjectValue?.ToString() ?? string.Empty;
174
175 if (!DateTime.TryParse(s, out TP))
176 {
177 if (long.TryParse(s, out long Ticks))
178 TP = DateTime.FromInteger(Ticks, DateTimeKind.Local);
179 else
180 return PatternMatchResult.NoMatch;
181 }
182 }
183 }
184
185 TP = TP.ToLocalTime();
186
187 int c = this.Arguments.Length;
188 if (c == 1)
189 return this.Arguments[0].PatternMatch(new DateTimeValue(TP), AlreadyFound);
190
191 if (c < 3)
192 return PatternMatchResult.NoMatch;
193
194 PatternMatchResult Result = this.Arguments[0].PatternMatch(new DoubleNumber(TP.Year), AlreadyFound);
195 if (Result != PatternMatchResult.Match)
196 return Result;
197
198 Result = this.Arguments[1].PatternMatch(new DoubleNumber(TP.Month), AlreadyFound);
199 if (Result != PatternMatchResult.Match)
200 return Result;
201
202 Result = this.Arguments[2].PatternMatch(new DoubleNumber(TP.Day), AlreadyFound);
203 if (Result != PatternMatchResult.Match)
204 return Result;
205
206 if (c == 3)
207 return TP.TimeOfDay == System.TimeSpan.Zero ? PatternMatchResult.Match : PatternMatchResult.NoMatch;
208
209 if (c < 6)
210 return PatternMatchResult.NoMatch;
211
212 Result = this.Arguments[3].PatternMatch(new DoubleNumber(TP.Hour), AlreadyFound);
213 if (Result != PatternMatchResult.Match)
214 return Result;
215
216 Result = this.Arguments[4].PatternMatch(new DoubleNumber(TP.Minute), AlreadyFound);
217 if (Result != PatternMatchResult.Match)
218 return Result;
219
220 Result = this.Arguments[5].PatternMatch(new DoubleNumber(TP.Second), AlreadyFound);
221 if (Result != PatternMatchResult.Match)
222 return Result;
223
224 if (c == 6)
225 return TP.Millisecond == 0 ? PatternMatchResult.Match : PatternMatchResult.NoMatch;
226
227 if (c != 7)
228 return PatternMatchResult.NoMatch;
229
230 return this.Arguments[6].PatternMatch(new DoubleNumber(TP.Millisecond), AlreadyFound);
231 }
232 }
233}
Class managing a script expression.
Definition: Expression.cs:39
static System.DateTime FromInteger(long Nr, DateTimeKind Kind)
Converts an integer to a System.DateTime. If integer is a 32-bit integer, it is considered a UNIX tim...
Definition: DateTime.cs:161
static bool TryParse(string s, out System.DateTime TP)
Parses DateTime values from short forms of strings.
Definition: DateTime.cs:187
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
DateTimeLocal(ScriptNode Year, ScriptNode Month, ScriptNode Day, ScriptNode Hour, ScriptNode Minute, ScriptNode Second, int Start, int Length, Expression Expression)
Creates a DateTime value.
DateTimeLocal(ScriptNode String, int Start, int Length, Expression Expression)
Creates a Local DateTime value.
DateTimeLocal(ScriptNode Year, ScriptNode Month, ScriptNode Day, int Start, int Length, Expression Expression)
Creates a DateTime value.
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
override string FunctionName
Name of the function
override string[] DefaultArgumentNames
Default Argument names
DateTimeLocal(ScriptNode Year, ScriptNode Month, ScriptNode Day, ScriptNode Hour, ScriptNode Minute, ScriptNode Second, ScriptNode MSecond, int Start, int Length, Expression Expression)
Creates a DateTime value.
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
static readonly ArgumentType[] argumentTypes7Scalar
Seven scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
static readonly ArgumentType[] argumentTypes6Scalar
Six scalar parameters.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ScriptNode.cs:169
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17