/// <summary> /// Return a TimeSpan from its utcOfset string representation (hours) e.g., "-7.0" /// </summary> public static TimeSpan ParseDatabaseUtcOffsetString(string utcOffsetAsString) { // This used to fail when the culture allowed , decimal places. It should now be fixed. // Although we do throw an error if it doesn't work // TimeSpan utcOffset = TimeSpan.FromHours(double.Parse(utcOffsetAsString, CultureInfo.InvariantCulture)); TimeSpan utcOffset; NumberStyles style = NumberStyles.Number | NumberStyles.AllowDecimalPoint; if (true == Double.TryParse(utcOffsetAsString, style, CultureInfo.InvariantCulture, out double utcOffsetDouble)) { utcOffset = TimeSpan.FromHours(utcOffsetDouble); } else { throw new ArgumentOutOfRangeException(nameof(utcOffsetAsString), String.Format("UTC offset could not be parsed from {0}.", utcOffsetAsString)); } //TimeSpan utcOffset = TimeSpan.FromHours(); if ((utcOffset < Constant.Time.MinimumUtcOffset) || (utcOffset > Constant.Time.MaximumUtcOffset)) { throw new ArgumentOutOfRangeException(nameof(utcOffsetAsString), String.Format("UTC offset must be between {0} and {1}, inclusive.", DateTimeHandler.ToStringDatabaseUtcOffset(Constant.Time.MinimumUtcOffset), DateTimeHandler.ToStringDatabaseUtcOffset(Constant.Time.MinimumUtcOffset))); } if (utcOffset.Ticks % Constant.Time.UtcOffsetGranularity.Ticks != 0) { throw new ArgumentOutOfRangeException(nameof(utcOffsetAsString), String.Format("UTC offset must be an exact multiple of {0} ({1}).", DateTimeHandler.ToStringDatabaseUtcOffset(Constant.Time.UtcOffsetGranularity), DateTimeHandler.ToStringDisplayUtcOffset(Constant.Time.UtcOffsetGranularity))); } return(utcOffset); }
/// <summary> /// Return "dd-MMM-yyyy HH:mm:ss+hh:mm" format of a DateTimeOffset e.g. 05-Apr-2016 12:05:01+5:00 /// </summary> public static string ToStringDisplayDateTimeUtcOffset(DateTimeOffset dateTime) { return(dateTime.DateTime.ToString(Constant.Time.DateTimeDisplayFormat, CultureInfo.CreateSpecificCulture("en-US")) + " " + DateTimeHandler.ToStringDisplayUtcOffset(dateTime.Offset)); }
/// <summary> /// Return a TimeSpan from its utcOfset string representation (hours) e.g., "-7.0" /// </summary> public static TimeSpan ParseDatabaseUtcOffsetString(string utcOffsetAsString) { TimeSpan utcOffset = TimeSpan.FromHours(double.Parse(utcOffsetAsString)); if ((utcOffset < Constant.Time.MinimumUtcOffset) || (utcOffset > Constant.Time.MaximumUtcOffset)) { throw new ArgumentOutOfRangeException(nameof(utcOffsetAsString), String.Format("UTC offset must be between {0} and {1}, inclusive.", DateTimeHandler.ToStringDatabaseUtcOffset(Constant.Time.MinimumUtcOffset), DateTimeHandler.ToStringDatabaseUtcOffset(Constant.Time.MinimumUtcOffset))); } if (utcOffset.Ticks % Constant.Time.UtcOffsetGranularity.Ticks != 0) { throw new ArgumentOutOfRangeException(nameof(utcOffsetAsString), String.Format("UTC offset must be an exact multiple of {0} ({1}).", DateTimeHandler.ToStringDatabaseUtcOffset(Constant.Time.UtcOffsetGranularity), DateTimeHandler.ToStringDisplayUtcOffset(Constant.Time.UtcOffsetGranularity))); } return(utcOffset); }