-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add converters to DatePicker and TimePicker for DateOnly and TimeOnly #30790
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #if NET6_0_OR_GREATER | ||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Globalization; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Microsoft.Maui.Controls; | ||
|
|
||
| internal class DateTimeTypeConverter : TypeConverter | ||
| { | ||
| public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType) | ||
| => sourceType == typeof(DateOnly) || sourceType == typeof(string); | ||
|
|
||
| public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||
| => destinationType == typeof(DateTime); | ||
|
|
||
| public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
| { | ||
| if (value is DateTime dateTime) | ||
| { | ||
| return dateTime; | ||
| } | ||
| if (value is DateOnly dateOnly) | ||
| { | ||
| return dateOnly.ToDateTime(TimeOnly.MinValue); | ||
| } | ||
| if (value is string stringValue) | ||
| { | ||
| if (DateOnly.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly dateTimeOnly)) | ||
| { | ||
| return dateTimeOnly.ToDateTime(TimeOnly.MinValue); | ||
| } | ||
| else if (DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) | ||
| { | ||
| return dateTime; | ||
| } | ||
| } | ||
|
|
||
| throw new NotImplementedException($"Cannot convert \"{value}\" into {typeof(DateTime)}"); | ||
| } | ||
|
|
||
| public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType) | ||
| { | ||
| if (value is DateOnly dateOnly) | ||
| { | ||
| return ConvertToDestinationType(dateOnly, destinationType, culture); | ||
| } | ||
| else if (value is DateTime dateTime) | ||
| { | ||
| return ConvertToDestinationType(dateTime, destinationType, culture); | ||
| } | ||
| else if (value is string stringValue) | ||
| { | ||
| if (DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) | ||
| { | ||
| return ConvertToDestinationType(dateTime, destinationType, culture); | ||
| } | ||
| else if (DateOnly.TryParse(stringValue, CultureInfo.InvariantCulture, out dateOnly)) | ||
| { | ||
| return ConvertToDestinationType(dateOnly, destinationType, culture); | ||
| } | ||
| } | ||
|
|
||
| throw new NotImplementedException($"Cannot convert \"{value}\" into {destinationType}"); | ||
| } | ||
|
|
||
| static object ConvertToDestinationType(DateOnly dateOnly, Type? destinationType, CultureInfo? culture) | ||
| { | ||
| if (destinationType == typeof(string)) | ||
| { | ||
| return dateOnly.ToString( CultureInfo.InvariantCulture); | ||
| } | ||
| else if (destinationType == typeof(DateTime)) | ||
| { | ||
| return dateOnly.ToDateTime(TimeOnly.MinValue); | ||
| } | ||
| else if (destinationType == typeof(DateOnly)) | ||
| { | ||
| return dateOnly; | ||
| } | ||
|
|
||
| throw new NotImplementedException($"Cannot convert \"{dateOnly}\" into {destinationType}"); | ||
| } | ||
|
|
||
| static object ConvertToDestinationType(DateTime dateTime, Type? destinationType, CultureInfo? culture) | ||
| { | ||
| if (destinationType == typeof(string)) | ||
| { | ||
| return dateTime.ToString(CultureInfo.InvariantCulture); | ||
| } | ||
| else if (destinationType == typeof(DateTime)) | ||
| { | ||
| return dateTime; | ||
| } | ||
| else if (destinationType == typeof(DateOnly)) | ||
| { | ||
| return DateOnly.FromDateTime(dateTime); | ||
| } | ||
|
|
||
| throw new NotImplementedException($"Cannot convert \"{dateTime}\" into {destinationType}"); | ||
| } | ||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #if NET6_0_OR_GREATER | ||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Globalization; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Microsoft.Maui.Controls; | ||
|
|
||
| internal class TimeSpanTypeConverter : TypeConverter | ||
| { | ||
| public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType) | ||
| => sourceType == typeof(TimeOnly); | ||
|
|
||
| public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||
| => destinationType == typeof(TimeSpan); | ||
|
|
||
| public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
| { | ||
| if (value is TimeSpan timeSpan) | ||
| { | ||
| return timeSpan; | ||
| } | ||
| else if (value is TimeOnly timeOnly) | ||
| { | ||
| return timeOnly.ToTimeSpan(); | ||
| } | ||
| else if (value is string stringValue) | ||
| { | ||
| if (TimeOnly.TryParse(stringValue, culture, out var timeOnlyResult)) | ||
| { | ||
| return timeOnlyResult.ToTimeSpan(); | ||
| } | ||
| else if (TimeSpan.TryParse(stringValue, culture, out var timeSpanResult)) | ||
| { | ||
| return timeSpanResult; | ||
| } | ||
| } | ||
| throw new NotImplementedException($"Cannot convert \"{value}\" into {typeof(TimeSpan)}"); | ||
| } | ||
|
|
||
| public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType) | ||
| { | ||
| if (value is TimeSpan timeSpan) | ||
| { | ||
| return ConvertToDestinationType(timeSpan, destinationType, culture); | ||
| } | ||
| else if (value is TimeOnly timeOnly) | ||
| { | ||
| return ConvertToDestinationType(timeOnly, destinationType, culture); | ||
| } | ||
| else if (value is string stringValue) | ||
| { | ||
| if (TimeOnly.TryParse(stringValue, culture, out timeOnly)) | ||
| { | ||
| return ConvertToDestinationType(timeOnly, destinationType, culture); | ||
| } | ||
| else if (TimeSpan.TryParse(stringValue, culture, out var timeSpan1)) | ||
| { | ||
| return ConvertToDestinationType(timeSpan1, destinationType, culture); | ||
| } | ||
| } | ||
|
|
||
| throw new NotImplementedException($"Cannot convert \"{value}\" into {destinationType}"); | ||
| } | ||
|
|
||
| static object ConvertToDestinationType(TimeOnly timeOnly, Type? destinationType, CultureInfo? culture) | ||
| { | ||
| if (destinationType == typeof(string)) | ||
| { | ||
| return timeOnly.ToString(culture); | ||
| } | ||
| else if (destinationType == typeof(TimeSpan)) | ||
| { | ||
| return timeOnly.ToTimeSpan(); | ||
| } | ||
| else if (destinationType == typeof(TimeOnly)) | ||
| { | ||
| return timeOnly; | ||
| } | ||
|
|
||
| throw new NotImplementedException($"Cannot convert \"{timeOnly}\" into {destinationType}"); | ||
| } | ||
|
|
||
| static object ConvertToDestinationType(TimeSpan timeSpan, Type? destinationType, CultureInfo? culture) | ||
| { | ||
| if (destinationType == typeof(string)) | ||
| { | ||
| return timeSpan.ToString(); | ||
| } | ||
| else if (destinationType == typeof(TimeSpan)) | ||
| { | ||
| return timeSpan; | ||
| } | ||
| else if (destinationType == typeof(TimeOnly)) | ||
| { | ||
| return TimeOnly.FromTimeSpan(timeSpan); | ||
| } | ||
|
|
||
| throw new NotImplementedException($"Cannot convert \"{timeSpan}\" into {destinationType}"); | ||
| } | ||
| } | ||
| #endif |
73 changes: 73 additions & 0 deletions
73
src/Controls/tests/Core.UnitTests/DateOnlyTypeConverterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #if NET6_0_OR_GREATER | ||
|
|
||
| namespace Microsoft.Maui.Controls.Core.UnitTests; | ||
|
|
||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Globalization; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| public class DateOnlyTypeConverterTests : BaseTestFixture | ||
| { | ||
| [Fact] | ||
| public void DateOnlyToDateTimeConversion() | ||
| { | ||
| var converter = new DateTimeTypeConverter(); | ||
|
|
||
| var dateOnlyValue = new DateOnly(2025, 2, 21); | ||
|
|
||
| var actualDateTime = converter.ConvertFrom(null, CultureInfo.InvariantCulture, dateOnlyValue); | ||
| var expectedDateTime = new DateTime(2025, 2, 21); | ||
|
|
||
| Assert.Equal(expectedDateTime, actualDateTime); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DateTimeToDateOnlyConversion() | ||
| { | ||
| var converter = new DateTimeTypeConverter(); | ||
|
|
||
| var dateTimeValue = new DateTime(2025, 2, 21); | ||
|
|
||
| var actualDateOnly = converter.ConvertTo(null, CultureInfo.InvariantCulture, dateTimeValue, typeof(DateOnly)); | ||
| var expectedDateOnly = new DateOnly(2025, 2, 21); | ||
|
|
||
| Assert.Equal(expectedDateOnly, actualDateOnly); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DateOnlyToDatePickerBinding() | ||
| { | ||
| var datePicker = new DatePicker(); | ||
| var source = new Issue20438DatePickerViewModel | ||
| { | ||
| SelectedDate = new DateOnly(2025, 3, 15) | ||
| }; | ||
| datePicker.BindingContext = source; | ||
| datePicker.SetBinding(DatePicker.DateProperty, "SelectedDate"); | ||
| var expectedDateTime = new DateTime(2025, 3, 15); | ||
| Assert.Equal(expectedDateTime, datePicker.Date); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DateOnlyToNonNullableBinding() | ||
| { | ||
| var dateProperty = BindableProperty.Create("Date", typeof(DateTime), typeof(DatePicker), null, BindingMode.TwoWay); | ||
| var source = new Issue20438DatePickerViewModel | ||
| { | ||
| SelectedDate = new DateOnly(2025, 3, 15) | ||
| }; | ||
| var bo = new MockBindable { BindingContext = source }; | ||
|
|
||
| bo.SetBinding(dateProperty, "SelectedDate"); | ||
| var expectedDateTime = new DateTime(2025, 3, 15); | ||
| Assert.Equal(expectedDateTime, bo.GetValue(dateProperty)); | ||
| } | ||
|
|
||
| public class Issue20438DatePickerViewModel | ||
| { | ||
| public DateOnly SelectedDate { get; set; } | ||
| } | ||
| } | ||
StephaneDelcroix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #endif | ||
72 changes: 72 additions & 0 deletions
72
src/Controls/tests/Core.UnitTests/TimeOnlyTypeConverterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #if NET6_0_OR_GREATER | ||
|
|
||
| namespace Microsoft.Maui.Controls.Core.UnitTests; | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| public class TimeOnlyTypeConverterTests : BaseTestFixture | ||
StephaneDelcroix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [Fact] | ||
| public void TimeOnlyToTimeSpanConversion() | ||
| { | ||
| var converter = new TimeSpanTypeConverter(); | ||
|
|
||
| var timeOnlyValue = new TimeOnly(8, 30, 0); | ||
|
|
||
| var actualTimeSpan = converter.ConvertFrom(null, CultureInfo.InvariantCulture, timeOnlyValue); | ||
| var expectedTimeSpan = new TimeSpan(8, 30, 0); | ||
|
|
||
| Assert.Equal(expectedTimeSpan, actualTimeSpan); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TimeSpanToTimeOnlyConversion() | ||
| { | ||
| var converter = new TimeSpanTypeConverter(); | ||
|
|
||
| var timeSpanValue = new TimeSpan(8, 30, 0); | ||
|
|
||
| var actualTimeOnly = converter.ConvertTo(null, CultureInfo.InvariantCulture, timeSpanValue, typeof(TimeOnly)); | ||
| var expectedTimeOnly = new TimeOnly(8, 30, 0); | ||
|
|
||
| Assert.Equal(expectedTimeOnly, actualTimeOnly); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TimeOnlyToTimePickerBinding() | ||
| { | ||
| var timePicker = new TimePicker(); | ||
| var source = new Issue20438TimePickerViewModel | ||
| { | ||
| SelectedTime = new TimeOnly(14, 30, 0) | ||
| }; | ||
| timePicker.BindingContext = source; | ||
| timePicker.SetBinding(TimePicker.TimeProperty, "SelectedTime"); | ||
| var expectedTimeSpan = new TimeSpan(14, 30, 0); | ||
| Assert.Equal(expectedTimeSpan, timePicker.Time); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TimeOnlyToNonNullableBinding() | ||
| { | ||
| var timeProperty = BindableProperty.Create("Time", typeof(TimeSpan), typeof(TimePicker), null, BindingMode.TwoWay); | ||
| var source = new Issue20438TimePickerViewModel | ||
| { | ||
| SelectedTime = new TimeOnly(14, 30, 0) | ||
| }; | ||
| var bo = new MockBindable { BindingContext = source }; | ||
|
|
||
| bo.SetBinding(timeProperty, "SelectedTime"); | ||
| var expectedTimeSpan = new TimeSpan(14, 30, 0); | ||
| Assert.Equal(expectedTimeSpan, bo.GetValue(timeProperty)); | ||
| } | ||
|
|
||
| public class Issue20438TimePickerViewModel | ||
| { | ||
| public TimeOnly SelectedTime { get; set; } | ||
| } | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there was a bug here