Background and Motivation
In .the current version of .NET (5.0.203) Utf8Parser class has only support for DateTime.
https://docs.microsoft.com/en-us/dotnet/api/system.buffers.text.utf8parser.tryparse?view=net-5.0#System_Buffers_Text_Utf8Parser_TryParse_System_ReadOnlySpan_System_Byte__System_DateTime__System_Int32__System_Char_
With .NET 6 Preview 4 two new types have been introduced DateOnly and TimeOnly. It would be good to have the support of these two types in the TryParse method of Utf8Parser class. In the current version if the date is only passed without time the out variable returns a null value. I believe there will be legitimate UseCases where only date and time parsing are required explicitly. In order to extract the DateTime now , developers have to fallback to Encoding.UTF8.GetString() and then use DateTime.TryParse() method to get the date / time value. This is resource intensive operation and allocations get increased.
Eg.
if (DateTime.TryParse(Encoding.UTF8.GetString(buffer, out var dateofJoining))
{
record.DateOfJoining = dateofJoining;
}
Proposed API
Additional two overload methods must be added to current Utf8Parser static class to support DateOnly and TimeOnly types.
Usage Examples
Eg
For DateOnly type
if (Utf8Parser.TryParse(buffer, out DateOnly value, out var bytesConsumed))
{
record.DateOfJoining = value;
}
For TimeOnly type
if (Utf8Parser.TryParse(buffer, out TimeOnly value, out var bytesConsumed))
{
record.ApplicationTime = value;
}
Alternative Designs
Risks
Background and Motivation
In .the current version of .NET (5.0.203) Utf8Parser class has only support for
DateTime.https://docs.microsoft.com/en-us/dotnet/api/system.buffers.text.utf8parser.tryparse?view=net-5.0#System_Buffers_Text_Utf8Parser_TryParse_System_ReadOnlySpan_System_Byte__System_DateTime__System_Int32__System_Char_
With .NET 6 Preview 4 two new types have been introduced
DateOnlyandTimeOnly. It would be good to have the support of these two types in theTryParsemethod ofUtf8Parserclass. In the current version if the date is only passed without time the out variable returns anullvalue. I believe there will be legitimate UseCases where only date and time parsing are required explicitly. In order to extract the DateTime now , developers have to fallback toEncoding.UTF8.GetString()and then useDateTime.TryParse()method to get the date / time value. This is resource intensive operation and allocations get increased.Eg.
Proposed API
Additional two overload methods must be added to current
Utf8Parserstatic class to supportDateOnlyandTimeOnlytypes.Usage Examples
Eg
For
DateOnlytypeFor
TimeOnlytypeAlternative Designs
Risks