/// <summary> /// Converts a localTime from a source time zone to a target time zone, adjusting for DST as needed. /// The localTime must be a local time in the sourceTimeZoneName time zone. /// </summary> /// <param name="sourceTimeZoneName">Time zone name which represents localTime.</param> /// <param name="localTime">The source local time.</param> /// <param name="targetTimeZoneName">The time zone name which to convert the localTime.</param> /// <returns>The local time for targetTimeZoneName.</returns> public static DateTime ToLocalTime(string sourceTimeZoneName, DateTime localTime, string targetTimeZoneName) { DateTime utc = TimeZoneInformation.ToUniversalTime(sourceTimeZoneName, localTime); DateTime lt = TimeZoneInformation.ToLocalTime(utc, targetTimeZoneName); return(lt); }
static TimeZoneInformation() { timeZones = 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.displayName = (string)subKey.GetValue("Display"); tzi.standardName = (string)subKey.GetValue("Std"); tzi.daylightName = (string)subKey.GetValue("Dlt"); tzi.InitTzi((byte[])subKey.GetValue("Tzi")); timeZones.Add(tzi); } } // Order list by offset timeZones.Sort(CompareByOffset); } }
private TZI tzi; // Current time zone information. #endregion Fields #region Constructors static TimeZoneInformation() { timeZones = 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.displayName = (string)subKey.GetValue("Display"); tzi.standardName = (string)subKey.GetValue("Std"); tzi.daylightName = (string)subKey.GetValue("Dlt"); tzi.InitTzi((byte[])subKey.GetValue("Tzi")); timeZones.Add(tzi); } } // Order list by offset timeZones.Sort(CompareByOffset); } }
private static int CompareByOffset(TimeZoneInformation x, TimeZoneInformation y) { if (x.StandardOffset.TotalHours < y.StandardOffset.TotalHours) { return(-1); } else if (x.StandardOffset.TotalHours > y.StandardOffset.TotalHours) { return(1); } else { return(0); } }
public override void Initialize(OptimusMiniSettingsList currentSettings) { _Timezones = new TimezoneItem[5]; for (int i = 0; i < 5; i++) { string lTimezone = currentSettings["Timezone" + i.ToString()]; string lLabel = currentSettings["Label" + i.ToString()]; if (lTimezone != "" && TimeZoneInformation.ContainsTimeZone(lTimezone)) { TimezoneItem lItem = new TimezoneItem(TimeZoneInformation.GetTimeZone(lTimezone), lLabel); _Timezones[i] = lItem; } else { _Timezones[i] = null; } } RequestNextUpdate(new TimeSpan(0, 0, 0)); }
public override void Repaint() { DateTime lUtc = DateTime.UtcNow; lock (_Graphic) { _Graphic.FillRectangle(Brushes.Black, 0, 0, 96, 96); for (int i = 0; i < 5; i++) { TimezoneItem lItem = _Timezones[i]; if (lItem == null) { continue; } string lTime = string.Format("{0} {1}", TimeZoneInformation.ToLocalTime(lUtc, lItem.Timezone.StandardName).ToShortTimeString(), lItem.Label); _Graphic.DrawString(lTime, SystemFonts.DefaultFont, Brushes.White, 0, i * 19); } _Graphic.Flush(); UpdateImage(_Bitmap); } }
public WorldTimeConfig(OptimusMiniSettingsList currentSettings) { InitializeComponent(); _Settings = currentSettings; _Items = new List <TimezoneItem>(); // ----- Fill select box with all timezones foreach (TimeZoneInformation lTimezone in TimeZoneInformation.TimeZones) { selectTimezone.Items.Add(lTimezone.DisplayName); } selectTimezone.SelectedIndex = 0; // ----- Add stored timezones for (int i = 0; i < 5; i++) { string lTimezone = _Settings["Timezone" + i.ToString()]; if (TimeZoneInformation.ContainsTimeZone(lTimezone)) { TimezoneItem lItem = new TimezoneItem(TimeZoneInformation.GetTimeZone(lTimezone), _Settings["Label" + i.ToString()]); _Items.Add(lItem); listTimezones.Items.Add(string.Format("{0}: {1}", lItem.Label, lItem.Timezone.DisplayName)); } } if (_Items.Count > 0) { listTimezones.Items[0].Selected = true; } // ----- Update controls UpdateActions(); }
/// <summary> /// Converts a local time in specified time zone to UTC time. /// </summary> /// <param name="standardTimeZoneName">The standard time zone name.</param> /// <param name="local">The local time to convert.</param> /// <returns>The UTC time.</returns> /// <exception cref="ArgumentException">Thrown if time zone name not found.</exception> /// <exception cref="NotSupportedException">Thrown if the method failed due to missing platform support.</exception> public static DateTime ToUniversalTime(string standardTimeZoneName, DateTime local) { TimeZoneInformation tzi = TimeZoneInformation.GetTimeZone(standardTimeZoneName); return(tzi.ToUniversalTime(local)); }
/// <summary> /// Converts the value of the utc time to local time in supplied time zone. /// </summary> /// <param name="utc">The time to convert.</param> /// <param name="targetTimeZoneName">The standard name of the time zone.</param> /// <returns>The local time.</returns> /// <exception cref="ArgumentException">Thrown if time zone not found.</exception> public static DateTime ToLocalTime(DateTime utc, string targetTimeZoneName) { TimeZoneInformation tzi = TimeZoneInformation.GetTimeZone(targetTimeZoneName); return(tzi.ToLocalTime(utc)); }
public TimezoneItem(TimeZoneInformation timezone, string label) { Timezone = timezone; Label = label; }
private void Repaint_Timezone(TimeZoneInformation timezone, int y) { }
private void Repaint_Timezone(TimeZoneInformation timezone,int y) { }
private static int CompareByOffset(TimeZoneInformation x, TimeZoneInformation y) { if (x.StandardOffset.TotalHours < y.StandardOffset.TotalHours) { return -1; } else if (x.StandardOffset.TotalHours > y.StandardOffset.TotalHours) { return 1; } else { return 0; } }