BadScript 2
Loading...
Searching...
No Matches
BadDateExtension.cs
Go to the documentation of this file.
1using System.Globalization;
11
13
15{
19 private static readonly Dictionary<string, CultureInfo> s_Cultures = new Dictionary<string, CultureInfo>();
20 private static BadObject DateToString(BadExecutionContext ctx, DateTimeOffset d, IReadOnlyList<BadObject> args)
21 {
22 if (args.Count == 0)
23 {
24 return d.ToString();
25 }
26
27 IBadString format = (IBadString)args[0]; //Type is checked in the function builder
28
29 if (args.Count == 1)
30 {
31 CultureInfo? defaultCulture = ctx.Scope.GetSingleton<BadRuntime>()
32 ?.Culture ??
33 CultureInfo.InvariantCulture;
34
35 return d.ToString(format.Value, defaultCulture);
36 }
37
38 if (args.Count != 2)
39 {
40 throw new BadRuntimeException("Invalid number of arguments");
41 }
42
43 IBadString culture = (IBadString)args[1]; //Type is checked in the function builder
44
45 if (s_Cultures.TryGetValue(culture.Value,
46 out CultureInfo? cultureInfo
47 )) //Cache Culture info to avoid creating it every time
48 {
49 return d.ToString(format.Value, cultureInfo);
50 }
51
52 cultureInfo = CultureInfo.CreateSpecificCulture(culture.Value);
53 s_Cultures.Add(culture.Value, cultureInfo);
54
55 return d.ToString(format.Value, cultureInfo);
56 }
57
58 private static BadObject TimeToString(BadExecutionContext ctx, TimeSpan d, IReadOnlyList<BadObject> args)
59 {
60 if (args.Count == 0)
61 {
62 return d.ToString();
63 }
64
65 IBadString format = (IBadString)args[0]; //Type is checked in the function builder
66
67 if (args.Count == 1)
68 {
69 CultureInfo? defaultCulture = ctx.Scope.GetSingleton<BadRuntime>()
70 ?.Culture ??
71 CultureInfo.InvariantCulture;
72
73 return d.ToString(format.Value, defaultCulture);
74 }
75
76 if (args.Count != 2)
77 {
78 throw new BadRuntimeException("Invalid number of arguments");
79 }
80
81 IBadString culture = (IBadString)args[1]; //Type is checked in the function builder
82
83 if (s_Cultures.TryGetValue(culture.Value,
84 out CultureInfo? cultureInfo
85 )) //Cache Culture info to avoid creating it every time
86 {
87 return d.ToString(format.Value, cultureInfo);
88 }
89
90 cultureInfo = CultureInfo.CreateSpecificCulture(culture.Value);
91 s_Cultures.Add(culture.Value, cultureInfo);
92
93 return d.ToString(format.Value, cultureInfo);
94 }
95 protected override void AddExtensions(BadInteropExtensionProvider provider)
96 {
97 #region Date
98 provider.RegisterObject<DateTimeOffset>("ToString",
99 d => new BadInteropFunction("ToString",
100 (c, a) => DateToString(c, d, a),
101 false,
103 new BadFunctionParameter("format",
104 true,
105 true,
106 false,
107 null,
109 ),
110 new BadFunctionParameter("culture",
111 true,
112 true,
113 false,
114 null,
116 )
117 )
118 );
119 provider.RegisterObject<DateTimeOffset>("Year", d => d.Year);
120 provider.RegisterObject<DateTimeOffset>("Month", d => d.Month);
121 provider.RegisterObject<DateTimeOffset>("Day", d => d.Day);
122 provider.RegisterObject<DateTimeOffset>("Hour", d => d.Hour);
123 provider.RegisterObject<DateTimeOffset>("Minute", d => d.Minute);
124 provider.RegisterObject<DateTimeOffset>("Second", d => d.Second);
125 provider.RegisterObject<DateTimeOffset>("Millisecond", d => d.Millisecond);
126 provider.RegisterObject<DateTimeOffset>("DayOfWeek", d => (int)d.DayOfWeek);
127 provider.RegisterObject<DateTimeOffset>("DayOfYear", d => d.DayOfYear);
128 provider.RegisterObject<DateTimeOffset>("TimeOfDay", d => d.TimeOfDay);
129 provider.RegisterObject<DateTimeOffset>("UnixTimeMilliseconds", d => d.ToUnixTimeMilliseconds());
130 provider.RegisterObject<DateTimeOffset>("UnixTimeSeconds", d => d.ToUnixTimeSeconds());
131 provider.RegisterObject<DateTimeOffset>("Offset", d => d.Offset);
132 provider.RegisterObject<DateTimeOffset>("ToShortTimeString", d => new BadInteropFunction("ToShortTimeString",
133 args =>args.Length < 1 ? d.Date.ToShortTimeString() : TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d.DateTime,((IBadString)args[0]).Value).ToShortTimeString(),
134 false,
136 new BadFunctionParameter("timeZone",
137 true,
138 false,
139 false,
140 null,
142 )
143 ) );
144
145 provider.RegisterObject<DateTimeOffset>("ToShortDateString", d => new BadInteropFunction("ToShortDateString",
146 args =>args.Length < 1 ? d.Date.ToShortDateString() : TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d.DateTime,((IBadString)args[0]).Value).ToShortDateString(),
147 false,
149 new BadFunctionParameter("timeZone",
150 true,
151 false,
152 false,
153 null,
155 )
156 ) );
157
158 provider.RegisterObject<DateTimeOffset>("ToLongTimeString", d => new BadInteropFunction("ToLongTimeString",
159 args =>args.Length < 1 ? d.Date.ToLongTimeString() : TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d.DateTime,((IBadString)args[0]).Value).ToLongTimeString(),
160 false,
162 new BadFunctionParameter("timeZone",
163 true,
164 false,
165 false,
166 null,
168 )
169 ) );
170
171 provider.RegisterObject<DateTimeOffset>("ToLongDateString", d => new BadInteropFunction("ToLongDateString",
172 args =>args.Length < 1 ? d.Date.ToLongDateString() : TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d.DateTime,((IBadString)args[0]).Value).ToLongDateString(),
173 false,
175 new BadFunctionParameter("timeZone",
176 true,
177 false,
178 false,
179 null,
181 )
182 ) );
183
184 provider.RegisterObject<DateTimeOffset>("Format", d => new BadInteropFunction("Format",
185 (ctx, args) =>
186 {
187 string format = ((IBadString)args[0]).Value;
188
189 CultureInfo? c = ctx.Scope.GetSingleton<BadRuntime>()
190 ?.Culture ??
191 CultureInfo.InvariantCulture;
192
193 if (args.Length == 3 && args[2] is IBadString str)
194 {
195 c = new CultureInfo(str.Value);
196 }
197
198 string? timeZone =
199 args.Length < 2 ? null : (args[1] as IBadString)?.Value;
200 if (timeZone != null)
201 {
202 return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d.DateTime, timeZone).ToString(format, c);
203 }
204 return d.ToString(format, c);
205 },
206 false,
208 new BadFunctionParameter("format",
209 false,
210 true,
211 false,
212 null,
214 ),
215 new BadFunctionParameter("timeZone",
216 true,
217 false,
218 false,
219 null,
221 ),
222 new BadFunctionParameter("culture",
223 true,
224 false,
225 false,
226 null,
228 )
229 ));
230
231 provider.RegisterObject<DateTimeOffset>("WeekOfYear", d => new BadInteropFunction("WeekOfYear",
232 (ctx, args) =>
233 {
234 CultureInfo? c = ctx.Scope.GetSingleton<BadRuntime>()
235 ?.Culture ??
236 CultureInfo.InvariantCulture;
237
238 if (args.Length == 1 && args[0] is IBadString str)
239 {
240 c = new CultureInfo(str.Value);
241 }
242
243 return c.Calendar.GetWeekOfYear(d.DateTime,
244 c.DateTimeFormat.CalendarWeekRule,
245 c.DateTimeFormat.FirstDayOfWeek
246 );
247 },
248 false,
250 new BadFunctionParameter("culture",
251 true,
252 false,
253 false,
254 null,
256 )
257 ));
258
259 provider.RegisterObject<DateTimeOffset>(
260 "AddYears",
261 d =>
263 "AddYears", (_, y) => d.AddYears((int)y),
266 "years",
267 false,
268 true,
269 false,
270 null,
272 )
273 )
274 );
275
276 provider.RegisterObject<DateTimeOffset>(
277 "AddMonths",
278 d =>
280 "AddMonths", (_, y) => d.AddMonths((int)y),
283 "months",
284 false,
285 true,
286 false,
287 null,
289 )
290 )
291 );
292
293 provider.RegisterObject<DateTimeOffset>(
294 "AddDays",
295 d =>
297 "AddDays", (_, y) => d.AddDays((int)y),
300 "days",
301 false,
302 true,
303 false,
304 null,
306 )
307 )
308 );
309
310 provider.RegisterObject<DateTimeOffset>(
311 "AddHours",
312 d =>
314 "AddHours", (_, y) => d.AddHours((int)y),
317 "hours",
318 false,
319 true,
320 false,
321 null,
323 )
324 )
325 );
326
327 provider.RegisterObject<DateTimeOffset>(
328 "AddMinutes",
329 d =>
331 "AddMinutes", (_, y) => d.AddMinutes((int)y),
334 "minutes",
335 false,
336 true,
337 false,
338 null,
340 )
341 )
342 );
343
344 provider.RegisterObject<DateTimeOffset>(
345 "AddSeconds",
346 d =>
348 "AddSeconds", (_, y) => d.AddSeconds((int)y),
351 "seconds",
352 false,
353 true,
354 false,
355 null,
357 )
358 )
359 );
360
361 provider.RegisterObject<DateTimeOffset>(
362 "AddMilliseconds",
363 d =>
365 "AddMilliseconds", (_, y) => d.AddMilliseconds((int)y),
368 "ms",
369 false,
370 true,
371 false,
372 null,
374 )
375 )
376 );
377
378 provider.RegisterObject<DateTimeOffset>(
379 "AddTicks",
380 d =>
382 "AddTicks", (_, y) => d.AddTicks((long)y),
385 "ticks",
386 false,
387 true,
388 false,
389 null,
391 )
392 )
393 );
394
395 Func<DateTimeOffset, BadObject> addFunc = d =>
397 "Add", (_, y) => d.Add(y),
400 "time",
401 false,
402 true,
403 false,
404 null,
406 )
407 );
409 provider.RegisterObject("Add", addFunc);
410
411
412 Func<DateTimeOffset, BadObject> subtractFunc = d =>
414 "Subtract", (_, y) => d.Subtract(y),
417 "time",
418 false,
419 true,
420 false,
421 null,
423 )
424 );
426 provider.RegisterObject("Subtract", subtractFunc);
427
428 provider.RegisterObject<DateTimeOffset>(
429 "ToUniversalTime",
430 d => new BadInteropFunction("ToUniversalTime",
431 args => d.ToUniversalTime(),
432 false,
434 )
435 );
436
439 (_, o) => o is IBadDate other && d == other.Value,
441 new BadFunctionParameter("right", false, false, false, null, BadAnyPrototype.Instance)
442 ));
443
446 (_, o) => o is not IBadDate other || d != other.Value,
448 new BadFunctionParameter("right", false, false, false, null, BadAnyPrototype.Instance)
449 ));
450
453 (_, o) => d > o,
455 new BadFunctionParameter("right", false, false, false, null, BadDate.Prototype)
456 ));
457
460 (_, o) => d >= o,
462 new BadFunctionParameter("right", false, false, false, null, BadDate.Prototype)
463 ));
464
467 (_, o) => d < o,
469 new BadFunctionParameter("right", false, false, false, null, BadDate.Prototype)
470 ));
471
474 (_, o) => d <= o,
476 new BadFunctionParameter("right", false, false, false, null, BadDate.Prototype)
477 ));
478
479
480 provider.RegisterObject<DateTimeOffset>(
481 "ToLocalTime",
482 d => new BadInteropFunction("ToLocalTime",
483 args => d.ToLocalTime(),
484 false,
486 )
487 );
488
489 provider.RegisterObject<DateTimeOffset>(
490 "ToOffset",
491 d => new BadInteropFunction("ToOffset",
492 args => d.ToOffset(((IBadTime)args[0]).Value),
493 false,
496 "offset",
497 false,
498 true,
499 false,
500 null,
502 )
503 )
504 );
505 #endregion
506
507 #region Time
508
511 (_, o) => o is IBadTime other && d == other.Value,
513 new BadFunctionParameter("right", false, false, false, null, BadAnyPrototype.Instance)
514 ));
515
518 (_, o) => o is not IBadTime other || d != other.Value,
520 new BadFunctionParameter("right", false, false, false, null, BadAnyPrototype.Instance)
521 ));
522
525 (_, o) => d > o,
527 new BadFunctionParameter("right", false, false, false, null, BadTime.Prototype)
528 ));
529
532 (_, o) => d >= o,
534 new BadFunctionParameter("right", false, false, false, null, BadTime.Prototype)
535 ));
536
539 (_, o) => d < o,
541 new BadFunctionParameter("right", false, false, false, null, BadTime.Prototype)
542 ));
543
546 (_, o) => d <= o,
548 new BadFunctionParameter("right", false, false, false, null, BadTime.Prototype)
549 ));
550 provider.RegisterObject<TimeSpan>("ToString",
551 d => new BadInteropFunction("ToString",
552 (c, a) => TimeToString(c, d, a),
553 false,
555 new BadFunctionParameter("format",
556 true,
557 true,
558 false,
559 null,
561 ),
562 new BadFunctionParameter("culture",
563 true,
564 true,
565 false,
566 null,
568 )
569 )
570 );
571
572 provider.RegisterObject<TimeSpan>("Hours", d => d.Hours);
573 provider.RegisterObject<TimeSpan>("Minutes", d => d.Minutes);
574 provider.RegisterObject<TimeSpan>("Seconds", d => d.Seconds);
575 provider.RegisterObject<TimeSpan>("Milliseconds", d => d.Milliseconds);
576 provider.RegisterObject<TimeSpan>("Ticks", d => d.Ticks);
577 provider.RegisterObject<TimeSpan>("TotalHours", d => (decimal)d.TotalHours);
578 provider.RegisterObject<TimeSpan>("TotalMinutes", d => (decimal)d.TotalMinutes);
579 provider.RegisterObject<TimeSpan>("TotalSeconds", d => (decimal)d.TotalSeconds);
580 provider.RegisterObject<TimeSpan>("TotalMilliseconds", d => (decimal)d.TotalMilliseconds);
581 provider.RegisterObject<TimeSpan>("Negate", d => new BadInteropFunction("Time.Negate", (_,_) => d.Negate(), false, BadTime.Prototype));
582 provider.RegisterObject<TimeSpan>("Add", d => new BadDynamicInteropFunction<TimeSpan>("Time.Add",
583 (_, y) => d.Add(y),
585 new BadFunctionParameter("time", false, true, false, null, BadTime.Prototype)
586 ));
587 provider.RegisterObject<TimeSpan>("Subtract", d => new BadDynamicInteropFunction<TimeSpan>("Time.Subtract",
588 (_, y) => d.Subtract(y),
590 new BadFunctionParameter("time", false, true, false, null, BadTime.Prototype)
591 ));
592 #endregion
593 }
594}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:30
Contains Static Data for the BadScript Language.
static readonly Dictionary< string, CultureInfo > s_Cultures
Culture Info Cache.
static BadObject DateToString(BadExecutionContext ctx, DateTimeOffset d, IReadOnlyList< BadObject > args)
static BadObject TimeToString(BadExecutionContext ctx, TimeSpan d, IReadOnlyList< BadObject > args)
override void AddExtensions(BadInteropExtensionProvider provider)
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
Public Extension API for the BS2 Runtime.
void RegisterObject(Type t, string propName, BadObject obj)
Registers the specified extension for the specified type.
Interop Function taking an array of arguments.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:224
static BadClassPrototype Prototype
Definition BadDate.cs:9
static BadClassPrototype Prototype
Definition BadTime.cs:8
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Helper Class that Builds a Native Class from a Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
new DateTimeOffset Value
The Date Value.
Definition IBadDate.cs:8
Implements the Interface for Native Strings.
Definition IBadString.cs:7
new string Value
The String Value.
Definition IBadString.cs:11
new TimeSpan Value
The TimeSpan Value.
Definition IBadTime.cs:8
Contains Shared Data Structures and Functionality.
Contains Common Interop Extensions for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.