public DateTime GetLocalDateTime(string microSiteId) { var citydatetime = DateTime.Now; if (microSiteId == "london" || microSiteId == "international") { return(DateTime.Now); } const string timeZonesCacheKey = "TimeZones"; var timeZoneList = _cacheProvider.GetFromCache <Dictionary <string, TimeZoneInformation> >(timeZonesCacheKey); if (timeZoneList == null) { //read and populate time zones from the registry var mZones = TimeZoneInformation.EnumZones(); Array.Sort(mZones, new TimeZoneComparer()); timeZoneList = new Dictionary <string, TimeZoneInformation>(); foreach (var tzone in mZones) { if (!timeZoneList.ContainsKey(tzone.Name)) { timeZoneList.Add(tzone.Name, tzone); } } //cache this for 5 mins if (timeZoneList.Count > 0) { _cacheProvider.AddToCache(timeZonesCacheKey, timeZoneList, DateTime.Now.AddDays(1)); } } var tzsn = GetCityTimeZoneStandardName(microSiteId); if (!timeZoneList.ContainsKey(tzsn)) { return(citydatetime); } var destinationTimeZoneInfo = timeZoneList[tzsn]; var local = DateTime.Now; var utc = local.ToUniversalTime(); if (destinationTimeZoneInfo == null) { return(citydatetime); } var destinationTime = destinationTimeZoneInfo.FromUniversalTime(utc); citydatetime = destinationTime; return(citydatetime); }
protected void Page_Init(object sender, EventArgs e) { if (!IsPostBack) { TimeZoneInformation[] zones = TimeZoneInformation.EnumZones(); selectTimezoneControl.Items.Add(new ListItem("Web Browser TimeZone", "-1")); foreach (TimeZoneInformation tz in zones) { selectTimezoneControl.Items.Add(new ListItem(tz.DisplayName, tz.Index.ToString())); } } }
public void TestTryParseTimezoneOffsetToTimeSpan() { TimeZoneInformation[] tzs = TimeZoneInformation.EnumZones(); foreach (TimeZoneInformation ti in tzs) { string tz = ti.CurrentUtcBiasString; TimeSpan span = TimeSpan.Zero; Assert.IsTrue(TimeZoneInformation.TryParseTimezoneOffsetToTimeSpan(tz, out span), string.Format("Error parsing {0}", tz)); Console.WriteLine("{0}: {1} - {2}", ti.DisplayName, tz, span); Assert.AreEqual(span, ti.CurrentUtcBias); } }
public void TestKnownTimeZones() { int previousHours = 14; TimeZoneInformation[] tzs = TimeZoneInformation.EnumZones(); foreach (TimeZoneInformation ti in tzs) { Assert.IsTrue(ti.CurrentUtcBias.Minutes == 0 || Math.Abs(ti.CurrentUtcBias.Minutes) == 30 || Math.Abs(ti.CurrentUtcBias.Minutes) == 45, string.Format("Minutes = {0}", ti.CurrentUtcBias.Minutes)); Console.WriteLine("{0}: {1}", ti.DisplayName, ti.CurrentUtcBias); Assert.IsTrue(ti.CurrentUtcBias.Hours <= 13 && ti.CurrentUtcBias.Hours >= -12); Assert.IsTrue(previousHours + 1 >= ti.CurrentUtcBias.Hours); // adjust an extra hour for daylight savings previousHours = ti.CurrentUtcBias.Hours; } }