Ejemplo n.º 1
0
        /// <summary>
        /// Parses the day expression.
        /// Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
        /// </summary>
        /// <returns></returns>
        public override Expr Parse()
        {
            var name = _tokenIt.ExpectId().ToLower();

            // 1. Day of week : "monday" or "Monday" etc.
            if (_days.ContainsKey(name))
            {
                return(new ConstantExpr(_days[name]));
            }

            // 2. DateTime ( today, yesterday, tommorrow )
            var      dateTime = _dayAliases[name]();
            TimeSpan time     = TimeSpan.MinValue;

            if (_tokenIt.NextToken.Token.Text != "at")
            {
                return(new ConstantExpr(dateTime));
            }

            time     = TimeExprPlugin.ParseTime(_parser, true, true);
            dateTime = new DateTime(dateTime.Year, dateTime.Month, (int)dateTime.Day, time.Hours, time.Minutes, time.Seconds);
            return(new ConstantExpr(dateTime));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses a date.
        /// </summary>
        /// <param name="parser"></param>
        /// <returns></returns>
        public static DateTime ParseDate(ILangParser parser)
        {
            var       year    = DateTime.Now.Year;
            var       month   = DateTime.Now.Month;
            double    day     = DateTime.Now.Day;
            var       time    = new TimeSpan(0, 0, 0);
            var       tokenIt = parser.TokenIt;
            TokenData n       = null;

            if (tokenIt.NextToken.Token.Kind == TokenKind.LiteralDate)
            {
                var parsedDate = (DateTime)tokenIt.NextToken.Token.Value;
                year  = parsedDate.Year;
                month = parsedDate.Month;
                day   = parsedDate.Day;
            }
            else
            {
                // 1. 1st token is definitely month name in long or short form. "oct" or "october".
                var monthNameOrAbbr = tokenIt.ExpectId(true, true);
                month = _months[monthNameOrAbbr.ToLower()];

                // 2. Check for "," after month.
                if (tokenIt.NextToken.Token == Tokens.Comma)
                {
                    tokenIt.Advance();
                }

                // 3. 2nd token is the day 10.
                day = tokenIt.ExpectNumber(false);

                // 4. Check for "st nd rd th" as in 1st, 2nd, 3rd 4th for the day part.
                n = tokenIt.Peek();
                var text = n.Token.Text.ToLower();
                if (text == "st" || text == "nd" || text == "rd" || text == "th")
                {
                    tokenIt.Advance();
                }

                // 5. Check for another "," after day part.
                n = tokenIt.Peek();
                var n2 = tokenIt.Peek(2);

                // IMPORTANT: Make sure not to interpret the "," as feb 12, if the the "," is part of a comma in array/map.
                if (n.Token == Tokens.Comma && n2.Token.Type == TokenTypes.LiteralNumber)
                {
                    tokenIt.Advance();
                }

                // 6. Finally check for year
                n = tokenIt.Peek();
                if (n.Token.Type == TokenTypes.LiteralNumber)
                {
                    year = Convert.ToInt32(n.Token.Text);
                    tokenIt.Advance();
                }
            }
            // 8. Now check for time.
            n = tokenIt.Peek();
            if (n.Token.Text == "at")
            {
                time = TimeExprPlugin.ParseTime(parser, true, true);
            }
            else
            {
                tokenIt.Advance();
            }

            var date = new DateTime(year, month, (int)day, time.Hours, time.Minutes, time.Seconds);

            return(date);
        }