Exemple #1
0
        /// <summary>
        /// Enumerate the available time zones
        /// </summary>
        /// <returns>The list of known time zones</returns>
        public static TimeZoneInformation[] EnumZones()
        {
            if (s_zones == null)
            {
                lock ( s_lockZones )
                {
                    if (s_zones == null)
                    {
                        List <TimeZoneInformation> zones = new List <TimeZoneInformation>();

                        using (RegistryKey key =
                                   Registry.LocalMachine.OpenSubKey(
                                       @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"))
                        {
                            string[] zoneNames = key.GetSubKeyNames();

                            foreach (string zoneName in zoneNames)
                            {
                                using (RegistryKey subKey = key.OpenSubKey(zoneName))
                                {
                                    TimeZoneInformation tzi = new TimeZoneInformation();
                                    tzi.m_name         = zoneName;
                                    tzi.m_displayName  = (string)subKey.GetValue("Display");
                                    tzi.m_standardName = (string)subKey.GetValue("Std");
                                    tzi.m_daylightName = (string)subKey.GetValue("Dlt");
                                    tzi.InitTzi((byte[])subKey.GetValue("Tzi"));
                                    tzi.m_index = (int)(subKey.GetValue("Index", zones.Count + 1));
                                    zones.Add(tzi);
                                }
                            }
                        }

                        zones.Sort(CompareByTzOffset);
                        s_zones = zones.ToArray();
                    }
                }
            }

            return(s_zones);
        }
Exemple #2
0
        /// <summary>
        /// Enumerate the available time zones
        /// </summary>
        /// <returns>The list of known time zones</returns>
        public static TimeZoneInformation[] EnumZones()
        {
            if ( s_zones == null )
            {
                lock( s_lockZones )
                {
                    if ( s_zones == null )
                    {
                        List<TimeZoneInformation> zones = new List<TimeZoneInformation>();

                        using ( RegistryKey key =
                                    Registry.LocalMachine.OpenSubKey(
                                    @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones" ) )
                        {
                            string[] zoneNames = key.GetSubKeyNames();

                            foreach ( string zoneName in zoneNames )
                            {
                                using ( RegistryKey subKey = key.OpenSubKey( zoneName ) )
                                {
                                    TimeZoneInformation tzi = new TimeZoneInformation();
                                    tzi.m_name = zoneName;
                                    tzi.m_displayName = (string) subKey.GetValue( "Display" );
                                    tzi.m_standardName = (string) subKey.GetValue( "Std" );
                                    tzi.m_daylightName = (string) subKey.GetValue( "Dlt" );
                                    tzi.InitTzi( (byte[]) subKey.GetValue( "Tzi" ) );
                                    tzi.m_index = (int)(subKey.GetValue("Index", zones.Count + 1));
                                    zones.Add( tzi );
                                }
                            }
                        }

                        zones.Sort(CompareByTzOffset);
                        s_zones = zones.ToArray();
                    }
                }
            }

            return s_zones;
        }
Exemple #3
0
 public static int CompareByTzOffset(TimeZoneInformation left, TimeZoneInformation right)
 {
     return right.Bias - left.Bias;
 }
Exemple #4
0
        /// <summary>
        /// Convert a time from the time zone with the supplied index to UTC.
        /// </summary>
        /// <param name="index">The time zone index.</param>
        /// <param name="utc">The time to convert.</param>
        /// <returns>The converted time.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is not found.</exception>
        /// <exception cref="NotSupportedException">Thrown if the method failed due to missing platform support.</exception>
        public static DateTime ToUniversalTime(int index, DateTime local)
        {
            TimeZoneInformation tzi = FromIndex(index);

            return(tzi.ToUniversalTime(local));
        }
Exemple #5
0
        /// <summary>
        /// Convert a time from UTC to the time zone with the supplied index.
        /// </summary>
        /// <param name="index">The time zone index.</param>
        /// <param name="utc">The time to convert.</param>
        /// <returns>The converted time.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is not found.</exception>
        public static DateTime FromUniversalTime(int index, DateTime utc)
        {
            TimeZoneInformation tzi = FromIndex(index);

            return(tzi.FromUniversalTime(utc));
        }
Exemple #6
0
 public static int CompareByTzOffset(TimeZoneInformation left, TimeZoneInformation right)
 {
     return(right.Bias - left.Bias);
 }