internal static DateTimeStyles ToDateTimeStyles(DateTimeAssumption dateTimeAssumption) { System.Diagnostics.Debug.Assert(dateTimeAssumption == DateTimeAssumption.AssumeLocal || dateTimeAssumption == DateTimeAssumption.AssumeUniversal); switch (dateTimeAssumption) { case DateTimeAssumption.AssumeLocal: return DateTimeStyles.AssumeLocal; case DateTimeAssumption.AssumeUniversal: return DateTimeStyles.AssumeUniversal; } return DateTimeStyles.AssumeUniversal; // Just to make it compile. This line will never be reached. }
// We cannot use TestCase attribute because arbitraryUniversalDateTime, arbitraryLocalDateTime, and arbitraryUnspecifiedDateTime are not compile time constants. private static void ToDateTime__ValueIsInAnyStandardDateTimeFormat_FormatKnow_ValidAssumption_CurrentCulture__ReturnsEquivalentDateTime(DateTimeAssumption dateTimeAssumption, DateTime dateTime) { // JUst to be sure that the calling test is properly written. Assert.That(dateTimeAssumption == DateTimeAssumption.AssumeLocal && dateTime.Kind == DateTimeKind.Local || dateTimeAssumption == DateTimeAssumption.AssumeUniversal && dateTime.Kind == DateTimeKind.Utc || dateTimeAssumption == DateTimeAssumption.AssumeLocal && dateTime.Kind == DateTimeKind.Unspecified); Console.WriteLine("{0}-- {1} -- ({2}){0}", Environment.NewLine, dateTime.ToString(StandardDateTimeFormats.GeneralDateLongTime), dateTimeAssumption); foreach (string standardDateTimeFormat in StandardDateTimeFormats.AllStandardDateTimeFormats) { Console.WriteLine("{0} - {1}", standardDateTimeFormat, dateTime.ToString(standardDateTimeFormat)); DateTime? result = StringConvert.ToDateTime(dateTime.ToString(standardDateTimeFormat), standardDateTimeFormat, CultureInfo.CurrentCulture, dateTimeAssumption); Assert.That(result.HasValue); // We know now that the result has value and that result.Value will not throw InvalidOperationException. // ReSharper disable PossibleInvalidOperationException Console.WriteLine("{0} - {1}", standardDateTimeFormat, result.Value.ToString(standardDateTimeFormat)); // ReSharper restore PossibleInvalidOperationException AssertThatToDateTimeResultIsEquivalentToOriginalDateTime(standardDateTimeFormat, dateTimeAssumption == DateTimeAssumption.AssumeUniversal ? result.Value : result.Value.ToLocalTime(), dateTime); } }
// We cannot use TestCase attribute because arbitraryUniversalDateTime and arbitraryLocalDateTime are not compile time constants. private static void ToDateTime__ValueIsInAnyStandardDateTimeFormat_CurrentCulture__ReturnsDateTimeOfKindUtc(DateTimeAssumption dateTimeAssumption, DateTime dateTime) { foreach (string standardDateTimeFormat in StandardDateTimeFormats.AllStandardDateTimeFormats) { DateTime? result = StringConvert.ToDateTime(dateTime.ToString(standardDateTimeFormat), null, CultureInfo.CurrentCulture, dateTimeAssumption); Assert.That(result.HasValue); Assert.That(result.Value.Kind, Is.EqualTo(DateTimeKind.Utc)); } }
// We cannot use TestCase attribute because arbitraryUniversalDateTime, arbitraryLocalDateTime, and arbitraryUnspecifiedDateTime are not compile time constants. private static void AssertThatForAllStandardDateTimeFormatResultIsAtLeastBestGuessDateTime(DateTimeAssumption dateTimeAssumption, DateTime dateTime) { foreach (string standardDateTimeFormat in StandardDateTimeFormats.AllStandardDateTimeFormats) { DateTime? result = StringConvert.ToDateTime(dateTime.ToString(standardDateTimeFormat), null, CultureInfo.CurrentCulture, dateTimeAssumption); Assert.That(result.HasValue); } }
/// <inheritdoc cref="ToDateTimeOrNow(Option{string}, Option{string}, Option{IFormatProvider}, DateTimeAssumption)" /> /// <summary> /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent using the specified format and culture-specific format information. /// If the conversion fails, the method returns the current date. /// </summary> /// <returns> /// Date time value equivalent to the <see cref="DateTime"/> contained in the <paramref name="value"/> if the conversion succeeded. /// <br/>-or-<br/>Current date if the <paramref name="value"/> is None option. /// <br/>-or-<br/>Current date if the conversion failed.<br/> /// In case of successful conversion, the returned date time will have <see cref="DateTime.Kind"/> set to <see cref="DateTimeKind.Utc"/>. /// </returns> public static DateTime ToDateTimeOrToday(Option<string> value, Option<string> format, Option<IFormatProvider> formatProvider, DateTimeAssumption dateTimeAssumption) { return ToDateTime(value, format, formatProvider, dateTimeAssumption).GetValueOrDefault(TimeGenerator.GetToday()); }
#pragma warning restore 1573 /// <summary> /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent using the specified format and culture-specific format information. /// If the conversion fails, the specified default value is returned. /// </summary> /// <inheritdoc cref="ToDateTime(Option{string}, Option{string}, Option{IFormatProvider}, DateTimeAssumption)" source="remarks" /> /// <inheritdoc cref="ToInt32(Option{string})" source="param[name='value']"/> /// <inheritdoc cref="ToDateTime(Option{string}, Option{string})" source="param[name='format']"/> /// <inheritdoc cref="ToDateTime(Option{string}, Option{string}, Option{IFormatProvider}, DateTimeAssumption)" source="param[name='formatProvider']" /> /// <inheritdoc cref="ToDateTime(Option{string}, Option{string}, Option{IFormatProvider}, DateTimeAssumption)" source="param[name='dateTimeAssumption']" /> /// <inheritdoc cref="ToInt32Or(Option{string}, int)" source="param[name='defaultValue']"/> /// <returns> /// Date time value equivalent to the <see cref="DateTime"/> contained in the <paramref name="value"/> if the conversion succeeded. /// <br/>-or-<br/><paramref name="defaultValue"/> if the <paramref name="value"/> is None option. /// <br/>-or-<br/><paramref name="defaultValue"/> if the conversion failed.<br/> /// In case of successful conversion, the returned date time will have <see cref="DateTime.Kind"/> set to <see cref="DateTimeKind.Utc"/>. /// </returns> /// <inheritdoc cref="ToDateTime(Option{string})" source="seealso"/> public static DateTime ToDateTimeOr(Option<string> value, Option<string> format, Option<IFormatProvider> formatProvider, DateTimeAssumption dateTimeAssumption, DateTime defaultValue) { return ToDateTime(value, format, formatProvider, dateTimeAssumption).GetValueOrDefault(defaultValue); }
#pragma warning restore 1573 // TODO-IG: Test with format provider set to invariant culture. #pragma warning disable 1573 // Parameter does not have XML comment. /// <summary> /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent using the specified format and culture-specific format information. /// The return value indicates whether the conversion succeeded or failed. /// </summary> /// <remarks> /// <para> /// If the <paramref name="format"/> is specified (Some) the <paramref name="value"/> must match the specified format exactly. /// If the <paramref name="format"/> is not specified (None) the method will try to convert the <paramref name="value"/> by using any of the standard .NET date and time format string (see <see cref="StandardDateTimeFormats"/>). /// </para> /// <para> /// If the <paramref name="formatProvider"/> is not specified, the method will use the current thread culture (<see cref="CultureInfo.CurrentCulture"/>) as the format provider. /// </para> /// <para> /// Date and time are returned as a Coordinated Universal Time (UTC). /// If the <paramref name="value"/> denotes a local time, through a time zone specifier or <see cref="DateTimeAssumption.AssumeLocal"/>, /// the date and time are converted from the local time to UTC. /// If the input string denotes a UTC time, through a time zone specifier or <see cref="DateTimeAssumption.AssumeUniversal"/>, no conversion occurs. /// </para> /// <para> /// The <see cref="DateTime.Kind"/> of the returned <see cref="DateTime"/> object is always <see cref="DateTimeKind.Utc"/>. /// </para> /// </remarks> /// <inheritdoc cref="ToInt32(Option{string})" source="param[name='value']"/> /// <inheritdoc cref="ToDateTime(Option{string}, Option{string})" source="param[name='format']"/> /// <param name="formatProvider"> /// An object that supplies culture-specific formatting information about the <paramref name="value"/>. /// If None, the method will use the current thread culture (<see cref="CultureInfo.CurrentCulture"/>). /// </param> /// <param name="dateTimeAssumption">An assumption about the date time stored in the <paramref name="value"/>; is it stored as a local or UTC time.</param> /// <returns> /// Date time value equivalent to the <see cref="DateTime"/> contained in the <paramref name="value"/> if the conversion succeeded. /// <br/>-or-<br/>null if the <paramref name="value"/> is None option. /// <br/>-or-<br/>null if the conversion failed.<br/> /// In case of successful conversion, the returned date time will have <see cref="DateTime.Kind"/> set to <see cref="DateTimeKind.Utc"/>. /// </returns> /// <inheritdoc cref="ToDateTime(Option{string})" source="seealso"/> public static DateTime? ToDateTime(Option<string> value, Option<string> format, Option<IFormatProvider> formatProvider, DateTimeAssumption dateTimeAssumption) { // TODO-IG: Check that dateTimeAssumptio is a valid enumeration value. // The TryParseExact() fails if the string parameter is null. // That means we don't need additional check if the value is None. DateTime result; return DateTime.TryParseExact(value.ValueOrNull, format.IsNone ? StandardDateTimeFormats.AllStandardDateTimeFormats : new [] { format.Value }, formatProvider.ValueOr(CultureInfo.CurrentCulture), DateTimeAssumptionHelper.ToDateTimeStyles(dateTimeAssumption) | DateTimeStyles.AdjustToUniversal, out result) ? result : (DateTime?)null; }