/// <summary> /// Get date to print as string. /// Time zone by user logged in. /// It will get the logged user and then get the time-zone and then print. /// </summary> /// <param name="timeZone"></param> /// <param name="dt"></param> /// <param name="format">if format null then default format.</param> /// <param name="addTimeZoneString">Add timezone string with Date. Eg. 26-Aug-2015 (GMT -07:00)</param> /// <returns>Returns nice string format based on logged user's selected time zone.</returns> public static string GetDateTime( TimeZoneSet timeZone, DateTime?dt, string format = null, bool addTimeZoneString = true) { if (dt == null) { return(""); } var dt2 = (DateTime)dt; if (format == null) { format = DateTimeFormat; } if (timeZone == null || !timeZone.IsTimeZoneInfoExist()) { return(dt2.ToString(format)); } var currentZone = timeZone.TimeZoneInfo; //time zone found. var newDate = TimeZoneInfo.ConvertTime(dt2, currentZone); var additionalString = ""; if (addTimeZoneString) { var userZone = timeZone.UserTimezone; additionalString = "(" + GmtConst + userZone.TimePartOnly + ")"; } return(newDate.ToString(format) + additionalString); }
/// <summary> /// Optimized fist check on cache then database. /// Get time zone from database base on user name. /// </summary> /// <param name="username"></param> /// <returns>Returns time zone of the user.</returns> public static TimeZoneSet Get(string username) { TimeZoneSet timeZoneInfo = null; timeZoneInfo = GetSavedTimeZone(username); if (timeZoneInfo != null) { //got time zone from cache. return(timeZoneInfo); } //if cache time zone not exist. var user = UserManager.GetUser(username); if (user != null) { var timezoneDb = _dbTimeZones.FirstOrDefault(n => n.UserTimeZoneID == user.UserTimeZoneID); if (timezoneDb != null) { timeZoneInfo = new TimeZoneSet(); timeZoneInfo.UserTimezone = timezoneDb; timeZoneInfo.TimeZoneInfo = SystemTimeZones.FirstOrDefault(n => n.Id == timezoneDb.InfoID); } if (timeZoneInfo != null && timeZoneInfo.TimeZoneInfo != null) { // Save the time zone to the cache. SaveTimeZone(timeZoneInfo, username); return(timeZoneInfo); } } return(null); }
private static void SaveTimeZone(TimeZoneSet timeZoneInfo, string log) { if (log == null || timeZoneInfo == null) { return; } //save to cache AppConfig.Caches.Set(CookiesNames.ZoneInfo + log, timeZoneInfo); }
/// <summary> /// Saved for current logged user. /// </summary> /// <param name="timeZoneInfo"></param> private static void SaveTimeZone(TimeZoneSet timeZoneInfo) { if (!HttpContext.Current.User.Identity.IsAuthenticated) { return; } var log = HttpContext.Current.User.Identity.Name; SaveTimeZone(timeZoneInfo, log); }
/// <summary> /// No need to convert dates based on time zones. /// </summary> /// <param name="timeZone"></param> /// <param name="dt"></param> /// <param name="format">if format null then default format.</param> /// <param name="addTimeZoneString">Add timezone string with Date. Eg. 26-Aug-2015 (GMT -07:00)</param> /// <returns>Returns nice string format based on logged user's selected time zone.</returns> public static string GetDate( TimeZoneSet timeZone, DateTime?dt, string format = null, bool addTimeZoneString = true) { if (format == null) { format = DateFormat; } return(GetDateTime(timeZone, dt, format, addTimeZoneString)); }