protected override bool ParseMatch(DatesRawData data, Match match, DateTime userDate)
        {
            var monthFixed = false;

            // parse month
            var mStr = data.Tokens[match.Groups[2].Index].Value;

            Console.WriteLine(mStr);
            var month = ParserUtils.FindIndex(mStr, Keywords.Months()) + 1;

            if (month == 0)
            {
                month = userDate.Month;             // # instead M
            }
            else
            {
                monthFixed = true;
            }

            // set for first date same month as for second, ignore second (will parse further)
            var t = data.Tokens[match.Groups[1].Index];

            int.TryParse(t.Value, out var day);

            // current token is number, store it as a day
            var period = new AbstractPeriod
            {
                Date = new DateTime(
                    userDate.Year,
                    month,
                    ParserUtils.GetDayValidForMonth(userDate.Year, month, day)
                    )
            };

            // fix from week to day, and year/month if it was
            period.Fix(FixPeriod.Week, FixPeriod.Day);
            if (monthFixed)
            {
                period.Fix(FixPeriod.Month);
            }

            // replace
            data.ReplaceTokensByDates(match.Groups[1].Index, 1, period);

            return(true);
        }
Beispiel #2
0
        protected override bool ParseMatch(DatesRawData data, Match match, DateTime userDate)
        {
            var dates      = new List <AbstractPeriod>();
            var monthFixed = false;

            // parse month
            var mStr  = data.Tokens[match.Index + match.Groups[1].Length].Value;
            var month = ParserUtils.FindIndex(mStr, Keywords.Months()) + 1;

            if (month == 0)
            {
                month = userDate.Month;             // # instead M
            }
            else
            {
                monthFixed = true;
            }

            // create dates
            for (var i = match.Index; i < match.Index + match.Groups[1].Length; i++)
            {
                var t = data.Tokens[i];
                int.TryParse(t.Value, out var day);
                if (day <= 0)
                {
                    continue;           // this is "AND" or other token
                }
                // current token is number, store it as a day
                var period = new AbstractPeriod
                {
                    Date = new DateTime(
                        userDate.Year,
                        month,
                        ParserUtils.GetDayValidForMonth(userDate.Year, month, day)
                        )
                };

                // fix from week to day, and year/month if it was
                period.Fix(FixPeriod.Week, FixPeriod.Day);
                if (monthFixed)
                {
                    period.Fix(FixPeriod.Month);
                }

                // store
                dates.Add(period);

                // compare with last if month not fixed
                if (!monthFixed && dates.Count > 0 && dates.Last().Date < period.Date)
                {
                    period.Date = new DateTime(
                        userDate.Year,
                        month + 1,
                        ParserUtils.GetDayValidForMonth(userDate.Year, month + 1, day)
                        );
                }
            }

            // replace all scanned tokens
            data.ReplaceTokensByDates(match.Index, match.Length, dates.ToArray());

            return(true);
        }