private static void GetHumanEnd(RecurrenceRule rrule, StringBuilder sb)
        {
            if (rrule.Occurrences.HasValue)
            {
                sb.Append(" pour ");
                sb.Append(rrule.Occurrences.Value);
                sb.Append(" fois");
            }

            if (rrule.EndDate.HasValue)
            {
                sb.Append(" jusqu'au ");
                sb.Append(rrule.EndDate.Value.ToString("d MMMM yyyy", FrenchCultureInfo));
            }
        }
Ejemplo n.º 2
0
        public static string GetText(RecurrenceRule rrule, CultureInfo cultureInfo)
        {
            if (rrule == null)
            {
                throw new ArgumentNullException(nameof(rrule));
            }

            if (cultureInfo == null)
            {
                cultureInfo = CultureInfo.CurrentUICulture;
            }

            if (!SupportedHumanizers.TryGetValue(cultureInfo, out var humanizer))
            {
                if (!cultureInfo.IsNeutralCulture)
                {
                    return(GetText(rrule, cultureInfo.Parent));
                }
            }

            if (humanizer != null)
            {
                if (rrule is DailyRecurrenceRule dailyRecurrenceRule)
                {
                    return(humanizer.GetText(dailyRecurrenceRule, cultureInfo));
                }

                if (rrule is WeeklyRecurrenceRule weeklyRecurrenceRule)
                {
                    return(humanizer.GetText(weeklyRecurrenceRule, cultureInfo));
                }

                if (rrule is MonthlyRecurrenceRule monthlyRecurrenceRule)
                {
                    return(humanizer.GetText(monthlyRecurrenceRule, cultureInfo));
                }

                if (rrule is YearlyRecurrenceRule yearlyRecurrenceRule)
                {
                    return(humanizer.GetText(yearlyRecurrenceRule, cultureInfo));
                }
            }

            return(null);
        }
        private static void GetEndHumanText(RecurrenceRule rrule, StringBuilder sb)
        {
            if (rrule.Occurrences.HasValue)
            {
                sb.Append(" for ");
                sb.Append(rrule.Occurrences.Value);
                if (rrule.Occurrences.Value <= 1)
                {
                    sb.Append(" time");
                }
                else
                {
                    sb.Append(" times");
                }
            }

            if (rrule.EndDate.HasValue)
            {
                sb.Append(" until ");
                sb.Append(rrule.EndDate.Value.ToString("MMMM d, yyyy", EnglishCultureInfo));
            }
        }
        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);
            }
        }
 public static bool TryParse(string rrule, out RecurrenceRule recurrenceRule)
 {
     return(TryParse(rrule, out recurrenceRule, out string error));
 }
Ejemplo n.º 6
0
 public static string GetText(RecurrenceRule rrule)
 {
     return(GetText(rrule, cultureInfo: null));
 }
 public static string?GetHumanText(this RecurrenceRule rrule)
 {
     return(RecurrenceRuleHumanizer.GetText(rrule, cultureInfo: null));
 }
 public static string GetHumanText(this RecurrenceRule rrule)
 {
     return(RecurrenceRuleHumanizer.GetText(rrule));
 }
 public static string GetHumanText(this RecurrenceRule rrule, CultureInfo cultureInfo)
 {
     return(RecurrenceRuleHumanizer.GetText(rrule, cultureInfo));
 }