Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TimeSpan.cs
1using System.Collections.Generic;
6
8{
13 {
22 : base(new ScriptNode[] { String }, argumentTypes1Scalar, Start, Length, Expression)
23 {
24 }
25
35 public TimeSpan(ScriptNode Hours, ScriptNode Minutes, ScriptNode Seconds, int Start, int Length, Expression Expression)
36 : base(new ScriptNode[] { Hours, Minutes, Seconds }, argumentTypes3Scalar, Start, Length, Expression)
37 {
38 }
39
50 public TimeSpan(ScriptNode Days, ScriptNode Hours, ScriptNode Minutes, ScriptNode Seconds, int Start, int Length, Expression Expression)
51 : base(new ScriptNode[] { Days, Hours, Minutes, Seconds }, argumentTypes4Scalar, Start, Length, Expression)
52 {
53 }
54
66 public TimeSpan(ScriptNode Days, ScriptNode Hours, ScriptNode Minutes, ScriptNode Seconds, ScriptNode MSeconds, int Start, int Length, Expression Expression)
67 : base(new ScriptNode[] { Days, Hours, Minutes, Seconds, MSeconds }, argumentTypes5Scalar, Start, Length, Expression)
68 {
69 }
70
74 public override string FunctionName => nameof(TimeSpan);
75
79 public override string[] DefaultArgumentNames
80 {
81 get { return new string[] { "Hour", "Minute", "Second" }; }
82 }
83
91 {
92 int i, c = Arguments.Length;
93
94 if (c == 1)
95 {
96 object Obj = Arguments[0].AssociatedObjectValue;
97
98 if (Obj is System.DateTime TP)
99 return new ObjectValue(TP.TimeOfDay);
100 else if (Obj is System.DateTimeOffset TPO)
101 return new ObjectValue(TPO.TimeOfDay);
102 else if (Obj is TimeSpan)
103 return Arguments[0];
104
105 string s = Obj?.ToString() ?? string.Empty;
106
107 if (TryParse(s, out System.TimeSpan TS))
108 return new ObjectValue(TS);
109 else
110 throw new ScriptRuntimeException("Unable to parse TimeSpan value: " + s, this);
111 }
112
113 double[] d = new double[c];
114 DoubleNumber n;
115
116 for (i = 0; i < c; i++)
117 {
118 n = Arguments[i] as DoubleNumber;
119 if (n is null)
120 throw new ScriptRuntimeException("Expected number arguments.", this);
121
122 d[i] = n.Value;
123 }
124
125 switch (c)
126 {
127 case 3:
128 return new ObjectValue(new System.TimeSpan((int)d[0], (int)d[1], (int)d[2]));
129
130 case 4:
131 return new ObjectValue(new System.TimeSpan((int)d[0], (int)d[1], (int)d[2], (int)d[3]));
132
133 case 5:
134 return new ObjectValue(new System.TimeSpan((int)d[0], (int)d[1], (int)d[2], (int)d[3], (int)d[4]));
135
136 default:
137 throw new ScriptRuntimeException("Invalid number of parameters.", this);
138 }
139 }
140
147 public static bool TryParse(string s, out System.TimeSpan TS)
148 {
149 s = s.Trim();
150
151 int c = s.Length;
152 int Pos = 0;
153 bool Sign = false;
154
155 if (c == 0)
156 {
157 TS = default;
158 return false;
159 }
160
161 if (s[0] == '+')
162 Pos++;
163 else if (s[0] == '-')
164 {
165 Sign = true;
166 Pos++;
167 }
168
169 if (!DateTime.TryParseInt(s, ref Pos, '.', out int Days, out _) || Days < System.TimeSpan.MinValue.Days || Days > System.TimeSpan.MaxValue.Days)
170 Days = 0;
171 else if (Pos >= c)
172 {
173 TS = new System.TimeSpan(Days, 0, 0, 0);
174 if (Sign)
175 TS = -TS;
176 return true;
177 }
178
179 if (!DateTime.TryParseInt(s, ref Pos, ':', out int Hour, out _) || Hour < 0 || Hour > 23)
180 {
181 TS = default;
182 return false;
183 }
184 else if (Pos >= c)
185 {
186 TS = new System.TimeSpan(Days, Hour, 0, 0);
187 if (Sign)
188 TS = -TS;
189 return true;
190 }
191
192 if (!DateTime.TryParseInt(s, ref Pos, ':', out int Minute, out _) || Minute < 0 || Minute > 59)
193 {
194 TS = default;
195 return false;
196 }
197 else if (Pos >= c)
198 {
199 TS = new System.TimeSpan(Days, Hour, Minute, 0);
200 if (Sign)
201 TS = -TS;
202 return true;
203 }
204
205 if (!DateTime.TryParseInt(s, ref Pos, '.', out int Second, out _) || Second < 0 || Second > 59)
206 {
207 TS = default;
208 return false;
209 }
210 else if (Pos >= c)
211 {
212 TS = new System.TimeSpan(Days, Hour, Minute, Second);
213 if (Sign)
214 TS = -TS;
215 return true;
216 }
217
218 if (!DateTime.TryParseInt(s, ref Pos, '\x0', out int MilliSecond, out int NrDigits) || Pos < c)
219 {
220 TS = default;
221 return false;
222 }
223
224 if (NrDigits > 3)
225 {
226 while (NrDigits > 4)
227 {
228 MilliSecond /= 10;
229 NrDigits--;
230 }
231
232 MilliSecond += 5;
233 MilliSecond /= 10;
234 }
235
236 TS = new System.TimeSpan(Days, Hour, Minute, Second, MilliSecond);
237 if (Sign)
238 TS = -TS;
239 return true;
240 }
241
248 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
249 {
250 object Obj = CheckAgainst.AssociatedObjectValue;
251
252 if (!(Obj is System.TimeSpan TS))
253 {
254 if (Obj is double d)
255 TS = new System.TimeSpan((long)d);
256 else
257 {
258 string s = CheckAgainst.AssociatedObjectValue?.ToString() ?? string.Empty;
259
260 if (!System.TimeSpan.TryParse(s, out TS))
261 {
262 if (long.TryParse(s, out long Ticks))
263 TS = new System.TimeSpan(Ticks);
264 else
265 return PatternMatchResult.NoMatch;
266 }
267 }
268 }
269
270 switch (this.Arguments.Length)
271 {
272 case 1:
273 return this.Arguments[0].PatternMatch(new ObjectValue(TS), AlreadyFound);
274
275 case 3:
276 PatternMatchResult Result = this.Arguments[0].PatternMatch(new DoubleNumber(TS.Hours), AlreadyFound);
277 if (Result != PatternMatchResult.Match)
278 return Result;
279
280 Result = this.Arguments[1].PatternMatch(new DoubleNumber(TS.Minutes), AlreadyFound);
281 if (Result != PatternMatchResult.Match)
282 return Result;
283
284 return this.Arguments[2].PatternMatch(new DoubleNumber(TS.Seconds), AlreadyFound);
285
286 case 4:
287 Result = this.Arguments[0].PatternMatch(new DoubleNumber(TS.Days), AlreadyFound);
288 if (Result != PatternMatchResult.Match)
289 return Result;
290
291 Result = this.Arguments[1].PatternMatch(new DoubleNumber(TS.Hours), AlreadyFound);
292 if (Result != PatternMatchResult.Match)
293 return Result;
294
295 Result = this.Arguments[2].PatternMatch(new DoubleNumber(TS.Minutes), AlreadyFound);
296 if (Result != PatternMatchResult.Match)
297 return Result;
298
299 return this.Arguments[3].PatternMatch(new DoubleNumber(TS.Seconds), AlreadyFound);
300
301 case 5:
302 Result = this.Arguments[0].PatternMatch(new DoubleNumber(TS.Days), AlreadyFound);
303 if (Result != PatternMatchResult.Match)
304 return Result;
305
306 Result = this.Arguments[1].PatternMatch(new DoubleNumber(TS.Hours), AlreadyFound);
307 if (Result != PatternMatchResult.Match)
308 return Result;
309
310 Result = this.Arguments[2].PatternMatch(new DoubleNumber(TS.Minutes), AlreadyFound);
311 if (Result != PatternMatchResult.Match)
312 return Result;
313
314 Result = this.Arguments[3].PatternMatch(new DoubleNumber(TS.Seconds), AlreadyFound);
315 if (Result != PatternMatchResult.Match)
316 return Result;
317
318 return this.Arguments[4].PatternMatch(new DoubleNumber(TS.Milliseconds), AlreadyFound);
319
320 default:
321 return PatternMatchResult.NoMatch;
322 }
323 }
324
325 }
326}
Class managing a script expression.
Definition: Expression.cs:39
TimeSpan(ScriptNode String, int Start, int Length, Expression Expression)
Creates a TimeSpan value.
Definition: TimeSpan.cs:21
TimeSpan(ScriptNode Days, ScriptNode Hours, ScriptNode Minutes, ScriptNode Seconds, ScriptNode MSeconds, int Start, int Length, Expression Expression)
Creates a TimeSpan value.
Definition: TimeSpan.cs:66
override string[] DefaultArgumentNames
Default Argument names
Definition: TimeSpan.cs:80
TimeSpan(ScriptNode Hours, ScriptNode Minutes, ScriptNode Seconds, int Start, int Length, Expression Expression)
Creates a TimeSpan value.
Definition: TimeSpan.cs:35
TimeSpan(ScriptNode Days, ScriptNode Hours, ScriptNode Minutes, ScriptNode Seconds, int Start, int Length, Expression Expression)
Creates a TimeSpan value.
Definition: TimeSpan.cs:50
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: TimeSpan.cs:90
override string FunctionName
Name of the function
Definition: TimeSpan.cs:74
static bool TryParse(string s, out System.TimeSpan TS)
Parses TimeSpan values from short forms of strings.
Definition: TimeSpan.cs:147
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: TimeSpan.cs:248
Base class for multivariate funcions.
static readonly ArgumentType[] argumentTypes5Scalar
Five scalar parameters.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
static readonly ArgumentType[] argumentTypes4Scalar
Four 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
override string ToString()
Definition: ScriptNode.cs:359
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