コード例 #1
0
        /// <summary>
        ///     Generate a <see cref="CalendarDays" /> info.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="defaultDayInfo">Default info in case of missing info</param>
        /// <returns></returns>
        public CalendarDays GenerateCalendarDays(DateTime from, DateTime to, DayInfo defaultDayInfo)
        {
            if (to < from)
            {
                throw new ArgumentException($"'{nameof(to)}' cannot precede '{nameof(from)}'");
            }

            if (defaultDayInfo is null)
            {
                throw new ArgumentNullException(nameof(defaultDayInfo));
            }

            var calendarDays = new CalendarDays();

            for (var cur = from; cur <= to; cur = cur.AddDays(1))
            {
                var info           = GetDayInfo(cur) ?? defaultDayInfo;
                var workingPeriods =
                    info.WorkingPeriods?.Select(wp => new CalendarDays.TimePeriod
                {
                    Begin = wp.Begin.ToString(),
                    End   = wp.End.ToString()
                }).ToList();

                calendarDays.Add(new CalendarDays.Day
                {
                    Date           = cur.ToString("yyyy-MM-dd"),
                    IsWorkingDay   = info.IsWorkingDay,
                    Description    = info.Description,
                    WorkingPeriods = workingPeriods
                });
            }

            return(calendarDays);
        }
コード例 #2
0
        /// <summary>
        ///     Parse a day info declaration.
        ///     <example>
        ///         Valid strings are:
        ///         [[]] is a day info without a description and a working time
        ///         [[day description]] is a day info with a description and without a working time
        ///         [[day description]] 00:00-01:00,22:00-23:00 is a day info with a description and a working time
        ///         [[]] 00:00-01:00 is a day info without a description and a working time
        ///     </example>
        /// </summary>
        /// <returns></returns>
        public static bool TryParse(string value, out DayInfo dayInfo)
        {
            dayInfo = default;
            IEnumerable <TimePeriod> timePeriods = default;

            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }
            var m = ParseRegex.Match(value);

            if (!m.Success)
            {
                return(false);
            }

            var description    = m.Groups["description"].Value;
            var strTimePeriods = m.Groups["timePeriods"].Value;

            if (!string.IsNullOrWhiteSpace(strTimePeriods) &&
                !TimePeriod.TryParseMulti(strTimePeriods, ",", out timePeriods))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(description))
            {
                description = null;
            }

            dayInfo = new DayInfo(description, timePeriods);

            return(true);
        }
コード例 #3
0
 public bool TryGetDayInfo(DateTime date, out DayInfo dayInfo)
 {
     dayInfo = default;
     if (YearMatchers.Any(_ => _.Match(date)) &&
         MonthMatchers.Any(_ => _.Match(date)) &&
         DayOfMonthMatchers.Any(_ => _.Match(date)) &&
         DayOfWeekMatchers.Any(_ => _.Match(date)))
     {
         dayInfo = new DayInfo(Description, WorkingPeriods);
     }
     return(dayInfo is not null);
 }
コード例 #4
0
        public DayRule(
            string description,
            IEnumerable <IYearMatcher> yearMatchers,
            IEnumerable <IMonthMatcher> monthMatchers,
            IEnumerable <IDayOfMonthMatcher> dayOfMonthMatchers,
            IEnumerable <IDayOfWeekMatcher> dayOfWeekMatchers,
            IEnumerable <TimePeriod> workingPeriods
            )
        {
            Description        = description;
            YearMatchers       = new List <IYearMatcher>(yearMatchers);
            MonthMatchers      = new List <IMonthMatcher>(monthMatchers);
            DayOfMonthMatchers = new List <IDayOfMonthMatcher>(dayOfMonthMatchers);
            DayOfWeekMatchers  = new List <IDayOfWeekMatcher>(dayOfWeekMatchers);
            WorkingPeriods     = workingPeriods is not null ? new List <TimePeriod>(workingPeriods) : null;

            DayInfo.NormalizeWorkingPeriods(WorkingPeriods);
        }
コード例 #5
0
        public bool TryGetDayInfo(DateTime date, out DayInfo dayInfo)
        {
            dayInfo = null;
            if (CalendarRules == null || !CalendarRules.Any())
            {
                return(false);                                               // no rules => nothing to do
            }
            foreach (var rule in CalendarRules)
            {
                if (!rule.Rule.TryGetDayInfo(date, out var curInfo))
                {
                    continue;      // something to evaluate
                }
                dayInfo = curInfo; // register the most recent found
                if (rule.Policy == DayRulePolicy.Accept)
                {
                    break;
                }
            }

            return(dayInfo != null);
        }