protected override string GetText(YearlyRecurrenceRule 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(" year");
            }
            else if (rrule.Interval == 2)
            {
                sb.Append(" other year");
            }
            else if (rrule.Interval > 2)
            {
                sb.Append(" ");
                sb.Append(rrule.Interval);
                sb.Append(" years");
            }

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

                    if (rrule.ByMonths != null && rrule.ByMonths.Any())
                    {
                        sb.Append(" of ");
                        sb.Append(rrule.ByMonths[0]);
                    }
                }
                else
                {
                    if (rrule.ByMonths != null && rrule.ByMonths.Any())
                    {
                        sb.Append(" on ");
                        sb.Append(rrule.ByMonths[0]);
                    }

                    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));

                if (rrule.ByMonths != null && rrule.ByMonths.Any())
                {
                    sb.Append(" of ");
                    sb.Append(rrule.ByMonths[0]);
                }
            }

            GetEndHumanText(rrule, sb);
            return(sb.ToString());
        }
        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);
            }
        }
        protected override string GetText(YearlyRecurrenceRule 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(" ans");
            }
            else
            {
                sb.Append(' ');
                sb.Append(rrule.Interval);
                sb.Append(" ans");
            }

            if (rrule.ByMonthDays != null && rrule.ByMonthDays.Any())
            {
                if (rrule.ByMonthDays.Any(day => day < 0))
                {
                    sb.Append(" le ");
                    ListToHumanText(sb, FrenchCultureInfo, rrule.ByMonthDays.Select(GetByMonthdayOrdinalHumanText).ToList(), ", ", " et ");
                    sb.Append(" jour");
                    if (rrule.ByMonths != null && rrule.ByMonths.Any())
                    {
                        var monthsList = ListToHumanText(FrenchCultureInfo, rrule.ByMonths.Select(MonthToString).ToList(), ", ", " et ");
                        if (MustPrecedeByApostrophe(monthsList))
                        {
                            sb.Append(" d'");
                        }
                        else
                        {
                            sb.Append(" de ");
                        }
                        sb.Append(monthsList);
                    }
                }
                else
                {
                    sb.Append(" le ");
                    ListToHumanText(sb, FrenchCultureInfo, rrule.ByMonthDays.Select(md => md.ToString(cultureInfo)).ToList(), ", ", " et ");
                    if (rrule.ByMonths != null && rrule.ByMonths.Any())
                    {
                        sb.Append(' ');
                        ListToHumanText(sb, FrenchCultureInfo, rrule.ByMonths.Select(MonthToString).ToList(), ", ", " et ");
                    }
                }
            }

            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));
                if (rrule.ByMonths != null && rrule.ByMonths.Any())
                {
                    var monthsList = ListToHumanText(FrenchCultureInfo, rrule.ByMonths.Select(MonthToString).ToList(), ", ", " et ");
                    if (MustPrecedeByApostrophe(monthsList))
                    {
                        sb.Append(" d'");
                    }
                    else
                    {
                        sb.Append(" de ");
                    }
                    sb.Append(monthsList);
                }
            }

            GetHumanEnd(rrule, sb);
            return(sb.ToString());
        }
예제 #4
0
 protected abstract string GetText(YearlyRecurrenceRule rrule, CultureInfo cultureInfo);