Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DateTimeUtc.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(DateTimeUtc);
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 = 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.ToUniversalTime());
107 else if (Obj is System.DateTimeOffset TPO)
108 return new DateTimeValue(TPO.ToUniversalTime().DateTime);
109 else if (Obj is long L)
110 return new DateTimeValue(DateTime.FromInteger(L, DateTimeKind.Utc));
111 else if (Obj is double Dbl)
112 return new DateTimeValue(DateTime.FromInteger((long)Dbl, DateTimeKind.Utc));
113 else
114 {
115 string s = Obj?.ToString() ?? string.Empty;
116
117 if (DateTime.TryParse(s, out TP))
118 {
119 if (TP.Kind != DateTimeKind.Utc)
120 TP = new System.DateTime(TP.Year, TP.Month, TP.Day, TP.Hour, TP.Minute, TP.Second, TP.Millisecond, DateTimeKind.Utc);
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.Utc));
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.Utc));
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.Utc));
151
152 default:
153 throw new ScriptRuntimeException("Invalid number of parameters.", this);
154 }
155 }
156
157
164 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
165 {
166 object Obj = CheckAgainst.AssociatedObjectValue;
167
168 if (!(Obj is System.DateTime TP))
169 {
170 if (Obj is double d)
171 TP = DateTime.FromInteger((long)d, DateTimeKind.Utc);
172 else
173 {
174 string s = CheckAgainst.AssociatedObjectValue?.ToString() ?? string.Empty;
175
176 if (!DateTime.TryParse(s, out TP))
177 {
178 if (long.TryParse(s, out long Ticks))
179 TP = DateTime.FromInteger(Ticks, DateTimeKind.Utc);
180 else
181 return PatternMatchResult.NoMatch;
182 }
183 }
184 }
185
186 TP = TP.ToUniversalTime();
187
188 int c = this.Arguments.Length;
189 if (c == 1)
190 return this.Arguments[0].PatternMatch(new DateTimeValue(TP), AlreadyFound);
191
192 if (c < 3)
193 return PatternMatchResult.NoMatch;
194
195 PatternMatchResult Result = this.Arguments[0].PatternMatch(new DoubleNumber(TP.Year), AlreadyFound);
196 if (Result != PatternMatchResult.Match)
197 return Result;
198
199 Result = this.Arguments[1].PatternMatch(new DoubleNumber(TP.Month), AlreadyFound);
200 if (Result != PatternMatchResult.Match)
201 return Result;
202
203 Result = this.Arguments[2].PatternMatch(new DoubleNumber(TP.Day), AlreadyFound);
204 if (Result != PatternMatchResult.Match)
205 return Result;
206
207 if (c == 3)
208 return TP.TimeOfDay == System.TimeSpan.Zero ? PatternMatchResult.Match : PatternMatchResult.NoMatch;
209
210 if (c < 6)
211 return PatternMatchResult.NoMatch;
212
213 Result = this.Arguments[3].PatternMatch(new DoubleNumber(TP.Hour), AlreadyFound);
214 if (Result != PatternMatchResult.Match)
215 return Result;
216
217 Result = this.Arguments[4].PatternMatch(new DoubleNumber(TP.Minute), AlreadyFound);
218 if (Result != PatternMatchResult.Match)
219 return Result;
220
221 Result = this.Arguments[5].PatternMatch(new DoubleNumber(TP.Second), AlreadyFound);
222 if (Result != PatternMatchResult.Match)
223 return Result;
224
225 if (c == 6)
226 return TP.Millisecond == 0 ? PatternMatchResult.Match : PatternMatchResult.NoMatch;
227
228 if (c != 7)
229 return PatternMatchResult.NoMatch;
230
231 return this.Arguments[6].PatternMatch(new DoubleNumber(TP.Millisecond), AlreadyFound);
232 }
233
234 }
235}
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
DateTimeUtc(ScriptNode Year, ScriptNode Month, ScriptNode Day, ScriptNode Hour, ScriptNode Minute, ScriptNode Second, ScriptNode MSecond, int Start, int Length, Expression Expression)
Creates a DateTime value.
Definition: DateTimeUtc.cs:72
override string[] DefaultArgumentNames
Default Argument names
Definition: DateTimeUtc.cs:87
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: DateTimeUtc.cs:164
DateTimeUtc(ScriptNode String, int Start, int Length, Expression Expression)
Creates a UTC DateTime value.
Definition: DateTimeUtc.cs:22
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: DateTimeUtc.cs:97
override string FunctionName
Name of the function
Definition: DateTimeUtc.cs:81
DateTimeUtc(ScriptNode Year, ScriptNode Month, ScriptNode Day, int Start, int Length, Expression Expression)
Creates a DateTime value.
Definition: DateTimeUtc.cs:36
DateTimeUtc(ScriptNode Year, ScriptNode Month, ScriptNode Day, ScriptNode Hour, ScriptNode Minute, ScriptNode Second, int Start, int Length, Expression Expression)
Creates a DateTime value.
Definition: DateTimeUtc.cs:53
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