Beispiel #1
0
        static private void PopulateiCalTimeZoneInfo(iCalTimeZoneInfo tzi, System.TimeZoneInfo.TransitionTime transition, int year)
        {
            Calendar c = CultureInfo.CurrentCulture.Calendar;

            RecurrencePattern recurrence = new RecurrencePattern();

            recurrence.Frequency = FrequencyType.Yearly;
            recurrence.ByMonth.Add(transition.Month);
            recurrence.ByHour.Add(transition.TimeOfDay.Hour);
            recurrence.ByMinute.Add(transition.TimeOfDay.Minute);

            if (transition.IsFixedDateRule)
            {
                recurrence.ByMonthDay.Add(transition.Day);
            }
            else
            {
                recurrence.ByDay.Add(new DaySpecifier(transition.DayOfWeek));
                int daysInMonth = c.GetDaysInMonth(year, transition.Month);
                int offset      = (transition.Week * 7) - 7;
                if (offset + 7 > daysInMonth)
                {
                    offset = daysInMonth - 7;
                }

                // Add the possible days of the month this could occur.
                for (int i = 1; i <= 7; i++)
                {
                    recurrence.ByMonthDay.Add(i + offset + (int)transition.DayOfWeek);
                }
            }

            tzi.AddRecurrencePattern(recurrence);
        }
Beispiel #2
0
        static private void PopulateiCalTimeZoneInfo(iCalTimeZoneInfo tzi, System.TimeZoneInfo.TransitionTime transition, int year)
        {
            Calendar c = CultureInfo.CurrentCulture.Calendar;

            RecurrencePattern recurrence = new RecurrencePattern();
            recurrence.Frequency = FrequencyType.Yearly;
            recurrence.ByMonth.Add(transition.Month);
            recurrence.ByHour.Add(transition.TimeOfDay.Hour);
            recurrence.ByMinute.Add(transition.TimeOfDay.Minute);

            if (transition.IsFixedDateRule)
            {
                recurrence.ByMonthDay.Add(transition.Day);
            }
            else
            {
                recurrence.ByDay.Add(new DaySpecifier(transition.DayOfWeek));
                int daysInMonth = c.GetDaysInMonth(year, transition.Month);
                int offset = (transition.Week * 7) - 7;
                if (offset + 7 > daysInMonth)
                    offset = daysInMonth - 7;

                // Add the possible days of the month this could occur.
                for (int i = 1; i <= 7; i++)
                    recurrence.ByMonthDay.Add(i + offset + (int)transition.DayOfWeek);
            }

            tzi.AddRecurrencePattern(recurrence);
        }
        public override bool Equals(object obj)
        {
            iCalTimeZoneInfo tzi = obj as iCalTimeZoneInfo;

            if (tzi != null)
            {
                return(object.Equals(TZName, tzi.TZName) &&
                       object.Equals(TZOffsetFrom, tzi.TZOffsetFrom) &&
                       object.Equals(TZOffsetTo, tzi.TZOffsetTo));
            }
            return(base.Equals(obj));
        }
Beispiel #4
0
        public static iCalTimeZone FromSystemTimeZone(System.TimeZoneInfo tzinfo)
        {
            var adjustmentRules = tzinfo.GetAdjustmentRules();
            var utcOffset       = tzinfo.BaseUtcOffset;
            var dday_tz         = new iCalTimeZone();

            foreach (var adjustmentRule in adjustmentRules)
            {
                var delta = adjustmentRule.DaylightDelta;
                dday_tz.TZID = tzinfo.Id;

                var dday_tzinfo_standard = new DDay.iCal.Components.iCalTimeZoneInfo();
                dday_tzinfo_standard.Name  = "STANDARD";
                dday_tzinfo_standard.Start = adjustmentRule.DateStart;
                if (dday_tzinfo_standard.Start.Year < 1800)
                {
                    dday_tzinfo_standard.Start = dday_tzinfo_standard.Start.AddYears(1800 - dday_tzinfo_standard.Start.Year);
                }
                dday_tzinfo_standard.TZOffsetFrom = new DDay.iCal.DataTypes.UTC_Offset(utcOffset + delta);
                dday_tzinfo_standard.TZOffsetTo   = new DDay.iCal.DataTypes.UTC_Offset(utcOffset);
                PopulateiCalTimeZoneInfo(dday_tzinfo_standard, adjustmentRule.DaylightTransitionEnd, adjustmentRule.DateStart.Year);

                // Add the "standard" time rule to the time zone
                dday_tz.AddChild(dday_tzinfo_standard);

                var dday_tzinfo_daylight = new DDay.iCal.Components.iCalTimeZoneInfo();
                dday_tzinfo_daylight.Name  = "DAYLIGHT";
                dday_tzinfo_daylight.Start = adjustmentRule.DateStart;
                if (dday_tzinfo_daylight.Start.Year < 1800)
                {
                    dday_tzinfo_daylight.Start = dday_tzinfo_daylight.Start.AddYears(1800 - dday_tzinfo_daylight.Start.Year);
                }
                dday_tzinfo_daylight.TZOffsetFrom = new DDay.iCal.DataTypes.UTC_Offset(utcOffset);
                dday_tzinfo_daylight.TZOffsetTo   = new DDay.iCal.DataTypes.UTC_Offset(utcOffset + delta);
                PopulateiCalTimeZoneInfo(dday_tzinfo_daylight, adjustmentRule.DaylightTransitionStart, adjustmentRule.DateStart.Year);

                // Add the "daylight" time rule to the time zone
                dday_tz.AddChild(dday_tzinfo_daylight);
            }

            return(dday_tz);
        }
Beispiel #5
0
        public static iCalTimeZone FromSystemTimeZone(System.TimeZoneInfo tzinfo)
        {
            var adjustmentRules = tzinfo.GetAdjustmentRules();
            var utcOffset = tzinfo.BaseUtcOffset;
            var dday_tz = new iCalTimeZone();

            foreach (var adjustmentRule in adjustmentRules)
            {                
                var delta = adjustmentRule.DaylightDelta;
                dday_tz.TZID = tzinfo.Id;                

                var dday_tzinfo_standard = new DDay.iCal.Components.iCalTimeZoneInfo();
                dday_tzinfo_standard.Name = "STANDARD";
                dday_tzinfo_standard.Start = adjustmentRule.DateStart;
                if (dday_tzinfo_standard.Start.Year < 1800)
                    dday_tzinfo_standard.Start = dday_tzinfo_standard.Start.AddYears(1800 - dday_tzinfo_standard.Start.Year);
                dday_tzinfo_standard.TZOffsetFrom = new DDay.iCal.DataTypes.UTC_Offset(utcOffset + delta);
                dday_tzinfo_standard.TZOffsetTo = new DDay.iCal.DataTypes.UTC_Offset(utcOffset);
                PopulateiCalTimeZoneInfo(dday_tzinfo_standard, adjustmentRule.DaylightTransitionEnd, adjustmentRule.DateStart.Year);

                // Add the "standard" time rule to the time zone
                dday_tz.AddChild(dday_tzinfo_standard);

                var dday_tzinfo_daylight = new DDay.iCal.Components.iCalTimeZoneInfo();
                dday_tzinfo_daylight.Name = "DAYLIGHT";
                dday_tzinfo_daylight.Start = adjustmentRule.DateStart;
                if (dday_tzinfo_daylight.Start.Year < 1800)
                    dday_tzinfo_daylight.Start = dday_tzinfo_daylight.Start.AddYears(1800 - dday_tzinfo_daylight.Start.Year);
                dday_tzinfo_daylight.TZOffsetFrom = new DDay.iCal.DataTypes.UTC_Offset(utcOffset);
                dday_tzinfo_daylight.TZOffsetTo = new DDay.iCal.DataTypes.UTC_Offset(utcOffset + delta);
                PopulateiCalTimeZoneInfo(dday_tzinfo_daylight, adjustmentRule.DaylightTransitionStart, adjustmentRule.DateStart.Year);

                // Add the "daylight" time rule to the time zone
                dday_tz.AddChild(dday_tzinfo_daylight);                
            }

            return dday_tz;
        }
Beispiel #6
0
        /// <summary>
        /// Retrieves the iCalTimeZoneInfo object that contains information
        /// about the TimeZone, with the name of the current timezone,
        /// offset from UTC, etc.
        /// </summary>
        /// <param name="dt">The iCalDateTime object for which to retrieve the iCalTimeZoneInfo.</param>
        /// <returns>A TimeZoneInfo object for the specified iCalDateTime</returns>
        public iCalTimeZoneInfo GetTimeZoneInfo(iCalDateTime dt)
        {
            iCalTimeZoneInfo tzi = null;

            TimeSpan mostRecent = TimeSpan.MaxValue;

            foreach (iCalTimeZoneInfo curr in TimeZoneInfos)
            {
                DateTime Start = new DateTime(dt.Year - 1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                DateTime End   = new DateTime(dt.Year + 1, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                DateTime dtUTC = dt.Value;
                dtUTC = DateTime.SpecifyKind(dtUTC, DateTimeKind.Utc);

                // Time zones must include an effective start date/time.
                if (curr.Start == null)
                {
                    continue;
                }

                // Make a copy of the current start value
                iCalDateTime currStart = curr.Start.Copy();
                if (curr.TZOffsetTo != null)
                {
                    int mult = curr.TZOffsetTo.Positive ? -1 : 1;
                    dtUTC = dtUTC.AddHours(curr.TZOffsetTo.Hours * mult);
                    dtUTC = dtUTC.AddMinutes(curr.TZOffsetTo.Minutes * mult);
                    dtUTC = dtUTC.AddSeconds(curr.TZOffsetTo.Seconds * mult);
                    // Offset the current start value to match our offset time...
                    currStart = currStart.AddHours(curr.TZOffsetTo.Hours * mult);
                    currStart = currStart.AddMinutes(curr.TZOffsetTo.Minutes * mult);
                    currStart = currStart.AddSeconds(curr.TZOffsetTo.Seconds * mult);
                }

                // Determine the UTC occurrences of the Time Zone changes
                if (curr.EvalStart == null ||
                    curr.EvalEnd == null ||
                    dtUTC < curr.EvalStart.Value ||
                    dtUTC > curr.EvalEnd.Value)
                {
                    curr.Evaluate(Start, End);
                }

                // If the date is past the last allowed date, then don't consider it!
                // NOTE: if this time zone ends as another begins, then there can
                // be up to a 1 year period of "unhandled" time unless we add a year
                // to the "Until" date.  For example, one time period "ends" in Oct. 2006
                // (the last occurrence), and the next begins in Apr. 2007.  If we didn't
                // add 1 year to the "Until" time, the 6 month period between Oct. 2006
                // and Apr. 2007 would be unhandled.

                // FIXME: this thinking may be flawed. We should try to find some other way...
                //
                //if (curr.Until != null &&
                //    dtUTC > curr.Until.AddYears(1))
                //    continue;

                foreach (Period p in curr.Periods)
                {
                    TimeSpan currentSpan = dtUTC - p.StartTime;
                    if (currentSpan.Ticks >= 0 &&
                        currentSpan.Ticks < mostRecent.Ticks)
                    {
                        mostRecent = currentSpan;
                        tzi        = curr;
                    }
                }
            }

            return(tzi);
        }
Beispiel #7
0
        public DateTime ToTimeZone(iCalTimeZoneInfo tzi)
        {
            DateTime value = UTC;

            int mult = tzi.TZOffsetTo.Positive ? 1 : -1;
            value = value.AddHours(tzi.TZOffsetTo.Hours * mult);
            value = value.AddMinutes(tzi.TZOffsetTo.Minutes * mult);
            value = value.AddSeconds(tzi.TZOffsetTo.Seconds * mult);
            value = DateTime.SpecifyKind(value, DateTimeKind.Unspecified);
            return value;
        }
Beispiel #8
0
        public void RRULE43()
        {
            iCalendar iCal = new iCalendar();

            iCalTimeZone tz = iCal.Create<iCalTimeZone>();

            tz.TZID = "US-Eastern";
            tz.Last_Modified = new DateTime(1987, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            iCalTimeZoneInfo standard = new iCalTimeZoneInfo(iCalTimeZone.STANDARD, tz);
            standard.Start = new DateTime(1967, 10, 29, 2, 0, 0, DateTimeKind.Utc);
            standard.AddRecurrencePattern(new RecurrencePattern("FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10"));
            standard.TZOffsetFrom = new UTC_Offset("-0400");
            standard.TZOffsetTo = new UTC_Offset("-0500");
            standard.TimeZoneName = "EST";

            iCalTimeZoneInfo daylight = new iCalTimeZoneInfo(iCalTimeZone.DAYLIGHT, tz);
            daylight.Start = new DateTime(1987, 4, 5, 2, 0, 0, DateTimeKind.Utc);
            daylight.AddRecurrencePattern(new RecurrencePattern("FREQ=YEARLY;BYDAY=1SU;BYMONTH=4"));
            daylight.TZOffsetFrom = new UTC_Offset("-0500");
            daylight.TZOffsetTo = new UTC_Offset("-0400");
            daylight.TimeZoneName = "EDT";

            Event evt = iCal.Create<Event>();
            evt.Summary = "Test event";
            evt.Start = new iCalDateTime(2007, 1, 24, 8, 0, 0, tzid, iCal);
            evt.Duration = TimeSpan.FromHours(1);
            evt.End = new iCalDateTime(2007, 1, 24, 9, 0, 0, tzid, iCal);
            RecurrencePattern recur = new RecurrencePattern("FREQ=MONTHLY;INTERVAL=2;BYDAY=4WE");
            evt.AddRecurrencePattern(recur);

            List<Occurrence> occurrences = evt.GetOccurrences(
                new DateTime(2007, 1, 24),
                new DateTime(2007, 12, 31));

            iCalDateTime[] DateTimes = new iCalDateTime[]
            {                
                new iCalDateTime(2007, 1, 24, 8, 0, 0, tzid, iCal),
                new iCalDateTime(2007, 3, 28, 8, 0, 0, tzid, iCal),
                new iCalDateTime(2007, 5, 23, 8, 0, 0, tzid, iCal),
                new iCalDateTime(2007, 7, 25, 8, 0, 0, tzid, iCal),
                new iCalDateTime(2007, 9, 26, 8, 0, 0, tzid, iCal),
                new iCalDateTime(2007, 11, 28, 8, 0, 0, tzid, iCal)
            };

            for (int i = 0; i < DateTimes.Length; i++)
                Assert.AreEqual(DateTimes[i], occurrences[i].Period.StartTime, "Event should occur on " + DateTimes[i]);

            Assert.AreEqual(
                DateTimes.Length,
                occurrences.Count,
                "There should be exactly " + DateTimes.Length +
                " occurrences; there were " + occurrences.Count);
        }