public static TimeSpan GetOffsetForDepartment(Department department) { TimeZoneInfo timeZoneInfo = null; TimeSpan timeSpan; try { if (!String.IsNullOrEmpty(department.TimeZone)) { timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(DateTimeHelpers.ConvertTimeZoneString(department.TimeZone)); } else { timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); // Default to Pacific as it's better then UTC } timeSpan = timeZoneInfo.BaseUtcOffset; var currentDateTime = DateTime.UtcNow.TimeConverter(department); if (timeZoneInfo.GetAdjustmentRules() != null && timeZoneInfo.GetAdjustmentRules().Any()) { timeSpan = timeZoneInfo.GetAdjustmentRules().Where(timeZoneAdjustment => timeZoneAdjustment.DateStart <= currentDateTime && timeZoneAdjustment.DateEnd >= currentDateTime).Aggregate(timeSpan, (current, timeZoneAdjustment) => current + timeZoneAdjustment.DaylightDelta); } } catch (Exception ex) { Framework.Logging.LogException(ex); timeSpan = new TimeSpan(-7, 0, 0); } return(timeSpan); }
public static string TimeConverterToString(this DateTime timestamp, Department department) { DateTime newTime = timestamp; TimeZoneInfo timeZoneInfo = null; try { if (!String.IsNullOrEmpty(department.TimeZone)) { timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(DateTimeHelpers.ConvertTimeZoneString(department.TimeZone)); } else { timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); // Default to Pacific as it's better then UTC } if (timeZoneInfo != null) { newTime = TimeZoneInfo.ConvertTimeFromUtc(timestamp, timeZoneInfo); } return(newTime.FormatForDepartment(department)); } catch (Exception ex) { var method = new StackTrace().GetFrame(1).GetMethod(); Framework.Logging.LogError(String.Format("TimeConverterToString error called from '{0}' of class '{1}' error {2}", method.Name, method.DeclaringType, ex.ToString())); return(timestamp.FormatForDepartment(department)); } }