private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            TimexProperty          parsed = null;
            string                 value  = null;
            string                 error  = null;
            IReadOnlyList <object> args;

            (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                (parsed, error) = FunctionUtils.ParseTimexProperty(args[0]);
            }

            // if the parsed TimexProperty has no types, then it cannot be resolved
            if (error == null && parsed.Types.Count == 0)
            {
                error = $"The parsed TimexProperty of {args[0]} in {expression} has no types. It can't be resolved to a string value.";
            }

            if (error == null)
            {
                var formatedTimex = TimexFormat.Format(parsed);
                try
                {
                    var resolvedValues = TimexResolver.Resolve(new string[] { formatedTimex });
                    value = resolvedValues.Values[0].Value;
                }
                catch (ArgumentException err)
                {
                    error = $"{args[0]} in {expression} is not a valid argument. {err.Message}";
                }
            }

            return(value, error);
        }
Exemplo n.º 2
0
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            TimexProperty          parsed = null;
            bool?                  value  = null;
            string                 error  = null;
            IReadOnlyList <object> args;

            (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                (parsed, error) = FunctionUtils.ParseTimexProperty(args[0]);
            }

            if (error == null)
            {
                value = (parsed.Month != null && parsed.DayOfMonth != null) || parsed.DayOfWeek != null;
            }

            return(value, error);
        }
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            TimexProperty parsed = null;
            string        result = null;
            string        error  = null;

            var(validYear, validMonth, validDay) = (0, 0, 0);
            var currentUtcTime    = DateTime.UtcNow;
            var convertedDateTime = currentUtcTime;
            IReadOnlyList <object> args;

            (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                (parsed, error) = FunctionUtils.ParseTimexProperty(args[0]);
            }

            if (error == null)
            {
                if (parsed.Year != null || parsed.Month == null || parsed.DayOfMonth == null)
                {
                    error = $"{args[0]} must be a timex string which only contains month and day-of-month, for example: 'XXXX-10-31'.";
                }
            }

            if (error == null)
            {
                if (args.Count == 2 && args[1] is string timezone)
                {
                    object convertedTimeZone = null;
                    (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(timezone);
                    if (error == null)
                    {
                        convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentUtcTime, (TimeZoneInfo)convertedTimeZone);
                    }
                }
                else
                {
                    convertedDateTime = currentUtcTime.ToLocalTime();
                }
            }

            if (error == null)
            {
                var(year, month, day) = (convertedDateTime.Year, convertedDateTime.Month, convertedDateTime.Day);
                if (parsed.Month <= month || (parsed.Month == month && parsed.DayOfMonth < day))
                {
                    validYear = year;
                }
                else
                {
                    validYear = year - 1;
                }

                validMonth = parsed.Month ?? 0;
                validDay   = parsed.DayOfMonth ?? 0;
                if (validMonth == 2 && validDay == 29)
                {
                    while (!DateTime.IsLeapYear(validYear))
                    {
                        validYear -= 1;
                    }
                }

                result = TimexProperty.FromDate(new DateTime(validYear, validMonth, validDay)).TimexValue;
            }

            return(result, error);
        }
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            TimexProperty parsed = null;
            string        result = null;
            string        error  = null;

            var(validHour, validMinute, validSecond) = (0, 0, 0);
            IReadOnlyList <object> args;
            var formatRegex       = new Regex("TXX:[0-5][0-9]:[0-5][0-9]");
            var currentUtcTime    = DateTime.UtcNow;
            var convertedDateTime = currentUtcTime;

            (args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                if (!formatRegex.IsMatch(args[0] as string))
                {
                    error = $"{args[0]}  must be a timex string which only contains minutes and seconds, for example: 'TXX:15:28'";
                }
            }

            if (error == null)
            {
                if (args.Count == 2 && args[1] is string timezone)
                {
                    object convertedTimeZone = null;
                    (convertedTimeZone, error) = FunctionUtils.ConvertTimeZoneFormat(timezone);
                    if (error == null)
                    {
                        convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentUtcTime, (TimeZoneInfo)convertedTimeZone);
                    }
                }
                else
                {
                    convertedDateTime = currentUtcTime.ToLocalTime();
                }
            }

            if (error == null)
            {
                (parsed, error) = FunctionUtils.ParseTimexProperty((args[0] as string).Replace("XX", "00"));
            }

            if (error == null)
            {
                var(hour, minute, second) = (convertedDateTime.Hour, convertedDateTime.Minute, convertedDateTime.Second);
                if (parsed.Minute < minute || (parsed.Minute == minute && parsed.Second < second))
                {
                    validHour = hour;
                }
                else
                {
                    validHour = hour - 1;
                }

                if (validHour < 0)
                {
                    validHour += 24;
                }

                validMinute = parsed.Minute ?? 0;
                validSecond = parsed.Second ?? 0;
                result      = TimexProperty.FromTime(new Time(validHour, validMinute, validSecond)).TimexValue;
            }

            return(result, error);
        }