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

Add converters to DatePicker and TimePicker for DateOnly and TimeOnly #21989

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Controls/src/Core/DateOnlyToDateTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#if NET6_0_OR_GREATER
using System;
using System.ComponentModel;
using System.Globalization;

namespace Microsoft.Maui.Controls
{
internal class DateOnlyToDateTimeConverter : TypeConverter, IExtendedTypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
=> sourceType == typeof(string) || sourceType == typeof(DateTime);

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
=> destinationType == typeof(string) || destinationType == typeof(DateTime);

object IExtendedTypeConverter.ConvertFromInvariantString(string value, IServiceProvider serviceProvider)
{
if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
{
return DateOnly.FromDateTime(result);
}
throw new NotSupportedException($"Cannot convert \"{value}\" into {typeof(DateOnly)}");
}

public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
{
if (value is DateTime dateTime)
{
return DateOnly.FromDateTime(dateTime);
}
if (value is string stringValue && DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
{
return DateOnly.FromDateTime(result);
}
throw new NotSupportedException($"Cannot convert \"{value}\" into {typeof(DateOnly)}");
}

public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (value is DateOnly dateOnly)
{
if (destinationType == typeof(string))
{
return dateOnly.ToString(culture);
}
if (destinationType == typeof(DateTime))
{
return dateOnly.ToDateTime(TimeOnly.MinValue);
}
}
throw new NotSupportedException($"Cannot convert \"{value}\" into {destinationType}");
}
}
}
#endif
3 changes: 3 additions & 0 deletions src/Controls/src/Core/DatePicker/DatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public DatePicker()
}

/// <include file="../../docs/Microsoft.Maui.Controls/DatePicker.xml" path="//Member[@MemberName='Date']/Docs/*" />
#if NET6_0_OR_GREATER
[System.ComponentModel.TypeConverter(typeof(DateOnlyToDateTimeConverter))]
#endif
public DateTime Date
{
get { return (DateTime)GetValue(DateProperty); }
Expand Down
55 changes: 55 additions & 0 deletions src/Controls/src/Core/TimeOnlyToTimeSpanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#if NET6_0_OR_GREATER
using System;
using System.ComponentModel;
using System.Globalization;

namespace Microsoft.Maui.Controls
{
internal class TimeOnlyToTimeSpanConverter : TypeConverter, IExtendedTypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
=> sourceType == typeof(string) || sourceType == typeof(TimeOnly);

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
=> destinationType == typeof(string) || destinationType == typeof(TimeSpan);

object IExtendedTypeConverter.ConvertFromInvariantString(string value, IServiceProvider serviceProvider)
{
if (TimeOnly.TryParse(value, out var result))
{
return result.ToTimeSpan();
}
throw new NotSupportedException($"Cannot convert \"{value}\" into {typeof(TimeSpan)}");
}

public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
{
if (value is TimeOnly timeOnly)
{
return timeOnly.ToTimeSpan();
}
if (value is string stringValue && TimeOnly.TryParse(stringValue, out var result))
{
return result.ToTimeSpan();
}
throw new NotSupportedException($"Cannot convert \"{value}\" into {typeof(TimeSpan)}");
}

public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (value is TimeSpan timeSpan)
{
if (destinationType == typeof(string))
{
return timeSpan.ToString();
}
if (destinationType == typeof(TimeOnly))
{
return TimeOnly.FromTimeSpan(timeSpan);
}
}
throw new NotSupportedException($"Cannot convert \"{value}\" into {destinationType}");
}
}
}
#endif
3 changes: 3 additions & 0 deletions src/Controls/src/Core/TimePicker/TimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public double CharacterSpacing
}

/// <include file="../../docs/Microsoft.Maui.Controls/TimePicker.xml" path="//Member[@MemberName='Time']/Docs/*" />
#if NET6_0_OR_GREATER
[System.ComponentModel.TypeConverter(typeof(TimeOnlyToTimeSpanConverter))]
#endif
public TimeSpan Time
{
get { return (TimeSpan)GetValue(TimeProperty); }
Expand Down