Exemplo n.º 1
0
        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 = DefaultFormat;

            (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                (format, locale, error) = FunctionUtils.DetermineFormatAndLocale(args, format, locale, 4);
            }

            if (error == null)
            {
                if (args[1] is string targetTimeZone)
                {
                    (value, error) = EvalConvertFromUTC(args[0], targetTimeZone, format, locale);
                }
                else
                {
                    error = $"{expression} should contain an ISO format timestamp, a destination time zone string, 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;
            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, 5);
            }

            if (error == null)
            {
                if (args[1].IsInteger() && args[2] is string timeUnit)
                {
                    (value, error) = EvalAddToTime(args[0], Convert.ToInt64(args[1], CultureInfo.InvariantCulture), timeUnit, format, locale);
                }
                else
                {
                    error = $"{expression} should contain an ISO format timestamp, 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;
            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 EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.ApplyWithOptionsAndError(
                       (args, options) =>
            {
                object result = null;
                string error = null;
                var timestamp = args[0];
                var format = FunctionUtils.DefaultDateTimeFormat;
                var locale = options.Locale != null ? new CultureInfo(options.Locale) : Thread.CurrentThread.CurrentCulture;
                (format, locale, error) = FunctionUtils.DetermineFormatAndLocale(args, format, locale, 3);
                if (error == null)
                {
                    if (timestamp.IsInteger())
                    {
                        var ticks = Convert.ToInt64(timestamp, CultureInfo.InvariantCulture);
                        var dateTime = new DateTime(ticks);
                        result = dateTime.ToString(format, locale);
                    }
                    else
                    {
                        error = $"formatTicks first arugment {timestamp} must be an integer";
                    }
                }

                return (result, error);
            }));
        }
        private static EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.ApplyWithOptionsAndError(
                       (args, options) =>
            {
                object result = null;
                string error = null;
                var timestamp = args[0];
                var format = FunctionUtils.DefaultDateTimeFormat;
                var locale = options.Locale != null ? new CultureInfo(options.Locale) : Thread.CurrentThread.CurrentCulture;
                (format, locale, error) = FunctionUtils.DetermineFormatAndLocale(args, format, locale, 3);

                if (error == null)
                {
                    if (timestamp is string tsString)
                    {
                        (result, error) = ParseTimestamp(tsString, dt => dt.ToString(format, locale));
                    }
                    else if (timestamp is DateTime dt)
                    {
                        result = dt.ToString(format, locale);
                    }
                    else
                    {
                        error = $"formatDateTime has invalid first argument {timestamp}";
                    }
                }

                return (result, error);
            }));
        }
        private static EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.ApplyWithOptionsAndError(
                       (args, options) =>
            {
                object result = null;
                string error = null;
                var timestamp = args[0];
                var format = FunctionUtils.DefaultDateTimeFormat;
                var locale = options.Locale != null ? new CultureInfo(options.Locale) : Thread.CurrentThread.CurrentCulture;
                (format, locale, error) = FunctionUtils.DetermineFormatAndLocale(args, format, locale, 3);
                if (error == null)
                {
                    if (timestamp.IsNumber())
                    {
                        var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                        dateTime = dateTime.AddSeconds(Convert.ToDouble(timestamp, CultureInfo.InvariantCulture));
                        result = dateTime.ToString(format, locale);
                    }
                    else
                    {
                        error = $"formatEpoch first argument {timestamp} is not a number";
                    }
                }

                return (result, error);
            }));
        }
Exemplo n.º 7
0
        private static EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.ApplyWithOptionsAndError((args, options) =>
            {
                string error = null;
                string format = FunctionUtils.DefaultDateTimeFormat;
                object result = null;
                var locale = options.Locale != null ? new CultureInfo(options.Locale) : Thread.CurrentThread.CurrentCulture;
                (format, locale, error) = FunctionUtils.DetermineFormatAndLocale(args, format, locale, 2);
                result = DateTime.UtcNow.ToString(format, locale);

                return (result, error);
            }));
        }
Exemplo n.º 8
0
        private static EvaluateExpressionDelegate Evaluator(Func <DateTime, int, DateTime> function)
        {
            return((expression, state, 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[1].IsInteger())
                    {
                        (value, error) = FunctionUtils.NormalizeToDateTime(args[0], dt =>
                        {
                            var result = dt;
                            var(interval, error) = FunctionUtils.ParseInt32(args[1]);
                            if (error == null)
                            {
                                result = function(dt, interval);
                            }

                            return (result, error);
                        });

                        if (error == null)
                        {
                            value = Convert.ToDateTime(value, CultureInfo.InvariantCulture).ToString(format, locale);
                        }
                    }
                    else
                    {
                        error = $"{expression} should contain an ISO format timestamp and a time interval integer.";
                    }
                }

                return (value, error);
            });
        }
Exemplo n.º 9
0
        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, 3);
            }

            if (error == null)
            {
                (value, error) = StartOfHourWithError(args[0], format, locale);
            }

            return(value, error);
        }