/// <summary> /// Parse a metadata-formatted date/time string into a DateTimeOffset. Return false on failure /// </summary> public static bool TryParseMetadataDateTaken(string dateTimeAsString, TimeZoneInfo imageSetTimeZone, out DateTimeOffset dateTimeOffset) { if (DateTime.TryParseExact(dateTimeAsString, Constant.Time.DateTimeMetadataFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime) == false) { dateTimeOffset = DateTimeOffset.MinValue; return(false); } dateTimeOffset = DateTimeHandler.CreateDateTimeOffset(dateTime, imageSetTimeZone); return(true); }
private static bool TryParseDateTaken(string dateTimeAsString, TimeZoneInfo imageSetTimeZone, out DateTimeOffset dateTimeOffset) { // use current culture as BitmapMetadata.DateTaken is not invariant if (DateTime.TryParse(dateTimeAsString, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dateTime) == false) { dateTimeOffset = DateTimeOffset.MinValue; return(false); } dateTimeOffset = DateTimeHandler.CreateDateTimeOffset(dateTime, imageSetTimeZone); return(true); }
/// <summary> /// Parse a metadata-formatted date/time string exactly into a DateTimeOffset. /// Note that this only accepts 'standard' date/time forms as described in Constant.Time.DateTimeMetadataFormat /// Return false on failure /// </summary> public static bool TryParseMetadataDateTaken(string dateTimeAsString, TimeZoneInfo imageSetTimeZone, out DateTimeOffset dateTimeOffset) { if (false == DateTime.TryParseExact(dateTimeAsString, Constant.Time.DateTimeMetadataFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime)) { // Commented out section skips the general form, which can accept weird incomplete values that are not really useful e.g. 3.5 //if (false == DateTime.TryParse(dateTimeAsString, out dateTime)) //{ dateTimeOffset = DateTimeOffset.MinValue; return(false); //} //return false; } dateTimeOffset = DateTimeHandler.CreateDateTimeOffset(dateTime, imageSetTimeZone); return(true); }
/// <summary> /// Parse a legacy date and time string into a DateTimeOffset. Return false on failure /// </summary> public static bool TryParseLegacyDateTime(string date, string time, TimeZoneInfo imageSetTimeZone, out DateTimeOffset dateTimeOffset) { return(DateTimeHandler.TryParseDateTaken(date + " " + time, imageSetTimeZone, out dateTimeOffset)); }
/// <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) { // 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 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); }