/// <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); }
/// <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; }
public static int CompareByTzOffset(TimeZoneInformation left, TimeZoneInformation right) { return right.Bias - left.Bias; }
/// <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)); }
/// <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)); }
public static int CompareByTzOffset(TimeZoneInformation left, TimeZoneInformation right) { return(right.Bias - left.Bias); }