Repository navigation
Description
Background and motivation
Converting between System.DateOnly or System.TimeOnly and string is currently not supported using a System.ComponentModel.TypeConverter. Many system types are supported out of the box (System.DateTimeOffset, System.Guid, System.TimeSpan, System.Uri etc.) so I think System.DateOnly and System.TimeOnly would be welcome additions.
I was expecting this to work, but converting from string actually throws:
System.NotSupportedException: TypeConverter cannot convert from System.String.
TypeConverter dateOnlyConverter = TypeDescriptor.GetConverter(typeof(DateOnly));
DateOnly? date = dateOnlyConverter.ConvertFromString("1940-10-09") as DateOnly?;TypeConverter timeOnlyConverter = TypeDescriptor.GetConverter(typeof(TimeOnly));
TimeOnly? time = timeOnlyConverter.ConvertFromString("7:00") as TimeOnly?;Having built-in support for type conversion would probably help to drive adoption of the relatively new DateOnly and TimeOnly type.
Addressing this issue would fix #59253 which is mentioned in System.Runtime work planned for .NET 7 (but only under the Backlog list, not the Planned for .NET 7 list).
API Proposal
namespace System.ComponentModel
{
public class DateOnlyConverter : System.ComponentModel.TypeConverter
{
public DateOnlyConverter() { }
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) { throw null; }
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] System.Type? destinationType) { throw null; }
public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) { throw null; }
public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) { throw null; }
}
public class TimeOnlyConverter : System.ComponentModel.TypeConverter
{
public TimeOnlyConverter() { }
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) { throw null; }
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] System.Type? destinationType) { throw null; }
public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) { throw null; }
public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) { throw null; }
}
}API Usage
Using the converter would most likely happen through TypeDescriptor.GetConverter(typeof(…)):
var dateOnlyConverter = TypeDescriptor.GetConverter(typeof(DateOnly));
var date = dateOnlyConverter.ConvertFromString("1940-10-09") as DateOnly?;
var timeOnlyConverter = TypeDescriptor.GetConverter(typeof(TimeOnly));
var time = timeOnlyConverter.ConvertFromString("7:00") as TimeOnly?;or even through a higher level abstractions such as Microsoft.Extensions.Configuration.Binder (which also uses TypeDescriptor.GetConverter internally) as described in #64739:
// Let's make the end of the world date configurable because we're not really sure about that date
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
["EndOfWorldDate"] = "2012-12-21",
["BreakfastTime"] = "7:00:00",
})
.Build();
var endOfTheWorldDate = configuration.GetValue<DateOnly>("EndOfWorldDate");
var breakfastTime = configuration.GetValue<TimeOnly>("BreakfastTime");It could also be used by directly creating a DateOnlyConverter or TimeOnlyConverter instance but this is a less likely scenario.
Alternative Designs
To the best of my knowledge, there's no alternative design that would make sense for adding new System.ComponentModel.TypeConverter subclasses.
Risks
System.ComponentModel.DateOnlyConverter and System.ComponentModel.TimeOnlyConverter being a new classes, no risks are introduced.
Activity
tarekgh commentedon May 1, 2022
Could you please merge this proposal with the one in #68744? there is no need to track them in two different proposals. Thanks for filing the proposal.
[-][API Proposal]: new System.ComponentModel.DateOnlyConverter class[/-][+][API Proposal]: new System.ComponentModel.DateOnlyConverter and System.ComponentModel.TimeOnlyConverter classes[/+]thomaslevesque commentedon May 9, 2022
This would be very useful. Many components and libraries use
TypeConverterinternally. Microsoft.Extensions.Configuration.Binder, as mentioned by the OP, but also ASP.NET Core model binding, JSON.NET...In the meantime, it's possible to make it work by adding the converter dynamically at runtime:
tarekgh commentedon May 9, 2022
@thomaslevesque that is right. I believe the ask is to avoid having different users implement the same converters to make their scenarios work.
tarekgh commentedon May 20, 2022
@0xced I am noticing some differences comparing the proposal to like DateTimeConverter. Any reason for that? you are proposing to have the extra API
IsValid(which there is no need to override it) and also you are missing the public constructor and the nullability annotations.tarekgh commentedon May 23, 2022
@0xced I have updated the APIs in the proposal. Let's me know if you have any concern.
bartonjs commentedon May 26, 2022
We added System.Half, Int128, and UInt128 to the list to get caught up on primitive-ish types in the TypeConverter space. (We didn't think there was enough of a scenario for NFloat, NInt, or NUInt).
0xced commentedon Jun 8, 2022
Thank you for implementing this, Tarek and Jeremy!