//ConfigurationInfo ci; ////------------------------------------------------------------------------------------------------------------------------------------------------------------- //public LocaliseTime(ConfigurationInfo ci) { // this.ci = ci; //} //------------------------------------------------------------------------------------------------------------------------------------------------------------- /// <summary> /// localisationOption can be 1=System default (normally UTC), 2=Frontend default, 3=User last login / access, /// DEPRECATED - 4 - DEPRECATED=current login credentials /// If 3 is not available, will default to 2; if 2 not available, will default to 1 /// 4 is only really relevant in a couple of special cases, the PasswordResetRequest and the PasswordReset, as the user does /// not actually login to complete these. We now handle the collection of this information in these two pages, if it differs from /// previous information. /// </summary> public static bool Localise(ConfigurationInfo ci, DateTime utcDT, int localisationOption, int userID, out DateTime localDT) { bool success = false; localDT = utcDT; int timezoneOffset = 0; try { if (DateTimeInformation.IsNullDate(utcDT) == false) { if (localisationOption > 3 || localisationOption < 1 || (localisationOption == 3 && userID == 0)) { Logger.LogError(6, "Unknown or invalid localisation option provided (" + localisationOption + " with userID " + userID + ") in LocaliseTime.Localise: " + Environment.StackTrace); } else { // try with 3 if (localisationOption == 3) { success = GetTimezoneOffset(ci, 0, userID, out timezoneOffset); } // try with 2 or if 3 failed if (localisationOption == 2 || (localisationOption == 3 && success == false)) { success = GetTimezoneOffset(ci, 2, 0, out timezoneOffset); } // try with 1 or if 3 failed if (localisationOption == 1 || success == false) { success = GetTimezoneOffset(ci, 1, 0, out timezoneOffset); } if (success == true) { // Timezones ahead of GMT / UTC actually are negative (a bit counterintuitively) so these will need to be added if (timezoneOffset == 0) { // Do nothing ... } else if (timezoneOffset < 0) { localDT = utcDT.Add(new TimeSpan(0, timezoneOffset * -1, 0)); } else { localDT = utcDT.Subtract(new TimeSpan(0, timezoneOffset, 0)); } } } } } catch (Exception ex) { Logger.LogError(5, "Problem localising the given date time (" + utcDT + ") using localisation option " + localisationOption + " and userID " + userID + ". Check it out:" + ex.ToString()); } finally { } return(success); }
//------------------------------------------------------------------------------------------------------------------------------------------------------------- /// <summary> /// The excel export code needs this version ... /// Optimised localisation where the timezone offset is known and can be utilised for multiple entries /// e.g. the search and export functions... /// Note that this always returns true! /// Returns the localised datetime object /// </summary> public static DateTime Localise(DateTime utcDT, int timezoneOffset) { DateTime localDate = utcDT; if (DateTimeInformation.IsNullDate(utcDT) == false) { // Timezones ahead of GMT / UTC actually are negative (a bit counterintuitively) so these will need to be added if (timezoneOffset == 0) { // Do nothing ... } else if (timezoneOffset < 0) { localDate = utcDT.Add(new TimeSpan(0, timezoneOffset * -1, 0)); } else { localDate = utcDT.Subtract(new TimeSpan(0, timezoneOffset, 0)); } } return(localDate); }