/// <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) { ArrayList zones = new ArrayList(); 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.m_index = 0;// (int)(subKey.GetValue("Index")); tzi.InitTzi((byte[])subKey.GetValue("Tzi")); zones.Add(tzi); } } } s_zones = new TimeZoneInformation[zones.Count]; zones.CopyTo(s_zones); } } } return(s_zones); }
/// <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)); }