Example #1
0
        public override bool TryParse(string value, out DateTime result, DateTime current)
        {
            result = current;

            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }

            var patternBuilder = new StringBuilder();

            foreach (var special in SpecialNames.SpecialNamesSpecification.Keys)
            {
                patternBuilder.AppendFormat(@"|(?<{0}>(?<=\b|\W){0}(?=\b|\W))", special);
            }

            patternBuilder.Remove(0, 1);             // Remove the first |.

            var pattern = patternBuilder.ToString();
            var regex   = new Regex(pattern, RegexOptions.IgnoreCase);
            var match   = regex.Match(value);

            result = DateParserBase.MergeDateAndTime(DateTime.Now, current);

            if (match.Success)
            {
                var index       = this.FindMatchingGroupIndex(match.Groups.Cast <Group>().Skip(1).ToList());
                var specialName = regex.GroupNameFromNumber(index + 1);

                result = result.AddDays(SpecialNames.SpecialNamesSpecification[specialName]);

                return(true);
            }
            return(false);
        }
Example #2
0
        public override bool TryParse(string value, out DateTime result, DateTime current)
        {
            result = current;

            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }

            var normalizedDate = this.NormalizeDateString(value, current);
            var dateParts      = normalizedDate.Split(new string[] { this.Separator }, StringSplitOptions.RemoveEmptyEntries);

            bool r = false;

            // TODO: Handle two parts separately.
            ////if (dateParts.Length == 2)
            ////{
            ////    dateParts.Add(current.Year.ToString());
            ////}

            // NOTE: Handle one part.
            if (dateParts.Length == 1)
            {
                result = current;

                int n;

                // We could have month or number.
                // If it is a number it could be day or year.
                // It is year only if cannot be a day.
                if (int.TryParse(dateParts[0], out n))
                {
                    var daysCount = this.DateTimeFormat.Calendar.GetDaysInMonth(current.Year, current.Month);

                    // Is it a valid day?
                    if (daysCount >= n && n > 0)
                    {
                        result = this.SetDay(current, n);
                        r      = true;
                    }
                    else if (n >= 0 && n < DateTime.MaxValue.Year)
                    {
                        // Is it a valid year?
                        result = this.SetYear(current, n);
                        r      = true;
                    }

                    // If not - give up - we cannot parse it!
                }
                else
                {
                    bool isLetter = Char.IsLetter(dateParts[0].ToString(), dateParts[0].Length - 1);

                    if (isLetter)
                    {
                        // It is obviosly a month. We should find its number!
                        var abbrMonths = this.DateTimeFormat.AbbreviatedMonthNames;
                        var months     = this.DateTimeFormat.MonthNames;
                        int i          = 0;
                        for (i = 0; i < abbrMonths.Length - 1; i++)
                        {
                            if (abbrMonths[i].Equals(dateParts[0], StringComparison.CurrentCultureIgnoreCase) ||
                                months[i].Equals(dateParts[0], StringComparison.CurrentCultureIgnoreCase))
                            {
                                break;
                            }
                        }
                        result = this.SetMonth(current, i + 1);
                        r      = true;
                    }
                }
            }
            else
            {
                DateTime dt = result;
                r = DateTime.TryParse(normalizedDate, this.DateTimeFormat, DateTimeStyles.None, out result);
                if (!r)
                {
                    result = current;
                }
            }
            result = DateParserBase.MergeDateAndTime(result, current);
            return(r);
        }