/// <summary> /// Determines whether [is between maintenance time] [the specified dayofweek]. /// N.B. really should not be here...JJG /// </summary> /// <param name="dayofweek">The dayofweek.</param> /// <param name="starttime">The starttime.</param> /// <param name="endtime">The endtime.</param> /// <returns></returns> public static bool IsBetweenMaintenanceTime(string dayofweek, string starttime, string endtime) { // Get the current server time since the zone conversion logic, first converts it to UTC and then to desired time zone. var currentUtcDateTime = DateTime.UtcNow.ToUniversalTime(); DateTime?rentalBranchTimeZone = currentUtcDateTime.ToLocalTime(); //Central rentalBranchTimeZone = TimeZoneInformation.ConvertDateTimeToTimeZone("u", rentalBranchTimeZone.ToString(), "Central Standard Time"); ErrorLog.DebugLog("IsBetweenMaintenanceTime RentalBranchTime: " + rentalBranchTimeZone + "starttime: " + starttime + " end times: " + endtime, "test"); // Current day of the week: //DayOfWeek day = RentalBranchTimeZone.Value.DayOfWeek; if (rentalBranchTimeZone != null && rentalBranchTimeZone.Value.DayOfWeek.Equals((DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayofweek))) { TimeSpan startinterval; TimeSpan endinterval; if ((TimeSpan.TryParse(starttime, out startinterval)) && TimeSpan.TryParse(endtime, out endinterval)) { if (endinterval == startinterval) { return(true); } if (endinterval < startinterval) { return(rentalBranchTimeZone.Value.TimeOfDay <= endinterval && rentalBranchTimeZone.Value.TimeOfDay >= startinterval); } return(rentalBranchTimeZone.Value.TimeOfDay >= startinterval && rentalBranchTimeZone.Value.TimeOfDay <= endinterval); } } return(false); }
/// <summary> /// Converts the date time string to time zone. /// </summary> /// <param name="outputFormat">The output format for the result.</param> /// <param name="source">The source date time value as string (will be Parsed).</param> /// <param name="timeZoneId">The TimeZone standard name.</param> /// <returns></returns> static public DateTime?ConvertDateTimeToTimeZone(string outputFormat, string source, string timeZoneId) { try { DateTime current = DateTime.Parse(source); TimeZoneInformation zone = FromName(timeZoneId); Assert.Test(zone != null, string.Format("TimeZoneInformation for {0} not found!", timeZoneId)); DateTime utc = current.ToUniversalTime(); DateTime dest = zone.FromUniversalTime(utc); return(dest); } catch (Exception ex) { ErrorLog.LogError(string.Format("ConvertDateTimeToTimeZone({0},{1}) failed: {2}", source, timeZoneId, ex.Message), "FnsUtility.TimeZoneInformation"); } return(null); }
/// <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 = (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="local">The local.</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)); }