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;

            (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);
        }
Exemplo n.º 2
0
        private static (string, string) EvalConvertFromUTC(object utcTimestamp, string timezone, string format, CultureInfo locale)
        {
            string error             = null;
            string result            = null;
            var    utcDt             = DateTime.UtcNow;
            object parsed            = null;
            object convertedTimeZone = null;

            (parsed, error) = FunctionUtils.NormalizeToDateTime(utcTimestamp);
            if (error == null)
            {
                utcDt = ((DateTime)parsed).ToUniversalTime();
            }

            if (error == null)
            {
                (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(timezone);

                if (error == null)
                {
                    var convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDt, (TimeZoneInfo)convertedTimeZone);
                    (result, error) = FunctionUtils.ReturnFormatTimeStampStr(convertedDateTime, format, locale);
                }
            }

            return(result, 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 string2)
                {
                    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, locale), null));
                    }
                }
                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);
        }
Exemplo n.º 4
0
        private static EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.ApplyWithError(
                       args =>
            {
                object value = null;
                string error = null;
                (value, error) = FunctionUtils.NormalizeToDateTime(args[0]);
                var timestamp = DateTime.Now;
                if (error == null)
                {
                    timestamp = (DateTime)value;
                }
                else if (DateTime.TryParseExact(
                             s: args[0].ToString(),
                             format: ConvertFromUtc.DefaultFormat,
                             provider: CultureInfo.InvariantCulture,
                             style: DateTimeStyles.RoundtripKind,
                             result: out var parsed))
                {
                    error = null;
                    timestamp = parsed;
                }

                if (error == null)
                {
                    if (timestamp.Hour == 0 && timestamp.Minute == 0)
                    {
                        value = "midnight";
                    }
                    else if (timestamp.Hour >= 0 && timestamp.Hour < 12)
                    {
                        value = "morning";
                    }
                    else if (timestamp.Hour == 12 && timestamp.Minute == 0)
                    {
                        value = "noon";
                    }
                    else if (timestamp.Hour < 18)
                    {
                        value = "afternoon";
                    }
                    else if (timestamp.Hour < 22 || (timestamp.Hour == 22 && timestamp.Minute == 0))
                    {
                        value = "evening";
                    }
                    else
                    {
                        value = "night";
                    }
                }

                return (value, error);
            }));
        }
        private static EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.Apply(
                       args =>
            {
                object value = null;
                string error = null;
                (value, error) = FunctionUtils.NormalizeToDateTime(args[0]);
                if (error == null)
                {
                    return true;
                }

                return false;
            }));
        }
        private static (object, string) StartOfDayWithError(object timestamp, string format)
        {
            string result = null;
            string error  = null;
            object parsed = null;

            (parsed, error) = FunctionUtils.NormalizeToDateTime(timestamp);

            if (error == null)
            {
                var ts         = (DateTime)parsed;
                var startOfDay = ts.Date;
                (result, error) = FunctionUtils.ReturnFormatTimeStampStr(startOfDay, format);
            }

            return(result, error);
        }
Exemplo n.º 7
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.º 8
0
        private static (object, string) StartOfHourWithError(object timestamp, 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;
                var startOfDay  = ts.Date;
                var hours       = ts.Hour;
                var startOfHour = startOfDay.AddHours(hours);
                (result, error) = FunctionUtils.ReturnFormatTimeStampStr(startOfHour, format, locale);
            }

            return(result, 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);
        }
Exemplo n.º 10
0
        private static EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.ApplyWithError(
                       args =>
            {
                object value = null;
                string error = null;
                (value, error) = FunctionUtils.NormalizeToDateTime(args[0]);
                if (error == null)
                {
                    var timestamp = (DateTime)value;
                    if (timestamp.Hour == 0 && timestamp.Minute == 0)
                    {
                        value = "midnight";
                    }
                    else if (timestamp.Hour >= 0 && timestamp.Hour < 12)
                    {
                        value = "morning";
                    }
                    else if (timestamp.Hour == 12 && timestamp.Minute == 0)
                    {
                        value = "noon";
                    }
                    else if (timestamp.Hour < 18)
                    {
                        value = "afternoon";
                    }
                    else if (timestamp.Hour < 22 || (timestamp.Hour == 22 && timestamp.Minute == 0))
                    {
                        value = "evening";
                    }
                    else
                    {
                        value = "night";
                    }
                }

                return (value, error);
            }));
        }
Exemplo n.º 11
0
        private static EvaluateExpressionDelegate Evaluator(Func <DateTime, int, DateTime> function)
        {
            return((expression, state, options) =>
            {
                object value = null;
                string error = null;
                IReadOnlyList <object> args;
                (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
                if (error == null)
                {
                    if (args[1].IsInteger())
                    {
                        var formatString = (args.Count == 3 && args[2] is string string1) ? string1 : FunctionUtils.DefaultDateTimeFormat;
                        (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(formatString, CultureInfo.InvariantCulture);
                        }
                    }
                    else
                    {
                        error = $"{expression} should contain an ISO format timestamp and a time interval integer.";
                    }
                }

                return (value, error);
            });
        }
        private static EvaluateExpressionDelegate Evaluator()
        {
            return(FunctionUtils.ApplyWithError(
                       args =>
            {
                object result = null;
                string error;
                (result, error) = FunctionUtils.NormalizeToDateTime(args[0]);
                if (error == null)
                {
                    var timestamp1 = (DateTime)result;
                    (result, error) = FunctionUtils.NormalizeToDateTime(args[1]);
                    if (error == null)
                    {
                        var timestamp2 = (DateTime)result;
                        var timex = new TimexProperty(timestamp2.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
                        result = TimexRelativeConvert.ConvertTimexToStringRelative(timex, timestamp1);
                    }
                }

                return (result, error);
            }));
        }
Exemplo n.º 13
0
 private static EvaluateExpressionDelegate Evaluator()
 {
     return(FunctionUtils.ApplyWithError(args => FunctionUtils.NormalizeToDateTime(args[0], dt => (dt.Date.ToString("M/dd/yyyy", CultureInfo.InvariantCulture), null))));
 }
Exemplo n.º 14
0
 private static EvaluateExpressionDelegate Evaluator()
 {
     return(FunctionUtils.ApplyWithError(args => FunctionUtils.NormalizeToDateTime(args[0], dt => FunctionUtils.ParseInt32(dt.DayOfWeek))));
 }
Exemplo n.º 15
0
 private static EvaluateExpressionDelegate Evaluator()
 {
     return(FunctionUtils.ApplyWithError(args => FunctionUtils.NormalizeToDateTime(args[0], dt => (dt.DayOfYear, null))));
 }