Repository navigation
Description
Background and motivation
Converting between 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.TimeOnly would be a welcome addition.
I was expecting this to work, but converting from string actually throws:
System.NotSupportedException: TypeConverter cannot convert from System.String.
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 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).
Note that a similar proposal for the System.DateOnly type has been filed as #68743.
API Proposal
public class TimeOnlyConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType);
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType);
public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value);
public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType);
public override bool IsValid(System.ComponentModel.ITypeDescriptorContext? context, object? value);
}API Usage
Using the converter would most likely happen through TypeDescriptor.GetConverter(typeof(TimeOnly)):
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:
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { ["BreakfastTime"] = "7:00:00" })
.Build();
var breakfastTime = configuration.GetValue<TimeOnly>("BreakfastTime");It could also be used by directly creating a TimeOnlyConverter instance but this is a less likely scenario:
var timeOnlyConverter = new System.ComponentModel.TimeOnlyConverter();
var time = timeOnlyConverter.ConvertFromString("7:00") as TimeOnly?;Alternative Designs
To the best of my knowledge, there's no alternative design that would make sense for adding a new System.ComponentModel.TypeConverter.
Risks
System.ComponentModel.TimeOnlyConverter being a new class, no risks are introduced.
Activity
tarekgh commentedon May 1, 2022
@0xced please move this proposal with the one in #68743. Feel free to close this one after merging the proposals. Thanks!
0xced commentedon May 1, 2022
I have just amended #68743 to cover both
DateOnlyandTimeOnly, so I'm closing this one.