Exemplo n.º 1
0
 void CompareTimeZone()
 {
     if (dawnTime < time && time < dayTime)
     {
         timezone = TIMEZONE.DAWN;
     }
     else if (dayTime < time && time < nightTime)
     {
         timezone = TIMEZONE.DAY;
     }
     else if (nightTime < time)
     {
         timezone = TIMEZONE.NIGHT;
     }
 }
Exemplo n.º 2
0
        //=====================================================================

        /// <summary>
        /// This is used to load the VCalendar.TimeZones collection with time zone information from the registry
        /// </summary>
        public static void LoadTimeZoneInfo()
        {
            string   keyName, display, standardDesc, dstDesc;
            TIMEZONE tz;

            VCalendar.TimeZones.Clear();

            // To keep things simple, we'll load the time zone data from the settings available in the registry.
            // We could take it a step further and load it from something like a copy of the Olson time zone
            // database but I haven't been that ambitious yet.
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                keyName = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones";
            }
            else
            {
                keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones";
            }

            using (var rk = Registry.LocalMachine.OpenSubKey(keyName))
            {
                foreach (string s in rk.GetSubKeyNames())
                {
                    using (var rsk = rk.OpenSubKey(s))
                    {
                        display      = (string)rsk.GetValue("Display");
                        standardDesc = (string)rsk.GetValue("Std");
                        dstDesc      = (string)rsk.GetValue("Dlt");
                        tz           = TIMEZONE.FromRegistry(rsk.GetValue("TZI"));
                    }

                    // Create the time zone object
                    VTimeZone vtz = new VTimeZone();
                    vtz.TimeZoneId.Value = display;

                    ObservanceRule or = vtz.ObservanceRules.Add(ObservanceRuleType.Standard);

                    or.OffsetFrom.TimeSpanValue = TimeSpan.FromMinutes(tz.nBias + tz.nDaylightBias).Negate();
                    or.OffsetTo.TimeSpanValue   = TimeSpan.FromMinutes(tz.nBias + tz.nStandardBias).Negate();
                    or.TimeZoneNames.Add(standardDesc);

                    // If the standard date month is zero, it doesn't use standard time.  Assume 01/01/1970 and
                    // set it up to return the offset.
                    if (tz.standardDate.wMonth == 0)
                    {
                        or.StartDateTime.DateTimeValue = new DateTime(1970, 1, 1);
                    }
                    else
                    {
                        // If year is zero, its a recurrence.  If not zero, it's a fixed date.
                        if (tz.standardDate.wYear == 0)
                        {
                            or.StartDateTime.DateTimeValue = tz.standardDate.ToDateTime(1970);

                            RRuleProperty rrule = new RRuleProperty();

                            rrule.Recurrence.RecurYearly(
                                (tz.standardDate.wDay > 4) ? DayOccurrence.Last : (DayOccurrence)tz.standardDate.wDay,
                                tz.standardDate.DayOfWeek(), tz.standardDate.wMonth, 1);

                            or.RecurrenceRules.Add(rrule);
                        }
                        else
                        {
                            or.StartDateTime.DateTimeValue = tz.standardDate.ToDateTime();
                            or.RecurDates.Add(or.StartDateTime.DateTimeValue);
                        }
                    }

                    // If the daylight month is zero, it doesn't use DST.  The standard rule will handle
                    // everything.
                    if (tz.daylightDate.wMonth != 0)
                    {
                        or = vtz.ObservanceRules.Add(ObservanceRuleType.Daylight);

                        or.OffsetFrom.TimeSpanValue = TimeSpan.FromMinutes(tz.nBias + tz.nStandardBias).Negate();
                        or.OffsetTo.TimeSpanValue   = TimeSpan.FromMinutes(tz.nBias + tz.nDaylightBias).Negate();
                        or.TimeZoneNames.Add(dstDesc);

                        // If year is zero, its a recurrence.  If not zero, it's a fixed date.
                        if (tz.daylightDate.wYear == 0)
                        {
                            or.StartDateTime.DateTimeValue = tz.daylightDate.ToDateTime(1970);

                            RRuleProperty rrule = new RRuleProperty();

                            rrule.Recurrence.RecurYearly(
                                (tz.daylightDate.wDay > 4) ? DayOccurrence.Last : (DayOccurrence)tz.daylightDate.wDay,
                                tz.daylightDate.DayOfWeek(), tz.daylightDate.wMonth, 1);

                            or.RecurrenceRules.Add(rrule);
                        }
                        else
                        {
                            or.StartDateTime.DateTimeValue = tz.daylightDate.ToDateTime();
                            or.RecurDates.Add(or.StartDateTime.DateTimeValue);
                        }
                    }

                    VCalendar.TimeZones.Add(vtz);
                }
            }

            // Put the time zones in sorted order
            VCalendar.TimeZones.Sort(true);
        }