コード例 #1
0
        public override ITimePeriod GetTimePeriod(DateTime baseDate)
        {
            var        date        = baseDate.Date;
            var        time        = baseDate.TimeOfDay;
            var        namedEntity = string.Empty;
            ITimeRange result      = null;

            var lowerText = Text.ToLower();

            if (lowerText == "today")
            {
                result = new Day(date);
            }
            else if (lowerText == "tonight")
            {
                result = GetTimeOfDayPeriod(TimeOfDay.Night, date);
            }
            else if (lowerText == "last night")
            {
                result = GetTimeOfDayPeriod(TimeOfDay.Night, date.AddDays(-1));
            }
            else if (lowerText.StartsWith("yesterday"))
            {
                if (lowerText == "yesterday")
                {
                    result = new Day(date.AddDays(-1));
                }
                else if (lowerText == "yesterday morning")
                {
                    result = GetTimeOfDayPeriod(TimeOfDay.Morning, date.AddDays(-1));
                }
                else if (lowerText == "yesterday afternoon")
                {
                    result = GetTimeOfDayPeriod(TimeOfDay.Afternoon, date.AddDays(-1));
                }
            }
            else if (lowerText.StartsWith("tomorrow"))
            {
                if (lowerText == "tomorrow")
                {
                    result = new Day(date.AddDays(1));
                }
                else if (lowerText == "tomorrow morning")
                {
                    result = GetTimeOfDayPeriod(TimeOfDay.Morning, date.AddDays(1));
                }
                else if (lowerText == "tomorrow afternoon")
                {
                    result = GetTimeOfDayPeriod(TimeOfDay.Afternoon, date.AddDays(1));
                }
                else if (lowerText == "tomorrow night")
                {
                    result = GetTimeOfDayPeriod(TimeOfDay.Night, date.AddDays(1));
                }
            }
            else if (lowerText.StartsWith("next ") || lowerText.StartsWith("last ") || lowerText.StartsWith("this"))
            {
                var pieces = lowerText.Split(' ');
                if (pieces.Length == 2)
                {
                    var inPast   = pieces[0] == "last";
                    var inFuture = pieces[0] == "next";
                    namedEntity = pieces[1];

                    if (namedEntity == "week")
                    {
                        result = new Week(date);
                        if (inPast)
                        {
                            result = (result as Week).GetPreviousWeek();
                        }
                        else if (inFuture)
                        {
                            result = (result as Week).GetNextWeek();
                        }
                    }
                    else if (namedEntity == "month")
                    {
                        result = new Month(date);
                        if (inPast)
                        {
                            result = (result as Month).GetPreviousMonth();
                        }
                        else if (inFuture)
                        {
                            result = (result as Month).GetNextMonth();
                        }
                    }
                    else if (namedEntity == "year")
                    {
                        result = new Year(date);
                        if (inPast)
                        {
                            result = (result as Year).GetPreviousYear();
                        }
                        else if (inFuture)
                        {
                            result = (result as Year).GetNextYear();
                        }
                    }
                }
            }
            else if (NamedDate.IsMatch(Text))
            {
                namedEntity = NamedDate.Match(Text).Value;
            }

            //Handle named entities (eg. "Wednesday" or "January")
            if (!string.IsNullOrEmpty(namedEntity))
            {
                var inPast = lowerText.StartsWith("last ");

                //TODO: Support "this" named entity (eg. "this March" is the March in the baseDate year)

                if (DateComponent.GetWeekDayFromName(namedEntity) != -1)
                {
                    var moment = DateComponent.GetNextDateFromWeekDay(namedEntity, date);
                    result = new Day(moment);
                    if (inPast)
                    {
                        result = new Day(moment.AddDays(-7));
                    }
                }
                else if (DateComponent.GetMonthFromName(namedEntity) != -1)
                {
                    var moment = DateComponent.GetNextDateFromMonth(namedEntity, date);
                    result = new Month(moment);
                    if (inPast)
                    {
                        result = new Month(moment.AddMonths(-12));
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        public override ITimePeriod GetTimePeriod(DateTime baseDate)
        {
            var startPoint = baseDate;
            CalendarTimeRange sequenceInterval = null;

            //Handle "every" as name/string value
            var retrievedEvery = EveryRetrieve.Match(Text).Value;

            if (retrievedEvery.Length > 0)
            {
                if (DateComponent.GetWeekDayFromName(retrievedEvery) != -1)
                {
                    startPoint       = DateComponent.GetNextDateFromWeekDay(retrievedEvery, baseDate);
                    sequenceInterval = new CalendarTimeRange(startPoint, new TimeSpan(7, 0, 0, 0));
                }
                else if (DateComponent.GetMonthFromName(retrievedEvery) != -1)
                {
                    startPoint       = DateComponent.GetNextDateFromMonth(retrievedEvery, baseDate);
                    sequenceInterval = new Months(startPoint, (YearMonth)startPoint.Month, 12);
                }
                else
                {
                    return(null);
                }
            }

            if (sequenceInterval == null)
            {
                var retrievedGenericSequence = GenericSequenceIntervalRetrieve.Match(Text).Value;
                var singularGenericSequence  = retrievedGenericSequence.TrimEnd('s').ToLower();

                //Handle "every" as numeric value
                var retrievedNumericInterval = NumericIntervalRetrieve.Match(Text).Value;
                var parsedNumericInterval    = 1;
                if (retrievedNumericInterval.Length > 0)
                {
                    parsedNumericInterval = int.Parse(retrievedNumericInterval);
                }

                TimeSpan duration;

                switch (singularGenericSequence)
                {
                case "minute":
                    duration         = new TimeSpan(0, parsedNumericInterval, 0);
                    sequenceInterval = new CalendarTimeRange(startPoint, duration);
                    break;

                case "hour":
                    duration         = new TimeSpan(parsedNumericInterval, 0, 0);
                    sequenceInterval = new CalendarTimeRange(startPoint, duration);
                    break;

                case "day":
                    duration         = new TimeSpan(parsedNumericInterval, 0, 0, 0);
                    sequenceInterval = new CalendarTimeRange(startPoint, duration);
                    break;

                case "week":
                    duration         = new TimeSpan(7 * parsedNumericInterval, 0, 0, 0);
                    sequenceInterval = new CalendarTimeRange(startPoint, duration);
                    break;

                case "month":
                    sequenceInterval = new Months(startPoint, (YearMonth)startPoint.Month, parsedNumericInterval);
                    break;

                case "year":
                    sequenceInterval = new Years(startPoint, parsedNumericInterval);
                    break;

                default:
                    return(null);
                }
            }

            var result = new SequencedTimeRange(sequenceInterval);

            return(result);
        }