private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options) { object value = null; string error = null; IReadOnlyList <object> args; var locale = options.Locale != null ? new CultureInfo(options.Locale) : Thread.CurrentThread.CurrentCulture; var format = FunctionUtils.DefaultDateTimeFormat; (args, error) = FunctionUtils.EvaluateChildren(expression, state, options); if (error == null) { (format, locale, error) = FunctionUtils.DetermineFormatAndLocale(args, format, locale, 4); } if (error == null) { if (args[0].IsInteger() && args[1] is string string1) { Func <DateTime, DateTime> timeConverter; (timeConverter, error) = FunctionUtils.DateTimeConverter(Convert.ToInt64(args[0], CultureInfo.InvariantCulture), string1); if (error == null) { value = timeConverter(DateTime.UtcNow).ToString(format, locale); } } else { error = $"{expression} should contain a time interval integer, a string unit of time, an optional output format string and an optional locale string."; } } return(value, error); }
private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options) { object value = null; string error = null; IReadOnlyList <object> args; (args, error) = FunctionUtils.EvaluateChildren(expression, state, options); if (error == null) { if (args[1].IsInteger() && args[2] is string string2) { var format = (args.Count == 4) ? (string)args[3] : FunctionUtils.DefaultDateTimeFormat; Func <DateTime, DateTime> timeConverter; (timeConverter, error) = FunctionUtils.DateTimeConverter(Convert.ToInt64(args[1], CultureInfo.InvariantCulture), string2); if (error == null) { (value, error) = FunctionUtils.NormalizeToDateTime(args[0], dt => (timeConverter(dt).ToString(format, CultureInfo.InvariantCulture), null)); } } else { error = $"{expression} should contain an ISO format timestamp, a time interval integer, a string unit of time and an optional output format string."; } } return(value, error); }
private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options) { object value = null; string error = null; IReadOnlyList <object> args; (args, error) = FunctionUtils.EvaluateChildren(expression, state, options); if (error == null) { if (args[0].IsInteger() && args[1] is string string1) { var format = (args.Count() == 3) ? (string)args[2] : FunctionUtils.DefaultDateTimeFormat; Func <DateTime, DateTime> timeConverter; (timeConverter, error) = FunctionUtils.DateTimeConverter(Convert.ToInt64(args[0]), string1); if (error == null) { value = timeConverter(DateTime.UtcNow).ToString(format); } } else { error = $"{expression} should contain a time interval integer, a string unit of time and an optional output format string."; } } return(value, error); }
private static (string, string) EvalAddToTime(object timestamp, long interval, string timeUnit, string format, CultureInfo locale) { string result = null; string error = null; object parsed = null; (parsed, error) = FunctionUtils.NormalizeToDateTime(timestamp); if (error == null) { var ts = (DateTime)parsed; Func <DateTime, DateTime> converter; (converter, error) = FunctionUtils.DateTimeConverter(interval, timeUnit, false); if (error == null) { var addedTimeStamp = converter(ts); (result, error) = FunctionUtils.ReturnFormatTimeStampStr(addedTimeStamp, format, locale); } } return(result, error); }