-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add converters for DateOnly and TimeOnly
- Loading branch information
Edward Miller
committed
Apr 23, 2024
1 parent
3f9472b
commit 2f0f12b
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,55 @@ | ||
#if NET6_0_OR_GREATER | ||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
|
||
namespace Microsoft.Maui.Controls | ||
{ | ||
internal class DateOnlyConverter : 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 |
This file contains 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 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,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 |
This file contains 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