Ejemplo n.º 1
0
        /// <inheritdoc/>
        public void ReadIcs(IcsReader reader)
        {
            ContentLine content;
            while (null != (content = reader.ReadContentLine()))
            {
                switch (content.Name.ToLowerInvariant())
                {
                    case "begin":
                        SeasonalChange adjustment;
                        switch (content.Value.ToLowerInvariant())
                        {
                            case "standard": adjustment = new StandardChange(); break;
                            case "daylight": adjustment = new DaylightChange(); break;
                            default:
                                throw new CalendarException(string.Format("'{0}' is not a component of a time zone.", content.Value));
                        }
                        adjustment.ReadIcs(reader);
                        Adjustments.Add(adjustment);
                        break;

                    case "end":
                        if (!content.Value.Equals(Component.Names.TimeZone, StringComparison.InvariantCultureIgnoreCase))
                            throw new CalendarException(String.Format("Expected 'END:{0}' not '{1}'.", Component.Names.TimeZone, content));
                        return;

                    case "tzid": Id = content.Value; break;
                    case "last-modified": ModifiedOn = content.ToDate(); break;
                    case "url": Uri = content.Value; break;
                }
            }

            throw new CalendarException("Unexpected end of file.");
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Creates a <see cref="VTimeZone"/> from a <see cref="TimeZoneInfo"/> object.
        /// </summary>
        /// <param name="tzi">
        ///   The <see cref="TimeZoneInfo"/> to convert.
        /// </param>
        /// <returns>
        ///   A <see cref="VTimeZone"/> representing the <paramref name="tzi"/>.
        /// </returns>
        /// <example>
        ///   The following snippet demonstrates how to create an instance of the <see cref="VTimeZone"/> class for the
        ///   local time zone.
        ///  <code>
        ///   var localTimeZone = VTimeZone.FromTimeZoneInfo(TimeZoneInfo.Local);
        ///  </code>
        /// </example>
        /// <seealso cref="VCalendar.ForLocalTimeZone"/>
        public static VTimeZone FromTimeZoneInfo(TimeZoneInfo tzi)
        {
            Guard.IsNotNull(tzi, "tzi");

            var tz = new VTimeZone { Id = tzi.DisplayName };

            // Simple case of no adjustments.
            if (!tzi.SupportsDaylightSavingTime)
            {
                tz.Adjustments.Add(new StandardChange
                {
                    StartsOn = new DateTime(0, DateTimeKind.Local),
                    Name = new Text(CultureInfo.CurrentCulture.Name, tzi.StandardName),
                    OffsetFrom = tzi.BaseUtcOffset,
                    OffsetTo = tzi.BaseUtcOffset
                });
                return tz;
            }

            // Convert TZI adjustments. Each adjustment is DaylightChange.
            // TODO: Need to synthesize the StandardChange.
            // TODO: Rewrite when RecurrenceRule is functional.
            foreach (var adjustment in tzi.GetAdjustmentRules())
            {
                var daylight = new DaylightChange
                {
                    Name = new Text(CultureInfo.CurrentCulture.Name, tzi.DaylightName),
                    StartsOn = adjustment.DateStart + adjustment.DaylightTransitionStart.TimeOfDay.TimeOfDay,
                    OffsetFrom = tzi.BaseUtcOffset,
                    OffsetTo = tzi.BaseUtcOffset + adjustment.DaylightDelta,
                };
                if (adjustment.DaylightTransitionStart.IsFixedDateRule)
                {
                    throw new NotImplementedException("TimeZoneInfo with fixed date rule.");
                }
                else
                {
                    var s = new StringBuilder("FREQ=YEARLY;WKST=MO");
                    s.AppendFormat(";BYMONTH={0}", adjustment.DaylightTransitionStart.Month);
                    s.AppendFormat(";Day={0}{1}",
                        adjustment.DaylightTransitionStart.Week == 5 ? -1 : adjustment.DaylightTransitionStart.Week,
                        adjustment.DaylightTransitionStart.DayOfWeek.ToRfc5545WeekDayName());
                    daylight.RecurrenceRule = RecurrenceRule.Parse(s.ToString());
                }
                tz.Adjustments.Add(daylight);
            }
            return tz;
        }