protected override string GetText(MonthlyRecurrenceRule rrule, CultureInfo cultureInfo)
        {
            if (rrule == null)
            {
                throw new ArgumentNullException(nameof(rrule));
            }
            if (cultureInfo == null)
            {
                throw new ArgumentNullException(nameof(cultureInfo));
            }

            var sb = new StringBuilder();

            sb.Append("every");
            if (rrule.Interval == 1)
            {
                sb.Append(" month");
            }
            else if (rrule.Interval == 2)
            {
                sb.Append(" other month");
            }
            else if (rrule.Interval > 2)
            {
                sb.Append(" ");
                sb.Append(rrule.Interval);
                sb.Append(" months");
            }

            if (rrule.ByMonthDays != null && rrule.ByMonthDays.Any())
            {
                if (rrule.ByMonthDays.Any(day => day < 0))
                {
                    sb.Append(" on the ");
                }
                else
                {
                    sb.Append(" the ");
                }
                ListToHumanText(sb, EnglishCultureInfo, rrule.ByMonthDays.Select(GetByMonthdayHumanText).ToList(), ", ", " and ");
            }

            if (rrule.ByWeekDays != null && rrule.ByWeekDays.Any())
            {
                sb.Append(" on ");
                if (rrule.BySetPositions != null && rrule.BySetPositions.Any())
                {
                    sb.Append("the ");
                    sb.Append(GetBySetPosHumanText(rrule.BySetPositions[0]));
                    sb.Append(" ");
                }
                sb.Append(GetWeekdayHumanText(rrule.ByWeekDays, options: WeekdayHumanTextOptions.AbbrDays | WeekdayHumanTextOptions.AbbrWeekdays | WeekdayHumanTextOptions.AbbrWeekendDays));
            }

            GetEndHumanText(rrule, sb);
            return(sb.ToString());
        }
        protected override string GetText(MonthlyRecurrenceRule rrule, CultureInfo cultureInfo)
        {
            if (rrule == null)
            {
                throw new ArgumentNullException(nameof(rrule));
            }
            if (cultureInfo == null)
            {
                throw new ArgumentNullException(nameof(cultureInfo));
            }

            var sb = new StringBuilder();

            sb.Append("tous les");
            if (rrule.Interval == 1)
            {
                sb.Append(" mois");
            }
            else
            {
                sb.Append(' ');
                sb.Append(rrule.Interval);
                sb.Append(" mois");
            }

            if (rrule.ByMonthDays != null && rrule.ByMonthDays.Any())
            {
                sb.Append(" le ");
                ListToHumanText(sb, FrenchCultureInfo, rrule.ByMonthDays.Select(GetByMonthdayOrdinalHumanText).ToList(), ", ", " et ");
                sb.Append(" jour");
            }

            if (rrule.ByWeekDays != null && rrule.ByWeekDays.Any())
            {
                if (rrule.BySetPositions != null && rrule.BySetPositions.Any())
                {
                    sb.Append(" le ");
                    sb.Append(GetBySetPosHumanText(rrule.BySetPositions[0]));
                }
                sb.Append(' ');
                sb.Append(GetWeekdayHumanText(rrule.ByWeekDays, options: WeekdayHumanTextOptions.AbbrDays | WeekdayHumanTextOptions.AbbrWeekdays | WeekdayHumanTextOptions.AbbrWeekendDays));
            }

            GetHumanEnd(rrule, sb);
            return(sb.ToString());
        }
コード例 #3
0
        public static bool TryParse(string rrule, out RecurrenceRule recurrenceRule, out string error)
        {
            if (rrule == null)
            {
                throw new ArgumentNullException(nameof(rrule));
            }
            recurrenceRule = null;
            error          = null;
            try
            {
                // Extract parts
                IDictionary <string, string> values = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                var parts = rrule.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var part in parts)
                {
                    var split = SplitPart(part);
                    if (values.ContainsKey(split.Item1))
                    {
                        error = $"Duplicate name: '{split.Item1}'.";
                        return(false);
                    }

                    if (string.Equals("UNTIL", split.Item1, StringComparison.OrdinalIgnoreCase) && values.ContainsKey("COUNT"))
                    {
                        error = "Cannot set UNTIL and COUNT in the same recurrence rule.";
                        return(false);
                    }

                    if (string.Equals("COUNT", split.Item1, StringComparison.OrdinalIgnoreCase) && values.ContainsKey("UNTIL"))
                    {
                        error = "Cannot set UNTIL and COUNT in the same recurrence rule.";
                        return(false);
                    }

                    values.Add(split.Item1, split.Item2);
                }

                // Set specific properties
                var frequency = values.GetValue("FREQ", Frequency.None);
                switch (frequency)
                {
                case Frequency.Daily:
                    var dailyRecurrenceRule = new DailyRecurrenceRule
                    {
                        ByMonthDays = ParseByMonthDays(values),
                        ByMonths    = ParseByMonth(values),
                        ByWeekDays  = ParseByDay(values)
                    };
                    recurrenceRule = dailyRecurrenceRule;
                    break;

                case Frequency.Weekly:
                    var weeklyRecurrence = new WeeklyRecurrenceRule
                    {
                        ByMonths   = ParseByMonth(values),
                        ByWeekDays = ParseByDay(values)
                    };
                    recurrenceRule = weeklyRecurrence;
                    break;

                case Frequency.Monthly:
                    var monthlyRecurrence = new MonthlyRecurrenceRule
                    {
                        ByWeekDays  = ParseByDayWithOffset(values),
                        ByMonthDays = ParseByMonthDays(values),
                        ByMonths    = ParseByMonth(values)
                    };
                    recurrenceRule = monthlyRecurrence;
                    break;

                case Frequency.Yearly:
                    var yearlyRecurrence = new YearlyRecurrenceRule
                    {
                        ByWeekDays     = ParseByDayWithOffset(values),
                        ByMonthDays    = ParseByMonthDays(values),
                        BySetPositions = ParseBySetPos(values),
                        ByMonths       = ParseByMonth(values),
                        ByYearDays     = ParseByYearDay(values)
                    };
                    //yearlyRecurrence.ByWeekNo = ParseByWeekNo(values);
                    recurrenceRule = yearlyRecurrence;
                    break;

                default:
                    error = "Unknown Frequency (FREQ).";
                    return(false);
                }

                // Set general properties
                // Set Interval
                recurrenceRule.Interval    = values.GetValue("INTERVAL", 1);
                recurrenceRule.Occurrences = values.GetValue("COUNT", (int?)null);
                var until = values.GetValue("UNTIL", (string)null);
                if (until != null)
                {
                    recurrenceRule.EndDate = Utilities.ParseDateTime(until);
                }
                recurrenceRule.BySetPositions = ParseBySetPos(values);
                recurrenceRule.WeekStart      = ParseWeekStart(values);

                return(true);
            }
            catch (FormatException e)
            {
                error = e.Message;
                return(false);
            }
        }
コード例 #4
0
 protected abstract string GetText(MonthlyRecurrenceRule rrule, CultureInfo cultureInfo);