示例#1
0
        public static bool TryMatchYesterday(ArgumentToken token, DateTime currentDate, string[] inputTokens, int startIdx, out TokenMatchResult result)
        {
            //Yesterday
            if (inputTokens[startIdx].Equals("yesterday", StringComparison.CurrentCultureIgnoreCase))
            {
                DateTime output = currentDate.Subtract(new TimeSpan(1, 0, 0, 0));
                result = new TokenMatchResult(token, inputTokens[startIdx], inputTokens[startIdx], MatchOutcome.Full, inputTokens[startIdx].Length, 1);
                result.SetArgValue(token, output);
                return(true);
            }

            result = TokenMatchResult.None;
            return(false);
        }
        public void TestTryGetValueByString()
        {
            ArgumentToken <int> token = new ArgumentToken <int> .Builder().Name("arg").Parser(int.TryParse).Build();

            TokenMatchResult result = new TokenMatchResult(token, "123", "123", MatchOutcome.Full, 3, 1);

            result.SetArgValue(token, 123);

            int  argValue;
            bool hasValue = result.TryGetArgValue("arg", out argValue);

            Assert.True(hasValue);
            Assert.Equal(123, argValue);
        }
示例#3
0
        public static bool TryMatchExplicitDate(ArgumentToken token, string[] inputTokens, int startIdx, out TokenMatchResult result)
        {
            DateTime output;

            //Explicit date
            if (DateTime.TryParse(inputTokens[startIdx], out output))
            {
                result = new TokenMatchResult(token, inputTokens[startIdx], inputTokens[startIdx], MatchOutcome.Full, inputTokens[startIdx].Length, 1);
                result.SetArgValue(token, output);
                return(true);
            }

            result = TokenMatchResult.None;
            return(false);
        }
示例#4
0
        public static bool TryMatchLastDayOfWeek(ArgumentToken token, DateTime currentDate, string[] inputTokens, int startIdx, out TokenMatchResult result)
        {
            //last <day-of-week>
            if (inputTokens.Length > startIdx + 1)
            {
                DayOfWeek dayOfWeek;
                if (DateParser.TryParseDayOfWeek(inputTokens[startIdx + 1], out dayOfWeek))
                {
                    DateTime output    = DateUtil.GetRelativeDateDayOfWeek(currentDate, dayOfWeek);
                    string   matchText = TokenUtils.GetMatchText(inputTokens, startIdx, 2);
                    result = new TokenMatchResult(token, matchText, matchText, MatchOutcome.Full, matchText.Length, 2);
                    result.SetArgValue(token, output);
                    return(true);
                }
            }

            result = TokenMatchResult.None;
            return(false);
        }
示例#5
0
        public static bool TryMatchLastDayOfMonth(ArgumentToken token, DateTime currentDate, string[] inputTokens, int startIdx, out TokenMatchResult result)
        {
            //last <month> <day-of-month>
            if (inputTokens.Length > startIdx + 2)
            {
                int month;
                int day;
                if (DateParser.TryParseMonth(inputTokens[startIdx + 1], out month) &&
                    DateParser.TryParseDayOfMonth(inputTokens[startIdx + 2], currentDate.Year - 1, month, out day))
                {
                    DateTime output    = new DateTime(currentDate.Year - 1, month, day);
                    string   matchText = TokenUtils.GetMatchText(inputTokens, startIdx, 3);
                    result = new TokenMatchResult(token, matchText, matchText, MatchOutcome.Full, matchText.Length, 3);
                    result.SetArgValue(token, output);
                    return(true);
                }
            }

            result = TokenMatchResult.None;
            return(false);
        }
示例#6
0
        public static bool TryMatchRelativeDate(ArgumentToken token, DateTime currentDate, string[] inputTokens, int startIdx, out TokenMatchResult result)
        {
            //<number> <time-unit> ago
            if (inputTokens.Length > startIdx + 2 &&
                inputTokens[startIdx + 2].Equals("ago", StringComparison.CurrentCultureIgnoreCase))
            {
                int      number;
                TimeUnit unit;
                if (int.TryParse(inputTokens[startIdx], out number) &&
                    DateParser.TryParseTimeUnit(inputTokens[startIdx + 1], out unit))
                {
                    DateTime output    = DateUtil.GetRelativeDate(currentDate, -number, unit);
                    string   matchText = TokenUtils.GetMatchText(inputTokens, startIdx, 3);
                    result = new TokenMatchResult(token, matchText, matchText, MatchOutcome.Full, matchText.Length, 3);
                    result.SetArgValue(token, output);
                    return(true);
                }
            }

            result = TokenMatchResult.None;
            return(false);
        }