private RecurrencePattern _GetRecurrencePattern(DomainModels.Event ev)
        {
            var rule = new RecurrencePattern
            {
                Interval = ev.Period.Type == PeriodType.Custom ? ev.Period.Cycle.Value : 1,
                Until    = ev.Period.PeriodEnd
            };

            switch (ev.Period.Type)
            {
            case PeriodType.Yearly:
                rule.Frequency = FrequencyType.Yearly;
                break;

            case PeriodType.Monthly:
                rule.Frequency = FrequencyType.Monthly;
                break;

            case PeriodType.Weekly:
                rule.Frequency = FrequencyType.Weekly;
                break;

            case PeriodType.Custom:
                rule.Frequency = FrequencyType.Daily;
                break;
            }

            return(rule);
        }
Exemplo n.º 2
0
        public DomainCalendar ConvertIntoDomain(Calendar iCalendar)
        {
            var domainCalendar = new DomainCalendar();
            var domainEvents   = new List <DomainModels.Event>();

            TimeZoneInfo iCalTimezone = TimeZoneInfo.Utc;

            if (iCalendar.TimeZones?.Any() == true)
            {
                iCalTimezone = TimeZoneInfo.FindSystemTimeZoneById(iCalendar.TimeZones.First().TzId);
            }
            else if (iCalendar.Properties["X-WR-TIMEZONE"].Value != null)
            {
                var ianaTimezone    = iCalendar.Properties["X-WR-TIMEZONE"].Value.ToString();
                var windowsTimezone = TZConvert.IanaToWindows(ianaTimezone);

                iCalTimezone = TimeZoneInfo.FindSystemTimeZoneById(windowsTimezone);
            }

            foreach (var iEvent in iCalendar.Events)
            {
                var dEvent = new DomainModels.Event()
                {
                    Name      = iEvent.Summary,
                    IsAllDay  = iEvent.IsAllDay,
                    IsPrivate = false
                };

                if (!string.IsNullOrEmpty(iEvent.Description))
                {
                    dEvent.Description = iEvent.Description;
                }

                if (iEvent.GeographicLocation != null)
                {
                    dEvent.Location = new DomainModels.Location()
                    {
                        Description = iEvent.Location,
                        Longitude   = iEvent.GeographicLocation.Longitude,
                        Lattitude   = iEvent.GeographicLocation.Latitude
                    };
                }

                if (iEvent.RecurrenceRules.Any())
                {
                    var rule = iEvent.RecurrenceRules.First();
                    dEvent.Period             = _GetPeriod(rule);
                    dEvent.Period.PeriodStart = iEvent.DtStart.Date;

                    if (!iEvent.IsAllDay)
                    {
                        dEvent.StartTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(iEvent.DtStart.Value, DateTimeKind.Unspecified), iCalTimezone).TimeOfDay;
                        dEvent.EndTime   = TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(iEvent.DtEnd.Value, DateTimeKind.Unspecified), iCalTimezone).TimeOfDay;
                    }
                }
                else
                {
                    if (iEvent.IsAllDay)
                    {
                        dEvent.StartDate = iEvent.DtStart.Value.Date;
                        dEvent.EndDate   = iEvent.DtEnd.Value.Date;

                        if (iEvent.IsAllDay && iEvent.DtEnd.Minute == 0)
                        {
                            dEvent.EndDate = dEvent.EndDate.Value.AddDays(-1);
                        }
                    }
                    else
                    {
                        var utcStart = TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(iEvent.DtStart.Value, DateTimeKind.Unspecified), iCalTimezone);
                        var utcEnd   = TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(iEvent.DtEnd.Value, DateTimeKind.Unspecified), iCalTimezone);

                        dEvent.StartDate = utcStart.Date;
                        dEvent.EndDate   = utcEnd.Date;
                        dEvent.StartTime = utcStart.TimeOfDay;
                        dEvent.EndTime   = utcEnd.TimeOfDay;
                    }
                }

                domainEvents.Add(dEvent);
            }

            if (domainEvents.Any())
            {
                domainCalendar.Events = domainEvents
                                        .Select(e => new DomainModels.EventCalendar {
                    Event = e, IsReadOnly = false
                });
            }

            return(domainCalendar);
        }