コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the FuzzyDateRange class with the specified From and To values.
        /// </summary>
        /// <param name="from">The From value. Can be any FuzzyDate.</param>
        /// <param name="to">The To value. Can be any FuzzyDate.</param>
        public FuzzyDateRange(FuzzyDate from, FuzzyDate to)
        {
            From = from;
            To   = to;

            RulesRunner.RunRules(this);
        }
コード例 #2
0
        /// <summary>
        /// Parses a date range from two fuzzy dates delimited by a hyphen or dash character.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static FuzzyDateRange Parse(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return(new FuzzyDateRange());
            }

            // Support several different delineators, but they can't overlap with FuzzyDate delineators
            value = value
                    .Replace("–", "-")             // En dash
                    .Replace("—", "-")             // Em dash
                    .Replace("−", "-")             // Minus sign
                    .Replace(" to ", "-");

            var regex = new Regex(@"([^\-]*)\-([^\-]*)");
            var match = regex.Match(value);

            if (match.Success)
            {
                var left  = match.Groups[1].Value;
                var right = match.Groups[2].Value;

                return(new FuzzyDateRange(FuzzyDate.Parse(left), FuzzyDate.Parse(right)));
            }

            throw new BadDateRangeFormatException();
        }