Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeOffsetConverter fix ArgumentException #915

Merged
merged 3 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class DateTimeOffsetConverterTests : BaseConverterTest<DateTimeOffsetConv
new object[] { testDateTimeOffsetLocal, testDateTimeLocal },
new object[] { testDateTimeOffsetUtc, testDateTimeUtc },
new object[] { testDateTimeOffsetUtc, testDateTimeUnspecified },
new object[] { testDateTimeOffsetNow, testDateTimeNow, CultureInfo.CurrentCulture },
new object[] { DateTimeOffset.MinValue, DateTime.MinValue, CultureInfo.CurrentCulture },
new object[] { DateTimeOffset.MaxValue, DateTime.MaxValue, CultureInfo.CurrentCulture },
new object[] { testDateTimeOffsetLocal, testDateTimeLocal, CultureInfo.CurrentCulture },
new object[] { testDateTimeOffsetUtc, testDateTimeUtc, CultureInfo.CurrentCulture },
new object[] { testDateTimeOffsetUtc, testDateTimeUnspecified, CultureInfo.CurrentCulture },
};

public static IReadOnlyList<object[]> DataReverse { get; } = new[]
Expand All @@ -33,29 +39,35 @@ public class DateTimeOffsetConverterTests : BaseConverterTest<DateTimeOffsetConv
new object[] { testDateTimeLocal, testDateTimeOffsetLocal },
new object[] { testDateTimeUtc, testDateTimeOffsetUtc },
new object[] { testDateTimeUnspecified, testDateTimeOffsetUtc },
new object[] { testDateTimeNow, testDateTimeOffsetNow, CultureInfo.CurrentCulture },
new object[] { DateTime.MinValue, DateTimeOffset.MinValue, CultureInfo.CurrentCulture },
new object[] { DateTime.MaxValue, DateTimeOffset.MaxValue, CultureInfo.CurrentCulture },
new object[] { testDateTimeLocal, testDateTimeOffsetLocal, CultureInfo.CurrentCulture },
new object[] { testDateTimeUtc, testDateTimeOffsetUtc, CultureInfo.CurrentCulture },
new object[] { testDateTimeUnspecified, testDateTimeOffsetUtc, CultureInfo.CurrentCulture },
};

[Theory]
[MemberData(nameof(Data))]
public void DateTimeOffsetConverter(DateTimeOffset value, DateTime expectedResult)
public void DateTimeOffsetConverter(DateTimeOffset value, DateTime expectedResult, CultureInfo? culture = null)
{
var dateTimeOffsetConverter = new DateTimeOffsetConverter();

var convertResult = ((ICommunityToolkitValueConverter)dateTimeOffsetConverter).Convert(value, typeof(DateTime), null, CultureInfo.CurrentCulture);
var convertFromResult = dateTimeOffsetConverter.ConvertFrom(value);
var convertResult = ((ICommunityToolkitValueConverter)dateTimeOffsetConverter).Convert(value, typeof(DateTime), null, culture);
var convertFromResult = dateTimeOffsetConverter.ConvertFrom(value, culture);

Assert.Equal(expectedResult, convertResult);
Assert.Equal(expectedResult, convertFromResult);
}

[Theory]
[MemberData(nameof(DataReverse))]
public void DateTimeOffsetConverterBack(DateTime value, DateTimeOffset expectedResult)
public void DateTimeOffsetConverterBack(DateTime value, DateTimeOffset expectedResult, CultureInfo? culture = null)
{
var dateTimeOffsetConverter = new DateTimeOffsetConverter();

var convertBackResult = (DateTimeOffset)(((ICommunityToolkitValueConverter)dateTimeOffsetConverter).ConvertBack(value, typeof(DateTimeOffset), null, CultureInfo.CurrentCulture) ?? throw new InvalidOperationException());
var convertBackToResult = dateTimeOffsetConverter.ConvertBackTo(value);
var convertBackResult = (DateTimeOffset)(((ICommunityToolkitValueConverter)dateTimeOffsetConverter).ConvertBack(value, typeof(DateTimeOffset), null, culture) ?? throw new InvalidOperationException());
var convertBackToResult = dateTimeOffsetConverter.ConvertBackTo(value, culture);

Assert.Equal(expectedResult, convertBackResult, new DateTimeOffsetComparer());
Assert.Equal(expectedResult, convertBackToResult, new DateTimeOffsetComparer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override DateTimeOffset ConvertBackTo(DateTime value, CultureInfo? cultur
};

return culture is null ?
new DateTimeOffset(value, offset) :
value.Kind == DateTimeKind.Local ? new DateTimeOffset(value) : new DateTimeOffset(value, offset) :
new DateTimeOffset(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Millisecond, culture.Calendar, offset);
VladislavAntonyuk marked this conversation as resolved.
Show resolved Hide resolved

}
Expand Down