示例#1
0
        /// <summary>
        /// Whether or not this uri plugin can handle the current token.
        /// </summary>
        /// <param name="current"></param>
        /// <returns></returns>
        public override bool CanHandle(Token current)
        {
            _time   = TimeSpan.MinValue;
            _endPos = -1;
            // Case 1: noon, "afternoon, midnight
            if (_aliases.ContainsKey(current.Text))
            {
                _time = CloneTime(_aliases[current.Text]);
                return(true);
            }

            // Check 1: Make sure it's a number.
            if (current.Kind != TokenKind.LiteralNumber)
            {
                return(false);
            }

            var nextToken     = _lexer.PeekToken();
            var nextTokenText = nextToken.Token.Text.ToLower();

            // Check 2: End token?
            if (nextToken.Token == ComLib.Lang.Core.Tokens.EndToken)
            {
                return(false);
            }

            // Check 3: Time format is 9:30 am or 930 am
            // So if next token is not ':' then it has to be "am" or "pm"
            if (nextTokenText != ComLib.Lang.Core.Tokens.Colon.Text &&
                nextTokenText != "am" && nextTokenText != "pm")
            {
                return(false);
            }

            var text = Lexer.State.CurrentChar + _lexer.Scanner.PeekMaxChars(10);
            // 1. Check for am/pm ( required )
            var isAm      = true;
            var ndxAmOrPm = text.IndexOf("am", StringComparison.InvariantCultureIgnoreCase);

            if (ndxAmOrPm == -1)
            {
                ndxAmOrPm = text.IndexOf("pm", StringComparison.InvariantCultureIgnoreCase);
                if (ndxAmOrPm > -1)
                {
                    isAm = false;
                }
            }

            // Case 3: No am/pm designator so not applicable
            if (ndxAmOrPm == -1)
            {
                return(false);
            }

            // Case 4: 830pm
            if (text.IndexOf(":") < 0)
            {
                text    = current.Text + text.Substring(0, ndxAmOrPm + 2);
                _time   = DateTimeTypeHelper.ParseTimeWithoutColons(current.Text, isAm);
                _endPos = _lexer.State.Pos + ndxAmOrPm + 1;
                return(true);
            }
            // Note: the longest time string can only be 9 chars e.g. :30:45 am
            text = current.Text + text.Substring(0, ndxAmOrPm + 2);
            var result = DateTimeTypeHelper.ParseTime(text);

            // Was not a valid time string. maybe some other plguin can handle it.
            if (!result.Item2)
            {
                return(false);
            }

            _endPos = _lexer.State.Pos + ndxAmOrPm + 1;
            _time   = result.Item1;
            return(true);
        }