Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DateTimeOffset.cs
1using System;
2using System.Collections.Generic;
7
9{
14 {
23 : base(new ScriptNode[] { String }, argumentTypes1Scalar, Start, Length, Expression)
24 {
25 }
26
38 : base(new ScriptNode[] { Year, Month, Day, TimeZone }, argumentTypes4Scalar, Start, Length, Expression)
39 {
40 }
41
56 ScriptNode TimeZone, int Start, int Length, Expression Expression)
58 {
59 }
60
76 ScriptNode MSecond, ScriptNode TimeZone, int Start, int Length, Expression Expression)
77 : base(new ScriptNode[] { Year, Month, Day, Hour, Minute, Second, MSecond, TimeZone }, argumentTypes8Scalar, Start, Length, Expression)
78 {
79 }
80
84 public override string FunctionName => nameof(DateTimeOffset);
85
89 public override string[] DefaultArgumentNames
90 {
91 get { return new string[] { "Year", "Month", "Day", "Hour", "Minute", "Second", "MSecond", "TZ" }; }
92 }
93
101 {
102 int i, c = Arguments.Length;
103 object Obj;
104
105 if (c == 1)
106 {
107 Obj = Arguments[0].AssociatedObjectValue;
108 string s = Obj?.ToString() ?? string.Empty;
109
110 if (TryParse(s, out System.DateTimeOffset TP))
111 return new ObjectValue(TP);
112 else
113 throw new ScriptRuntimeException("Unable to parse DateTimeOffset value: " + s, this);
114 }
115
116 Obj = Arguments[c - 1].AssociatedObjectValue;
117
118 if (!(Obj is System.TimeSpan TimeZone))
119 {
120 string s = Obj?.ToString() ?? string.Empty;
121
122 if (!TimeSpan.TryParse(s, out TimeZone))
123 throw new ScriptRuntimeException("Unable to parse Time-Zone value: " + s, this);
124 }
125
126 c--;
127
128 double[] d = new double[c];
129 DoubleNumber n;
130
131 for (i = 0; i < c; i++)
132 {
133 n = Arguments[i] as DoubleNumber;
134 if (n is null)
135 throw new ScriptRuntimeException("Expected number arguments.", this);
136
137 d[i] = n.Value;
138 }
139
140 switch (c)
141 {
142 case 3:
143 return new ObjectValue(new System.DateTimeOffset((int)d[0], (int)d[1], (int)d[2], 0, 0, 0, TimeZone));
144
145 case 6:
146 return new ObjectValue(new System.DateTimeOffset((int)d[0], (int)d[1], (int)d[2], (int)d[3], (int)d[4], (int)d[5], TimeZone));
147
148 case 7:
149 return new ObjectValue(new System.DateTimeOffset((int)d[0], (int)d[1], (int)d[2], (int)d[3], (int)d[4], (int)d[5], (int)d[6], TimeZone));
150
151 default:
152 throw new ScriptRuntimeException("Invalid number of parameters.", this);
153 }
154 }
155
162 public static bool TryParse(string s, out System.DateTimeOffset TP)
163 {
164 s = s.Trim();
165
166 System.TimeSpan TimeZone;
167
168 if (s.EndsWith("z", StringComparison.CurrentCultureIgnoreCase))
169 {
170 TimeZone = System.TimeSpan.Zero;
171 s = s.Substring(0, s.Length - 1).TrimEnd();
172 }
173 else
174 {
175 int i = s.LastIndexOfAny(PlusMinus);
176 if (i < 0)
177 {
178 TP = default;
179 return false;
180 }
181
182 if (!TimeSpan.TryParse(s.Substring(i + 1), out TimeZone))
183 {
184 TP = default;
185 return false;
186 }
187
188 if (s[i] == '-')
189 TimeZone = -TimeZone;
190
191 s = s.Substring(0, i).TrimEnd();
192 }
193
194 if (!DateTime.TryParse(s, out System.DateTime TP0))
195 {
196 TP = default;
197 return false;
198 }
199
200 TP = new System.DateTimeOffset(TP0, TimeZone);
201 return true;
202 }
203
204 private static readonly char[] PlusMinus = new char[] { '+', '-' };
205
212 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
213 {
214 object Obj = CheckAgainst.AssociatedObjectValue;
215
216 if (!(Obj is System.DateTimeOffset TPO))
217 {
218 if (Obj is System.DateTime TP)
219 {
220 switch (TP.Kind)
221 {
222 case DateTimeKind.Utc:
223 default:
224 TPO = new System.DateTimeOffset(TP, System.TimeSpan.Zero);
225 break;
226
227 case DateTimeKind.Local:
228 TPO = new System.DateTimeOffset(TP, TimeZoneInfo.Local.GetUtcOffset(System.DateTime.Now));
229 break;
230 }
231 }
232 else if (Obj is double d)
233 {
234 TP = DateTime.FromInteger((long)d, DateTimeKind.Unspecified);
235 TPO = new System.DateTimeOffset(TP, System.TimeSpan.Zero);
236 }
237 else
238 {
239 string s = Obj?.ToString() ?? string.Empty;
240
241 if (!TryParse(s, out TPO))
242 return PatternMatchResult.NoMatch;
243 }
244 }
245
246 int c = this.Arguments.Length;
247 if (c == 1)
248 return this.Arguments[0].PatternMatch(new ObjectValue(TPO), AlreadyFound);
249
250 if (c < 4)
251 return PatternMatchResult.NoMatch;
252
253 PatternMatchResult Result = this.Arguments[c - 1].PatternMatch(new ObjectValue(TPO.Offset), AlreadyFound);
254 if (Result != PatternMatchResult.Match)
255 return Result;
256
257 c--;
258
259 Result = this.Arguments[0].PatternMatch(new DoubleNumber(TPO.Year), AlreadyFound);
260 if (Result != PatternMatchResult.Match)
261 return Result;
262
263 Result = this.Arguments[1].PatternMatch(new DoubleNumber(TPO.Month), AlreadyFound);
264 if (Result != PatternMatchResult.Match)
265 return Result;
266
267 Result = this.Arguments[2].PatternMatch(new DoubleNumber(TPO.Day), AlreadyFound);
268 if (Result != PatternMatchResult.Match)
269 return Result;
270
271 if (c < 6)
272 return PatternMatchResult.NoMatch;
273
274 Result = this.Arguments[3].PatternMatch(new DoubleNumber(TPO.Hour), AlreadyFound);
275 if (Result != PatternMatchResult.Match)
276 return Result;
277
278 Result = this.Arguments[4].PatternMatch(new DoubleNumber(TPO.Minute), AlreadyFound);
279 if (Result != PatternMatchResult.Match)
280 return Result;
281
282 Result = this.Arguments[5].PatternMatch(new DoubleNumber(TPO.Second), AlreadyFound);
283 if (Result != PatternMatchResult.Match)
284 return Result;
285
286 if (c == 6)
287 return TPO.Millisecond == 0 ? PatternMatchResult.Match : PatternMatchResult.NoMatch;
288
289 if (c != 7)
290 return PatternMatchResult.NoMatch;
291
292 return this.Arguments[6].PatternMatch(new DoubleNumber(TPO.Millisecond), AlreadyFound);
293 }
294
295 }
296}
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.
static bool TryParse(string s, out System.DateTimeOffset TP)
Parses DateTimeOffset values from short forms of strings.
DateTimeOffset(ScriptNode Year, ScriptNode Month, ScriptNode Day, ScriptNode Hour, ScriptNode Minute, ScriptNode Second, ScriptNode MSecond, ScriptNode TimeZone, int Start, int Length, Expression Expression)
Creates a DateTimeOffset value.
override string FunctionName
Name of the function
DateTimeOffset(ScriptNode String, int Start, int Length, Expression Expression)
Creates a DateTimeOffset value.
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
DateTimeOffset(ScriptNode Year, ScriptNode Month, ScriptNode Day, ScriptNode Hour, ScriptNode Minute, ScriptNode Second, ScriptNode TimeZone, int Start, int Length, Expression Expression)
Creates a DateTimeOffset value.
DateTimeOffset(ScriptNode Year, ScriptNode Month, ScriptNode Day, ScriptNode TimeZone, int Start, int Length, Expression Expression)
Creates a DateTimeOffset value.
override string[] DefaultArgumentNames
Default Argument names
static bool TryParse(string s, out System.TimeSpan TS)
Parses TimeSpan values from short forms of strings.
Definition: TimeSpan.cs:147
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes7Scalar
Seven scalar parameters.
static readonly ArgumentType[] argumentTypes8Scalar
Eight 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